-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
111 lines (95 loc) · 2.53 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
package main
import (
"flag"
"fmt"
"os"
"github.com/c-bata/go-prompt"
// "github.com/chdb-io/chdb-go/cli"
// "github.com/chdb-io/chdb-go/cli/completer"
// "github.com/chdb-io/chdb-go/cli/history"
"github.com/chdb-io/chdb-go/chdb"
)
func main() {
// Define command line flags
pathFlag := flag.String("path", "",
`Specify a custom path for the session, default is a temporary directory and
data will lost after exit. If you want to keep the data, specify a path to a directory.`)
helpFlag := flag.Bool("help", false,
`Show this help message and exit.
Usage: chdb-go [options] [sql [output format]]
Example:
./chdb-go 'SELECT 123' # default output CSV
./chdb-go 'SELECT 123' JSON
./chdb-go # enter interactive mode, data will lost after exit
./chdb-go --path sess_path # enter persistent interactive mode
`)
flag.Parse()
if *helpFlag {
flag.Usage()
return
}
// If path is specified or no additional arguments, enter interactive mode
if len(flag.Args()) == 0 {
var err error
var session *chdb.Session
if *pathFlag != "" {
session, err = chdb.NewSession(*pathFlag)
} else {
session, err = chdb.NewSession()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err)
os.Exit(1)
}
defer session.Close()
interactiveMode(session)
} else {
// Execute a single query from command line arguments
args := flag.Args()
sql := args[0]
format := "CSV" // Default format
if len(args) > 1 {
format = args[1]
}
result, err := chdb.Query(sql, format)
if err != nil {
fmt.Println(err)
return
}
// Print the result
fmt.Println(result)
}
}
func interactiveMode(session *chdb.Session) {
fmt.Println("Enter your SQL commands; type 'exit' to quit.")
p := prompt.New(
func(query string) {
if query == "exit" {
os.Exit(0)
}
result, err := session.Query(query, "CSV")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(result)
},
func(d prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "SELECT", Description: "SELECT"},
{Text: "INSERT", Description: "INSERT"},
{Text: "UPDATE", Description: "UPDATE"},
{Text: "DELETE", Description: "DELETE"},
{Text: "CREATE", Description: "CREATE"},
{Text: "ALTER", Description: "ALTER"},
{Text: "DROP", Description: "DROP"},
{Text: "DESCRIBE", Description: "DESCRIBE"},
{Text: "SHOW", Description: "SHOW"},
{Text: "OPTIMIZE", Description: "OPTIMIZE"},
}
},
prompt.OptionPrefix(":) "),
prompt.OptionTitle("chdb-go"),
)
p.Run()
}