11package main
22
33import (
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
7776func 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