-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
executable file
·1162 lines (1060 loc) · 28.2 KB
/
parser.go
File metadata and controls
executable file
·1162 lines (1060 loc) · 28.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
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sqlr
import (
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
"sync"
)
// scalar is a wrapper to force scalar binding semantics.
type scalar struct {
v any
}
// ambiguousSentinel is used to bubble up an "ambiguous field" condition
// through singleLookup without changing call signatures.
type ambiguousSentinel struct {
name string
}
var structIndexCache = newFieldCache(cacheSize)
// parse performs the SQL building and parameter binding. It walks the input
// SQL, substitutes :name placeholders (including rows blocks :name{a,b}),
// tracks placeholder counting, and emits dialect-specific placeholders.
func parse(dialect Dialect, q string, inputs []any, config Config) (string, []any, error) {
// Build fallback resolvers and detect fast bag (map[string]any) materialized in Bind.
fastBag := parseFastBag(inputs)
lookupFB, rowsLookupFB, err := makeMultiResolver(inputs)
if err != nil {
return "", nil, err
}
lookup := parseMakeValueLookup(fastBag, lookupFB)
rowsLookup := parseMakeRowsLookup(fastBag, rowsLookupFB)
// Rough placeholder estimate to pre-size buffers.
est := strings.Count(q, ":") - strings.Count(q, "::")
if est < 0 {
est = 0
}
args := make([]any, 0, est)
var buf strings.Builder
extraPer := 1
switch dialect {
case Postgres, SQLServer:
extraPer = 4
}
buf.Grow(len(q) + 16 + est*extraPer)
// Parser state
const (
sText = iota
sSQ // '...'
sDQ // "..."
sBT // `...` (MySQL/SQLite)
sBR // [...] (SQL Server)
sLC // line comment -- or # (MySQL only)
sBC // block comment /* ... */
sDQD // $tag$ ... $tag$ (dollar-quoted)
)
state := sText
var dqTag string // active $tag$ for PG-like dollar-quoting
n := 0
for i := 0; i < len(q); {
c := q[i]
switch state {
case sText:
// 1) Try entering a quoted/comment state (writes the opener to buf if any)
if newState, newI, newTag, ok := parseTryEnterSpecial(q, i, dialect, &buf); ok {
state, i, dqTag = newState, newI, newTag
continue
}
// 2) Try a :name or :name{...} placeholder
if parseIsParamStart(q, i) {
newI, handled, err := parseHandlePlaceholder(q, i, dialect, config, lookup, rowsLookup, &buf, &args, &n)
if err != nil {
return "", nil, err
}
if handled {
i = newI
continue
}
}
// 3) Plain text byte
buf.WriteByte(c)
i++
case sSQ:
// single-quoted literal with backslash and doubled-quote handling
if c == '\\' {
buf.WriteByte(c)
i++
if i < len(q) {
buf.WriteByte(q[i])
i++
}
continue
}
buf.WriteByte(c)
i++
if c == '\'' {
if i < len(q) && q[i] == '\'' {
buf.WriteByte(q[i])
i++
} else {
state = sText
}
}
case sDQ:
// double-quoted literal with backslash and doubled-quote handling
if c == '\\' {
buf.WriteByte(c)
i++
if i < len(q) {
buf.WriteByte(q[i])
i++
}
continue
}
buf.WriteByte(c)
i++
if c == '"' {
if i < len(q) && q[i] == '"' {
buf.WriteByte(q[i])
i++
} else {
state = sText
}
}
case sBT:
// backtick-quoted identifier (MySQL/SQLite)
buf.WriteByte(c)
i++
if c == '`' {
if i < len(q) && q[i] == '`' {
buf.WriteByte(q[i])
i++
} else {
state = sText
}
}
case sBR:
// bracket-quoted identifier (SQL Server)
buf.WriteByte(c)
i++
if c == ']' {
if i < len(q) && q[i] == ']' {
buf.WriteByte(q[i])
i++
} else {
state = sText
}
}
case sLC:
// line comment: -- ... or # ... (MySQL)
buf.WriteByte(c)
i++
if c == '\n' || c == '\r' {
state = sText
}
case sBC:
// block comment: /* ... */
buf.WriteByte(c)
i++
if c == '*' && i < len(q) && q[i] == '/' {
buf.WriteByte('/')
i++
state = sText
}
case sDQD:
// dollar-quoted block: $tag$ ... $tag$
if dqTag == "" {
buf.WriteString(q[i:])
i = len(q)
break
}
p := strings.Index(q[i:], dqTag)
if p < 0 {
buf.WriteString(q[i:])
i = len(q)
} else {
buf.WriteString(q[i : i+p])
buf.WriteString(dqTag)
i += p + len(dqTag)
dqTag = ""
state = sText
}
}
}
return buf.String(), args, nil
}
// parseFastBag returns the last input if it is a map[string]any, otherwise nil.
func parseFastBag(inputs []any) map[string]any {
if len(inputs) == 0 {
return nil
}
if m, ok := inputs[len(inputs)-1].(map[string]any); ok && m != nil {
return m
}
return nil
}
// parseMakeValueLookup returns a composite lookup that checks the fast bag first,
// then falls back to the generic resolver.
func parseMakeValueLookup(fastBag map[string]any, fallback func(string) (any, bool)) func(string) (any, bool) {
return func(name string) (any, bool) {
if fastBag != nil {
if v, ok := fastBag[name]; ok {
return v, true
}
}
return fallback(name)
}
}
// parseMakeRowsLookup returns a composite rows-lookup that checks the fast bag first,
// then falls back to the generic rows resolver.
func parseMakeRowsLookup(
fastBag map[string]any,
fallback func(string) ([]rowVal, bool),
) func(string) ([]rowVal, bool) {
return func(name string) ([]rowVal, bool) {
if fastBag != nil {
if v, ok := fastBag[name]; ok {
if rows, ok := rowsFromSliceValue(reflect.ValueOf(v)); ok {
return rows, true
}
}
}
return fallback(name)
}
}
// parseIsParamStart checks if q[i] starts a :name placeholder (not a :: cast).
func parseIsParamStart(q string, i int) bool {
return q[i] == ':' && (i+1) < len(q) && q[i+1] != ':' && !(i > 0 && q[i-1] == ':')
}
// parseTryEnterSpecial inspects q[i] for comment/string/identifier openers,
// writes them to buf and returns the new state and cursor when matched.
func parseTryEnterSpecial(q string, i int, dialect Dialect, buf *strings.Builder) (newState int, newI int, dqTag string, ok bool) {
c := q[i]
// line comment: -- or # (MySQL)
if c == '-' && i+1 < len(q) && q[i+1] == '-' {
buf.WriteString("--")
return 5 /* sLC */, i + 2, "", true
}
if c == '#' && dialect == MySQL {
buf.WriteByte('#')
return 5 /* sLC */, i + 1, "", true
}
// block comment: /*
if c == '/' && i+1 < len(q) && q[i+1] == '*' {
buf.WriteString("/*")
return 6 /* sBC */, i + 2, "", true
}
// single-quoted literal
if c == '\'' {
buf.WriteByte(c)
return 1 /* sSQ */, i + 1, "", true
}
// double-quoted literal
if c == '"' {
buf.WriteByte(c)
return 2 /* sDQ */, i + 1, "", true
}
// backtick-quoted identifier (MySQL/SQLite)
if c == '`' && (dialect == MySQL || dialect == SQLite) {
buf.WriteByte(c)
return 3 /* sBT */, i + 1, "", true
}
// bracket-quoted identifier (SQL Server)
if c == '[' && dialect == SQLServer {
buf.WriteByte(c)
return 4 /* sBR */, i + 1, "", true
}
// dollar-quoted: $tag$
if c == '$' {
if tag, ok := readDollarTag(q[i:]); ok {
buf.WriteString(tag)
return 7 /* sDQD */, i + len(tag), tag, true
}
}
return 0, 0, "", false
}
// parseReadName tries to read an identifier after ':' at position j.
// Returns the name and the index just after the name.
func parseReadName(q string, j int) (name string, k int, ok bool) {
if !isAlphaUnderscore(q[j]) {
return "", j, false
}
k = j + 1
for k < len(q) && isAlphaNumUnderscore(q[k]) {
k++
}
return q[j:k], k, true
}
// parseHandlePlaceholder handles :name and :name{...} from q[i] (where q[i]==':').
// On success, it advances the cursor and emits into buf/args adjusting n.
func parseHandlePlaceholder(
q string,
i int,
dialect Dialect,
config Config,
lookup func(string) (any, bool),
rowsLookup func(string) ([]rowVal, bool),
buf *strings.Builder,
args *[]any,
n *int,
) (newI int, handled bool, err error) {
j := i + 1
if j >= len(q) {
return i, false, nil
}
name, k, ok := parseReadName(q, j)
if !ok {
return i, false, nil
}
// Enforce MaxNameLen
if config.MaxNameLen > 0 && len(name) > config.MaxNameLen {
return 0, false, fmt.Errorf("%w: %q (%d > %d)", ErrParamNameTooLong, name, len(name), config.MaxNameLen)
}
// :name{...} rows-block
if k < len(q) && q[k] == '{' {
k2, cols, ok := readCols(q, k)
if !ok {
return 0, true, fmt.Errorf("%w: :%s{...}", ErrRowsMalformed, name)
}
if len(cols) == 0 {
return 0, true, fmt.Errorf("%w: :%s{...} without columns", ErrRowsMalformed, name)
}
rows, ok := rowsLookup(name)
if !ok {
return 0, true, fmt.Errorf("%w: :%s{...}", ErrParamMissing, name)
}
if len(rows) == 0 {
return 0, true, fmt.Errorf("%w: :%s{...}", ErrRowsEmpty, name)
}
if err := parseEnsureAdd(*n, len(rows)*len(cols), config); err != nil {
return 0, true, err
}
if err := parseEmitRowsBlock(name, cols, rows, dialect, buf, args, n); err != nil {
return 0, true, err
}
return k2, true, nil
}
// Simple :name (single value or slice expansion)
v, ok := lookup(name)
if !ok {
return 0, true, fmt.Errorf("%w: %s", ErrParamMissing, name)
}
// Bubble up ambiguous from fallback path only
if a, isAmbiguous := v.(ambiguousSentinel); isAmbiguous {
return 0, true, fmt.Errorf("%w: %q", ErrFieldAmbiguous, a.name)
}
return parseEmitValue(name, v, dialect, config, buf, args, n, k)
}
// parseEmitValue emits either a single placeholder or a list (slice/array expansion).
func parseEmitValue(
name string,
v any,
dialect Dialect,
config Config,
buf *strings.Builder,
args *[]any,
n *int,
k int,
) (newI int, handled bool, err error) {
// Single placeholder for scalar wrapper / driver.Valuer
if sc, ok := v.(scalar); ok {
if err := parseEnsureAdd(*n, 1, config); err != nil {
return 0, true, err
}
*n++
writePlaceholder(buf, dialect, *n)
*args = append(*args, sc.v)
return k, true, nil
}
if _, ok := v.(driver.Valuer); ok {
if err := parseEnsureAdd(*n, 1, config); err != nil {
return 0, true, err
}
*n++
writePlaceholder(buf, dialect, *n)
*args = append(*args, v)
return k, true, nil
}
// []byte (or byte-slice-like) → single placeholder
if bs, ok := v.([]byte); ok {
if err := parseEnsureAdd(*n, 1, config); err != nil {
return 0, true, err
}
*n++
writePlaceholder(buf, dialect, *n)
*args = append(*args, bs)
return k, true, nil
}
rv := reflect.ValueOf(v)
if rv.IsValid() && rv.Kind() == reflect.Slice && rv.Type().Elem().Kind() == reflect.Uint8 {
if err := parseEnsureAdd(*n, 1, config); err != nil {
return 0, true, err
}
*n++
writePlaceholder(buf, dialect, *n)
if rv.Type() != reflect.TypeOf([]byte(nil)) && rv.Type().ConvertibleTo(reflect.TypeOf([]byte(nil))) {
*args = append(*args, rv.Convert(reflect.TypeOf([]byte(nil))).Interface())
} else {
*args = append(*args, v)
}
return k, true, nil
}
// Slice/array expansion (non-byte)
if (rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array) && rv.Type().Elem().Kind() != reflect.Uint8 {
ln := rv.Len()
if ln == 0 {
return 0, true, fmt.Errorf("%w: %s", ErrSliceEmpty, name)
}
if err := parseEnsureAdd(*n, ln, config); err != nil {
return 0, true, err
}
parseGrowArgs(args, ln)
parseGrowSQL(buf, ln)
for t := 0; t < ln; t++ {
if t > 0 {
buf.WriteString(", ")
}
*n++
writePlaceholder(buf, dialect, *n)
*args = append(*args, rv.Index(t).Interface())
}
return k, true, nil
}
// Fallback: single placeholder
if err := parseEnsureAdd(*n, 1, config); err != nil {
return 0, true, err
}
*n++
writePlaceholder(buf, dialect, *n)
*args = append(*args, v)
return k, true, nil
}
// parseEmitRowsBlock emits VALUES-like tuples for :name{col1,col2,...} using rows.
func parseEmitRowsBlock(
name string,
cols []string,
rows []rowVal,
dialect Dialect,
buf *strings.Builder,
args *[]any,
n *int,
) error {
// Pre-compute fast-path structures (map keys and struct paths).
var (
colKeys []reflect.Value
mapKeyT reflect.Type
colPathByType map[reflect.Type][][]int
)
rv0 := deIndirect(reflect.ValueOf(rows[0]))
if rv0.IsValid() && rv0.Kind() == reflect.Map {
mapKeyT = rv0.Type().Key()
if mapKeyT.Kind() == reflect.String || reflect.TypeOf("").ConvertibleTo(mapKeyT) {
colKeys = make([]reflect.Value, len(cols))
for i, col := range cols {
kv := reflect.ValueOf(col)
if kv.Type() != mapKeyT && kv.Type().ConvertibleTo(mapKeyT) {
kv = kv.Convert(mapKeyT)
}
colKeys[i] = kv
}
}
}
if rv0.IsValid() && rv0.Kind() == reflect.Struct {
colPathByType = make(map[reflect.Type][][]int, 4)
baseT := rv0.Type()
baseMap := fieldIndexMap(baseT)
paths := make([][]int, len(cols))
for i, col := range cols {
fi, ok := baseMap[col]
if !ok {
return fmt.Errorf("%w: %q in :%s{...} (record 0)", ErrColumnNotFound, col, name)
}
if fi.ambiguous {
return fmt.Errorf("%w: %q in :%s{...} (record 0)", ErrFieldAmbiguous, col, name)
}
paths[i] = fi.index
}
colPathByType[baseT] = paths
}
need := len(rows) * len(cols)
parseGrowArgs(args, need)
parseGrowSQLRows(buf, len(cols), len(rows))
for r := 0; r < len(rows); r++ {
if r > 0 {
buf.WriteString(", ")
}
buf.WriteByte('(')
rv := deIndirect(reflect.ValueOf(rows[r]))
useMapFast := (colKeys != nil && rv.IsValid() && rv.Kind() == reflect.Map && rv.Type().Key() == mapKeyT)
for cidx := range cols {
if cidx > 0 {
buf.WriteString(", ")
}
var v any
var ok bool
if rv.IsValid() && rv.Kind() == reflect.Struct {
paths, has := colPathByType[rv.Type()]
if !has {
if colPathByType == nil {
colPathByType = make(map[reflect.Type][][]int, 4)
}
fm := fieldIndexMap(rv.Type())
paths = make([][]int, len(cols))
for iCol, col := range cols {
fi, hit := fm[col]
if !hit {
return fmt.Errorf("%w: %q in :%s{...} (record %d)", ErrColumnNotFound, col, name, r)
}
if fi.ambiguous {
return fmt.Errorf("%w: %q in :%s{...} (record %d)", ErrFieldAmbiguous, col, name, r)
}
paths[iCol] = fi.index
}
colPathByType[rv.Type()] = paths
}
v, ok = getValueByPathAny(rv, paths[cidx])
} else if useMapFast {
mv := rv.MapIndex(colKeys[cidx])
if mv.IsValid() {
v, ok = mv.Interface(), true
}
} else {
v, ok = getColValue(rows[r], cols[cidx])
}
if !ok {
return fmt.Errorf("%w: %q in :%s{...} (record %d)", ErrColumnNotFound, cols[cidx], name, r)
}
*n++
writePlaceholder(buf, dialect, *n)
*args = append(*args, v)
}
buf.WriteByte(')')
}
return nil
}
// parseEnsureAdd enforces MaxParams, returning an error if the limit would be exceeded.
func parseEnsureAdd(cur, add int, cfg Config) error {
if cfg.MaxParams > 0 && cur+add > cfg.MaxParams {
return fmt.Errorf("%w: requested=%d, limit=%d", ErrTooManyParams, cur+add, cfg.MaxParams)
}
return nil
}
// parseGrowArgs grows the args slice capacity geometrically to accommodate 'need' more items.
func parseGrowArgs(args *[]any, need int) {
extra := need - (cap(*args) - len(*args))
if extra <= 0 {
return
}
// Geometric growth: double current capacity and add extra,
// but ensure it's at least len+need.
newCap := cap(*args)*2 + extra
minCap := len(*args) + need
if newCap < minCap {
newCap = minCap
}
na := make([]any, len(*args), newCap)
copy(na, *args)
*args = na
}
// parseGrowSQL reserves space in the SQL buffer for a slice expansion of length ln.
func parseGrowSQL(buf *strings.Builder, ln int) {
// approx per placeholder + ", " separators
const approxPerPlaceholder = 6
if ln <= 0 {
return
}
buf.Grow(ln*approxPerPlaceholder + 2*(ln-1))
}
// parseGrowSQLRows reserves buffer space for :rows{...} expansion.
func parseGrowSQLRows(buf *strings.Builder, numCols, numRows int) {
const approxPerPlaceholder = 6
if numCols <= 0 || numRows <= 0 {
return
}
need := numCols * numRows
perRowSep := 2 // "(" + ")"
if numCols > 1 {
perRowSep += 2 * (numCols - 1) // ", " between cols
}
extraSQL := need*approxPerPlaceholder + numRows*perRowSep
if numRows > 1 {
extraSQL += 2 * (numRows - 1) // ", " between rows
}
buf.Grow(extraSQL)
}
// writePlaceholder emits a dialect-specific placeholder token for argument idx.
func writePlaceholder(b *strings.Builder, d Dialect, idx int) {
switch d {
case Postgres:
b.WriteByte('$')
var tmp [20]byte
n := strconv.AppendInt(tmp[:0], int64(idx), 10)
b.Write(n)
case SQLServer:
b.WriteString("@p")
var tmp [20]byte
n := strconv.AppendInt(tmp[:0], int64(idx), 10)
b.Write(n)
default: // MySQL, SQLite
b.WriteByte('?')
}
}
// --------------------------------
// Resolver
// --------------------------------
type rowVal any
// makeMultiResolver returns two resolvers:
// 1. lookup(name) → single value for :name
// 2. rowsLookup(name) → []rowVal for :name{...} blocks
//
// Resolution is "last one wins": later Bind() inputs override earlier ones.
func makeMultiResolver(inputs []any) (
lookup func(string) (any, bool),
rowsLookup func(string) ([]rowVal, bool),
err error,
) {
// Last-one-wins resolution: iterate inputs in reverse order
return func(name string) (any, bool) {
for i := len(inputs) - 1; i >= 0; i-- {
if v, ok := singleLookup(inputs[i], name); ok {
return v, true
}
}
return nil, false
},
func(name string) ([]rowVal, bool) {
for i := len(inputs) - 1; i >= 0; i-- {
if rows, ok := singleRowsLookup(inputs[i], name); ok {
return rows, true
}
}
return nil, false
},
nil
}
// singleLookup resolves a :name from a single Bind() input.
// Supports map-like, struct-like (flattened), and pointers/interfaces thereof.
func singleLookup(in any, name string) (any, bool) {
v := reflect.ValueOf(in)
if !v.IsValid() {
return nil, false
}
for v.IsValid() && (v.Kind() == reflect.Interface || v.Kind() == reflect.Pointer) {
if v.IsNil() {
return nil, false
}
v = v.Elem()
}
// FAST-PATH: map[string]any
if m, ok := v.Interface().(map[string]any); ok {
val, ok := m[name]
return val, ok
}
switch v.Kind() {
case reflect.Map:
keyT := v.Type().Key()
key := reflect.ValueOf(name)
if key.Type() != keyT {
if key.Type().ConvertibleTo(keyT) {
key = key.Convert(keyT)
} else {
return nil, false
}
}
mv := v.MapIndex(key)
if mv.IsValid() {
return mv.Interface(), true
}
return nil, false
case reflect.Struct:
m := fieldIndexMap(v.Type())
if fi, ok := m[name]; ok {
if fi.ambiguous {
// bubble sentinel; parse() will turn this into ErrFieldAmbiguous
return ambiguousSentinel{name: name}, true
}
val, _ := getValueByPathAny(v, fi.index)
if fi.scalar {
return scalar{v: val}, true
}
return val, true
}
}
return nil, false
}
// getColValue extracts a value by column name from a row (struct/map, possibly wrapped).
// It returns (value, true) on success or (nil, false) if the column is missing/unsupported.
func getColValue(row any, col string) (any, bool) {
// FAST-PATH: map[string]any
if m, ok := row.(map[string]any); ok {
v, ok := m[col]
return v, ok
}
rv := reflect.ValueOf(row)
for rv.IsValid() && (rv.Kind() == reflect.Pointer || rv.Kind() == reflect.Interface) {
if rv.IsNil() {
return nil, false
}
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Map:
keyT := rv.Type().Key()
key := reflect.ValueOf(col)
if key.Type() != keyT {
if key.Type().ConvertibleTo(keyT) {
key = key.Convert(keyT)
} else {
return nil, false
}
}
v := rv.MapIndex(key)
if v.IsValid() {
return v.Interface(), true
}
return nil, false
case reflect.Struct:
m := fieldIndexMap(rv.Type())
if fi, ok := m[col]; ok {
val, _ := getValueByPathAny(rv, fi.index)
return val, true
}
return nil, false
default:
return nil, false
}
}
// singleRowsLookup resolves :name{...} rows from a single Bind() input.
// Accepts: map[string] → []struct/[]map, or bare []struct/[]map with conventional name "rows".
func singleRowsLookup(in any, name string) ([]rowVal, bool) {
v := reflect.ValueOf(in)
if !v.IsValid() {
return nil, false
}
// Case 1: map with the given key pointing to a []struct/[]map (possibly via interface or pointer)
if v.Kind() == reflect.Map && v.Type().Key().Kind() == reflect.String {
mv := v.MapIndex(reflect.ValueOf(name))
if mv.IsValid() {
return rowsFromSliceValue(mv)
}
}
// Case 2: convenience convention—if name == "rows" and 'in' itself is []struct/[]map
if name == "rows" {
return rowsFromSliceValue(v)
}
return nil, false
}
// rowsFromSliceValue returns the slice elements as []rowVal if v is a slice of
// struct or map (possibly wrapped in interface/pointer). Returns (nil,false) otherwise.
func rowsFromSliceValue(v reflect.Value) ([]rowVal, bool) {
// Unwrap interface{} and pointer until the actual value
for v.IsValid() && (v.Kind() == reflect.Interface || v.Kind() == reflect.Pointer) {
if v.IsNil() {
return nil, false
}
v = v.Elem()
}
if v.Kind() != reflect.Slice {
return nil, false
}
ln := v.Len()
out := make([]rowVal, ln)
for i := 0; i < ln; i++ {
out[i] = v.Index(i).Interface()
}
if ln == 0 {
return out, true
}
// Accept struct or map for individual elements
el := v.Index(0)
// Unwrap potential interface/pointer element
for el.IsValid() && (el.Kind() == reflect.Interface || el.Kind() == reflect.Pointer) {
if el.IsNil() {
return nil, false
}
el = el.Elem()
}
if el.Kind() == reflect.Struct || el.Kind() == reflect.Map {
return out, true
}
return nil, false
}
// fieldIndexMap returns a mapping from column name → fieldInfo for the given type.
// It flattens nested structs (excluding time.Time), honors `db:"name"` tags,
// and supports `db:"name,scalar"` to force scalar binding.
// The result is cached in a two-tier cache.
func fieldIndexMap(t reflect.Type) map[string]fieldInfo {
if m, ok := structIndexCache.get(t); ok {
return m
}
// Normalize to struct
base := t
for base.Kind() == reflect.Pointer {
base = base.Elem()
}
if base.Kind() != reflect.Struct {
m := make(map[string]fieldInfo)
structIndexCache.put(t, m)
return m
}
m := make(map[string]fieldInfo, base.NumField())
visited := map[reflect.Type]bool{}
var walk func(rt reflect.Type, path []int)
walk = func(rt reflect.Type, path []int) {
// Follow pointers for current type
for rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
if rt.Kind() != reflect.Struct {
return
}
if visited[rt] {
return
}
visited[rt] = true
defer delete(visited, rt)
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.PkgPath != "" { // unexported
continue
}
tag := f.Tag.Get("db")
if tag == "-" {
continue
}
name := f.Name
scalar := false
if tag != "" {
parts := strings.Split(tag, ",")
if parts[0] != "" {
name = parts[0]
}
for _, p := range parts[1:] {
if strings.TrimSpace(p) == "scalar" {
scalar = true
}
}
}
ft := f.Type
// Decide whether to flatten this field
if shouldFlatten(ft) {
// Recurse into element (if pointer, Elem())
nextT := ft
if nextT.Kind() == reflect.Pointer {
nextT = nextT.Elem()
}
walk(nextT, appendIndex(path, i))
continue
}
// Leaf: handle collisions
if prev, exists := m[name]; exists {
// Mark as ambiguous; index is irrelevant once ambiguous.
if !prev.ambiguous {
m[name] = fieldInfo{ambiguous: true}
}
// If already ambiguous, leave it as-is.
continue
}
m[name] = fieldInfo{index: appendIndex(path, i), scalar: scalar}
}
}
walk(base, nil)
structIndexCache.put(t, m)
return m
}
// shouldFlatten decides whether to descend into ft (struct or *struct).
func shouldFlatten(ft reflect.Type) bool {
// If *T implements sql.Scanner → treat as leaf (no flatten)
if reflect.PointerTo(ft).Implements(scannerIface) || ft.Implements(scannerIface) {
return false
}
tt := ft
if tt.Kind() == reflect.Pointer {
tt = tt.Elem()
}
if tt.Kind() != reflect.Struct {
return false
}
// Do not flatten time.Time (common leaf struct)
if tt.PkgPath() == "time" && tt.Name() == "Time" {
return false
}
return true
}
// appendIndex returns a new index path with idx appended.
func appendIndex(path []int, idx int) []int {
out := make([]int, len(path)+1)
copy(out, path)
out[len(path)] = idx
return out
}
// --------------------------------
// Cache
// --------------------------------
// fieldInfo describes a leaf field: its full index path and whether it's marked
// as "scalar" via tag option (no slice expansion).
type fieldInfo struct {
index []int // full index path for FieldByIndex-like ops
scalar bool
ambiguous bool // true if multiple fields with same name found (only for top-level fields)
}
// fieldCache implements a two-tier map with cheap rotation to bound memory.
// 'curr' is the hot set; 'prev' is the previous generation. Lookups promote.
type fieldCache struct {
mu sync.RWMutex
curr map[reflect.Type]map[string]fieldInfo
prev map[reflect.Type]map[string]fieldInfo
max int
}