-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (70 loc) · 1.49 KB
/
Copy pathmain.go
File metadata and controls
82 lines (70 loc) · 1.49 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
dbPath := "testdb.kv"
kv := &KV{Path: dbPath}
if err := kv.Open(); err != nil {
fmt.Println("Failed to open KV store:", err)
return
}
defer kv.Close()
db := &DB{Path: dbPath, kv: kv}
fmt.Println("Welcome to the GopherSQL Interactive SQL Shell!")
fmt.Println("Type your SQL queries below. Type 'exit' or 'quit' to close.")
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("db> ")
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
continue
}
if strings.EqualFold(input, "exit") || strings.EqualFold(input, "quit") {
break
}
tx := &DBTX{}
db.Begin(tx)
res, err := ExecuteQuery(tx, input)
if err != nil {
fmt.Println("Error:", err)
db.Abort(tx)
continue
}
if err := db.Commit(tx); err != nil {
fmt.Println("Commit Error:", err)
continue
}
if res != nil {
// It was a SELECT query
if results, ok := res.([]Record); ok {
if len(results) == 0 {
fmt.Println("(0 rows)")
} else {
for _, rec := range results {
for i, val := range rec.Vals {
if i > 0 {
fmt.Print(" | ")
}
if val.Type == TYPE_INT64 {
fmt.Printf("%s: %d", rec.Cols[i], val.I64)
} else {
fmt.Printf("%s: %s", rec.Cols[i], string(val.Str))
}
}
fmt.Println()
}
fmt.Printf("(%d rows)\n", len(results))
}
}
} else {
fmt.Println("Success.")
}
}
}