-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesql.go
87 lines (68 loc) · 1.77 KB
/
esql.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/codenotary/immudb/embedded/store"
"github.com/codenotary/immudb/pkg/sql"
"github.com/olekukonko/tablewriter"
)
func main() {
catalogStore, err := store.Open("catalog", store.DefaultOptions())
if err != nil {
log.Fatal(err)
}
dataStore, err := store.Open("sqldata", store.DefaultOptions())
if err != nil {
log.Fatal(err)
}
engine, err := sql.NewEngine(catalogStore, dataStore, []byte("sql"))
if err != nil {
log.Fatal(err)
}
_, err = engine.ExecStmt("CREATE DATABASE db1")
if err != nil {
log.Fatal(err)
}
_, err = engine.ExecStmt("USE DATABASE db1")
if err != nil {
log.Fatal(err)
}
_, err = engine.ExecStmt("CREATE TABLE table1 (id INTEGER, title STRING, PRIMARY KEY id)")
if err != nil {
log.Fatal(err)
}
rowCount := 10
for i := 0; i < rowCount; i++ {
_, err = engine.ExecStmt(fmt.Sprintf("UPSERT INTO table1 (id, title) VALUES (%d, 'title%d')", i, i))
if err != nil {
log.Fatal(err)
}
}
time.Sleep(time.Duration(rowCount) * time.Millisecond)
r, err := engine.QueryStmt("SELECT id, title FROM table1")
if err != nil {
log.Fatal(err)
}
consoleTable := tablewriter.NewWriter(os.Stdout)
consoleTable.SetHeader([]string{"Id", "Title"})
for i := 0; i < rowCount; i++ {
row, err := r.Read()
if err != nil {
log.Fatal(err)
}
if uint64(i) != row.Values[0] {
log.Fatalf("expected %d, actual %d", uint64(i), row.Values[0])
}
if fmt.Sprintf("title%d", i) != row.Values[1] {
log.Fatalf("expected %s, actual %s", fmt.Sprintf("title%d", i), row.Values[1])
}
consoleRow := make([]string, 0)
for _, consoleCol := range row.Values {
consoleRow = append(consoleRow, fmt.Sprintf("%v", consoleCol))
}
consoleTable.Append(consoleRow)
}
consoleTable.Render()
}