-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
778 lines (697 loc) · 15.8 KB
/
util.go
File metadata and controls
778 lines (697 loc) · 15.8 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 (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pkg/errors"
)
var ErrQueryReturnedZeroRows error = errors.New("Query returned 0 rows")
var ErrMismatchedRowsAffected error = errors.New("Mismatched rows affected")
// structInfo is a map to fieldInfo by db_name
type structInfo map[string]*fieldInfo
func (si structInfo) primaryKey(db_name string) bool {
fieldInfo, ok := si[db_name]
if !ok {
panic(fmt.Sprintf("isPrimaryKey: db_name %s not found.", db_name))
}
return fieldInfo.primaryKey
}
func (si structInfo) onlyPrimaryKey() *fieldInfo {
var (
fi *fieldInfo
)
for _, info := range si {
if info.primaryKey {
if fi != nil {
// more than one
return nil
}
fi = info
}
}
return fi
}
type NullTime struct {
Time time.Time
Valid bool
}
// Scan implements the Scanner interface.
func (ni *NullTime) Scan(value any) error {
// log.Printf("Scan %T %s", value, value)
if value == nil {
ni.Time, ni.Valid = time.Time{}, false
return nil
}
var err error
switch v := value.(type) {
case time.Time:
ni.Time = v
ni.Valid = true
case string:
ni.Time, err = time.Parse(time.RFC3339Nano, v)
if err != nil {
return fmt.Errorf("NullTime.Scan: %w", err)
}
ni.Valid = true
default:
return fmt.Errorf("Unable to scan time: %T %s", value, value)
}
// pretty.Println(ni)
return nil
}
type NullJson struct {
Data []byte
Valid bool
}
func (nj *NullJson) Scan(value any) error {
switch v := value.(type) {
case nil:
return nil
case []byte:
if len(v) == 0 {
return nil
}
nj.Data = v
nj.Valid = true
return nil
case string:
if len(v) == 0 {
return nil
}
nj.Data = []byte(v)
nj.Valid = true
return nil
default:
return errors.Errorf(`sqlpro.NullJson.Scan: Unable to scan type "%T"`, value)
}
}
type NullRawMessage struct {
Data json.RawMessage
Valid bool
}
func (nj *NullRawMessage) Scan(value any) error {
switch v := value.(type) {
case nil:
return nil
case []byte:
if len(v) == 0 {
return nil
}
nj.Data = v
nj.Valid = true
return nil
case string:
if len(v) == 0 {
return nil
}
nj.Data = []byte(v)
nj.Valid = true
return nil
default:
return errors.Errorf("sqlpro.NullRawMessage.Scan: Unable to Scan type %T", value)
}
}
type fieldInfo struct {
structField reflect.StructField
name string
dbName string
omitEmpty bool
primaryKey bool
null bool
readOnly bool
notNull bool
isJson, jsonIgnoreError bool
emptyValue string
ptr bool // set true if the field is a pointer
}
// allowNull returns true if the given can store "null" values
func (fi *fieldInfo) allowNull() bool {
if fi.ptr {
if fi.notNull {
return false
}
return true
}
if fi.null {
return true
}
return false
}
// getStructInfo returns a per dbName to fieldInfo map
func getStructInfo(t reflect.Type) structInfo {
si := structInfo{}
// Resolve anonymous fields
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous {
if field.Type.Kind() == reflect.Ptr {
panic(fmt.Sprintf("Unable to scan into embedded pointer type %q", field.Type))
}
for dbName, info := range getStructInfo(field.Type) {
si[dbName] = info
}
}
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous {
// These are resolved above
continue
}
dbTag := field.Tag.Get("db")
if dbTag == "" {
// ignore field
continue
}
path := strings.Split(dbTag, ",")
if path[0] == "-" {
// ignore field
continue
}
if field.PkgPath != "" {
// unexported field
panic(fmt.Errorf("getStructInfo: Unable to use unexported field for sqlpro: %s", field.Name))
}
info := fieldInfo{
dbName: path[0],
structField: field,
name: field.Name,
omitEmpty: false,
readOnly: false,
primaryKey: false,
}
if info.dbName == "-" {
continue
}
switch field.Type.Kind() {
case reflect.Ptr:
info.ptr = true
info.emptyValue = "null"
case reflect.String:
info.emptyValue = "''"
case reflect.Int:
info.emptyValue = "0"
default:
info.emptyValue = "''"
}
if info.dbName == "" {
info.dbName = field.Name
}
for idx, p := range path {
if idx == 0 {
continue
}
switch p {
case "pk":
info.primaryKey = true
case "omitempty":
info.omitEmpty = true
case "null":
info.null = true
case "notnull":
info.notNull = true
case "json":
info.isJson = true
case "json_ignore_error":
info.jsonIgnoreError = true
case "readonly":
info.readOnly = true
default:
// ignore unrecognized
}
}
if info.allowNull() && info.emptyValue == "null" {
info.emptyValue = "''"
}
si[info.dbName] = &info
}
// logrus.Infof("%s %#v", t.Name(), si)
return si
}
// replaceArgs rewrites the string sqlS to embed the slice args given
// it returns the new placeholder string and the reduced list of arguments.
func (db2 *db) replaceArgs(sqlS string, args ...any) (string, []any, error) {
var (
nthArg, lenRunes int
newArgs []any
sb strings.Builder
runes []rune
currRune rune
)
// pretty.Println(args)
sb = strings.Builder{}
nthArg = 0
runes = []rune(sqlS)
lenRunes = len(runes)
for i := 0; i < lenRunes; i++ {
currRune = runes[i]
// skip quoted strings
if currRune == '\'' || currRune == '"' {
// forward to the next rune outside the quoted string
quoteChar := currRune
sb.WriteRune(currRune)
i++ // move past opening quote
for i < lenRunes {
sb.WriteRune(runes[i])
if runes[i] == quoteChar {
// Check for escaped quote (e.g., '' or "")
if i+1 < lenRunes && runes[i+1] == quoteChar {
i++
sb.WriteRune(runes[i]) // write second quote of pair
i++
continue
}
break // found closing quote
}
i++
}
continue
}
if currRune != db2.PlaceholderKey && currRune != db2.PlaceholderValue {
sb.WriteRune(currRune)
continue
}
if nthArg >= len(args) {
return "", nil, fmt.Errorf("replaceArgs: Expecting #%d arg. Got: %d args.", (nthArg + 1), len(args))
}
arg := args[nthArg]
nthArg++
// replace column and table names ("Key")
if currRune == db2.PlaceholderKey {
switch v := arg.(type) {
case *string:
sb.WriteString(db2.Esc(*v))
case string:
sb.WriteString(db2.Esc(v))
default:
return "", nil, fmt.Errorf("replaceArgs: Unable to replace %s with type %T, need *string or string.", string(currRune), arg)
}
continue
}
isValue := false
switch arg.(type) {
case json.RawMessage:
isValue = true
default:
if db2.timeFormat != "" {
t, ok := toTime(arg)
if ok {
arg = t.Format(db2.timeFormat)
isValue = true
}
}
}
if isValue || driver.IsValue(arg) {
newArgs = append(newArgs, arg)
db2.appendPlaceholder(&sb, len(newArgs)-1)
continue
}
rv := reflect.ValueOf(arg)
if rv.IsValid() && rv.Type().Kind() == reflect.Slice {
l := rv.Len()
if l == 0 {
return "", nil, fmt.Errorf(`sqlpro: replaceArgs: Unable to merge empty slice: "%s"`, sqlS)
}
sb.WriteRune('(')
fi := &fieldInfo{ptr: rv.Type().Elem().Kind() == reflect.Ptr}
for i := 0; i < l; i++ {
if i > 0 {
sb.WriteRune(',')
}
item := rv.Index(i).Interface()
if l > db2.MaxPlaceholder {
// append literals
switch v := item.(type) {
case string:
sb.WriteString(db2.EscValue(v))
case *string:
if v == nil {
sb.WriteString("null")
} else {
sb.WriteString(db2.EscValue(*v))
}
case int:
sb.WriteString(strconv.FormatInt(int64(v), 10))
case int32:
sb.WriteString(strconv.FormatInt(int64(v), 10))
case int64:
sb.WriteString(strconv.FormatInt(v, 10))
case *int:
if v == nil {
sb.WriteString("null")
} else {
sb.WriteString(strconv.FormatInt(int64(*v), 10))
}
case *int32:
if v == nil {
sb.WriteString("null")
} else {
sb.WriteString(strconv.FormatInt(int64(*v), 10))
}
case *int64:
if v == nil {
sb.WriteString("null")
} else {
sb.WriteString(strconv.FormatInt(*v, 10))
}
default:
return "", nil, errors.Errorf("Unable to add type: %T in slice placeholder. Can only add string, *string, int, int32, int64, *int, *int32 and *int64", item)
}
} else {
newArgs = append(newArgs, db2.nullValue(item, fi))
db2.appendPlaceholder(&sb, len(newArgs)-1)
}
}
sb.WriteRune(')')
// pretty.Println(parts)
continue
}
newArgs = append(newArgs, arg)
db2.appendPlaceholder(&sb, len(newArgs)-1)
}
// append left over args
for i := nthArg; i < len(args); i++ {
newArgs = append(newArgs, args[i])
}
// log.Printf("%s %v -> \"%s\"", sqlS, args, sb.String())
return sb.String(), newArgs, nil
}
// appendPlaceholder adds one placeholder to the built
func (db2 *db) appendPlaceholder(sb *strings.Builder, numArg int) {
switch db2.PlaceholderMode {
case QUESTION:
sb.WriteRune('?')
case DOLLAR:
sb.WriteRune('$')
sb.WriteString(strconv.Itoa(numArg + 1))
}
}
func (db2 *db) escValueForInsert(value any, fi *fieldInfo) string {
vIn := db2.valueForInsert(value, fi)
switch v := vIn.(type) {
case string:
return db2.EscValue(v)
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
return strconv.FormatFloat(float64(v), 'f', -1, 64)
case int64:
return strconv.FormatInt(v, 10)
case time.Time:
return db2.EscValue(v.Format(time.RFC3339Nano))
case bool:
if v {
return "TRUE"
} else {
return "FALSE"
}
case nil:
return "NULL"
default:
panic(fmt.Sprintf("unknown type %T", v))
}
}
func toTime(v any) (time.Time, bool) {
rv := reflect.ValueOf(v)
if !rv.IsValid() {
return time.Time{}, false
}
rt := rv.Type()
timeType := reflect.TypeOf(time.Time{})
// Dereference pointer
if rt.Kind() == reflect.Ptr {
if rv.IsNil() {
return time.Time{}, false
}
rv = rv.Elem()
rt = rv.Type()
}
// Direct match
if rt == timeType {
return rv.Interface().(time.Time), true
}
// Convertible types (e.g. SimpleTime)
if rt.ConvertibleTo(timeType) {
converted := rv.Convert(timeType) // reflect.Value → time.Time
return converted.Interface().(time.Time), true
}
return time.Time{}, false
}
// valueForInsert returns string, int64, float32, float64, bool or nil
func (db2 *db) valueForInsert(value any, fi *fieldInfo) any {
var s string
v0 := db2.nullValue(value, fi)
if v0 == nil {
return nil
}
switch v := v0.(type) {
case int:
return int64(v)
case *int:
return int64(*v)
case int8:
return int64(v)
case *int8:
return int64(*v)
case int16:
return int64(v)
case *int16:
return int64(*v)
case int32:
return int64(v)
case *int32:
return int64(*v)
case int64:
return v
case *int64:
return *v
case uint:
return int64(v)
case *uint:
return int64(*v)
case uint8:
return int64(v)
case *uint8:
return int64(*v)
case uint16:
return int64(v)
case *uint16:
return int64(*v)
case uint32:
return int64(v)
case *uint32:
return int64(*v)
case uint64:
return int64(v)
case *uint64:
return int64(*v)
case float32:
return v
case *float32:
return *v
case float64:
return v
case *float64:
return *v
case bool:
if v == false {
return false
} else {
return true
}
case *bool:
if *v == false {
return false
} else {
return true
}
case []uint8:
s = string(v)
case json.RawMessage:
s = string(v)
case string:
s = v
case *string:
s = *v
case time.Time:
if db2.timeFormat == "" {
return v
}
return v.Format(db2.timeFormat)
case *time.Time:
if db2.timeFormat == "" {
return *v
}
return v.Format(db2.timeFormat)
default:
t, isTime := toTime(v0)
if isTime {
if db2.timeFormat == "" {
return t
}
return t.Format(db2.timeFormat)
}
vr, ok := value.(driver.Valuer)
if ok {
v2, _ := vr.Value()
return db2.valueForInsert(v2, fi)
}
sv := reflect.ValueOf(value)
// try to use a pointer to check if the driver.Valuer is satisfied
if sv.Kind() != reflect.Pointer {
pv := reflect.New(sv.Type())
pv.Elem().Set(sv)
var anyVal any = pv.Interface()
vr2, ok2 := anyVal.(driver.Valuer)
if ok2 {
v3, _ := vr2.Value()
return db2.valueForInsert(v3, fi)
}
}
switch sv.Kind() {
case reflect.Int:
return sv.Int()
case reflect.String:
s = sv.String()
default:
panic(fmt.Sprintf("EscValueForInsert failed: %T value %v in type: %s", value, value, sv.Kind()))
}
}
return s
}
// nullValue returns the escaped value suitable for UPDATE & INSERT
func (db2 *db) nullValue(value any, fi *fieldInfo) any {
if isZero(value) {
if fi.allowNull() {
return nil
}
// a pointer which does not allow to store null
if fi.ptr {
panic(fmt.Errorf(`Unable to store <nil> pointer in "notnull" field: %s`, fi.name))
}
}
return value
}
// argsToString builds a debug string from given args
func argsToString(args ...any) string {
var (
s string
sb strings.Builder
rv reflect.Value
argPrint any
)
if len(args) == 0 {
return " <none>"
}
sb = strings.Builder{}
for idx, arg := range args {
if arg == nil {
sb.WriteString(fmt.Sprintf(" #%d <nil>\n", idx+1))
continue
}
switch arg.(type) {
case bool, *bool:
s = "%v"
case int64, int32, uint64, uint32, int,
*int64, *int32, *uint64, *uint32, *int:
s = "%d"
case float64, float32,
*float64, *float32:
s = "%b"
case string, *string:
s = "%s"
default:
s = "%v"
}
rv = reflect.ValueOf(arg)
argPrint = reflect.Indirect(rv).Interface()
sb.WriteString(fmt.Sprintf(" #%d %s "+s+"\n", idx+1, rv.Type(), argPrint))
}
return sb.String()
}
func (db2 *db) Close() error {
if db2.sqlDB == nil {
panic("sqlpro.DB.Close: Unable to close, use Open to initialize the wrapper")
}
if db2.sqlTx != nil {
panic("sqlpro.TX.Close: Unable to close a tx handle")
}
db2.isClosed = true
// log.Printf("%s sqlpro.Close: %s", db, db.DSN)
return db2.sqlDB.Close()
}
func (db2 *db) IsClosed() bool {
if db2 == nil {
return true
}
return db2.isClosed
}
// Open opens a database connection and returns an sqlpro wrap handle
func Open(driverS, dsn string) (DB, error) {
var driver dbDriver
var driverName string
switch driverS {
default:
return nil, fmt.Errorf(`Unknown driver "%s"`, driverS)
case "sqlite3":
driver = SQLITE3
driverName = "sqlite3"
case "sqlite":
driver = SQLITE3
driverName = "sqlite"
case "postgres":
driver = POSTGRES
driverName = "pgx"
}
conn, err := sql.Open(driverName, dsn)
if err != nil {
return nil, err
}
// conn.SetMaxOpenConns(1)
err = conn.Ping()
if err != nil {
conn.Close()
return nil, err
}
wrapper := newSqlPro(conn)
wrapper.sqlDB = conn
wrapper.driver = driver
// wrapper.Debug = true
wrapper.DSN = dsn
switch driver {
case POSTGRES:
wrapper.PlaceholderMode = DOLLAR
wrapper.UseReturningForLastId = true
wrapper.SupportsLastInsertId = false
case SQLITE3:
wrapper.timeFormat = time.RFC3339Nano
default:
return nil, errors.Errorf("sqlpro.Open: Unsupported driver '%s'.", driver)
}
return wrapper, nil
}
// Open -> handle
// handle.New -> NewConnection
// handle.Wrap -> Wrap yourself
// handle.Tx -> NewTransaction
// handle.Prepare -> NewPrearedStatement
// IlikeSql returns driver compatible ILIKE where clause snippet. match is
// escaped using %...%. This panics for unknown driver. E.g. "schule" and driver
// "postgres" this will return "ILIKE 'schule'"
func IlikeSql(driver dbDriver, match string) string {
v := escValue("%"+strings.ReplaceAll(match, `%`, `\%`)+`%`) + ` ESCAPE '\'`
switch driver {
default:
panic(fmt.Errorf("unsupported driver %q", driver))
case POSTGRES:
return `ILIKE ` + v
case SQLITE3:
return `LIKE ` + v + ` COLLATE NOCASE`
}
}