-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
216 lines (190 loc) · 4.1 KB
/
Copy pathexample_test.go
File metadata and controls
216 lines (190 loc) · 4.1 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package zu_test
import (
"context"
"errors"
"fmt"
"log"
zu "github.com/tamnd/zu-go"
)
// open is the two lines every example starts with, on a database that
// never touches the filesystem.
func open() (*zu.DB, *zu.Conn) {
db, err := zu.Memory()
if err != nil {
log.Fatal(err)
}
conn, err := db.Connect(context.Background())
if err != nil {
log.Fatal(err)
}
return db, conn
}
func Example() {
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
rows, err := conn.Query(ctx, `UNWIND [1, 2, 3] AS n RETURN n, n * n AS square`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for row := range rows.All() {
var n, square int64
if err := row.Scan(&n, &square); err != nil {
log.Fatal(err)
}
fmt.Println(n, square)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
// Output:
// 1 1
// 2 4
// 3 9
}
func ExampleCollect() {
type row struct {
N int64 `zu:"n"`
Word string `zu:"word"`
}
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
rows, err := conn.Query(ctx, `UNWIND [1, 2] AS n RETURN n, "row" AS word`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
got, err := zu.Collect[row](rows)
if err != nil {
log.Fatal(err)
}
fmt.Println(got)
// Output: [{1 row} {2 row}]
}
func ExampleIter() {
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
rows, err := conn.Query(ctx, `UNWIND [1, 2, 3, 4, 5] AS n RETURN n`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// A single value rather than a struct, and a loop that stops when
// it has what it came for.
for n, err := range zu.Iter[int64](rows) {
if err != nil {
log.Fatal(err)
}
if n > 3 {
break
}
fmt.Println(n)
}
// Output:
// 1
// 2
// 3
}
func ExampleNamed() {
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
// Parameters are named rather than positional, so there is no order
// to get wrong. The dollar may be written or left off.
rows, err := conn.Query(ctx, `RETURN $a + $b AS total`,
zu.Named("a", 40), zu.Named("b", 2))
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var total int64
if rows.Next() {
if err := rows.Scan(&total); err != nil {
log.Fatal(err)
}
}
fmt.Println(total)
// Output: 42
}
func ExampleRows_Int64s() {
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
rows, err := conn.Query(ctx, `UNWIND [10, 20, 30] AS n RETURN n`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// The whole column, contiguous and without a copy. It belongs to
// the result and is valid exactly until Close.
col, err := rows.Int64s(0)
if err != nil {
log.Fatal(err)
}
var sum int64
for _, n := range col {
sum += n
}
fmt.Println(len(col), sum)
// Output: 3 60
}
func ExampleError() {
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
_, err := conn.Query(ctx, `RETRUN 1`)
var e *zu.Error
if errors.As(err, &e) {
fmt.Println("condition:", e.Code)
fmt.Println("retryable:", e.Retryable)
if e.Position.Valid() {
// The excerpt is the line the position is on, so a caller
// can underline the token rather than print five digits.
fmt.Println(e.Excerpt)
fmt.Printf("%*s\n", e.Position.Column, "^")
}
fmt.Println("docs:", e.DocURL)
}
fmt.Println("refused:", errors.Is(err, zu.Refused))
// Output:
// condition: 42001
// retryable: false
// RETRUN 1
// ^
// docs: https://zu.dev/docs/errors/42001
// refused: true
}
func ExampleTx() {
ctx := context.Background()
db, conn := open()
defer db.Close()
defer conn.Close()
tx, err := conn.Begin(ctx)
if err != nil {
log.Fatal(err)
}
// On the path where the commit worked this answers ErrDone, which
// is not a failure and is why it can be ignored here.
defer tx.Rollback(ctx)
if err := tx.Exec(ctx, `RETURN 1 AS one`); err != nil {
log.Fatal(err)
}
if err := tx.Commit(ctx); err != nil {
log.Fatal(err)
}
open, err := conn.InTransaction()
if err != nil {
log.Fatal(err)
}
fmt.Println("still open:", open)
// Output: still open: false
}