forked from secretflow/scql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
353 lines (317 loc) · 9.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2023 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/influxdata/go-prompt"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/secretflow/scql/pkg/proto-gen/scql"
"github.com/secretflow/scql/pkg/scdb/client"
"github.com/secretflow/scql/pkg/util/tableview"
)
var (
// for all cmd
host string
userName string
passwd string
usersConfFileName string
sync bool
pollingTimes int
pollingTimeIntervalSeconds int
// for source
sourceFile string
version = "scql version"
)
type UserCredential struct {
UserName string
Password string
}
var (
curDBName string
curUser UserCredential
userConfMap map[string]UserCredential
)
var rootCmd = &cobra.Command{
Use: "scdbclient",
Short: "terminal client for scdb",
Long: "A terminal client to work with scdb",
ValidArgs: []string{"prompt", "source", "help"},
Args: cobra.OnlyValidArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return setupUserConfig()
},
Version: version,
}
var sourceCmd = &cobra.Command{
Use: "source",
Short: "execute SCQL statements in a script file",
Run: func(cmd *cobra.Command, args []string) {
runSourceMode()
},
}
var promptCmd = &cobra.Command{
Use: "prompt",
Short: "command prompt lets you issue query to scdb server in interactive mode",
Run: func(cmd *cobra.Command, args []string) {
runPromptMode()
},
}
func initFlags() {
rootCmd.PersistentFlags().StringVar(&host, "host", "http://localhost:8080", "scdb server host")
rootCmd.PersistentFlags().StringVar(&userName, "userName", "", "user name for scdb user")
rootCmd.PersistentFlags().StringVar(&passwd, "passwd", "", "user passwd for scdb user")
rootCmd.PersistentFlags().StringVar(&usersConfFileName, "usersConfFileName", "cmd/scdbclient/users.json", "user conf file name")
rootCmd.PersistentFlags().BoolVar(&sync, "sync", false, "query in sync or async mode")
rootCmd.PersistentFlags().IntVar(&pollingTimes, "pollingTimes", 0, "polling times, if pollingTimes <= 0, polling until the result is obtained and exit")
rootCmd.PersistentFlags().IntVar(&pollingTimeIntervalSeconds, "pollingTimeIntervalSeconds", 1, "polling times interval in seconds")
sourceCmd.PersistentFlags().StringVar(&sourceFile, "sourceFile", "", "scql script file")
rootCmd.AddCommand(sourceCmd)
rootCmd.AddCommand(promptCmd)
}
func setupUserConfig() error {
if usersConfFileName != "" {
jsonFile, err := os.Open(usersConfFileName)
if err != nil {
log.Fatalf("read input fail: %v", usersConfFileName)
}
defer func() {
err = jsonFile.Close()
}()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return err
}
if err = json.Unmarshal(byteValue, &userConfMap); err != nil {
return err
}
} else if userName != "" {
userConfMap[userName] = UserCredential{
UserName: userName,
Password: passwd,
}
}
if userName != "" {
if err := switchUser(userName); err != nil {
log.Fatalf("fail to switch to user %s err: %v", userName, err)
}
}
return nil
}
func switchUser(userName string) error {
if _, exist := userConfMap[userName]; !exist {
return fmt.Errorf("user %s doesn't exist", userName)
}
curUser = userConfMap[userName]
return nil
}
func executor(sql string) {
sql = strings.TrimSpace(sql)
sql = strings.TrimSuffix(sql, ";")
lowerSQL := strings.ToLower(sql)
if sql == "" || strings.HasPrefix(sql, "#") || strings.HasPrefix(sql, "-") || strings.HasPrefix(sql, "/") {
return
}
if sql == "quit" || sql == "exit" {
fmt.Fprintln(os.Stdout, "Bye ^_^")
os.Exit(0)
}
if strings.HasPrefix(lowerSQL, "switch") {
items := strings.Split(lowerSQL, " ")
if len(items) != 2 {
fmt.Fprintf(os.Stderr, "expect `switch $user`, but got: %v", lowerSQL)
return
}
userName := items[1]
if err := switchUser(userName); err != nil {
fmt.Fprintf(os.Stderr, "switch failed %v\n", err)
}
return
}
if strings.HasPrefix(lowerSQL, "use") {
items := strings.Split(lowerSQL, " ")
if len(items) != 2 {
fmt.Fprintf(os.Stderr, "expect `use $dbName`, but got: %v", lowerSQL)
return
}
curDBName = items[1]
return
}
runSql(curDBName, sql, &curUser, sync)
}
func completer(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{}
}
func livePrefix() (string, bool) {
if len(curDBName) > 0 {
return fmt.Sprintf("[%s]%s> ", curDBName, curUser.UserName), true
}
return fmt.Sprintf("%s> ", curUser.UserName), true
}
func runSourceMode() {
if sourceFile == "" {
log.Fatalf("miss sourceFile parameters")
}
fi, err := os.Open(sourceFile)
if err != nil {
log.Fatalf("read input fail: %v", sourceFile)
}
defer fi.Close()
bs := bufio.NewScanner(fi)
for {
if !bs.Scan() {
break
}
sql := bs.Text()
log.Println(sql)
sql = strings.TrimSpace(sql)
sql = strings.TrimSuffix(sql, ";")
lowerSQL := strings.ToLower(sql)
if sql == "" || strings.HasPrefix(sql, "#") || strings.HasPrefix(sql, "-") || strings.HasPrefix(sql, "/") {
continue
}
if strings.HasPrefix(lowerSQL, "switch") {
items := strings.Split(lowerSQL, " ")
if len(items) != 2 {
log.Fatalf("expect `switch $user`, but got: %v", lowerSQL)
}
userName := items[1]
if err := switchUser(userName); err != nil {
log.Fatalf("switch failed: %v\n", err)
}
continue
}
if err := runSql("", sql, &curUser, sync); err != nil {
log.Fatalf("run sql err: %v", err)
}
}
}
func runPromptMode() {
p := prompt.New(executor, completer,
prompt.OptionPrefix("> "),
prompt.OptionLivePrefix(livePrefix),
prompt.OptionPrefixTextColor(prompt.Yellow),
)
//p.Run() will not support multiple-line input,
//Refer to: https://github.com/c-bata/go-prompt/issues/25
for {
inputStr := p.Input()
for _, sql := range strings.Split(inputStr, ";") {
executor(sql)
}
}
}
func newUserCredential(user *UserCredential) *scql.SCDBCredential {
return &scql.SCDBCredential{
User: &scql.User{
AccountSystemType: scql.User_NATIVE_USER,
User: &scql.User_NativeUser_{
NativeUser: &scql.User_NativeUser{
Name: user.UserName,
Password: user.Password,
},
},
},
}
}
func runSql(dbName, sql string, userAuth *UserCredential, sync bool) (err error) {
user := newUserCredential(userAuth)
startTime := time.Now()
httpClient := &http.Client{Timeout: 1800 * time.Second}
stub := client.NewClient(host, httpClient, pollingTimes, time.Duration(pollingTimeIntervalSeconds)*time.Second)
if len(dbName) > 0 {
stub.SetDBName(dbName)
}
var result *scql.SCDBQueryResultResponse
if sync {
result, err = stub.SubmitAndGet(user, sql)
if err != nil {
fmt.Fprintf(os.Stderr, "[submit and get]err: %v\n", err)
return err
}
} else {
submitResponse, err := stub.Submit(user, sql)
if err != nil {
fmt.Fprintf(os.Stderr, "[submit]err: %v\n", err)
return err
}
if submitResponse.ScdbSessionId == "" {
fmt.Fprintf(os.Stderr, "[submit]errorCode: %v, msg: %v\n", submitResponse.Status.Code, submitResponse.Status.Message)
return fmt.Errorf("[submit]errorCode: %v, msg: %v", submitResponse.Status.Code, submitResponse.Status.Message)
}
result, err = fetchResult(user, submitResponse.ScdbSessionId, pollingTimes, pollingTimeIntervalSeconds, stub)
if err != nil {
fmt.Fprintf(os.Stderr, "[fetch]err: %v\n", err)
return err
}
}
if result.GetStatus().GetCode() == int32(scql.Code_OK) {
for _, warn := range result.Warnings {
fmt.Printf("Warning : %v\n", warn.Reason)
}
if len(result.OutColumns) > 0 {
fmt.Fprintf(os.Stdout, "[fetch]\n")
// table view
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
if err := tableview.ConvertToTable(result.OutColumns, table); err != nil {
log.Fatalf("[fetch]convertToTable with err:%v\n", err)
}
fmt.Printf("%v rows in set: (%v)\n", table.NumLines(), time.Since(startTime))
table.Render()
return nil
} else if result.AffectedRows > 0 {
fmt.Printf("[fetch] %v rows affected\n", result.AffectedRows)
return nil
}
fmt.Fprintf(os.Stdout, "[fetch] OK for DDL/DCL\n")
} else {
fmt.Fprintf(os.Stdout, "[fetch]err: Code: %v, message:%v\n", result.GetStatus().GetCode(), result.GetStatus().GetMessage())
}
return nil
}
func fetchResult(user *scql.SCDBCredential, sessionId string, maxFetchNum int, fetchInterval int, c *client.Client) (*scql.SCDBQueryResultResponse, error) {
count := 0
for {
if maxFetchNum > 0 && count > maxFetchNum {
return nil, fmt.Errorf("exceed max number of fetch number %v", maxFetchNum)
}
count += 1
<-time.After(time.Duration(pollingTimeIntervalSeconds) * time.Second)
response, err := c.FetchOnce(user, sessionId)
if err != nil {
return nil, err
}
if response.GetStatus().GetCode() == int32(scql.Code_NOT_READY) {
continue
}
return response, nil
}
}
func main() {
initFlags()
err := rootCmd.Execute()
if err != nil {
log.Fatalf("Failed to execute cobra.Command: %v", err)
}
}