forked from chaisql/chai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
145 lines (123 loc) · 2.95 KB
/
example_test.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
142
143
144
145
package genji_test
import (
"encoding/json"
"fmt"
"os"
"github.com/genjidb/genji"
"github.com/genjidb/genji/document"
)
type User struct {
ID int64
Name string
Age uint32
Address struct {
City string
ZipCode string
}
}
func Example() {
// Create a database instance, here we'll store everything in memory
db, err := genji.Open(":memory:")
if err != nil {
panic(err)
}
defer db.Close()
// Create a table. Genji tables are schemaless by default, you don't need to specify a schema.
err = db.Exec("CREATE TABLE user")
if err != nil {
panic(err)
}
// Create an index.
err = db.Exec("CREATE INDEX idx_user_name ON user (name)")
if err != nil {
panic(err)
}
// Insert some data
err = db.Exec("INSERT INTO user (id, name, age) VALUES (?, ?, ?)", 10, "foo", 15)
if err != nil {
panic(err)
}
// Insert some data using document notation
err = db.Exec(`INSERT INTO user VALUES {id: 12, "name": "bar", age: ?, address: {city: "Lyon", zipcode: "69001"}}`, 16)
if err != nil {
panic(err)
}
// Structs can be used to describe a document
err = db.Exec("INSERT INTO user VALUES ?, ?", &User{ID: 1, Name: "baz", Age: 100}, &User{ID: 2, Name: "bat"})
if err != nil {
panic(err)
}
// Query some documents
stream, err := db.Query("SELECT * FROM user WHERE id > ?", 1)
if err != nil {
panic(err)
}
// always close the result when you're done with it
defer stream.Close()
// Iterate over the results
err = stream.Iterate(func(d document.Document) error {
var u User
err = document.StructScan(d, &u)
if err != nil {
return err
}
fmt.Println(u)
return nil
})
if err != nil {
panic(err)
}
// Count results
count, err := stream.Count()
if err != nil {
panic(err)
}
fmt.Println("Count:", count)
// Get first document from the results
d, err := stream.First()
if err != nil {
panic(err)
}
// Scan into a struct
var u User
err = document.StructScan(d, &u)
if err != nil {
panic(err)
}
enc := json.NewEncoder(os.Stdout)
// Apply some manual transformations
err = stream.
// Filter all even ids
Filter(func(d document.Document) (bool, error) {
v, err := d.GetByField("id")
if err != nil {
return false, err
}
return int64(v.V.(float64))%2 == 0, err
}).
// Enrich the documents with a new field
Map(func(d document.Document) (document.Document, error) {
var fb document.FieldBuffer
err := fb.ScanDocument(d)
if err != nil {
return nil, err
}
fb.Add("group", document.NewTextValue("admin"))
return &fb, nil
}).
// Iterate on them
Iterate(func(d document.Document) error {
return enc.Encode(d)
})
if err != nil {
panic(err)
}
// Output:
// {10 foo 15 { }}
// {12 bar 16 {Lyon 69001}}
// {2 bat 0 { }}
// Count: 3
// {"id":10,"name":"foo","age":15,"group":"admin"}
// {"id":12,"name":"bar","age":16,"address":{"city":"Lyon","zipcode":"69001"},"group":"admin"}
// {"id":2,"name":"bat","age":0,"address":{"city":"","zipcode":""},"group":"admin"}
}