-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan.go
384 lines (344 loc) · 9.08 KB
/
scan.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package sqlpro
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
)
type voidScan struct{}
func (vs *voidScan) Scan(interface{}) error {
// Don't do anything
return nil
}
// scanRow scans one row into the given target
func scanRow(target reflect.Value, rows *sql.Rows) error {
var (
err error
cols []string
data []interface{}
targetV, fieldV reflect.Value
info structInfo
isSlice bool
isStruct bool
)
cols, err = rows.Columns()
if err != nil {
return err
}
data = make([]interface{}, len(cols))
if target.Kind() == reflect.Ptr {
if target.IsNil() {
// nil pointer
// if target.Type().Elem().Kind() == reflect.Struct {
target.Set(reflect.New(target.Type().Elem()))
// }
}
// log.Printf("Kind: %v", target.Elem().Kind())
if target.Elem().Kind() == reflect.Struct {
targetV = target.Elem()
} else {
targetV = target
}
} else {
targetV = target
}
switch targetV.Kind() {
case reflect.Struct:
info = getStructInfo(reflect.ValueOf(targetV.Interface()).Type())
isStruct = true
case reflect.Slice:
isSlice = true
var isPointer bool
if targetV.Type().Elem().Kind() == reflect.Ptr {
isPointer = true
}
// append placeholders to the slice
for range cols {
var newEl reflect.Value
if isPointer {
newEl = reflect.New(targetV.Type().Elem().Elem())
} else {
newEl = reflect.Indirect(reflect.New(targetV.Type().Elem()))
}
targetV.Set(reflect.Append(targetV, newEl))
}
}
switch targetV.Interface().(type) {
case time.Time, *time.Time:
isStruct = false
}
// if target.Kind() == reflect.Ptr {
// log.Printf("Target: %v %s %v %s", target.IsValid(), target.Type(), target.IsNil(), target.Type().Elem().Kind())
// }
nullValueByIdx := make(map[int]reflect.Value, 0)
for idx, col := range cols {
skip := false
// logrus.Infof("%v %v %v %v", idx, col, isStruct, isSlice)
if isStruct {
finfo, ok := info[col]
if !ok {
skip = true
} else {
fieldV = targetV.FieldByName(finfo.name)
if finfo.isJson {
// log.Printf("Setting field to json: %v idx: %d", finfo.name, idx)
data[idx] = &NullJson{}
nullValueByIdx[idx] = fieldV
continue
}
}
} else if isSlice {
fieldV = targetV.Index(idx)
} else {
if idx == 0 {
// first column will be mapped
fieldV = targetV
} else {
skip = true
}
}
if skip {
// column not mapped in struct, we still need to allocate
data[idx] = &voidScan{}
continue
}
// log.Printf("NIL?: %v %s %T", fieldV.IsValid(), fieldV.Type(), fieldV.Interface())
// Init Null Scanners for some Pointer Types
switch fieldV.Interface().(type) { // FIXME: we could use reflect's Type here
case *json.RawMessage, json.RawMessage:
data[idx] = &NullRawMessage{}
nullValueByIdx[idx] = fieldV
case *string, string:
data[idx] = &sql.NullString{}
nullValueByIdx[idx] = fieldV
case *int64, int64, *int32, int32, *int16, int16, *int8, int8, *int, int,
uint32, *uint32, uint16, *uint16, uint8, *uint8, uint64, *uint64, uint, *uint:
data[idx] = &sql.NullInt64{}
nullValueByIdx[idx] = fieldV
case *float64, float64, *float32, float32:
data[idx] = &sql.NullFloat64{}
nullValueByIdx[idx] = fieldV
case *bool, bool:
data[idx] = &sql.NullBool{}
nullValueByIdx[idx] = fieldV
case time.Time, *time.Time:
data[idx] = &NullTime{}
nullValueByIdx[idx] = fieldV
default:
if fieldV.Kind() != reflect.Ptr {
// Pass a pointer
data[idx] = fieldV.Addr().Interface()
} else {
if fieldV.IsNil() {
fieldV.Set(reflect.New(fieldV.Type().Elem()))
}
data[idx] = fieldV.Interface()
}
}
}
err = rows.Scan(data...)
if err != nil {
return err
}
// Read back data from Null scanners which we used above
for idx, fieldV := range nullValueByIdx {
switch v := data[idx].(type) {
case *NullJson:
if (*v).Valid {
// unmarshal
newData := reflect.New(fieldV.Type())
err = json.Unmarshal((*v).Data, newData.Interface())
if err != nil {
return errors.Wrapf(err, "Error unmarshalling data: %q", string((*v).Data))
}
fieldV.Set(reflect.Indirect(reflect.Value(newData)))
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
continue
case *NullRawMessage:
if (*v).Valid {
if fieldV.Type().Kind() == reflect.Ptr {
fieldV.Set(reflect.ValueOf(&(*v).Data))
} else {
fieldV.Set(reflect.ValueOf((*v).Data))
}
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
continue
}
switch v0 := fieldV.Interface().(type) {
case *string, *int32, *int16, *int8, *int64, *uint64, *uint, *uint16, *uint32, *float64, *int, *bool:
switch v := data[idx].(type) {
case *sql.NullBool:
if (*v).Valid {
fieldV.Set(reflect.ValueOf(&(*v).Bool))
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
case *sql.NullString:
if (*v).Valid {
fieldV.Set(reflect.ValueOf(&(*v).String))
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
case *sql.NullInt64:
if (*v).Valid {
i64 := (*v).Int64
switch v0.(type) {
case *int64:
fieldV.Set(reflect.ValueOf(&i64))
case *int32:
i32 := int32(i64)
fieldV.Set(reflect.ValueOf(&i32))
case *int16:
i16 := int16(i64)
fieldV.Set(reflect.ValueOf(&i16))
case *int8:
i8 := int8(i64)
fieldV.Set(reflect.ValueOf(&i8))
case *int:
i := int(i64)
fieldV.Set(reflect.ValueOf(&i))
case *uint64:
ui64 := uint64(i64)
fieldV.Set(reflect.ValueOf(&ui64))
case *uint32:
ui32 := uint32(i64)
fieldV.Set(reflect.ValueOf(&ui32))
case *uint16:
ui16 := uint16(i64)
fieldV.Set(reflect.ValueOf(&ui16))
case *uint8:
ui8 := uint8(i64)
fieldV.Set(reflect.ValueOf(&ui8))
case *uint:
ui := uint(i64)
fieldV.Set(reflect.ValueOf(&ui))
}
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
case *sql.NullFloat64:
if (*v).Valid {
f64 := (*v).Float64
switch v0.(type) {
case *float64:
fieldV.Set(reflect.ValueOf(&f64))
case *float32:
f32 := float32(f64)
fieldV.Set(reflect.ValueOf(&f32))
}
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
}
case string, int64, float64, int, int32, int8, int16:
switch v := data[idx].(type) {
case *sql.NullString:
fieldV.SetString((*v).String)
case *sql.NullInt64:
switch v0.(type) {
case int64, int32, int16, int8, int:
fieldV.SetInt((*v).Int64)
}
case *sql.NullFloat64:
fieldV.SetFloat((*v).Float64)
}
case uint64, uint32, uint16, uint8, uint:
switch v := data[idx].(type) {
case *sql.NullInt64:
fieldV.SetUint(uint64((*v).Int64))
}
case bool:
switch v := data[idx].(type) {
case *sql.NullBool:
fieldV.SetBool((*v).Bool)
}
case time.Time:
switch v := data[idx].(type) {
case *NullTime:
if (*v).Valid {
fieldV.Set(reflect.ValueOf(v.Time))
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
default:
panic(fmt.Sprintf("sqlpro: unable to read back time.Time into %q", cols[idx]))
}
case *time.Time:
switch v := data[idx].(type) {
case *NullTime:
if (*v).Valid {
fieldV.Set(reflect.ValueOf(&(*v).Time))
} else {
fieldV.Set(reflect.Zero(fieldV.Type()))
}
default:
panic(fmt.Sprintf("sqlpro: unable to read back *time.Time into %q", cols[idx]))
}
default:
panic(fmt.Sprintf("sqlpro: unable to read back null into %q: %T", cols[idx], fieldV.Interface()))
}
}
return nil
}
// Scan reads data from the given rows into the target.
//
// *int64, *string, etc: First column of first row
// *struct: First row
// []int64, []*int64, []string, []*string: First column, all rows
// []struct, []*struct: All columns, all rows
//
// The mapping into structs is done by analyzing the struct's tag names
// and using the given "db" key for the mapping. The mapping works on
// exported fields only. Use "-" as mapping name to ignore the field.
func Scan(target interface{}, rows *sql.Rows) error {
var (
targetValue reflect.Value
rowMode bool
err error
)
if target == nil {
panic(fmt.Errorf("Scan: target must not be <nil>."))
}
v := reflect.ValueOf(target)
if v.Type().Kind() != reflect.Ptr {
panic(fmt.Errorf("Scan: non-pointer %v", v.Type()))
}
targetValue = v.Elem()
if !targetValue.CanAddr() {
panic("Scan: Unable to use unadressable field as target.")
}
if targetValue.Type().Kind() != reflect.Slice {
rowMode = true
}
for rows.Next() {
if rowMode {
err = scanRow(targetValue, rows)
if err != nil {
return err
}
// Only one row in row mode
return nil
}
// slice mode
// create an item suitable for appending to the slice
rowValues := reflect.MakeSlice(targetValue.Type(), 1, 1)
rowValue := rowValues.Index(0)
err = scanRow(rowValue, rows)
if err != nil {
return err
}
targetValue.Set(reflect.Append(targetValue, rowValue))
}
if rowMode {
// If we get here with row mode, it means we have nothing found
// return an error
return ErrQueryReturnedZeroRows
}
return nil
}