-
Notifications
You must be signed in to change notification settings - Fork 32
/
example_scanner_test.go
270 lines (229 loc) · 4.82 KB
/
example_scanner_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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package scan_test
import (
"database/sql"
"encoding/json"
"fmt"
"math/rand"
"os"
"github.com/blockloop/scan/v2"
_ "github.com/proullon/ramsql/driver"
)
func mustDB(name string, queries ...string) *sql.DB {
db, err := sql.Open("ramsql", name)
if err != nil {
panic(err)
}
for _, s := range queries {
_, err = db.Exec(s)
if err != nil {
panic(err)
}
}
return db
}
func exampleDB() *sql.DB {
return mustDB(fmt.Sprintf("%d", rand.Uint64()), `CREATE TABLE person (
id INTEGER NOT NULL,
name VARCHAR(120)
);`,
`INSERT INTO person (id, name) VALUES (1, 'brett', 1);`,
`INSERT INTO person (id, name) VALUES (2, 'fred', 1);`,
`INSERT INTO person (id) VALUES (3);`,
)
}
func exampleNestedDB() *sql.DB {
return mustDB(fmt.Sprintf("%d", rand.Uint64()), `
CREATE TABLE IF NOT EXISTS person (
id INT NOT NULL,
name VARCHAR(120),
company_id INT
);`,
`CREATE TABLE IF NOT EXISTS company (
id INTEGER NOT NULL,
name VARCHAR(120)
);
`,
`INSERT INTO person (id, name, company_id) VALUES (1, 'brett', 1);`,
`INSERT INTO person (id, name, company_id) VALUES (2, 'fred', 1);`,
`INSERT INTO company (id, name) VALUES (1, 'costco');`,
)
}
func ExampleRow() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT id,name FROM person LIMIT 1")
if err != nil {
panic(err)
}
var person struct {
ID int `db:"id"`
Name string `db:"name"`
}
err = scan.Row(&person, rows)
if err != nil {
panic(err)
}
err = json.NewEncoder(os.Stdout).Encode(&person)
if err != nil {
panic(err)
}
// Output:
// {"ID":1,"Name":"brett"}
}
func ExampleRowNested() {
db := exampleNestedDB()
defer db.Close()
rows, err := db.Query(`
SELECT person.id,person.name,company.name FROM person
JOIN company on company.id = person.company_id
LIMIT 1
`)
if err != nil {
panic(err)
}
var person struct {
ID int `db:"person.id"`
Name string `db:"person.name"`
Company struct {
Name string `db:"company.name"`
}
}
err = scan.RowStrict(&person, rows)
if err != nil {
panic(err)
}
err = json.NewEncoder(os.Stdout).Encode(&person)
if err != nil {
panic(err)
}
// Output:
// {"ID":1,"Name":"brett","Company":{"Name":"costco"}}
}
func ExampleRowStrict() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT id,name FROM person LIMIT 1")
if err != nil {
panic(err)
}
var person struct {
ID int
Name string `db:"name"`
}
err = scan.RowStrict(&person, rows)
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(&person)
// Output:
// {"ID":0,"Name":"brett"}
}
func ExampleRowPtr() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT id,name FROM person where id = 3 LIMIT 1")
if err != nil {
panic(err)
}
var person struct {
ID int
Name *string `db:"name"`
}
err = scan.RowStrict(&person, rows)
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(&person)
// Output:
// {"ID":0,"Name":null}
}
func ExampleRowPtrType() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT id,name FROM person where id = 3 LIMIT 1")
if err != nil {
panic(err)
}
type NullableString *string
var person struct {
ID int
Name NullableString `db:"name"`
}
err = scan.RowStrict(&person, rows)
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(&person)
// Output:
// {"ID":0,"Name":null}
}
func ExampleRow_scalar() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT name FROM person LIMIT 1")
if err != nil {
panic(err)
}
var name string
err = scan.Row(&name, rows)
if err != nil {
panic(err)
}
fmt.Printf("%q", name)
// Output:
// "brett"
}
func ExampleRows() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT id,name FROM person ORDER BY id ASC")
if err != nil {
panic(err)
}
var persons []struct {
ID int `db:"id"`
Name *string `db:"name"`
}
err = scan.Rows(&persons, rows)
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(&persons)
// Output:
// [{"ID":1,"Name":"brett"},{"ID":2,"Name":"fred"},{"ID":3,"Name":null}]
}
func ExampleRowsStrict() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT id,name FROM person ORDER BY id ASC")
if err != nil {
panic(err)
}
var persons []struct {
ID int
Name *string `db:"name"`
}
err = scan.Rows(&persons, rows)
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(&persons)
// Output:
// [{"ID":0,"Name":"brett"},{"ID":0,"Name":"fred"},{"ID":0,"Name":null}]
}
func ExampleRows_primitive() {
db := exampleDB()
defer db.Close()
rows, err := db.Query("SELECT name FROM person WHERE name IS NOT NULL ORDER BY id ASC")
if err != nil {
panic(err)
}
var names []string
err = scan.Rows(&names, rows)
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(&names)
// Output:
// ["brett","fred"]
}