-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathqrm.go
More file actions
520 lines (405 loc) · 15.2 KB
/
Copy pathqrm.go
File metadata and controls
520 lines (405 loc) · 15.2 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package qrm
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/go-jet/jet/v2/internal/utils/must"
)
// Config holds the configuration settings for QRM scanning behavior.
type Config struct {
// StrictScan, when true, causes the scanning function to panic if it encounters any
// unused columns in the SQL query result. This ensures that every column is mapped
// to a field in the destination struct.
// Does not apply to statements build with SELECT_JSON_OBJ or SELECT_JSON_ARR
StrictScan bool
// StrictFieldMapping, when true, causes the scanning function to panic if it encounters any
// destination struct fields that do not have matching columns in the SQL query result.
//
// Optional fields:
// If a destination field (including struct/slice fields) is not always selected by a query,
// it can be marked as optional using `qrm:"optional"`. When StrictFieldMapping is enabled,
// unmapped fields under an optional field will not trigger a panic.
// Does not apply to statements build with SELECT_JSON_OBJ or SELECT_JSON_ARR
StrictFieldMapping bool
// JsonUnmarshalFunc is called by the Query method to unmarshal JSON query results created by
// SELECT_JSON_OBJ and SELECT_JSON_ARR statements.
// It can be replaced with any implementation that matches the standard "encoding/json" `Unmarshal` function signature.
// By default, it uses the `Unmarshal` function from Go's standard `encoding/json` package.
JsonUnmarshalFunc func(data []byte, v any) error
}
// GlobalConfig is the package-wide configuration for SQL scanning.
// This variable is not thread safe, and it should be modified only once, for instance, during application initialization.
var GlobalConfig = Config{
StrictScan: false,
StrictFieldMapping: false,
JsonUnmarshalFunc: json.Unmarshal,
}
// ErrNoRows is returned by Query when query result set is empty
var ErrNoRows = errors.New("qrm: no rows in result set")
// QueryJsonObj executes a SQL query that returns a JSON object, unmarshals the result into the provided destination,
// and returns the number of rows processed.
//
// The query must return exactly one row with a single column; otherwise, an error is returned.
//
// Parameters:
//
// ctx - The context for managing query execution (timeouts, cancellations).
// db - The database connection or transaction that implements the Queryable interface.
// query - The SQL query string to be executed.
// args - A slice of arguments to be used with the query.
// destPtr - A pointer to the variable where the unmarshaled JSON result will be stored.
// The destination should be a pointer to a struct or map[string]any.
//
// Returns:
//
// rowsProcessed - The number of rows processed by the query execution.
// err - An error if query execution or unmarshaling fails.
func QueryJsonObj(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error) {
must.BeInitializedPtr(destPtr, "jet: destination is nil")
must.BeTypeKind(destPtr, reflect.Ptr, jsonDestObjErr)
destType := reflect.TypeOf(destPtr).Elem()
must.BeTrue(destType.Kind() == reflect.Struct || destType.Kind() == reflect.Map, jsonDestObjErr)
return queryJson(ctx, db, query, args, destPtr)
}
// QueryJsonArr executes a SQL query that returns a JSON array, unmarshals the result into the provided destination,
// and returns the number of rows processed.
//
// The query must return exactly one row with a single column; otherwise, an error is returned.
//
// Parameters:
//
// ctx - The context for managing query execution (timeouts, cancellations).
// db - The database connection or transaction that implements the Queryable interface.
// query - The SQL query string to be executed.
// args - A slice of arguments to be used with the query.
// destPtr - A pointer to the variable where the unmarshaled JSON array will be stored.
// The destination should be a pointer to a slice of structs or []map[string]any.
//
// Returns:
//
// rowsProcessed - The number of rows processed by the query execution.
// err - An error if query execution or unmarshaling fails.
func QueryJsonArr(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error) {
must.BeInitializedPtr(destPtr, "jet: destination is nil")
must.BeTypeKind(destPtr, reflect.Ptr, jsonDestArrErr)
destType := reflect.TypeOf(destPtr).Elem()
must.BeTrue(destType.Kind() == reflect.Slice, jsonDestArrErr)
return queryJson(ctx, db, query, args, destPtr)
}
var jsonDestObjErr = "jet: SELECT_JSON_OBJ destination has to be a pointer to struct or pointer to map[string]any"
var jsonDestArrErr = "jet: SELECT_JSON_ARR destination has to be a pointer to slice of struct or pointer to []map[string]any"
func queryJson(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error) {
must.BeInitializedPtr(db, "jet: db is nil")
var rows *sql.Rows
rows, err = db.QueryContext(ctx, query, args...)
if err != nil {
return 0, err
}
defer rows.Close()
if !rows.Next() {
err = rows.Err()
if err != nil {
return 0, err
}
return 0, ErrNoRows
}
var jsonData []byte
err = rows.Scan(&jsonData)
if err != nil {
return 1, err
}
if jsonData != nil {
err = GlobalConfig.JsonUnmarshalFunc(jsonData, &destPtr)
if err != nil {
return 1, fmt.Errorf("jet: invalid json, %w", err)
}
}
if rows.Next() {
return 1, fmt.Errorf("jet: query returned more then one row")
}
err = rows.Close()
if err != nil {
return 1, err
}
return 1, nil
}
// Query executes a Query Result Mapping (QRM) of the provided SQL `query` with a list of parameterized arguments `args`
// over the database connection `db` using the provided context `ctx` and stores the result in the destination `destPtr`.
//
// The destination must be a pointer to either a struct or a slice of structs
// If the destination is a pointer to a struct and no rows are returned, the method returns qrm.ErrNoRows.
//
// Parameters:
//
// ctx - The context for managing query execution (timeouts, cancellations).
// db - The database connection or transaction implementing the Queryable interface.
// query - The SQL query string to be executed.
// args - A slice of arguments to be used with the query.
// destPtr - A pointer to the variable where the query result will be stored. This can be a pointer to a struct or a slice of structs.
//
// Returns:
//
// rowsProcessed - The number of rows processed by the query execution.
// err - An error if query execution or result mapping fails, or if no rows are found when a struct is expected.
func Query(ctx context.Context, db Queryable, query string, args []interface{}, destPtr interface{}) (rowsProcessed int64, err error) {
must.BeInitializedPtr(db, "jet: db is nil")
must.BeInitializedPtr(destPtr, "jet: destination is nil")
must.BeTypeKind(destPtr, reflect.Ptr, "jet: destination has to be a pointer to slice or pointer to struct")
destinationPtrType := reflect.TypeOf(destPtr)
if destinationPtrType.Elem().Kind() == reflect.Slice {
rowsProcessed, err := queryToSlice(ctx, db, query, args, destPtr)
if err != nil {
return rowsProcessed, fmt.Errorf("jet: %w", err)
}
return rowsProcessed, nil
} else if destinationPtrType.Elem().Kind() == reflect.Struct {
tempSlicePtrValue := reflect.New(reflect.SliceOf(destinationPtrType))
tempSliceValue := tempSlicePtrValue.Elem()
rowsProcessed, err := queryToSlice(ctx, db, query, args, tempSlicePtrValue.Interface())
if err != nil {
return rowsProcessed, fmt.Errorf("jet: %w", err)
}
if rowsProcessed == 0 {
return 0, ErrNoRows
}
// edge case when row result set contains only NULLs.
if tempSliceValue.Len() == 0 {
return rowsProcessed, nil
}
structValue := reflect.ValueOf(destPtr).Elem()
firstTempStruct := tempSliceValue.Index(0).Elem()
if structValue.Type().AssignableTo(firstTempStruct.Type()) {
structValue.Set(tempSliceValue.Index(0).Elem())
}
return rowsProcessed, nil
} else {
panic("jet: destination has to be a pointer to slice or pointer to struct")
}
}
// ScanOneRowToDest will scan one row into struct destination
func ScanOneRowToDest(scanContext *ScanContext, rows *sql.Rows, destPtr interface{}) error {
must.BeInitializedPtr(destPtr, "jet: destination is nil")
must.BeTypeKind(destPtr, reflect.Ptr, "jet: destination has to be a pointer to slice or pointer to struct")
if len(scanContext.row) == 0 {
return errors.New("empty row slice")
}
err := rows.Scan(scanContext.row...)
if err != nil {
return fmt.Errorf("jet: rows scan error, %w", err)
}
destValuePtr := reflect.ValueOf(destPtr)
scanContext.rowNum++
_, err = mapRowToStruct(scanContext, "", destValuePtr, nil)
if err != nil {
return fmt.Errorf("jet: failed to scan a row into destination, %w", err)
}
if scanContext.rowNum == 1 {
scanContext.ensureStrictness()
}
return nil
}
func queryToSlice(ctx context.Context, db Queryable, query string, args []interface{}, slicePtr interface{}) (rowsProcessed int64, err error) {
if ctx == nil {
ctx = context.Background()
}
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return
}
defer rows.Close()
scanContext, err := NewScanContext(rows)
if err != nil {
return
}
if len(scanContext.row) == 0 {
return
}
slicePtrValue := reflect.ValueOf(slicePtr)
for rows.Next() {
err = rows.Scan(scanContext.row...)
if err != nil {
return scanContext.rowNum, err
}
scanContext.rowNum++
_, err = mapRowToSlice(scanContext, "", slicePtrValue, nil)
if err != nil {
return scanContext.rowNum, err
}
if scanContext.rowNum == 1 {
scanContext.ensureStrictness()
}
}
err = rows.Close()
if err != nil {
return scanContext.rowNum, err
}
return scanContext.rowNum, rows.Err()
}
func mapRowToSlice(
scanContext *ScanContext,
groupKey string,
slicePtrValue reflect.Value,
field *reflect.StructField) (updated bool, err error) {
sliceElemType := getSliceElemType(slicePtrValue)
if isSimpleModelType(sliceElemType) {
updated, err = mapRowToBaseTypeSlice(scanContext, slicePtrValue, field)
return
}
must.TypeBeOfKind(sliceElemType, reflect.Struct, "jet: unsupported slice element type"+fieldToString(field))
structGroupKey := scanContext.getGroupKey(sliceElemType, field)
groupKey = concat(groupKey, ",", structGroupKey)
index, ok := scanContext.uniqueDestObjectsMap[groupKey]
if ok {
structPtrValue := getSliceElemPtrAt(slicePtrValue, index)
return mapRowToStruct(scanContext, groupKey, structPtrValue, field, true)
}
destinationStructPtr := newElemPtrValueForSlice(slicePtrValue)
updated, err = mapRowToStruct(scanContext, groupKey, destinationStructPtr, field)
if err != nil {
return
}
if updated {
scanContext.uniqueDestObjectsMap[groupKey] = slicePtrValue.Elem().Len()
err = appendElemToSlice(slicePtrValue, destinationStructPtr)
if err != nil {
return
}
}
return
}
func mapRowToBaseTypeSlice(scanContext *ScanContext, slicePtrValue reflect.Value, field *reflect.StructField) (updated bool, err error) {
index := 0
if field != nil {
typeName, columnName, _ := getTypeAndFieldName("", *field)
if index = scanContext.typeToColumnIndex(typeName, columnName); index < 0 {
return
}
}
rowElemPtr := scanContext.rowElemValueClonePtr(index)
if rowElemPtr.IsValid() && !rowElemPtr.IsNil() {
updated = true
err = appendElemToSlice(slicePtrValue, rowElemPtr)
if err != nil {
return
}
}
return
}
func mapRowToStruct(
scanContext *ScanContext,
groupKey string,
structPtrValue reflect.Value,
parentField *reflect.StructField,
onlySlices ...bool, // small optimization, not to assign to already assigned struct fields
) (updated bool, err error) {
mapOnlySlices := len(onlySlices) > 0
structType := structPtrValue.Type().Elem()
if scanContext.typesVisited.contains(&structType) {
return false, nil
}
scanContext.typesVisited.push(&structType)
defer scanContext.typesVisited.pop()
typeInf := scanContext.getTypeInfo(structType, parentField)
structValue := structPtrValue.Elem()
for i := 0; i < structValue.NumField(); i++ {
field := structType.Field(i)
fieldValue := structValue.Field(i)
if !fieldValue.CanSet() { // private field
continue
}
fieldMappingInfo := typeInf.fieldMappings[i]
switch fieldMappingInfo.Type {
case complexType:
var changed bool
changed, err = mapRowToDestinationValue(scanContext, concat(groupKey, ":", field.Name), fieldValue, &field)
if err != nil {
return
}
if changed {
updated = true
}
default:
if mapOnlySlices || fieldMappingInfo.rowIndex == -1 {
continue
}
scannedValue := scanContext.rowElemValue(fieldMappingInfo.rowIndex)
if !scannedValue.IsValid() {
setZeroValue(fieldValue) // scannedValue is nil, destination should be set to zero value
continue
}
updated = true
switch fieldMappingInfo.Type {
case implementsScanner:
initializeValueIfNilPtr(fieldValue)
fieldScanner := getScanner(fieldValue)
value := scannedValue.Interface()
err := fieldScanner.Scan(value)
if err != nil {
return updated, qrmAssignError(scannedValue, field, err)
}
case jsonUnmarshal:
value, ok := scannedValue.Interface().([]byte)
if !ok {
return updated, qrmAssignError(scannedValue, field, fmt.Errorf("value not convertable to []byte"))
}
fieldInterface := fieldValue.Addr().Interface()
err := json.Unmarshal(value, fieldInterface)
if err != nil {
return updated, qrmAssignError(scannedValue, field, fmt.Errorf("invalid json, %w", err))
}
default: // simple type
err := assign(scannedValue, fieldValue)
if err != nil {
return updated, qrmAssignError(scannedValue, field, err)
}
}
}
}
return
}
func qrmAssignError(scannedValue reflect.Value, field reflect.StructField, err error) error {
return fmt.Errorf(`can't assign %T(%q) to '%s %s': %w`, scannedValue.Interface(), scannedValue.Interface(),
field.Name, field.Type.String(), err)
}
func mapRowToDestinationValue(
scanContext *ScanContext,
groupKey string,
dest reflect.Value,
structField *reflect.StructField) (updated bool, err error) {
var destPtrValue reflect.Value
if dest.Kind() != reflect.Ptr {
destPtrValue = dest.Addr()
} else {
if dest.IsNil() {
destPtrValue = reflect.New(dest.Type().Elem())
} else {
destPtrValue = dest
}
}
updated, err = mapRowToDestinationPtr(scanContext, groupKey, destPtrValue, structField)
if err != nil {
return
}
if dest.Kind() == reflect.Ptr && dest.IsNil() && updated {
dest.Set(destPtrValue)
}
return
}
func mapRowToDestinationPtr(
scanContext *ScanContext,
groupKey string,
destPtrValue reflect.Value,
structField *reflect.StructField) (updated bool, err error) {
must.ValueBeOfTypeKind(destPtrValue, reflect.Ptr, "jet: internal error. Destination is not pointer.")
destValueKind := destPtrValue.Elem().Kind()
if destValueKind == reflect.Struct {
return mapRowToStruct(scanContext, groupKey, destPtrValue, structField)
} else if destValueKind == reflect.Slice {
return mapRowToSlice(scanContext, groupKey, destPtrValue, structField)
} else {
panic("jet: unsupported dest type: " + structField.Name + " " + structField.Type.String())
}
}