-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.go
540 lines (494 loc) · 14.6 KB
/
sql.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package psql
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"time"
"unsafe"
"github.com/gopsql/db"
)
var (
ErrInvalidTarget = errors.New("target must be pointer of a struct, slice or map")
ErrNoConnection = errors.New("no connection")
ErrNoSQL = errors.New("no sql statements to execute")
ErrTypeAssertionFailed = errors.New("type assertion failed")
)
type (
// SQL can be created with Model.NewSQL()
SQL struct {
main interface {
String() string
}
model *Model
sql string
values []interface{}
}
Tx = db.Tx
jsonbRaw map[string]json.RawMessage
fieldsFunc = func([]string, string) []string
)
// Can be used in Find(), add table name to all field names.
var AddTableName fieldsFunc = func(fields []string, tableName string) (out []string) {
for _, field := range fields {
if strings.Contains(field, ".") {
out = append(out, field)
continue
}
out = append(out, tableName+"."+field)
}
return
}
func (j *jsonbRaw) Scan(src interface{}) error { // necessary for github.com/lib/pq
if src == nil {
return nil
}
switch source := src.(type) {
case string:
return json.Unmarshal([]byte(source), j)
case []byte:
return json.Unmarshal(source, j)
default:
return ErrTypeAssertionFailed
}
}
// Create new SQL with SQL statement as first argument, The rest
// arguments are for any placeholder parameters in the statement.
func (m Model) NewSQL(sql string, values ...interface{}) *SQL {
sql = strings.TrimSpace(sql)
if c, ok := m.connection.(db.ConvertParameters); ok {
sql, values = c.ConvertParameters(sql, values)
}
return &SQL{
model: &m,
sql: sql,
values: values,
}
}
// Perform operations on the chain.
func (s *SQL) Tap(funcs ...func(*SQL) *SQL) *SQL {
for i := range funcs {
s = funcs[i](s)
}
return s
}
func (s SQL) String() string {
if s.main != nil {
return s.main.String()
}
return s.sql
}
func (s SQL) Values() []interface{} {
return s.values
}
// MustQuery is like Query but panics if query operation fails.
func (s SQL) MustQuery(target interface{}) {
if err := s.Query(target); err != nil {
panic(err)
}
}
// Query executes the SQL query and put the results into the target.
// Target must be a pointer to a struct, a slice or a map.
// For use cases, see Find() and Select().
func (s SQL) Query(target interface{}) error {
return s.QueryCtxTx(context.Background(), nil, target)
}
// MustQueryCtx is like QueryCtx but panics if query operation fails.
func (s SQL) MustQueryCtx(ctx context.Context, target interface{}) {
if err := s.QueryCtx(ctx, target); err != nil {
panic(err)
}
}
// QueryCtx executes the SQL query and put the results into the target.
// Target must be a pointer to a struct, a slice or a map.
// For use cases, see Find() and Select().
func (s SQL) QueryCtx(ctx context.Context, target interface{}) error {
return s.QueryCtxTx(ctx, nil, target)
}
// MustQueryCtxTx is like QueryCtxTx but panics if query operation fails.
func (s SQL) MustQueryCtxTx(ctx context.Context, tx Tx, target interface{}) {
if err := s.QueryCtxTx(ctx, tx, target); err != nil {
panic(err)
}
}
// QueryCtxTx executes the SQL query and put the results into the target.
// Target must be a pointer to a struct, a slice or a map.
// For use cases, see Find() and Select().
func (s SQL) QueryCtxTx(ctx context.Context, tx Tx, target interface{}) error {
sqlQuery := s.String()
if sqlQuery == "" {
return nil
}
if s.model.connection == nil {
return ErrNoConnection
}
var rv reflect.Value
var rt reflect.Type
targetIsRV := false
switch v := target.(type) {
case *reflect.Value:
rv = *v
targetIsRV = true
case reflect.Value:
rv = v
targetIsRV = true
}
if targetIsRV {
rt = rv.Type()
if rt.Kind() == reflect.Ptr {
rv = reflect.Indirect(rv)
rt = rv.Type()
}
if !rv.CanAddr() {
return ErrInvalidTarget
}
} else {
rv = reflect.Indirect(reflect.ValueOf(target))
rt = reflect.TypeOf(target)
if rt.Kind() != reflect.Ptr {
return ErrInvalidTarget
}
rt = rt.Elem()
}
kind := rt.Kind()
if kind == reflect.Slice {
rt = rt.Elem()
}
var mi *modelInfo
if s.model.structType != nil && rt == s.model.structType {
// use model's existing info if type is the same
mi = s.model.modelInfo
} else {
// different type of struct
mi = &modelInfo{tableName: s.model.tableName}
mi.setColumnNamer(s.model.columnNamer)
mi.updateColumnNames(rt)
}
if kind == reflect.Struct { // if target is not a slice, use QueryRow instead
start := time.Now()
defer s.log(sqlQuery, s.values, start)
if tx != nil {
return mi.scan(rv, tx.QueryRowContext(ctx, sqlQuery, s.values...))
}
return mi.scan(rv, s.model.connection.QueryRowContext(ctx, sqlQuery, s.values...))
} else if kind == reflect.Map {
start := time.Now()
defer s.log(sqlQuery, s.values, start)
var rows db.Rows
var err error
if tx != nil {
rows, err = tx.QueryContext(ctx, sqlQuery, s.values...)
} else {
rows, err = s.model.connection.QueryContext(ctx, sqlQuery, s.values...)
}
if err != nil {
return err
}
defer rows.Close()
columns, _ := rows.Columns()
columnLen := len(columns)
if rv.IsNil() {
rv.Set(reflect.MakeMapWithSize(rt, 0))
}
mapKeyType, mapValueType := rt.Key(), rt.Elem()
isSlice := mapValueType.Kind() == reflect.Slice
valueTypes := mapValueTypes(rt)
for rows.Next() {
mapKeys, end, dests := newDestsForMapType(mapKeyType, mapValueType, columnLen)
if err := rows.Scan(dests...); err != nil {
return err
}
if isSlice {
slice := rv.MapIndex(mapKeys[0])
if !slice.IsValid() {
slice = reflect.MakeSlice(valueTypes[0], 0, 0)
}
rv.SetMapIndex(mapKeys[0], reflect.Append(slice, end))
continue
}
subMap := rv
i := 0
for ; i < len(mapKeys)-1; i++ { // map[type]map...
if !subMap.MapIndex(mapKeys[i]).IsValid() {
subMap.SetMapIndex(mapKeys[i], reflect.MakeMap(valueTypes[i]))
}
subMap = subMap.MapIndex(mapKeys[i])
}
subMap.SetMapIndex(mapKeys[i], end)
}
return rows.Err()
} else if kind != reflect.Slice {
return ErrInvalidTarget
}
start := time.Now()
defer s.log(sqlQuery, s.values, start)
var rows db.Rows
var err error
if tx != nil {
rows, err = tx.QueryContext(ctx, sqlQuery, s.values...)
} else {
rows, err = s.model.connection.QueryContext(ctx, sqlQuery, s.values...)
}
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
nv := reflect.New(rt).Elem()
if err := mi.scan(nv, rows); err != nil {
return err
}
rv.Set(reflect.Append(rv, nv))
}
return rows.Err()
}
// scan a scannable (Row or Rows) into every field of a struct
func (mi *modelInfo) scan(rv reflect.Value, scannable db.Scannable) error {
if rv.Kind() != reflect.Struct || (len(mi.modelFields) == 0 && len(mi.jsonbColumns) == 0) {
return scannable.Scan(rv.Addr().Interface())
}
f := rv.FieldByName(tableNameField)
if f.Kind() == reflect.String {
// hack
reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem().SetString(mi.tableName)
}
dests := []interface{}{}
for _, field := range mi.modelFields {
if field.Jsonb != "" {
continue
}
pointer := field.getFieldValueAddrFromStruct(rv)
dests = append(dests, pointer)
}
jsonbValues := []jsonbRaw{}
for range mi.jsonbColumns {
jsonb := jsonbRaw{}
dests = append(dests, &jsonb)
jsonbValues = append(jsonbValues, jsonb)
}
if err := scannable.Scan(dests...); err != nil {
return err
}
for _, jsonb := range jsonbValues {
for _, field := range mi.modelFields {
if field.Jsonb == "" {
continue
}
val, ok := jsonb[field.ColumnName]
if !ok {
continue
}
pointer := field.getFieldValueAddrFromStruct(rv)
if err := json.Unmarshal(val, pointer); err != nil {
if field.Strict {
return fmt.Errorf("error unmarshaling field %s of %s: %v", field.ColumnName, field.Jsonb, err)
}
continue
}
}
}
return nil
}
// MustQueryRow is like QueryRow but panics if query row operation fails.
func (s SQL) MustQueryRow(dest ...interface{}) {
if err := s.QueryRow(dest...); err != nil {
panic(err)
}
}
// QueryRow gets results from the first row, and put values of each column to
// corresponding dest. For use cases, see Insert().
//
// var u struct {
// name string
// id int
// }
// psql.NewModelTable("users", conn).Select("name, id").MustQueryRow(&u.name, &u.id)
func (s SQL) QueryRow(dest ...interface{}) error {
return s.QueryRowCtxTx(context.Background(), nil, dest...)
}
// MustQueryRowCtx is like QueryRowCtx but panics if query row operation fails.
func (s SQL) MustQueryRowCtx(ctx context.Context, dest ...interface{}) {
if err := s.QueryRowCtx(ctx, dest...); err != nil {
panic(err)
}
}
// QueryRowCtx gets results from the first row, and put values of each column
// to corresponding dest. For use cases, see Insert().
func (s SQL) QueryRowCtx(ctx context.Context, dest ...interface{}) error {
return s.QueryRowCtxTx(ctx, nil, dest...)
}
// MustQueryRowCtxTx is like QueryRowCtxTx but panics if query row operation
// fails.
func (s SQL) MustQueryRowCtxTx(ctx context.Context, tx Tx, dest ...interface{}) {
if err := s.QueryRowCtxTx(ctx, tx, dest...); err != nil {
panic(err)
}
}
// QueryRowCtxTx gets results from the first row, and put values of each column
// to corresponding dest. For use cases, see Insert().
func (s SQL) QueryRowCtxTx(ctx context.Context, tx Tx, dest ...interface{}) error {
sqlQuery := s.String()
if sqlQuery == "" {
return nil
}
if s.model.connection == nil {
return ErrNoConnection
}
start := time.Now()
defer s.log(sqlQuery, s.values, start)
if tx != nil {
return tx.QueryRowContext(ctx, sqlQuery, s.values...).Scan(dest...)
}
return s.model.connection.QueryRowContext(ctx, sqlQuery, s.values...).Scan(dest...)
}
// MustExecute is like Execute but panics if execute operation fails.
func (s SQL) MustExecute(dest ...interface{}) {
if err := s.Execute(dest...); err != nil {
panic(err)
}
}
// Execute executes a query without returning any rows by an UPDATE, INSERT, or
// DELETE. You can get number of rows affected by providing pointer of int or
// int64 to the optional dest. For use cases, see Update().
func (s SQL) Execute(dest ...interface{}) error {
return s.ExecuteCtxTx(context.Background(), nil, dest...)
}
// MustExecuteCtx is like ExecuteCtx but panics if execute operation fails.
func (s SQL) MustExecuteCtx(ctx context.Context, dest ...interface{}) {
if err := s.ExecuteCtx(ctx, dest...); err != nil {
panic(err)
}
}
// ExecuteCtx executes a query without returning any rows by an UPDATE,
// INSERT, or DELETE. You can get number of rows affected by providing pointer
// of int or int64 to the optional dest. For use cases, see Update().
func (s SQL) ExecuteCtx(ctx context.Context, dest ...interface{}) error {
return s.ExecuteCtxTx(ctx, nil, dest...)
}
// MustExecuteCtxTx is like ExecuteCtxTx but panics if execute operation fails.
func (s SQL) MustExecuteCtxTx(ctx context.Context, tx Tx, dest ...interface{}) {
if err := s.ExecuteCtxTx(ctx, tx, dest...); err != nil {
panic(err)
}
}
// ExecuteCtxTx executes a query without returning any rows by an UPDATE,
// INSERT, or DELETE. You can get number of rows affected by providing pointer
// of int or int64 to the optional dest. For use cases, see Update().
func (s SQL) ExecuteCtxTx(ctx context.Context, tx Tx, dest ...interface{}) error {
sqlQuery := s.String()
if sqlQuery == "" {
return ErrNoSQL
}
if s.model.connection == nil {
return ErrNoConnection
}
start := time.Now()
defer s.log(sqlQuery, s.values, start)
if tx != nil {
return returnRowsAffected(dest)(tx.ExecContext(ctx, sqlQuery, s.values...))
}
return returnRowsAffected(dest)(s.model.connection.ExecContext(ctx, sqlQuery, s.values...))
}
func (s SQL) log(sql string, args []interface{}, startTime time.Time) {
s.model.log(sql, args, time.Since(startTime))
}
func returnRowsAffected(dest []interface{}) func(db.Result, error) error {
return func(result db.Result, err error) error {
if err != nil {
return err
}
if len(dest) == 0 {
return nil
}
ra, err := result.RowsAffected()
if err != nil {
return err
}
switch x := dest[0].(type) {
case *int:
*x = int(ra)
case *int64:
*x = ra
}
return nil
}
}
// Get all element types of a map recursively, for example:
// mapValueTypes(reflect.TypeOf(map[string]map[int]map[bool]int{})) returns:
// [ map[int]map[bool]int, map[bool]int, int ]
func mapValueTypes(mapType reflect.Type) (types []reflect.Type) {
if mapType.Kind() != reflect.Map {
return
}
mapValueType := mapType.Elem()
types = append(types, mapValueType)
types = append(types, mapValueTypes(mapValueType)...)
return
}
// Make new destination pointers from map type for Scannable. The "end" is the
// last non-map type value. Map keys are paths to the "end" value.
func newDestsForMapType(mapKeyType, mapValueType reflect.Type, columnLen int) (mapKeys []reflect.Value, end reflect.Value, dests []interface{}) {
isSlice := mapValueType.Kind() == reflect.Slice
if isSlice {
mapValueType = mapValueType.Elem()
}
newMapKey := reflect.New(mapKeyType).Elem()
newMapVal := reflect.New(mapValueType).Elem()
switch mapKeyType.Kind() {
case reflect.Struct:
for i := 0; i < columnLen && i < mapKeyType.NumField(); i++ {
dests = append(dests, getAddrOfStructField(mapKeyType.Field(i), newMapKey.Field(i)))
}
case reflect.Array:
for i := 0; i < columnLen && i < mapKeyType.Len(); i++ {
dests = append(dests, newMapKey.Index(i).Addr().Interface())
}
default:
dests = append(dests, newMapKey.Addr().Interface())
}
mapKeys = append(mapKeys, newMapKey)
end = newMapVal
size := columnLen - len(dests)
switch mapValueType.Kind() {
case reflect.Struct:
if size == 1 {
if dest, ok := newMapVal.Addr().Interface().(sql.Scanner); ok {
dests = append(dests, dest)
return
}
}
for i := 0; i < size; i++ {
dests = append(dests, getAddrOfStructField(mapValueType.Field(i), newMapVal.Field(i)))
}
case reflect.Map:
if isSlice {
// can't handle this kind of data structure at the moment
panic("sorry, but map[type][]map... is not yet supported")
}
k, e, d := newDestsForMapType(mapValueType.Key(), mapValueType.Elem(), size)
mapKeys = append(mapKeys, k...)
end = e
dests = append(dests, d...)
case reflect.Slice:
newMapVal.Set(reflect.MakeSlice(reflect.SliceOf(mapValueType.Elem()), size, size))
fallthrough
case reflect.Array:
for i := 0; i < size; i++ {
dests = append(dests, newMapVal.Index(i).Addr().Interface())
}
default:
if size > 0 {
dests = append(dests, newMapVal.Addr().Interface())
}
}
return
}
func getAddrOfStructField(field reflect.StructField, value reflect.Value) interface{} {
if field.PkgPath == "" {
return value.Addr().Interface()
}
return reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Interface()
}