-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlc.go
More file actions
754 lines (683 loc) · 29 KB
/
sqlc.go
File metadata and controls
754 lines (683 loc) · 29 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//
// //// SQLite Cloud
// //////////// ///
// /// /// /// Product : SQLite Cloud CLI Application
// /// /// /// Version : 1.1.0
// // /// /// /// Date : 2021/10/08
// /// /// /// /// Author : Andreas Pfeil
// /// /// /// ///
// /// ////////// /// /// Description : Features: Connection Strings,
// //// /// /// Batch processing, Many output
// //// ////////// /// formats, Line truncation for Terminals,
// //// //// History, Static & Dynamic Autocomplete
// //// /////
// /// Copyright : 2021 by SQLite Cloud Inc.
//
// -----------------------------------------------------------------------TAB=2
package main
import (
sqlitecloud "github.com/sqlitecloud/sqlitecloud-go"
"bufio"
"errors"
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/docopt/docopt-go"
"github.com/peterh/liner"
"golang.org/x/term"
)
var app_name = "sqlc"
var long_name = "SQLite Cloud Command Line Application"
var version = "version 1.1.0"
var copyright = "(c) 2021 by SQLite Cloud Inc."
var history_file = fmt.Sprintf("~/.%s_history.txt", app_name)
var banner = fmt.Sprintf(` _____
/ / %s, %s
/ ___/ / %s
\ ___/ /
\_ ___/ Enter ".help" for usage hints.`, long_name, version, copyright)
var usage = long_name + ` Command Line Interface.
Usage:
sqlc [URL] [options] [<FILE>...]
sqlc -?|--help|--version
Arguments:
URL "sqlitecloud://user:pass@host.com:port/dbname?timeout=10&compress=NO"
FILE... Execute SQL commands from FILE(s) after connecting to the SQLite Cloud database
Examples:
sqlc "sqlitecloud://user:pass@host.com:8860/dbname?timeout=10&compress=lz4&tls=intern"
sqlc --host hostname -u user --password=pass -d dbname -c LZ4 --tls=no
sqlc --version
sqlc -?
General Options:
--cmd COMMAND Run "COMMAND" before executing FILE... or reading from stdin
-l, --list List available databases, then exit
-d, --dbname NAME Use database NAME
-b, --bail Stop after hitting an error
-?, --help Show this screen
--version Display version information
Output Format Options:
-o, --output FILE Switch to BATCH mode, execute SQL Commands and send output to FILE, then exit.
In BATCH mode, the default output format is switched to QUOTE.
--echo Disables --quiet, print command(s) before execution
--quiet Disables --echo, run command(s) quietly (no messages, only query output)
--noheader Turn headers off
--nullvalue TEXT Set text string for NULL values [default: "NULL"]
--newline SEP Set output row separator [default: "\r\n"]
--separator SEP Set output column separator [default: "|"]
--format (LIST|CSV|QUOTE|TABS|LINE|JSON|HTML|XML|MARKDOWN|TABLE|BOX)
Specify the Output mode [default::BOX]
Connection Options:
-h, --host HOSTNAME Connect to SQLite Cloud database server host name [default::localhost]
-p, --port PORT Use specified port to connect to SQLIte Cloud database server [default::8860]
-u, --user USERNAME Use USERNAME for authentication
-w, --password PASSWORD Use PASSWORD for authentication
-t, --timeout SECS Set Timeout for network operations to SECS seconds [default::10]
-c, --compress (NO|LZ4) Use line compression [default::NO]
-z, --zerotext Text ends with 0 value
-m, --memory Use in-memory database
-e, --create Create database if it does not exist
-i, --nonlinearizable Use non-linearizable mode for queries
-a, --apikey KEY Use API key for authentication
-k, --token TOKEN Use TOKEN for authentication
-n, --noblob Disable BLOB support
-x, --maxdata SIZE Set maximum data size for queries
-y, --maxrows ROWS Set maximum number of rows for queries
-r, --maxrowset ROWS Set maximum number of rows per rowset for queries
--tls [YES|NO|INTERN|FILE] Encrypt the database connection using the host's root CA set (YES), a custom CA with a PEM from FILE (FILE), the internal SQLiteCloud CA (INTERN), or disable the encryption (NO) [default::YES]
`
var help = `
.help Show this message
.bail [on|off] Stop after hitting an error [default: off]
.echo [on|off] Print command(s) before execution [default: off]
.quiet [on|off] Run command(s) quietly (no messages, only query output) [default: on]
.noheader [on|off] Turn table headers off or on [default: off]
.nullvalue TEXT Set TEXT string for NULL values [default: "NULL"]
.newline TEXT Set output row separator [default: "\r\n"]
.separator TEXT Set output column separator [default: "<auto>"]
.format [LIST|CSV|QUOTE|TABS|LINE|JSON|HTML|XML|MARKDOWN|TABLE|BOX]
Specify the Output mode [default: BOX]
.width [-1|0|<num>] Sets the maximum allowed query result length per line to the
terminal width(-1), unlimited (0) or any other width(<num>) [default: -1]
.timeout Set Timeout for network operations to SECS seconds [default: 10]
.compress Use line compression [default: NO]
.exit, .quit Exit this program
If no parameter is specified, then the default value is used as the parameter value.
Boolean settings are toggled if no parameter is specified.
`
type Parameter struct {
URL string `docopt:"URL"`
OutFile string `docopt:"--output"`
Command string `docopt:"--cmd"`
List bool `docopt:"--list"`
Bail bool `docopt:"--bail"`
Echo bool `docopt:"--echo"`
Quiet bool `docopt:"--quiet"`
NoHeader bool `docopt:"--noheader"`
NullText string `docopt:"--nullvalue"`
NewLine string `docopt:"--newline"`
Separator string `docopt:"--separator"`
Format string `docopt:"--format"`
OutPutFormat int `docopt:"--outputformat"`
Host string `docopt:"--host"`
Port int `docopt:"--port"`
User string `docopt:"--user"`
Password string `docopt:"--password"`
Database string `docopt:"--dbname"`
Zerotext bool `docopt:"--zerotext"`
Memory bool `docopt:"--memory"`
Create bool `docopt:"--create"`
NonLinearizable bool `docopt:"--nonlinearizable"`
ApiKey string `docopt:"--apikey"`
Token string `docopt:"--token"`
NoBlob bool `docopt:"--noblob"`
MaxData int `docopt:"--maxdata"`
MaxRows int `docopt:"--maxrows"`
MaxRowset int `docopt:"--maxrowset"`
Timeout int `docopt:"--timeout"`
Compress string `docopt:"--compress"`
Tls string `docopt:"--tls"`
UseStdIn bool `docopt:"-"`
Files []string `docopt:"<FILE>"`
}
var tokens = []string{".echo ", ".help ", ".bail ", ".quiet ", ".noheader ", ".nullvalue ", ".newline ", ".separator ", ".format ",
".width ", ".quit ",
"SELECT ", "AS ", "FROM ", "JOIN ", "ON ", "USING ", "WHERE ", "LIKE ", "OR ", "AND ", "GROUP ", "ORDER ", "BY ", "ASC ", "DESC ", "LIMIT ", "TO ",
"INSERT ", "UPDATE ", "DROP ", "IF ", "NOT ", "EXISTS ", "FAIL ", "IGNORE ", "TABLE ", "VALUES ", "SET ", "INTO ",
"CREATE ", "ALTER ", "NULL ", "INTEGER ", "TEXT ", "PRIMARY ", "UNIQUE ", "DEFAULT ",
"ABORT ", "ACTION ", "AFTER ", "ALL ", "ALWAYS ", "ANALYZE ", "ADD ", "ATTACH ",
"AUTOINCREMENT ", "BEFORE ", "BEGIN ", "BETWEEN ", "CASCADE ", "CASE ", "CAST ", "CHECK ", "COLLATE ", "COLUMN ",
"COMMIT ", "CONFLICT ", "CONSTRAINT ", "CROSS ", "CURRENT ", "CURRENT_DATE ", "CURRENT_TIME ",
"CURRENT_TIMESTAMP ", "DATABASE ", "DEFERRABLE ", "DEFERRED ", "DELETE ", "DETACH ", "DISTINCT ",
"DO ", "EACH ", "ELSE ", "END ", "ESCAPE ", "EXCEPT ", "EXCLUDE ", "EXCLUSIVE ", "EXPLAIN ",
"FILTER ", "FIRST ", "FOLLOWING ", "FOR ", "FOREIGN ", "FULL ", "GENERATED ", "GLOB ", "GROUPS ",
"HAVING ", "IMMEDIATE ", "IN ", "INDEX ", "INDEXED ", "INITIALLY ", "INNER ", "INSTEAD ",
"INTERSECT ", "IS ", "ISNULL ", "KEY ", "LAST ", "LEFT ", "MATCH ", "MATERIALIZED ",
"NATURAL ", "NO ", "NOTHING ", "NOTNULL ", "NULLS ", "OF ", "OFFSET ",
"OTHERS ", "OUTER ", "OVER ", "PARTITION ", "PLAN ", "PRAGMA ", "PRECEDING ", "QUERY ", "RAISE ", "RANGE ",
"RECURSIVE ", "REFERENCES ", "REGEXP ", "REINDEX ", "RELEASE ", "RENAME ", "REPLACE ", "RESTRICT ", "RETURNING ",
"RIGHT ", "ROLLBACK ", "ROW ", "ROWS ", "SAVEPOINT ", "TEMP ", "TEMPORARY ", "THEN ",
"TIES ", "TRANSACTION ", "TRIGGER ", "UNBOUNDED ", "UNION ", "VACUUM ",
"VIEW ", "VIRTUAL ", "WHEN ", "WINDOW ", "WITH ", "WITHOUT ",
"ABS( ", "CHANGES( ", "CHAR( ", "COALESCE( ", "GLOB( ", "HEX( ", "IFNULL( ", "IIF( ", "INSTR( ", "LAST_INSERT_ROWID( ",
"LENGTH( ", "LIKE( ", "LIKELIHOOD( ", "LIKELY( ", "LOAD_EXTENSION( ", "LOWER( ", "LTRIM( ", "MAX( ", "MIN( ",
"NULLIF( ", "PRINTF( ", "QUOTE( ", "RANDOM() ", "RANDOMBLOB( ", "REPLACE( ", "ROUND( ", "RTRIM( ", "SIGN( ",
"SOUNDEX( ", "SQLITE_COMPILEOPTION_GET( ", "SQLITE_COMPILEOPTION_USED( ", "SQLITE_OFFSET( ", "SQLITE_SOURCE_ID() ",
"SQLITE_VERSION() ", "SUBSTR( ", "SUBSTRING( ", "TOTAL_CHANGES() ", "TRIM( ", "TYPEOF( ", "UNICODE( ", "UNLIKELY( ",
"UPPER( ", "ZEROBLOB( ", "DATE( ", "TIME( ", "DATETIME( ", "JULIANDAY( ", "STRFTIME( ", "DAYS ", "HOURS ", "MINUTES ",
"NOW", "SECONDS ", "MONTHS ", "YEARS ", "START OF MONTH ", "START OF YEAR ", "START OF DAY ", "WEEKDAY ", "UNIXEPOCH ",
"LOCALTIME ", "UTC ", "AVG( ", "COUNT( * ) ", "COUNT( ", "GROUP_CONCAT( ", "SUM( ", "TOTAL( ", "ACOS( ", "ACOSH( ",
"ASIN( ", "ASINH( ", "ATAN( ", "ATAN2( ", "ATANH( ", "CEIL( ", "CEILING( ", "COS( ", "COSH( ", "DEGREES( ", "EXP( ",
"FLOOR( ", "LN( ", "LOG( ", "LOG10( ", "LOG2( ", "MOD( ", "PI() ", "POW( ", "POWER( ", "RADIANS( ", "SIN( ", "SINH( ",
"SQRT( ", "TAN( ", "TANH( ", "TRUNC( ", "JSON( ", "JSON_ARRAY( ", "JSON_ARRAY_LENGTH( ", "JSON_EXTRACT( ",
"JSON_INSERT( ", "JSON_OBJECT( ", "JSON_PATCH( ", "JSON_REMOVE( ", "JSON_REPLACE( ", "JSON_SET( ", "JSON_TYPE( ",
"JSON_VALID( ", "JSON_QUOTE( ", "JSON_GROUP_ARRAY( ", "JSON_GROUP_OBJECT( ", "JSON_EACH( ", "JSON_TREE( ",
"LIST TABLES", "TABLES", "LIST DATABASES", "DATABASES", "LIST COMMANDS", "COMMANDS", "LIST INFO", "INFO",
"AUTH USER ", "USER", "PASS", "CLOSE CONNECTION ", "CONNECTION", "CREATE DATABASE ", "DATABASE",
"DISABLE PLUGIN ", "PLUGIN", "REMOVE DATABASE ", "REMOVE KEY ", "ENABLE PLUGIN ",
"GET DATABASE", "GET DATABASE ID", "ID", "GET KEY ", "LIST CONNECTIONS", "CONNECTIONS", "LIST DATABASE CONNECTIONS ",
"LIST DATABASE CONNECTIONS ID ", "LIST NODES", "LIST PLUGINS", "LIST CLIENT KEYS",
"LIST DATABASE KEYS", "LISTEN ", "NOTIFY ", "PING", "REMOVE NODE ", "SET KEY ", "UNLISTEN ", "UNUSE DATABASE", "USE DATABASE",
"on", "off", "enable", "disable", "true", "false",
}
var dynamic_tokens = []string{}
func replaceControlChars(in string) string {
for from, to := range map[string]string{"\\0": string(0), "\\a": "\a", "\\b": "\b", "\\t": "\t", "\\n": "\n", "\\v": "\v", "\\f": "\f", "\\r": "\r"} {
in = strings.ReplaceAll(in, from, to)
}
return in
}
func getFirstNoneEmptyString(args []string) string {
for _, v := range args {
v = strings.TrimSpace(v)
if v != "" {
return v
}
}
return ""
}
func parseParameters() (Parameter, error) {
parameter := Parameter{}
var outputformat int
// Parse Command Line Parameter (Attention: "::" -> ":<CTRL+Space>")
if p, err := docopt.ParseArgs(strings.ReplaceAll(usage, "::", ": "), nil, fmt.Sprintf("%s %s, %s", app_name, version, copyright)); err == nil {
// Preprocessing...
if format, _ := p.String("--format"); format == "" { // If --format was not specified, use default values...
if list, _ := p.Bool("--list"); list {
p["--format"] = "LIST"
} // use --format=LIST when in list mode
if output, _ := p.String("--output"); output != "" {
p["--format"] = "QUOTE"
} // use --format=QUOTE when in batch mode
}
if format, _ := p.String("--format"); format == "" {
p["--format"] = "BOX"
} // use --format=BOX when --format is stil not specified
format, err := p.String("--format")
if err != nil {
return Parameter{}, err
}
outputformat, err = sqlitecloud.GetOutputFormatFromString(format)
if err != nil {
return Parameter{}, err
}
p["--outputformat"] = outputformat
// If the connection string is set, parse and apply the connection string...
if url, isSet := p["URL"]; isSet && url != "<nil>" {
if conf, err := sqlitecloud.ParseConnectionString(reflect.ValueOf(url).String()); err == nil {
p["--host"] = getFirstNoneEmptyString([]string{dropError(p.String("--host")), conf.Host})
p["--user"] = getFirstNoneEmptyString([]string{dropError(p.String("--user")), conf.Username})
p["--password"] = getFirstNoneEmptyString([]string{dropError(p.String("--password")), conf.Password})
p["--dbname"] = getFirstNoneEmptyString([]string{dropError(p.String("--dbname")), conf.Database})
if conf.Port > 0 {
p["--port"] = getFirstNoneEmptyString([]string{dropError(p.String("--port")), fmt.Sprintf("%d", conf.Port)})
}
if conf.Timeout > 0 {
p["--timeout"] = getFirstNoneEmptyString([]string{dropError(p.String("--timeout")), fmt.Sprintf("%d", conf.Timeout)})
}
if conf.Compression {
p["--compress"] = getFirstNoneEmptyString([]string{dropError(p.String("--compress")), conf.CompressMode})
}
if conf.Zerotext {
if b, err := p.Bool("--zerotext"); err == nil {
p["--zerotext"] = b || conf.Zerotext
}
}
if conf.Memory {
if b, err := p.Bool("--memory"); err == nil {
p["--memory"] = b || conf.Memory
}
}
if conf.Create {
if b, err := p.Bool("--create"); err == nil {
p["--create"] = b || conf.Create
}
}
if conf.NonLinearizable {
if b, err := p.Bool("--nonlinearizable"); err == nil {
p["--nonlinearizable"] = b || conf.NonLinearizable
}
}
p["--tls"] = getFirstNoneEmptyString([]string{dropError(p.String("--tls")), conf.Pem})
if !conf.Secure {
p["--tls"] = "NO"
} else if conf.TlsInsecureSkipVerify {
p["--tls"] = "SKIP"
}
p["--apikey"] = getFirstNoneEmptyString([]string{dropError(p.String("--apikey")), conf.ApiKey})
p["--token"] = getFirstNoneEmptyString([]string{dropError(p.String("--token")), conf.Token})
if conf.NoBlob {
if b, err := p.Bool("--noblob"); err == nil {
p["--noblob"] = b || conf.NoBlob
}
}
if conf.MaxData > 0 {
p["--maxdata"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxdata")), fmt.Sprintf("%d", conf.MaxData)})
}
if conf.MaxRows > 0 {
p["--maxrows"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxrows")), fmt.Sprintf("%d", conf.MaxRows)})
}
if conf.MaxRowset > 0 {
p["--maxrowset"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxrowset")), fmt.Sprintf("%d", conf.MaxRowset)})
}
}
} else {
return Parameter{}, err
}
// Set default Values
p["--host"] = getFirstNoneEmptyString([]string{dropError(p.String("--host")), "localhost"})
p["--port"] = getFirstNoneEmptyString([]string{dropError(p.String("--port")), "8860"})
p["--timeout"] = getFirstNoneEmptyString([]string{dropError(p.String("--timeout")), "10"})
p["--compress"] = getFirstNoneEmptyString([]string{dropError(p.String("--compress")), sqlitecloud.CompressModeLZ4})
p["--tls"] = getFirstNoneEmptyString([]string{dropError(p.String("--tls")), "YES"})
p["--separator"] = getFirstNoneEmptyString([]string{dropError(p.String("--separator")), dropError(sqlitecloud.GetDefaultSeparatorForOutputFormat(outputformat)), "|"})
p["--maxdata"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxdata")), "0"})
p["--maxrows"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxrows")), "0"})
p["--maxrowset"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxrowset")), "0"})
// Fix invalid(=unset) parameters, quotation & control-chars
for k, v := range p {
switch reflect.ValueOf(v).Kind() {
case reflect.Invalid:
p[k] = ""
case reflect.String:
p[k] = replaceControlChars(strings.Trim(reflect.ValueOf(v).String(), "'\""))
default:
}
}
// for k, v := range p { fmt.Printf( "%s='%v'\r\n", k, v ) }
// Copy map data into Object
if err := p.Bind(¶meter); err != nil {
return Parameter{}, err
}
if parameter.NewLine == "" {
parameter.NewLine = "\r\n"
}
// Postprocessing...
if parameter.OutFile != "" {
parameter.Echo = false
parameter.Quiet = true
}
if q, _ := p.Bool("--echo"); q {
parameter.Echo = true
}
if parameter.Echo {
parameter.Quiet = false
}
if q, _ := p.Bool("--quiet"); q {
parameter.Quiet = true
parameter.Echo = false
}
} else {
return Parameter{}, err
}
return parameter, nil
}
func autocomplete(line string, pos int) (head string, suggestions []string, tail string) {
start := line[0:pos]
tail = strings.TrimPrefix(line[pos:], " ")
split := strings.LastIndex(start, " ")
head = ""
line = start
if split > 0 {
head = start[0 : split+1]
line = start[split+1:]
}
// fmt.Printf( "start=>%s<, tail=>%s<, head=>%s<, line=>%s<\r\n", start, tail, head, line )
for _, token := range dynamic_tokens {
if strings.HasPrefix(strings.ToLower(token), strings.ToLower(line)) {
suggestions = append(suggestions, token)
}
}
for _, token := range tokens {
if strings.HasPrefix(strings.ToLower(token), strings.ToLower(line)) {
suggestions = append(suggestions, token)
}
}
return
}
func main() {
width := -1
out := bufio.NewWriter(os.Stdout)
if parameter, err := parseParameters(); err != nil {
fatal(out, fmt.Sprintf("ERROR: Could not parse arguments (%s).", err.Error()), ¶meter)
} else {
if parameter.OutFile != "" {
_ = os.Remove(parameter.OutFile)
if file, err := os.OpenFile(parameter.OutFile, os.O_CREATE|os.O_RDWR, 0664); err != nil {
fatal(out, fmt.Sprintf("ERROR: Could not create '%s' for writing", parameter.OutFile), ¶meter)
} else {
out = bufio.NewWriter(file)
width = 0
defer func() {
if err := file.Close(); err != nil {
fatal(out, fmt.Sprintf("ERROR: Could not close file '%s': %s", parameter.OutFile, err), ¶meter)
}
}()
}
}
// print( out, fmt.Sprintf( "%s %s, %s", long_name, version, copyright ), ¶meter )
config := sqlitecloud.SQCloudConfig{
Host: parameter.Host,
Port: parameter.Port,
Username: parameter.User,
Password: parameter.Password,
Token: parameter.Token,
Database: parameter.Database,
Timeout: time.Duration(parameter.Timeout) * time.Second,
Compression: parameter.Compress == sqlitecloud.CompressModeLZ4,
CompressMode: parameter.Compress,
Zerotext: parameter.Zerotext,
Memory: parameter.Memory,
Create: parameter.Create,
NonLinearizable: parameter.NonLinearizable,
ApiKey: parameter.ApiKey,
NoBlob: parameter.NoBlob,
MaxData: parameter.MaxData,
MaxRows: parameter.MaxRows,
MaxRowset: parameter.MaxRowset,
}
config.Secure, config.TlsInsecureSkipVerify, config.Pem = sqlitecloud.ParseTlsString(parameter.Tls)
var db *sqlitecloud.SQCloud = sqlitecloud.New(config)
if err := db.Connect(); err != nil {
fatal(out, fmt.Sprintf("ERROR: %s.", err.Error()), ¶meter)
} else {
defer db.Close()
db.Callback = func(conn *sqlitecloud.SQCloud, json string) {
print(out, json, ¶meter)
out.Flush()
}
//print( out, fmt.Sprintf( "Connected to %s.", parameter.Host ), ¶meter )
//print( out, strings.Split( help, "\n" )[ 0 ], ¶meter )
print(out, strings.ReplaceAll(banner, "<HOST>", parameter.Host), ¶meter)
print(out, "", ¶meter)
if parameter.List {
Execute(db, out, "LIST DATABASES", width, ¶meter)
os.Exit(0)
}
// Execute single Command
if parameter.Command != "" {
Execute(db, out, parameter.Command, width, ¶meter)
}
// Batch Mode starts here ///////////////////////////////////////////////////////////////
// Execute Files
if len(parameter.Files) > 0 {
ExecuteFiles(db, out, parameter.Files, width, ¶meter)
}
// Execute Stdin
if parameter.UseStdIn {
if err := ExecuteBuffer(db, out, os.Stdin, width, ¶meter); err != nil {
bail(out, fmt.Sprintf("Could not execute (%s)", err.Error()), ¶meter)
}
}
// End Batch Mode
if parameter.OutFile != "" {
os.Exit(0)
}
// Interactive Mode starts here /////////////////////////////////////////////////////////
editor := liner.NewLiner()
defer editor.Close()
editor.SetCtrlCAborts(true)
editor.SetMultiLineMode(true)
editor.SetWordCompleter(autocomplete)
if strings.HasPrefix(history_file, "~/") { // fix Home directory for POSIX and Windows
if dir, err := os.UserHomeDir(); err == nil {
history_file = fmt.Sprintf("%s/%s", dir, strings.TrimPrefix(history_file, "~/"))
}
}
if f, err := os.Open(history_file); err == nil {
editor.ReadHistory(f)
f.Close()
}
prompt := "sqlc > "
prompt = "\\u@\\H:\\p/\\d > "
refreshDB := false
Loop:
for {
out.Flush()
if refreshDB {
db.Database, _ = db.GetDatabase()
}
renderdPrompt := prompt
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\H", parameter.Host)
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\p", fmt.Sprintf("%d", parameter.Port))
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\u", parameter.User)
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\d", db.Database)
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\T", time.Now().Format("2006-01-02"))
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\t", time.Now().Format("15:04:05"))
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\w", fmt.Sprintf("/%s", dropError(os.Getwd())))
command, err := editor.Prompt(renderdPrompt)
switch err {
case io.EOF:
break
case nil:
switch tokens := strings.Split(strings.ToLower(strings.TrimSpace(command)), " "); tokens[0] {
case "":
continue Loop
case ".exit", "exit":
break Loop
case ".help":
print(out, help, ¶meter)
case ".bail":
parameter.Bail = getNextTokenValueAsBool(out, parameter.Bail, tokens, ¶meter)
case ".echo":
parameter.Echo = getNextTokenValueAsBool(out, parameter.Echo, tokens, ¶meter)
case ".quiet":
parameter.Quiet = getNextTokenValueAsBool(out, parameter.Quiet, tokens, ¶meter)
case ".noheader":
parameter.NoHeader = getNextTokenValueAsBool(out, parameter.NoHeader, tokens, ¶meter)
case ".width":
width = getNextTokenValueAsInteger(out, width, -1, tokens, ¶meter)
case ".nullvalue":
parameter.NullText = getNextTokenValueAsString(out, parameter.NullText, "NULL", "", tokens, ¶meter)
case ".newline":
parameter.NewLine = getNextTokenValueAsString(out, parameter.NewLine, "\r\n", "", tokens, ¶meter)
case ".prompt":
prompt = getNextTokenValueAsString(out, prompt, "sqlc >", "", tokens, ¶meter)
case ".separator":
parameter.Separator = getNextTokenValueAsString(out, parameter.Separator, "<auto>", "", tokens, ¶meter)
case ".timeout":
parameter.Timeout = getNextTokenValueAsInteger(out, parameter.Timeout, 10, tokens, ¶meter)
case ".compress":
parameter.Compress = getNextTokenValueAsString(out, parameter.Compress, sqlitecloud.CompressModeNo, "|no|lz4|", tokens, ¶meter)
db.Compress(parameter.Compress)
case ".format":
newFormat := getNextTokenValueAsString(out, parameter.Format, "BOX", "|list|csv|quote|tabs|line|json|html|xml|markdown|table|box|", tokens, ¶meter)
if newFormat != parameter.Format {
parameter.Format = newFormat
parameter.Separator = "<auto>"
parameter.OutPutFormat, _ = sqlitecloud.GetOutputFormatFromString(newFormat)
}
default:
Execute(db, out, command, width, ¶meter)
switch tokens[0] {
case ".quit", "quit":
break Loop
default:
editor.AppendHistory(command)
}
commandLower := strings.ToLower(command)
refreshDB = strings.Contains(commandLower, "use database") || strings.Contains(commandLower, "unuse database") || strings.Contains(commandLower, "create table")
}
default:
bail(out, err.Error(), ¶meter)
}
}
if f, err := os.Create(history_file); err == nil {
_, _ = editor.WriteHistory(f)
_ = f.Close()
}
}
}
}
func Execute(db *sqlitecloud.SQCloud, out *bufio.Writer, cmd string, width int, Settings *Parameter) {
Seperator := Settings.Separator
if cmd == "" {
return
}
if Settings.OutPutFormat == sqlitecloud.OUTFORMAT_XML {
Seperator = cmd
}
if width < 0 {
if w, _, err := term.GetSize(0); err == nil {
width = w
}
}
if Settings.Echo {
print(out, cmd, Settings)
}
start := time.Now()
if res, err := db.Select(cmd); res != nil {
defer res.Free()
if err == nil {
if !res.IsRowSet() || (res.GetNumberOfRows() > 0 && res.GetNumberOfColumns() > 0) {
res.DumpToWriter(out, Settings.OutPutFormat, Settings.NoHeader, Seperator, Settings.NullText, Settings.NewLine, uint(width), Settings.Quiet)
}
if res.IsRowSet() {
print(out, fmt.Sprintf("Rows: %d - Cols: %d: %s Time: %s", res.GetNumberOfRows(), res.GetNumberOfColumns(), renderByteCount(int64(res.GetUncompressedChuckSizeSum())), time.Since(start)), Settings)
}
}
} else {
bail(out, err.Error(), Settings)
// bail(out, err.Error()+fmt.Sprintf(" (%d:%d:%d)", db.GetErrorCode(), db.GetExtErrorCode(), db.GetErrorOffset()), Settings)
}
print(out, "", Settings) // Empty line
}
func ExecuteBuffer(db *sqlitecloud.SQCloud, out *bufio.Writer, in *os.File, width int, Settings *Parameter) error {
if scanner := bufio.NewScanner(in); scanner != nil {
for scanner.Scan() {
line := scanner.Text()
if strings.ToUpper(line) == ".GOTO_PROMPT" {
return nil // break out of sql script
}
if strings.TrimSpace(line) != "" {
Execute(db, out, line, width, Settings)
}
}
return scanner.Err() // nil or some error
}
return errors.New("Could not instanciate the line scanner")
}
func ExecuteFile(db *sqlitecloud.SQCloud, out *bufio.Writer, FilePath string, width int, Settings *Parameter) error {
file, err := os.Open(FilePath)
if err == nil {
defer func() {
_ = file.Close()
}()
if err := ExecuteBuffer(db, out, file, width, Settings); err != nil {
return err
}
}
return err
}
func ExecuteFiles(db *sqlitecloud.SQCloud, out *bufio.Writer, FilePathes []string, width int, Settings *Parameter) error {
for _, file := range FilePathes {
err := ExecuteFile(db, out, file, width, Settings)
if err != nil {
return err
}
}
return nil
}
func print(out *bufio.Writer, Message string, Settings *Parameter) {
if !Settings.Quiet {
_, _ = io.WriteString(out, Message)
_, _ = io.WriteString(out, Settings.NewLine)
}
}
func bail(out *bufio.Writer, Message string, Settings *Parameter) {
print(out, Message, Settings)
if Settings.Bail {
os.Exit(1)
}
}
func fatal(out *bufio.Writer, Message string, Settings *Parameter) {
_, _ = io.WriteString(out, Message)
_, _ = io.WriteString(out, Settings.NewLine)
_ = out.Flush()
os.Exit(1)
}
func renderByteCount(count int64) string {
const unit = 1000
if count < unit {
return fmt.Sprintf("%d Bytes", count)
}
div, exp := int64(unit), 0
for n := count / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cBytes", float64(count)/float64(div), "kMGTPE"[exp])
}
func dropError(val string, err error) string { return val }
func getNextTokenValueAsBool(out *bufio.Writer, oldValue bool, tokens []string, Settings *Parameter) bool {
switch len(tokens) {
case 1:
return !oldValue
case 2:
switch tokens[1] {
case "on", "true", "1", "enable":
return true
case "off", "false", "0", "disable":
return false
default:
bail(out, fmt.Sprintf("SYNTHAX ERROR: Invalid parameter \"%s\".", tokens[1]), Settings)
}
default:
bail(out, "SYNTHAX ERROR: Wrong number of parameters.", Settings)
}
return oldValue
}
func getNextTokenValueAsInteger(out *bufio.Writer, oldValue int, defaultValue int, tokens []string, Settings *Parameter) int {
switch len(tokens) {
case 1:
return defaultValue
case 2:
if i, err := strconv.Atoi(tokens[1]); err == nil {
return i
}
bail(out, fmt.Sprintf("SYNTHAX ERROR: \"%s\" is not a valid number.", tokens[1]), Settings)
default:
bail(out, "SYNTHAX ERROR: Wrong number of parameters.", Settings)
}
return oldValue
}
func getNextTokenValueAsString(out *bufio.Writer, oldValue string, defaultValue string, allowedValues string, tokens []string, Settings *Parameter) string {
switch len(tokens) {
case 1:
return defaultValue
case 2:
if allowedValues == "" {
return replaceControlChars(tokens[1])
}
if strings.Contains(allowedValues, fmt.Sprintf("|%s|", tokens[1])) {
return tokens[1]
}
bail(out, fmt.Sprintf("SYNTHAX ERROR: Invalid parameter \"%s\".", tokens[1]), Settings)
default:
bail(out, "SYNTHAX ERROR: Wrong number of parameters.", Settings)
}
return oldValue
}