-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.go
More file actions
778 lines (674 loc) · 18.5 KB
/
exec.go
File metadata and controls
778 lines (674 loc) · 18.5 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package sqlpro
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"maps"
"reflect"
"slices"
"strings"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
)
// checkData checks that the given data is either one of:
//
// *[]*strcut
// *[]struct
// []*struct
// []struct
// *struct
//
// For structs the function returns true, nil, for slices false, nil
func checkData(data any) (rv reflect.Value, structMode bool, err error) {
erro := func() (reflect.Value, bool, error) {
return rv, false, fmt.Errorf("Insert/Update needs a struct or slice of structs.")
}
rv = reflect.Indirect(reflect.ValueOf(data))
switch rv.Type().Kind() {
case reflect.Slice:
switch rv.Type().Elem().Kind() {
case reflect.Ptr:
if rv.Type().Elem().Elem().Kind() != reflect.Struct {
return erro()
}
case reflect.Interface, reflect.Struct:
default:
return rv, false, fmt.Errorf("Insert/Update needs a slice of structs. Have: %s", rv.Type().Elem().Kind())
}
case reflect.Struct:
structMode = true
default:
return erro()
}
return rv, structMode, nil
}
func (db2 *db) Insert(table string, data any) error {
return db2.InsertContext(context.Background(), table, data)
}
// Insert takes a table name and a struct and inserts
// the record in the DB.
// The given data needs to be:
//
// *[]*strcut
// *[]struct
// []*struct
// []struct
// struct
// *struct
//
// sqlpro will executes one INSERT statement per row.
// result.LastInsertId will be used to set the first primary
// key column.
func (db2 *db) InsertContext(ctx context.Context, table string, data any) error {
var (
rv reflect.Value
structMode bool
err error
)
rv, structMode, err = checkData(data)
if err != nil {
return err
}
if !structMode {
for i := 0; i < rv.Len(); i++ {
row := reflect.Indirect(rv.Index(i))
insert_id, structInfo, err := db2.insertStruct(ctx, table, row.Interface())
if err != nil {
return err
}
pk := structInfo.onlyPrimaryKey()
if pk != nil && pk.structField.Type.Kind() == reflect.Int64 {
setPrimaryKey(row.FieldByName(pk.name), insert_id)
}
}
} else {
insert_id, structInfo, err := db2.insertStruct(ctx, table, rv.Interface())
if err != nil {
return err
}
pk := structInfo.onlyPrimaryKey()
// log.Printf("PK: %d", insert_id)
if pk != nil && rv.CanAddr() {
switch pk.structField.Type.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
setPrimaryKey(rv.FieldByName(pk.name), insert_id)
}
}
}
// data
return nil
}
func setPrimaryKey(rv reflect.Value, id int64) {
switch rv.Type().Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
rv.SetInt(id)
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
rv.SetUint(uint64(id))
default:
err := fmt.Errorf("Unknown type to set primary key: %s", rv.Type())
panic(err)
}
}
func (db2 *db) InsertBulk(table string, data any) error {
return db2.InsertBulkContext(context.Background(), table, data)
}
// InsertBulk takes a table name and a slice of struct and inserts
// the record in the DB with one Exec.
// The given data needs to be:
//
// *[]*struct
// *[]struct
// []*struct
// []struct
//
// sqlpro will executes one INSERT statement per call.
func (db2 *db) InsertBulkContext(ctx context.Context, table string, data any) error {
return db2.insertBulkContext(ctx, table, data, false, nil)
}
// InsertBulkOnConflictDoNothingContext works like InsertBulkContext but adds a
// "ON CONFLICT DO NOTHING" to the insert command.
func (db2 *db) InsertBulkOnConflictDoNothingContext(ctx context.Context, table string, data any, cols ...string) error {
return db2.insertBulkContext(ctx, table, data, true, cols)
}
type copyFromData struct {
keyMap map[string]*fieldInfo
columns []string
rows []map[string]any
values []any
nextCounter int
db *db
}
func newCopyFromData(db *db, keyMap map[string]*fieldInfo, rows []map[string]any) (cfd *copyFromData) {
return ©FromData{
keyMap: keyMap,
columns: slices.Collect(maps.Keys(keyMap)),
rows: rows,
nextCounter: -1,
db: db,
}
}
func (cfd copyFromData) Columns() []string {
return cfd.columns
}
func (cfd copyFromData) Len() int64 {
return int64(len(cfd.rows))
}
func (cfd *copyFromData) Next() bool {
if len(cfd.rows)-1 > cfd.nextCounter {
cfd.nextCounter++
return true
}
return false
}
func (cfd copyFromData) Err() error {
return nil
}
func (cfd copyFromData) Values() (values []any, err error) {
defer func() {
r := recover()
if r == nil {
return
}
err = fmt.Errorf("panic: %v", r)
}()
values = make([]any, len(cfd.columns))
row := cfd.rows[cfd.nextCounter]
for idx, col := range cfd.columns {
values[idx] = cfd.db.valueForInsert(row[col], cfd.keyMap[col])
}
return values, nil
}
func (db2 *db) copyFrom(ctx context.Context, table string, data *copyFromData) error {
pgxConn := db2.pgxConn()
if pgxConn == nil {
panic("copyFrom needs pgx connection")
}
rowsAffected, err := pgxConn.CopyFrom(ctx, pgx.Identifier{table}, data.Columns(), data)
if err != nil {
return err
}
if rowsAffected != data.Len() {
err = ErrMismatchedRowsAffected
}
return nil
}
func (db2 *db) insertBulkContext(ctx context.Context, table string, data any, onConflictDoNothing bool, conflictCols []string) (err error) {
var (
rv reflect.Value
structMode bool
)
rv, structMode, err = checkData(data)
if err != nil {
return err
}
if structMode {
return fmt.Errorf("InsertBulk: Need Slice to insert bulk.")
}
key_map := map[string]*fieldInfo{}
rows := []map[string]any{}
if rv.Len() == 0 {
return nil
}
for i := 0; i < rv.Len(); i++ {
row := reflect.Indirect(rv.Index(i)).Interface()
values, structInfo, err := db2.valuesFromStruct(row)
if err != nil {
return fmt.Errorf("sqlpro.insertBulkContext: %w", err)
}
rows = append(rows, values)
for key := range values {
key_map[key] = structInfo[key]
}
}
// Use faster COPY FROM for postgres if possible
if !onConflictDoNothing && db2.pgxConn() != nil {
return db2.copyFrom(ctx, table, newCopyFromData(db2, key_map, rows))
}
insert := strings.Builder{} // make([]string, 0)
keys := make([]string, 0, len(key_map))
insert.WriteString("INSERT INTO ")
insert.WriteString(db2.Esc(table))
insert.WriteString(" (")
idx := 0
for key := range key_map {
if idx > 0 {
insert.WriteRune(',')
}
insert.WriteString(db2.Esc(key))
keys = append(keys, key)
idx++
}
insert.WriteString(") VALUES \n")
for idx, row := range rows {
if idx > 0 {
insert.WriteRune(',')
}
insert.WriteRune('(')
for idx2, key := range keys {
if idx2 > 0 {
insert.WriteRune(',')
}
insert.WriteString(db2.escValueForInsert(row[key], key_map[key]))
}
insert.WriteRune(')')
insert.WriteRune('\n')
}
if onConflictDoNothing {
if len(conflictCols) > 0 {
cCols := []string{}
for _, cc := range conflictCols {
cCols = append(cCols, db2.Esc(cc))
}
insert.WriteString(" ON CONFLICT (" + strings.Join(cCols, ",") + ") DO NOTHING")
} else {
insert.WriteString(" ON CONFLICT DO NOTHING")
}
}
rowsAffected, _, err := db2.execContext(ctx, insert.String())
if !onConflictDoNothing && err == nil && rowsAffected != int64(len(rows)) {
err = ErrMismatchedRowsAffected
}
if err != nil {
return db2.sqlError(err, insert.String(), []any{})
}
return nil
}
func (db2 *db) UpdateBulk(table string, data any) error {
return db2.UpdateBulkContext(context.Background(), table, data)
}
// UpdateBulkContext updates all records of the passed slice. It using a single
// exec to send the data to the database. This is generally faster than calling Update
// with a slice (which sends individual update requests).
func (db2 *db) UpdateBulkContext(ctx context.Context, table string, data any) error {
var (
rv reflect.Value
structMode bool
err error
)
rv, structMode, err = checkData(data)
if err != nil {
return err
}
if structMode {
return fmt.Errorf("UpdateBulk: Need Slice to update bulk.")
}
l := rv.Len()
if l == 0 {
return nil
}
update := strings.Builder{} // make([]string, 0)
for i := 0; i < l; i++ {
row := reflect.Indirect(rv.Index(i)).Interface()
values, structInfo, err := db2.valuesFromStruct(row)
if err != nil {
return errors.Wrap(err, "sqlpro.UpdateBulk error.")
}
where := strings.Builder{}
whereCount := 0
update.WriteString("UPDATE ")
update.WriteString(db2.Esc(table))
update.WriteString(" SET ")
idx2 := 0
for key, value := range values {
value2 := db2.nullValue(value, structInfo[key])
if structInfo[key].primaryKey {
// skip primary keys for update
if value2 == nil {
return fmt.Errorf("Unable to build UPDATE clause with <nil> primary key: %s", key)
}
if whereCount > 0 {
where.WriteString(" AND ")
}
where.WriteString(db2.Esc(key))
where.WriteRune('=')
where.WriteString(db2.escValueForInsert(value2, structInfo[key]))
whereCount++
} else {
if idx2 > 0 {
update.WriteRune(',')
}
idx2++
update.WriteString(db2.Esc(key))
update.WriteRune('=')
update.WriteString(db2.escValueForInsert(value2, structInfo[key]))
}
}
update.WriteString(" WHERE ")
update.Write([]byte(where.String()))
update.WriteRune(';')
update.WriteRune('\n')
}
rowsAffected, _, err := db2.execContext(ctx, update.String())
if err == nil && rowsAffected != 1 {
err = ErrMismatchedRowsAffected
}
if err != nil {
return db2.sqlError(err, update.String(), []any{})
}
return nil
}
func (db2 *db) insertStruct(ctx context.Context, table string, row any) (int64, structInfo, error) {
values, info, err := db2.valuesFromStruct(row)
if err != nil {
return 0, nil, err
}
sql, args, err := db2.insertClauseFromValues(table, values, info)
if err != nil {
return 0, nil, err
}
if db2.UseReturningForLastId {
pk := info.onlyPrimaryKey()
if pk != nil {
// Fail if transaction present and not in write mode
if db2.sqlTx != nil && !db2.txWriteMode {
return 0, nil, fmt.Errorf("[%s] Trying to write into read-only transaction: %s", db2, sql)
}
sql = sql + " RETURNING " + db2.Esc(pk.dbName)
var insert_id_any any
if db2.Debug || db2.DebugExec {
log.Printf("%s SQL: %s\nARGS:\n%s", db2, sql, argsToString(args...))
}
err := db2.QueryContext(ctx, &insert_id_any, sql, args...)
if err != nil {
return 0, nil, err
}
insert_id, _ := insert_id_any.(int64) // ignore conversion error, return 0 in that case
// log.Printf("Returning ID: %T %v", insert_id_any, insert_id_any)
return insert_id, info, nil
}
}
// log.Printf("SQL: %s Debug: %v", sql, db.Debug)
rowsAffected, insert_id, err := db2.execContext(ctx, sql, args...)
if err == nil && rowsAffected != 1 {
err = ErrMismatchedRowsAffected
}
if err != nil {
return 0, nil, err
}
return insert_id, info, nil
}
func (db2 *db) insertClauseFromValues(table string, values map[string]any, info structInfo) (string, []any, error) {
cols := make([]string, 0, len(values))
vs := make([]string, 0, len(values))
args := make([]any, 0, len(values))
for col, value := range values {
cols = append(cols, db2.Esc(col))
vs = append(vs, "?")
args = append(args, db2.nullValue(value, info[col]))
}
return fmt.Sprintf("INSERT INTO %s (%s) VALUES(%s)",
db2.Esc(table),
strings.Join(cols, ","),
strings.Join(vs, ","),
), args, nil
}
func (db2 *db) updateClauseFromRow(table string, row any) (string, []any, error) {
var (
valid bool
args []any
whereArgs []any
pk_value any
)
values, structInfo, err := db2.valuesFromStruct(row)
if err != nil {
return "", nil, err
}
update := strings.Builder{}
where := strings.Builder{}
update.WriteString("UPDATE ")
update.WriteString(db2.Esc(table))
update.WriteString(" SET ")
where.WriteString(" WHERE ")
for key, value := range values {
if structInfo.primaryKey(key) {
// skip primary keys for update
pk_value = db2.nullValue(value, structInfo[key])
if pk_value == nil {
return "", args, fmt.Errorf("Unable to build UPDATE clause with <nil> key: %s", key)
}
if len(whereArgs) > 0 {
where.WriteString(" AND ")
}
where.WriteString(db2.Esc(key))
where.WriteString("=")
where.WriteRune(db2.PlaceholderValue)
whereArgs = append(whereArgs, pk_value)
valid = true
} else {
if len(args) > 0 {
update.WriteString(",")
}
update.WriteString(db2.Esc(key))
update.WriteString("=")
update.WriteRune(db2.PlaceholderValue)
args = append(args, db2.nullValue(value, structInfo[key]))
}
}
if !valid {
return "", args, fmt.Errorf("Unable to build UPDATE clause, at least one key needed.")
}
args = append(args, whereArgs...)
// Add where clause
return update.String() + where.String(), args, nil
}
func (db2 *db) Update(table string, data any) error {
return db2.UpdateContext(context.Background(), table, data)
}
// Update updates the given struct or slice of structs
// The WHERE clause is put together from the "pk" columns.
// If not all "pk" columns have non empty values, Update returns
// an error.
func (db2 *db) UpdateContext(ctx context.Context, table string, data any) error {
var (
rv reflect.Value
structMode bool
err error
update string
args []any
)
if db2 == nil {
panic("Update on <nil> handle.")
}
rv, structMode, err = checkData(data)
if err != nil {
return err
}
if structMode {
update, args, err = db2.updateClauseFromRow(table, rv.Interface())
if err != nil {
return err
}
rowsAffected, _, err := db2.execContext(ctx, update, args...)
if err == nil && rowsAffected != 1 {
err = ErrMismatchedRowsAffected
}
if err != nil {
return err
}
} else {
for i := 0; i < rv.Len(); i++ {
row := reflect.Indirect(rv.Index(i))
update, args, err = db2.updateClauseFromRow(table, row.Interface())
if err != nil {
return err
}
rowsAffected, _, err := db2.execContext(ctx, update, args...)
if err == nil && rowsAffected != 1 {
err = ErrMismatchedRowsAffected
}
if err != nil {
return err
}
}
}
return nil
}
// Save saves the given data. It performs an INSERT if the only primary key is
// zero, and and UPDATE if it is not. It panics if it the record has no primary
// key or less than one
func (db2 *db) Save(table string, data any) error {
rv, structMode, err := checkData(data)
if err != nil {
return err
}
if structMode {
return db2.saveRow(table, data)
} else {
for i := 0; i < rv.Len(); i++ {
err = db2.saveRow(table, rv.Index(i).Interface())
if err != nil {
return err
}
}
}
return nil
}
func (db2 *db) saveRow(table string, data any) error {
row := reflect.Indirect(reflect.ValueOf(data))
values, info, err := db2.valuesFromStruct(row.Interface())
if err != nil {
return err
}
pk := info.onlyPrimaryKey()
if pk == nil {
return fmt.Errorf("Save needs a struct with exactly one 'pk' field.")
}
pk_value, ok := values[pk.dbName]
if !ok || isZero(pk_value) {
return db2.Insert(table, data)
} else {
return db2.Update(table, data)
}
}
// valuesFromStruct returns the relevant values
// from struct, as map
func (db2 *db) valuesFromStruct(data any) (map[string]any, structInfo, error) {
var (
info structInfo
values map[string]any
dataV reflect.Value
err error
)
values = make(map[string]any, 0)
dataV = reflect.ValueOf(data)
info = getStructInfo(dataV.Type())
for _, fieldInfo := range info {
dataF := dataV.FieldByName(fieldInfo.name)
actualData := dataF.Interface()
isZero := isZero(actualData)
if isZero && fieldInfo.omitEmpty {
continue
}
if fieldInfo.readOnly {
continue
}
if fieldInfo.isJson {
if isZero {
actualData = reflect.Zero(fieldInfo.structField.Type).Interface()
}
actualData, err = json.Marshal(actualData)
if err != nil {
if !fieldInfo.jsonIgnoreError {
return nil, nil, errors.Wrap(err, "Unable to marshal as data as json.")
}
}
// If the database accepts "null" we write NULL, if the db does not accept null
// we write "null", if it is not specified we write NULL if the json renders to "null"
if isZero && (fieldInfo.null || !fieldInfo.notNull && string(actualData.([]byte)) == "null") {
actualData = nil
}
}
values[fieldInfo.dbName] = actualData
// log.Printf("Name: %s Value: %v %v", fieldInfo.name, dataF.Interface(), isZero)
}
return values, info, nil
}
// isZero returns true if given "x" equals Go's empty value.
func isZero(x any) bool {
if x == nil {
return true
}
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}
// execContext wraps DB.Exec and returns the number of affected rows as reported
// by the driver as well as the ID inserted, if the driver supports it.
func (db2 *db) execContext(ctx context.Context, execSql string, args ...any) (rowsAffected, insertID int64, err error) {
var (
execSql0 string
newArgs []any
)
if db2.txExecQueryMtx != nil {
db2.txExecQueryMtx.Lock()
defer db2.txExecQueryMtx.Unlock()
}
if db2.Debug || db2.DebugExec {
log.Printf("%s SQL: %s\nARGS:\n%s", db2, execSql, argsToString(args...))
}
// Fail if transaction present and not in write mode
if db2.sqlTx != nil && !db2.txWriteMode {
return 0, 0, fmt.Errorf("[%s] Trying to write into read-only transaction: %s", db2, execSql)
}
if len(args) > 0 {
execSql0, newArgs, err = db2.replaceArgs(execSql, args...)
if err != nil {
return 0, 0, err
}
} else {
execSql0 = execSql
newArgs = args
}
// logrus.Infof("[%p] EXEC #%d %s %s", db.sqlDB, db.transID, aurora.Green(fmt.Sprintf("%p", db.db)), execSql0[0:10])
var result sql.Result
// tries := 0
for {
result, err = db2.db.ExecContext(ctx, execSql0, newArgs...)
if err != nil {
// pp.Println(err)
// sqlErr, ok := err.(sqlite3.Error)
// if ok {
// if sqlErr.Code == 5 { // SQLITE_BUSY
// tries++
// time.Sleep(50 * time.Millisecond)
// if tries < 3 {
// continue
// }
// }
// }
return 0, 0, db2.debugError(db2.sqlError(err, execSql0, newArgs))
}
break
}
row_count, err := result.RowsAffected()
if err != nil {
// Ignore the error here, we might get
// no RowsAffected available after the empty statement from pq driver
// which is ok and not a real error (it happens with empty statements)
}
if !db2.SupportsLastInsertId {
return row_count, 0, nil
}
last_insert_id, err := result.LastInsertId()
if err != nil {
return row_count, 0, db2.debugError(err)
}
return row_count, last_insert_id, nil
}