/
scan.go
411 lines (390 loc) · 12.6 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package tux_pgx_scan
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/araddon/dateparse"
"github.com/iancoleman/strcase"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/pkg/errors"
"reflect"
"strings"
"time"
"unsafe"
)
type dbconn interface {
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
}
func getStructPropertyName(columnName string) string {
if len(columnName) == 2 {
return strings.ToUpper(columnName)
} else {
if strings.ToLower(columnName[len(columnName)-2:]) == "id" {
return columnName[:len(columnName)-2] + strings.ToUpper(columnName[len(columnName)-2:])
}
return strcase.ToCamel(columnName)
}
}
// https://stackoverflow.com/questions/54119616/ignore-case-in-golang-reflection-fieldbyname
func getStructProperty(name string, v reflect.Value) (reflect.Value, error) {
name = strings.ToLower(strings.ReplaceAll(name, "_", ""))
structColumn := v.FieldByNameFunc(func(n string) bool { return strings.ToLower(n) == name })
if !structColumn.IsValid() {
return reflect.Value{}, errors.Errorf("rowI returned column name %v which was not found in the destination address", name)
} else {
return structColumn, nil
}
}
func placeData(structColumn reflect.Value, structColumnType reflect.Type, val interface{}) error {
switch val.(type) {
case string:
switch structColumn.Interface().(type) {
case time.Time:
myVal := val.(string)
if theTime, err := dateparse.ParseAny(myVal); err != nil {
return err
} else {
structColumn.Set(reflect.ValueOf(theTime))
}
default:
if structColumnType.Kind() == reflect.Slice ||
structColumnType.Kind() == reflect.Struct {
var result interface{}
if err := json.Unmarshal([]byte(val.(string)), &result); err == nil {
return placeData(structColumn, structColumnType, result)
}
}
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
}
case float64:
myVal := val.(float64)
switch structColumn.Interface().(type) {
case sql.NullInt64:
s := sql.NullInt64{
Int64: int64(myVal),
Valid: true,
}
structColumn.Set(reflect.ValueOf(s))
default:
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
}
case int32:
myVal := val.(int32)
switch structColumn.Interface().(type) {
case sql.NullInt64:
s := sql.NullInt64{
Int64: int64(myVal),
Valid: true,
}
structColumn.Set(reflect.ValueOf(s))
default:
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
}
case map[string]interface{}:
if err := doSingleRowProperty(false, structColumn, val); err != nil {
return err
}
case pgtype.TextArray:
myVal := val.(pgtype.TextArray)
var arr []string
if err := myVal.AssignTo(&arr); err != nil {
return errors.Errorf("could not assign pgtype.TextArray: %v", err)
} else {
switch structColumn.Kind() {
case reflect.Slice:
if !structColumn.CanAddr() {
return errors.New("cannot get address of slice element for pgtype.TextArray")
} else {
structColumn.Set(reflect.MakeSlice(structColumn.Type(), len(arr), len(arr)))
for idx, _ := range arr {
switch structColumn.Type().Elem().Kind() {
case reflect.Ptr:
structColumn.Index(idx).Set(reflect.ValueOf(&arr[idx]))
case reflect.String:
structColumn.Index(idx).Set(reflect.ValueOf(arr[idx]).Convert(structColumn.Index(idx).Type()))
default:
return errors.New("unknown type when appending to slice")
}
}
}
default:
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
}
}
case bool:
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
case pgtype.Int4Array:
myVal := val.(pgtype.Int4Array)
var arr []int
if err := myVal.AssignTo(&arr); err != nil {
return errors.Errorf("could not assign pgtype.Int4Array: %v", err)
} else {
switch structColumn.Kind() {
case reflect.Slice:
if !structColumn.CanAddr() {
return errors.New("cannot get address of slice element for pgtype.Int4Array")
} else if err := myVal.AssignTo(structColumn.Addr().Interface()); err != nil {
return errors.Errorf("could not scan to slice: %v", err)
}
default:
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
}
}
case sql.NullInt64:
myVal := val.(sql.NullInt64)
val := myVal.Int64
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
case pgtype.Numeric:
myVal := val.(pgtype.Numeric)
switch structColumn.Kind() {
case reflect.Float64:
var s float64
if err := myVal.AssignTo(&s); err != nil {
return errors.Errorf("could not set pgtype.Numeric: %v", err)
}
structColumn.Set(reflect.ValueOf(s))
case reflect.Struct: // if both sides are pgtype.Numbric, so just set it, convert may not be neccesarry
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
default:
return errors.Errorf("uknown format %v", structColumn.Kind())
}
case pgtype.Float8Array:
myVal := val.(pgtype.Float8Array)
var arr []float64
if err := myVal.AssignTo(&arr); err != nil {
return errors.Errorf("could not assign pgtype.Float8Array: %v", err)
} else {
switch structColumn.Kind() {
case reflect.Slice:
if !structColumn.CanAddr() {
return errors.New("cannot get address of slice element for pgtype.Int4Array")
} else if err := myVal.AssignTo(structColumn.Addr().Interface()); err != nil {
return errors.Errorf("could not scan to slice: %v", err)
}
default:
structColumn.Set(reflect.ValueOf(val).Convert(structColumnType))
}
}
default:
if reflect.TypeOf(val).Kind() == reflect.Slice && structColumn.Kind() == reflect.Slice {
if err := doSliceProperty(structColumn, val); err != nil {
return err
}
} else {
structColumn.Set(reflect.ValueOf(val).Convert(structColumn.Type()))
}
}
return nil
}
func doStructColumnProperty(originalColumnName string, currentElement reflect.Value, val interface{}) error {
structColumn, err := getStructProperty(originalColumnName, currentElement)
if err != nil {
return err
}
structColumnType := structColumn.Type()
if structColumn.Kind() == reflect.Ptr { // check if pointer
if structColumn.IsZero() { // check if pointer is not allocated
structColumn.Set(reflect.New(structColumnType.Elem())) // allocate
}
structColumn = structColumn.Elem()
structColumnType = structColumnType.Elem()
}
/**
for example to convert from reflect.Int32 to reflect.Int
TODO: i need to check her for errors and to provide proper error message with column
name and row number maybe, for example when getting a float to float64 instead of pg.Numeric
*/
if err := placeData(structColumn, structColumnType, val); err != nil {
return err
}
return nil
}
func doSingleRowProperty(isSlice bool, element reflect.Value, val interface{}) error {
var currentElement reflect.Value
if isSlice {
currentElement = reflect.New(element.Type().Elem())
} else {
currentElement = element
}
rowVal := reflect.ValueOf(val)
dataElement := currentElement
pointerDataElement := currentElement
for dataElement.Type().Kind() == reflect.Ptr {
if dataElement.IsZero() {
dataElement.Set(reflect.New(dataElement.Type().Elem()))
}
pointerDataElement = dataElement
dataElement = dataElement.Elem()
}
switch rowVal.Kind() {
case reflect.Map:
for _, columnNameVal := range rowVal.MapKeys() {
columnName := columnNameVal.Interface().(string)
myVal := rowVal.MapIndex(columnNameVal).Interface()
if myVal == nil {
continue
}
switch dataElement.Kind() {
case reflect.Struct:
if err := doStructColumnProperty(columnName, dataElement, myVal); err != nil {
return err
}
default:
fieldStructPropertyName := getStructPropertyName(columnName)
fieldVal := dataElement.FieldByName(fieldStructPropertyName)
if !fieldVal.IsValid() {
return errors.New("internal error: couldn't get field from a struct")
}
fieldVal.Set(reflect.ValueOf(myVal).Convert(fieldVal.Type()))
}
}
default:
dataElement.Set(rowVal.Convert(dataElement.Type()))
}
if isSlice {
if element.Type().Elem().Kind() == reflect.Ptr {
element.Set(reflect.Append(element, pointerDataElement))
} else {
element.Set(reflect.Append(element, dataElement))
}
} else {
if element.Type().Kind() == reflect.Ptr {
element.Set(pointerDataElement)
} else {
element.Set(dataElement)
}
}
return nil
}
func doSliceProperty(sliceVal reflect.Value, val interface{}) error {
if reflect.TypeOf(val).Kind() != reflect.Slice {
return errors.New("doSliceProperty got an element which is not a slice")
}
rows := val.([]interface{})
for _, row := range rows {
if err := doSingleRowProperty(true, sliceVal, row); err != nil {
return err
}
}
return nil
}
func BytesToString(b []byte) string {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{bh.Data, bh.Len}
return *(*string)(unsafe.Pointer(&sh))
}
type MyQueryScanRet struct {
Rows pgx.Rows
}
func (m *MyQueryScanRet) Scan(dest ...interface{}) error {
return m.Rows.Scan(dest...)
}
func MyQueryScan(ctx context.Context, conn dbconn, sql string, args ...interface{}) (*MyQueryScanRet, bool, error) {
if rows, err := conn.Query(ctx, sql, args...); err != nil {
return nil, true, errors.Errorf("could not select from db: %v", err)
} else {
defer rows.Close()
rowNumber := 0
for rows.Next() {
rowNumber++
if rowNumber > 1 {
return nil, true, errors.New("current version does not support result with more then one line")
}
ret := MyQueryScanRet{
Rows: rows,
}
return &ret, false, nil
}
return nil, true, nil
}
}
func MyQuery(ctx context.Context, conn dbconn, dstAddr interface{}, sql string, args ...interface{}) (bool, error) {
barAddrVal := reflect.ValueOf(dstAddr)
if rows, err := conn.Query(ctx, sql, args...); err != nil {
return true, errors.Errorf("could not select from db: %v", err)
} else {
defer rows.Close()
currentElement := barAddrVal.Elem()
rowNumber := 0
for rows.Next() {
rowNumber++
// log.Printf("working on row %v",rowNumber)
if barAddrVal.Elem().Kind() == reflect.Slice {
sliceElm := barAddrVal.Elem()
for sliceElm.Len() < rowNumber {
newItem := reflect.New(sliceElm.Type().Elem())
sliceElm.Set(reflect.Append(sliceElm, newItem.Elem()))
}
currentElement = barAddrVal.Elem().Index(rowNumber - 1)
if !currentElement.IsValid() {
return true, errors.New("slice item source is not valid")
}
}
if values, err := rows.Values(); err != nil {
return true, errors.Errorf("could not fetch values from db: %v", err)
} else {
fields := rows.FieldDescriptions()
for idx, column := range fields {
val := values[idx]
// log.Printf("working on column %s value %v",column.Name,val)
if val == nil {
continue
}
switch currentElement.Kind() {
case reflect.Struct:
if err := doStructColumnProperty(string(column.Name), currentElement, val); err != nil {
return true, err
}
default:
myVal := reflect.ValueOf(val) // if reflect.Kind = reflect.Interface, to change it
if currentElement.Kind() == reflect.Ptr {
if currentElement.Type().Elem().Kind() == reflect.Struct {
if currentElement.IsZero() {
currentElement.Set(reflect.New(currentElement.Type().Elem()))
}
if err := doStructColumnProperty(string(column.Name), currentElement.Elem(), val); err != nil {
return true, err
}
} else {
valIntPtr := reflect.New(currentElement.Type().Elem())
if myVal.Type().String() == "pgtype.Numeric" {
var num = myVal.Interface().(pgtype.Numeric)
var res float64
if err := num.AssignTo(&res); err != nil {
return false, err
}
resF := reflect.ValueOf(res)
valIntPtr.Elem().Set(resF.Convert(valIntPtr.Elem().Type()))
} else {
valIntPtr.Elem().Set(myVal.Convert(currentElement.Type().Elem()))
}
currentElement.Set(valIntPtr)
}
} else {
if currentElement.Kind() == reflect.String && myVal.Kind() == reflect.Array { //UUID
b := myVal.Interface()
a := fmt.Sprintf("%x", b)
currentElement.Set(reflect.ValueOf(a))
} else {
if myVal.Type().String() == "pgtype.Numeric" {
var num = myVal.Interface().(pgtype.Numeric)
var res float64
if err := num.AssignTo(&res); err != nil {
return false, err
}
resF := reflect.ValueOf(res)
currentElement.Set(resF.Convert(currentElement.Type()))
} else {
currentElement.Set(myVal.Convert(currentElement.Type()))
}
}
}
}
}
}
}
return rowNumber == 0, rows.Err()
}
}