1
1
package main
2
2
3
3
import (
4
- "context"
5
4
"flag"
6
5
"fmt"
7
6
"os"
8
7
9
8
"github.com/c-bata/go-prompt"
10
9
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"
14
13
15
14
"github.com/chdb-io/chdb-go/chdb"
16
15
)
@@ -63,9 +62,9 @@ data will lost after exit. If you want to keep the data, specify a path to a dir
63
62
format = args [1 ]
64
63
}
65
64
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 )
69
68
return
70
69
}
71
70
@@ -77,50 +76,36 @@ data will lost after exit. If you want to keep the data, specify a path to a dir
77
76
func interactiveMode (session * chdb.Session ) {
78
77
fmt .Println ("Enter your SQL commands; type 'exit' to quit." )
79
78
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
-
89
79
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" ),
101
109
)
102
-
103
110
p .Run ()
104
111
}
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