|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/c-bata/go-prompt" |
| 10 | + |
| 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" |
| 14 | + |
| 15 | + "github.com/chdb-io/chdb-go/chdb" |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + // Define command line flags |
| 20 | + pathFlag := flag.String("path", "", |
| 21 | +`Specify a custom path for the session, default is a temporary directory and |
| 22 | +data will lost after exit. If you want to keep the data, specify a path to a directory.`) |
| 23 | + |
| 24 | + helpFlag := flag.Bool("help", false, |
| 25 | +`Show this help message and exit. |
| 26 | + Usage: chdb-go [options] [sql [output format]] |
| 27 | + Example: |
| 28 | + ./chdb-go 'SELECT 123' # default output CSV |
| 29 | + ./chdb-go 'SELECT 123' JSON |
| 30 | + ./chdb-go # enter interactive mode, data will lost after exit |
| 31 | + ./chdb-go --path sess_path # enter persistent interactive mode |
| 32 | +`) |
| 33 | + |
| 34 | + flag.Parse() |
| 35 | + |
| 36 | + if *helpFlag { |
| 37 | + flag.Usage() |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // If path is specified or no additional arguments, enter interactive mode |
| 42 | + if len(flag.Args()) == 0 { |
| 43 | + var err error |
| 44 | + var session *chdb.Session |
| 45 | + if *pathFlag != "" { |
| 46 | + session, err = chdb.NewSession(*pathFlag) |
| 47 | + } else { |
| 48 | + session, err = chdb.NewSession() |
| 49 | + } |
| 50 | + if err != nil { |
| 51 | + fmt.Fprintf(os.Stderr, "Failed to create session: %s\n", err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + defer session.Close() |
| 55 | + |
| 56 | + interactiveMode(session) |
| 57 | + } else { |
| 58 | + // Execute a single query from command line arguments |
| 59 | + args := flag.Args() |
| 60 | + sql := args[0] |
| 61 | + format := "CSV" // Default format |
| 62 | + if len(args) > 1 { |
| 63 | + format = args[1] |
| 64 | + } |
| 65 | + |
| 66 | + result := chdb.Query(sql, format) |
| 67 | + if result == nil { |
| 68 | + fmt.Println("No result or an error occurred.") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // Print the result |
| 73 | + fmt.Println(result) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func interactiveMode(session *chdb.Session) { |
| 78 | + fmt.Println("Enter your SQL commands; type 'exit' to quit.") |
| 79 | + |
| 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 | + 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 | + }), |
| 101 | + ) |
| 102 | + |
| 103 | + p.Run() |
| 104 | +} |
| 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