Skip to content

Commit 6d90f19

Browse files
committed
Simple cli for now
1 parent 84be538 commit 6d90f19

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

main.go

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package main
22

33
import (
4-
"context"
54
"flag"
65
"fmt"
76
"os"
87

98
"github.com/c-bata/go-prompt"
109

11-
"github.com/chdb-io/chdb-go/cli"
12-
"github.com/chdb-io/chdb-go/cli/completer"
13-
"github.com/chdb-io/chdb-go/cli/history"
10+
// "github.com/chdb-io/chdb-go/cli"
11+
// "github.com/chdb-io/chdb-go/cli/completer"
12+
// "github.com/chdb-io/chdb-go/cli/history"
1413

1514
"github.com/chdb-io/chdb-go/chdb"
1615
)
@@ -63,9 +62,9 @@ data will lost after exit. If you want to keep the data, specify a path to a dir
6362
format = args[1]
6463
}
6564

66-
result := chdb.Query(sql, format)
67-
if result == nil {
68-
fmt.Println("No result or an error occurred.")
65+
result, err := chdb.Query(sql, format)
66+
if err != nil {
67+
fmt.Println(err)
6968
return
7069
}
7170

@@ -77,50 +76,36 @@ data will lost after exit. If you want to keep the data, specify a path to a dir
7776
func interactiveMode(session *chdb.Session) {
7877
fmt.Println("Enter your SQL commands; type 'exit' to quit.")
7978

80-
h, uh, err := initHistory("")
81-
if err != nil {
82-
fmt.Errorf("Failed to init history: %s", err)
83-
return
84-
}
85-
86-
c := cli.New(session, h, true)
87-
complete := completer.New()
88-
8979
p := prompt.New(
90-
c.Executor,
91-
complete.Complete,
92-
prompt.OptionTitle("chDB golang cli."),
93-
prompt.OptionHistory(h.RowsToStrArr(uh)),
94-
prompt.OptionPrefix(c.GetCurrentDB(context.Background())+" :) "),
95-
prompt.OptionLivePrefix(c.GetLivePrefixState),
96-
prompt.OptionPrefixTextColor(prompt.White),
97-
prompt.OptionAddKeyBind(prompt.KeyBind{
98-
Key: prompt.F3,
99-
Fn: c.MultilineControl,
100-
}),
80+
func(query string) {
81+
if query == "exit" {
82+
os.Exit(0)
83+
}
84+
85+
result, err := chdb.Query(query, "CSV")
86+
if err != nil {
87+
fmt.Println(err)
88+
return
89+
}
90+
91+
fmt.Println(result)
92+
},
93+
func(d prompt.Document) []prompt.Suggest {
94+
return []prompt.Suggest{
95+
{Text: "SELECT", Description: "SELECT"},
96+
{Text: "INSERT", Description: "INSERT"},
97+
{Text: "UPDATE", Description: "UPDATE"},
98+
{Text: "DELETE", Description: "DELETE"},
99+
{Text: "CREATE", Description: "CREATE"},
100+
{Text: "ALTER", Description: "ALTER"},
101+
{Text: "DROP", Description: "DROP"},
102+
{Text: "DESCRIBE", Description: "DESCRIBE"},
103+
{Text: "SHOW", Description: "SHOW"},
104+
{Text: "OPTIMIZE", Description: "OPTIMIZE"},
105+
}
106+
},
107+
prompt.OptionPrefix(":) "),
108+
prompt.OptionTitle("chdb-go"),
101109
)
102-
103110
p.Run()
104111
}
105-
106-
func initHistory(path string) (*history.History, []*history.Row, error) {
107-
var historyPath string
108-
if path != "" {
109-
historyPath = path
110-
} else {
111-
home, _ := os.UserHomeDir()
112-
historyPath = home + "/.chdb-go-cli-history"
113-
}
114-
115-
h, err := history.New(historyPath)
116-
if err != nil {
117-
return nil, nil, err
118-
}
119-
120-
uh, err := h.Read()
121-
if err != nil {
122-
return nil, nil, err
123-
}
124-
125-
return h, uh, nil
126-
}

0 commit comments

Comments
 (0)