-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (114 loc) · 3.35 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
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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/codenotary/immudb/pkg/api/schema"
"github.com/codenotary/immudb/pkg/client"
"google.golang.org/grpc/metadata"
"github.com/olekukonko/tablewriter"
)
func main() {
c, err := client.NewImmuClient(client.DefaultOptions())
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
lr, err := c.Login(ctx, []byte(`immudb`), []byte(`immudb`))
if err != nil {
log.Fatal(err)
}
md := metadata.Pairs("authorization", lr.Token)
ctx = metadata.NewOutgoingContext(ctx, md)
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: `
BEGIN TRANSACTION
CREATE TABLE Clients (id INTEGER, name STRING, deactive BOOLEAN, PRIMARY KEY id)
CREATE INDEX ON Clients(name)
COMMIT
`})
if err != nil {
log.Fatal(err)
}
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: `
BEGIN TRANSACTION
CREATE TABLE Products (id INTEGER, name STRING, price INTEGER, stock INTEGER, PRIMARY KEY id)
CREATE INDEX ON Products(name)
CREATE INDEX ON Products(stock)
COMMIT
`})
if err != nil {
log.Fatal(err)
}
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: `
BEGIN TRANSACTION
CREATE TABLE Orders (id INTEGER, ts INTEGER, client_id INTEGER, PRIMARY KEY id)
CREATE INDEX ON Orders(ts)
CREATE TABLE OrderItems (id INTEGER, order_id INTEGER, amount INTEGER, PRIMARY KEY id)
CREATE INDEX ON OrderItems(order_id)
COMMIT
`})
if err != nil {
log.Fatal(err)
}
// Ingest Clients
for i := 0; i < 100; i++ {
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: fmt.Sprintf("UPSERT INTO Clients (id, name) VALUES (%d, 'client%d')", i, i)})
if err != nil {
log.Fatal(err)
}
}
// Ingest Products
for i := 0; i < 100; i++ {
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: fmt.Sprintf("UPSERT INTO Products (id, name, price, stock) VALUES (%d, 'product%d', %d, %d)", i, i, i*10, 100-i)})
if err != nil {
log.Fatal(err)
}
}
// Ingest Orders
for i := 0; i < 200; i++ {
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: fmt.Sprintf("UPSERT INTO Orders (id, ts, client_id) VALUES (%d, NOW(), %d)", i, i%100)})
if err != nil {
log.Fatal(err)
}
}
// Ingest OrderItems
for i := 0; i < 1000; i++ {
_, err = c.SQLExec(ctx, &schema.SQLExecRequest{Sql: fmt.Sprintf("UPSERT INTO OrderItems (id, order_id, amount) VALUES (%d, %d, %d)", i, i%200, 10+i)})
if err != nil {
log.Fatal(err)
}
}
q := "SELECT id, name, deactive FROM Clients WHERE deactive != NULL OR name < 'client20'"
qres, err := c.SQLQuery(ctx, &schema.SQLQueryRequest{Sql: q})
if err != nil {
log.Fatal(err)
}
fmt.Printf("QUERY: '%s'\r\n", q)
renderQueryResult(qres)
fmt.Println()
q = "SELECT id, ts, c.name AS client_name FROM Orders INNER JOIN (Clients AS c) ON client_id = c.id WHERE id < 100"
qres, err = c.SQLQuery(ctx, &schema.SQLQueryRequest{Sql: q})
if err != nil {
log.Fatal(err)
}
fmt.Printf("QUERY: '%s'\r\n", q)
renderQueryResult(qres)
c.Logout(ctx)
}
func renderQueryResult(qres *schema.SQLQueryResult) {
consoleTable := tablewriter.NewWriter(os.Stdout)
cols := make([]string, len(qres.Columns))
for i, c := range qres.Columns {
cols[i] = c.Name
}
consoleTable.SetHeader(cols)
for _, r := range qres.Rows {
row := make([]string, len(r.Values))
for i, v := range r.Values {
row[i] = schema.RenderValue(v.Operation)
}
consoleTable.Append(row)
}
consoleTable.Render()
}