-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner_examples_test.go
327 lines (307 loc) · 7.93 KB
/
scanner_examples_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package sqlh_test
import (
"fmt"
"time"
"github.com/nofeaturesonlybugs/set"
"github.com/nofeaturesonlybugs/sqlh"
"github.com/nofeaturesonlybugs/sqlh/examples"
)
func ExampleScanner_Select_structSlice() {
type MyStruct struct {
Message string
Number int
}
db, err := examples.Connect(examples.ExSimpleMapper)
if err != nil {
fmt.Println(err.Error())
}
//
scanner := &sqlh.Scanner{
// Mapper is pure defaults. Uses exported struct names as column names.
Mapper: &set.Mapper{},
}
fmt.Println("Dest is slice struct:")
var rv []MyStruct
err = scanner.Select(db, &rv, "select * from mytable")
if err != nil {
fmt.Println(err.Error())
}
for _, row := range rv {
fmt.Printf("%v %v\n", row.Message, row.Number)
}
// Output: Dest is slice struct:
// Hello, World! 42
// So long! 100
}
func ExampleScanner_Select_structSliceOfPointers() {
type MyStruct struct {
Message string
Number int
}
db, err := examples.Connect(examples.ExSimpleMapper)
if err != nil {
fmt.Println(err.Error())
}
//
scanner := &sqlh.Scanner{
// Mapper is pure defaults. Uses exported struct names as column names.
Mapper: &set.Mapper{},
}
fmt.Println("Dest is slice of pointers:")
var rv []*MyStruct
err = scanner.Select(db, &rv, "select * from mytable")
if err != nil {
fmt.Println(err.Error())
}
for _, row := range rv {
fmt.Printf("%v %v\n", row.Message, row.Number)
}
// Output: Dest is slice of pointers:
// Hello, World! 42
// So long! 100
}
func ExampleScanner_Select_tags() {
type MyStruct struct {
Message string `json:"message"`
Number int `json:"value" db:"num"`
}
db, err := examples.Connect(examples.ExTags)
if err != nil {
fmt.Println(err.Error())
}
//
scanner := &sqlh.Scanner{
// Mapper uses struct tag db or json, db higher priority
Mapper: &set.Mapper{
Tags: []string{"db", "json"},
},
}
var rv []*MyStruct
err = scanner.Select(db, &rv, "select message, num from mytable")
if err != nil {
fmt.Println(err.Error())
}
for _, row := range rv {
fmt.Printf("%v %v\n", row.Message, row.Number)
}
// Output: Hello, World! 42
// So long! 100
}
func ExampleScanner_Select_nestedStruct() {
type Common struct {
Id int `json:"id"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
type MyStruct struct {
// Structs can share the structure in Common.
// This would work just as well if the embed was not a pointer.
// Note how set.Mapper.Elevated is set!
*Common
Message string `json:"message"`
Number int `json:"value" db:"num"`
}
db, err := examples.Connect(examples.ExNestedStruct)
if err != nil {
fmt.Println(err.Error())
}
//
scanner := &sqlh.Scanner{
//
Mapper: &set.Mapper{
Elevated: set.NewTypeList(Common{}),
Tags: []string{"db", "json"},
},
}
var rv []*MyStruct
err = scanner.Select(db, &rv, "select id, created, modified, message, num from mytable")
if err != nil {
fmt.Println(err.Error())
}
for _, row := range rv {
fmt.Printf("%v %v %v\n", row.Id, row.Message, row.Number)
}
// Output: 1 Hello, World! 42
// 2 So long! 100
}
func ExampleScanner_Select_nestedTwice() {
type Common struct {
Id int `json:"id"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
type Person struct {
Common
First string `json:"first"`
Last string `json:"last"`
}
// Note here the natural mapping of SQL columns to nested structs.
type Sale struct {
Common
// customer_first and customer_last map to Customer.
Customer Person `json:"customer"`
// contact_first and contact_last map to Contact.
Contact Person `json:"contact"`
}
db, err := examples.Connect(examples.ExNestedTwice)
if err != nil {
fmt.Println(err.Error())
}
//
scanner := &sqlh.Scanner{
// Mapper uses struct tag db or json, db higher priority.
// Mapper elevates Common to same level as other fields.
Mapper: &set.Mapper{
Elevated: set.NewTypeList(Common{}),
Join: "_",
Tags: []string{"db", "json"},
},
}
var rv []*Sale
query := `
select
s.id, s.created, s.modified,
s.customer_id, c.first as customer_first, c.last as customer_last,
s.vendor_id as contact_id, v.first as contact_first, v.last as contact_last
from sales s
inner join customers c on s.customer_id = c.id
inner join vendors v on s.vendor_id = v.id
`
err = scanner.Select(db, &rv, query)
if err != nil {
fmt.Println(err.Error())
}
for _, row := range rv {
fmt.Printf("%v %v.%v %v %v.%v %v\n", row.Id, row.Customer.Id, row.Customer.First, row.Customer.Last, row.Contact.Id, row.Contact.First, row.Contact.Last)
}
// Output: 1 10.Bob Smith 100.Sally Johnson
// 2 20.Fred Jones 200.Betty Walker
}
func ExampleScanner_Select_scalar() {
db, err := examples.Connect(examples.ExScalar)
if err != nil {
fmt.Println(err.Error())
}
scanner := sqlh.Scanner{
Mapper: &set.Mapper{},
}
var n int
err = scanner.Select(db, &n, "select count(*) as n from thetable")
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(n)
// Output: 64
}
func ExampleScanner_Select_scalarSlice() {
db, err := examples.Connect(examples.ExScalarSlice)
if err != nil {
fmt.Println(err.Error())
}
scanner := sqlh.Scanner{
Mapper: &set.Mapper{},
}
fmt.Println("Dest is slice of scalar:")
var ids []int
err = scanner.Select(db, &ids, "select id from thetable where col = ?", "some value")
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(ids)
// Output: Dest is slice of scalar:
// [1 2 3]
}
func ExampleScanner_Select_scalarSliceOfPointers() {
db, err := examples.Connect(examples.ExScalarSlice)
if err != nil {
fmt.Println(err.Error())
}
scanner := sqlh.Scanner{
Mapper: &set.Mapper{},
}
fmt.Println("Dest is slice of pointer-to-scalar:")
var ptrs []*int
err = scanner.Select(db, &ptrs, "select id from thetable where col = ?", "some value")
if err != nil {
fmt.Println(err.Error())
}
var ids []int
for _, ptr := range ptrs {
ids = append(ids, *ptr)
}
fmt.Println(ids)
// Output: Dest is slice of pointer-to-scalar:
// [1 2 3]
}
func ExampleScanner_Select_struct() {
db, err := examples.Connect(examples.ExStruct)
if err != nil {
fmt.Println(err.Error())
}
scanner := sqlh.Scanner{
Mapper: &set.Mapper{
Tags: []string{"db", "json"},
},
}
type Temp struct {
Min time.Time `json:"min"`
Max time.Time `json:"max"`
}
var dest *Temp
err = scanner.Select(db, &dest, "select min(col) as min, max(col) as max from thetable")
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(dest.Min.Format(time.RFC3339), dest.Max.Format(time.RFC3339))
// Output: 1970-01-01T00:00:00Z 2012-01-01T00:00:00Z
}
func ExampleScanner_Select_structNotFound() {
type Common struct {
Id int `json:"id"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
type Person struct {
Common
First string `json:"first"`
Last string `json:"last"`
}
// Note here the natural mapping of SQL columns to nested structs.
type Sale struct {
Common
// customer_first and customer_last map to Customer.
Customer Person `json:"customer"`
// contact_first and contact_last map to Contact.
Contact Person `json:"contact"`
}
//
db, err := examples.Connect(examples.ExStructNotFound)
if err != nil {
fmt.Println(err.Error())
}
scanner := sqlh.Scanner{
Mapper: &set.Mapper{
Elevated: set.NewTypeList(Common{}),
Tags: []string{"db", "json"},
Join: "_",
},
}
query := `
select
s.id, s.created, s.modified,
s.customer_id, c.first as customer_first, c.last as customer_last,
s.vendor_id as contact_id, v.first as contact_first, v.last as contact_last
from sales s
inner join customers c on s.customer_id = c.id
inner join vendors v on s.vendor_id = v.id
`
// When destination is a pointer to struct and no rows are found then the dest pointer
// remains nil and no error is returned.
var dest *Sale
err = scanner.Select(db, &dest, query)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("Is nil: %v\n", dest == nil)
// Output: Is nil: true
}