-
Notifications
You must be signed in to change notification settings - Fork 8
/
row_column.go
1247 lines (1180 loc) · 38.4 KB
/
row_column.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
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 sq
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"time"
"github.com/bokwoon95/sq/internal/googleuuid"
"github.com/bokwoon95/sq/internal/pqarray"
)
// Row represents the state of a row after a call to rows.Next().
type Row struct {
dialect string
sqlRows *sql.Rows
runningIndex int
fields []Field
scanDest []any
queryIsStatic bool
columns []string
columnTypes []*sql.ColumnType
values []any
columnIndex map[string]int
}
// Column returns the names of the columns returned by the query. This method
// can only be called in a rowmapper if it is paired with a raw SQL query e.g.
// Queryf("SELECT * FROM my_table"). Otherwise, an error will be returned.
func (row *Row) Columns() []string {
if row.queryIsStatic {
return row.columns
}
if row.sqlRows == nil {
return nil
}
columns, err := row.sqlRows.Columns()
if err != nil {
panic(fmt.Errorf(callsite(1)+"sqlRows.Columns: %w", err))
}
return columns
}
// ColumnTypes returns the column types returned by the query. This method can
// only be called in a rowmapper if it is paired with a raw SQL query e.g.
// Queryf("SELECT * FROM my_table"). Otherwise, an error will be returned.
func (row *Row) ColumnTypes() []*sql.ColumnType {
if row.queryIsStatic {
return row.columnTypes
}
if row.sqlRows == nil {
return nil
}
columnTypes, err := row.sqlRows.ColumnTypes()
if err != nil {
panic(fmt.Errorf(callsite(1)+"sqlRows.ColumnTypes: %w", err))
}
return columnTypes
}
// Values returns the values of the current row. This method can only be called
// in a rowmapper if it is paired with a raw SQL query e.g. Queryf("SELECT *
// FROM my_table"). Otherwise, an error will be returned.
func (row *Row) Values() []any {
if row.queryIsStatic {
values := make([]any, len(row.values))
copy(values, row.values)
return values
}
if row.sqlRows == nil {
return nil
}
columns, err := row.sqlRows.Columns()
if err != nil {
panic(fmt.Errorf(callsite(1)+"sqlRows.Columns: %w", err))
}
values := make([]any, len(columns))
scanDest := make([]any, len(columns))
for i := range values {
scanDest[i] = &values[i]
}
err = row.sqlRows.Scan(scanDest...)
if err != nil {
panic(fmt.Errorf(callsite(1)+"sqlRows.Scan: %w", err))
}
return values
}
// Value returns the value of the expression. It is intended for use cases
// where you only know the name of the column but not its type to scan into.
// The underlying type of the value is determined by the database driver you
// are using.
func (row *Row) Value(format string, values ...any) any {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s is not present in query (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
return row.values[index]
}
if row.sqlRows == nil {
var value any
row.fields = append(row.fields, Expr(format, values...))
row.scanDest = append(row.scanDest, &value)
return nil
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*any)
return *scanDest
}
// Scan scans the expression into destPtr.
func (row *Row) Scan(destPtr any, format string, values ...any) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call Scan for static queries"))
}
row.scan(destPtr, Expr(format, values...), 1)
}
// ScanField scans the field into destPtr.
func (row *Row) ScanField(destPtr any, field Field) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call ScanField for static queries"))
}
row.scan(destPtr, field, 1)
}
func (row *Row) scan(destPtr any, field Field, skip int) {
if row.sqlRows == nil {
row.fields = append(row.fields, field)
switch destPtr.(type) {
case *bool, *sql.NullBool:
row.scanDest = append(row.scanDest, &sql.NullBool{})
case *float64, *sql.NullFloat64:
row.scanDest = append(row.scanDest, &sql.NullFloat64{})
case *int32, *sql.NullInt32:
row.scanDest = append(row.scanDest, &sql.NullInt32{})
case *int, *int64, *sql.NullInt64:
row.scanDest = append(row.scanDest, &sql.NullInt64{})
case *string, *sql.NullString:
row.scanDest = append(row.scanDest, &sql.NullString{})
case *time.Time, *sql.NullTime:
row.scanDest = append(row.scanDest, &sql.NullTime{})
default:
if reflect.TypeOf(destPtr).Kind() != reflect.Ptr {
panic(fmt.Errorf(callsite(skip+1)+"cannot pass in non pointer value (%#v) as destPtr", destPtr))
}
row.scanDest = append(row.scanDest, destPtr)
}
return
}
defer func() {
row.runningIndex++
}()
switch destPtr := destPtr.(type) {
case *bool:
scanDest := row.scanDest[row.runningIndex].(*sql.NullBool)
*destPtr = scanDest.Bool
case *sql.NullBool:
scanDest := row.scanDest[row.runningIndex].(*sql.NullBool)
*destPtr = *scanDest
case *float64:
scanDest := row.scanDest[row.runningIndex].(*sql.NullFloat64)
*destPtr = scanDest.Float64
case *sql.NullFloat64:
scanDest := row.scanDest[row.runningIndex].(*sql.NullFloat64)
*destPtr = *scanDest
case *int:
scanDest := row.scanDest[row.runningIndex].(*sql.NullInt64)
*destPtr = int(scanDest.Int64)
case *int32:
scanDest := row.scanDest[row.runningIndex].(*sql.NullInt32)
*destPtr = scanDest.Int32
case *sql.NullInt32:
scanDest := row.scanDest[row.runningIndex].(*sql.NullInt32)
*destPtr = *scanDest
case *int64:
scanDest := row.scanDest[row.runningIndex].(*sql.NullInt64)
*destPtr = scanDest.Int64
case *sql.NullInt64:
scanDest := row.scanDest[row.runningIndex].(*sql.NullInt64)
*destPtr = *scanDest
case *string:
scanDest := row.scanDest[row.runningIndex].(*sql.NullString)
*destPtr = scanDest.String
case *sql.NullString:
scanDest := row.scanDest[row.runningIndex].(*sql.NullString)
*destPtr = *scanDest
case *time.Time:
scanDest := row.scanDest[row.runningIndex].(*sql.NullTime)
*destPtr = scanDest.Time
case *sql.NullTime:
scanDest := row.scanDest[row.runningIndex].(*sql.NullTime)
*destPtr = *scanDest
default:
destValue := reflect.ValueOf(destPtr).Elem()
srcValue := reflect.ValueOf(row.scanDest[row.runningIndex]).Elem()
destValue.Set(srcValue)
}
}
// Array scans the array expression into destPtr. The destPtr must be a pointer
// to a []string, []int, []int64, []int32, []float64, []float32 or []bool.
func (row *Row) Array(destPtr any, format string, values ...any) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call Array for static queries"))
}
row.array(destPtr, Expr(format, values...), 1)
}
// ArrayField scans the array field into destPtr. The destPtr must be a pointer
// to a []string, []int, []int64, []int32, []float64, []float32 or []bool.
func (row *Row) ArrayField(destPtr any, field Array) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call ArrayField for static queries"))
}
row.array(destPtr, field, 1)
}
func (row *Row) array(destPtr any, field Array, skip int) {
if row.sqlRows == nil {
if reflect.TypeOf(destPtr).Kind() != reflect.Ptr {
panic(fmt.Errorf(callsite(skip+1)+"cannot pass in non pointer value (%#v) as destPtr", destPtr))
}
if row.dialect == DialectPostgres {
switch destPtr.(type) {
case *[]string, *[]int, *[]int64, *[]int32, *[]float64, *[]float32, *[]bool:
break
default:
panic(fmt.Errorf(callsite(skip+1)+"destptr (%T) must be either a pointer to a []string, []int, []int64, []int32, []float64, []float32 or []bool", destPtr))
}
}
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &nullBytes{
dialect: row.dialect,
displayType: displayTypeString,
})
return
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*nullBytes)
if !scanDest.valid {
return
}
if row.dialect != DialectPostgres {
err := json.Unmarshal(scanDest.bytes, destPtr)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unmarshaling json %q into %T: %w", string(scanDest.bytes), destPtr, err))
}
return
}
switch destPtr := destPtr.(type) {
case *[]string:
var array pqarray.StringArray
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to string array: %w", string(scanDest.bytes), err))
}
*destPtr = array
case *[]int:
var array pqarray.Int64Array
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to int64 array: %w", string(scanDest.bytes), err))
}
*destPtr = (*destPtr)[:cap(*destPtr)]
if len(*destPtr) < len(array) {
*destPtr = make([]int, len(array))
}
*destPtr = (*destPtr)[:len(array)]
for i, num := range array {
(*destPtr)[i] = int(num)
}
case *[]int64:
var array pqarray.Int64Array
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to int64 array: %w", string(scanDest.bytes), err))
}
*destPtr = array
case *[]int32:
var array pqarray.Int32Array
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to int32 array: %w", string(scanDest.bytes), err))
}
*destPtr = array
case *[]float64:
var array pqarray.Float64Array
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to float64 array: %w", string(scanDest.bytes), err))
}
*destPtr = array
case *[]float32:
var array pqarray.Float32Array
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to float32 array: %w", string(scanDest.bytes), err))
}
*destPtr = array
case *[]bool:
var array pqarray.BoolArray
err := array.Scan(scanDest.bytes)
if err != nil {
panic(fmt.Errorf(callsite(skip+1)+"unable to convert %q to bool array: %w", string(scanDest.bytes), err))
}
*destPtr = array
default:
panic(fmt.Errorf(callsite(skip+1)+"destptr (%T) must be either a pointer to a []string, []int, []int64, []int32, []float64, []float32 or []bool", destPtr))
}
}
// Bytes returns the []byte value of the expression.
func (row *Row) Bytes(format string, values ...any) []byte {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
panic(fmt.Errorf(callsite(1)+"%d is int64, not []byte", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not []byte", value))
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not []byte", value))
case []byte:
return value
case string:
return []byte(value)
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not []byte", value))
case nil:
return nil
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not []byte", value))
}
}
if row.sqlRows == nil {
row.fields = append(row.fields, Expr(format, values...))
row.scanDest = append(row.scanDest, &nullBytes{
dialect: row.dialect,
})
return nil
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*nullBytes)
var b []byte
if scanDest.valid {
b = make([]byte, len(scanDest.bytes))
copy(b, scanDest.bytes)
}
return b
}
// BytesField returns the []byte value of the field.
func (row *Row) BytesField(field Binary) []byte {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call BytesField for static queries"))
}
if row.sqlRows == nil {
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &nullBytes{
dialect: row.dialect,
})
return nil
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*nullBytes)
var b []byte
if scanDest.valid {
b = make([]byte, len(scanDest.bytes))
copy(b, scanDest.bytes)
}
return b
}
// == Bool == //
// Bool returns the bool value of the expression.
func (row *Row) Bool(format string, values ...any) bool {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
if value == 1 {
return true
}
if value == 0 {
return false
}
panic(fmt.Errorf(callsite(1)+"%d is int64, not bool", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not bool", value))
case bool:
return value
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
if string(value) == "1" {
return true
}
if string(value) == "0" {
return false
}
panic(fmt.Errorf(callsite(1)+"%#v is []byte, not bool", value))
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not bool", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not bool", value))
case nil:
return false
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not bool", value))
}
}
return row.NullBoolField(Expr(format, values...)).Bool
}
// BoolField returns the bool value of the field.
func (row *Row) BoolField(field Boolean) bool {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call BoolField for static queries"))
}
return row.NullBoolField(field).Bool
}
// NullBool returns the sql.NullBool value of the expression.
func (row *Row) NullBool(format string, values ...any) sql.NullBool {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
if value == 1 {
return sql.NullBool{Bool: true, Valid: true}
}
if value == 0 {
return sql.NullBool{Bool: false, Valid: true}
}
panic(fmt.Errorf(callsite(1)+"%d is int64, not bool", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not bool", value))
case bool:
return sql.NullBool{Bool: value, Valid: true}
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
if string(value) == "1" {
return sql.NullBool{Bool: true, Valid: true}
}
if string(value) == "0" {
return sql.NullBool{Bool: false, Valid: true}
}
panic(fmt.Errorf(callsite(1)+"%d is []byte, not bool", value))
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not bool", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not bool", value))
case nil:
return sql.NullBool{}
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not bool", value))
}
}
return row.NullBoolField(Expr(format, values...))
}
// NullBoolField returns the sql.NullBool value of the field.
func (row *Row) NullBoolField(field Boolean) sql.NullBool {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call NullBoolField for static queries"))
}
if row.sqlRows == nil {
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &sql.NullBool{})
return sql.NullBool{}
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*sql.NullBool)
return *scanDest
}
// Enum scans the enum expression into destPtr.
func (row *Row) Enum(destPtr Enumeration, format string, values ...any) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call Enum for static queries"))
}
row.enum(destPtr, Expr(format, values...), 1)
}
// EnumField scans the enum field into destPtr.
func (row *Row) EnumField(destPtr Enumeration, field Enum) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call EnumField for static queries"))
}
row.enum(destPtr, field, 1)
}
func (row *Row) enum(destPtr Enumeration, field Enum, skip int) {
if row.sqlRows == nil {
destType := reflect.TypeOf(destPtr)
if destType.Kind() != reflect.Ptr {
panic(fmt.Errorf(callsite(skip+1)+"cannot pass in non pointer value (%#v) as destPtr", destPtr))
}
row.fields = append(row.fields, field)
switch destType.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.String:
row.scanDest = append(row.scanDest, &sql.NullString{})
default:
panic(fmt.Errorf(callsite(skip+1)+"underlying type of %[1]v is neither an integer or string (%[1]T)", destPtr))
}
return
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*sql.NullString)
names := destPtr.Enumerate()
enumIndex := 0
destValue := reflect.ValueOf(destPtr).Elem()
if scanDest.Valid {
enumIndex = getEnumIndex(scanDest.String, names, destValue.Type())
}
if enumIndex < 0 {
panic(fmt.Errorf(callsite(skip+1)+"%q is not a valid %T", scanDest.String, destPtr))
}
switch destValue.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
destValue.SetInt(int64(enumIndex))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
destValue.SetUint(uint64(enumIndex))
case reflect.String:
destValue.SetString(scanDest.String)
}
}
// Float64 returns the float64 value of the expression.
func (row *Row) Float64(format string, values ...any) float64 {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
return float64(value)
case float64:
return value
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not float64", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
n, err := strconv.ParseFloat(string(value), 64)
if err != nil {
panic(fmt.Errorf(callsite(1)+"%d is []byte, not float64", value))
}
return n
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not float64", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not float64", value))
case nil:
return 0
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not float64", value))
}
}
return row.NullFloat64Field(Expr(format, values...)).Float64
}
// Float64Field returns the float64 value of the field.
func (row *Row) Float64Field(field Number) float64 {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call Float64Field for static queries"))
}
return row.NullFloat64Field(field).Float64
}
// NullFloat64 returns the sql.NullFloat64 valye of the expression.
func (row *Row) NullFloat64(format string, values ...any) sql.NullFloat64 {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
return sql.NullFloat64{Float64: float64(value), Valid: true}
case float64:
return sql.NullFloat64{Float64: value, Valid: true}
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not float64", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
n, err := strconv.ParseFloat(string(value), 64)
if err != nil {
panic(fmt.Errorf(callsite(1)+"%d is []byte, not float64", value))
}
return sql.NullFloat64{Float64: n, Valid: true}
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not float64", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not float64", value))
case nil:
return sql.NullFloat64{}
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not float64", value))
}
}
return row.NullFloat64Field(Expr(format, values...))
}
// NullFloat64Field returns the sql.NullFloat64 value of the field.
func (row *Row) NullFloat64Field(field Number) sql.NullFloat64 {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call NullFloat64Field for static queries"))
}
if row.sqlRows == nil {
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &sql.NullFloat64{})
return sql.NullFloat64{}
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*sql.NullFloat64)
return *scanDest
}
// Int returns the int value of the expression.
func (row *Row) Int(format string, values ...any) int {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
return int(value)
case float64:
return int(value)
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not int", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
n, err := strconv.Atoi(string(value))
if err != nil {
panic(fmt.Errorf(callsite(1)+"%d is []byte, not int", value))
}
return n
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not int", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not int", value))
case nil:
return 0
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not int", value))
}
}
return int(row.NullInt64Field(Expr(format, values...)).Int64)
}
// IntField returns the int value of the field.
func (row *Row) IntField(field Number) int {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call IntField for static queries"))
}
return int(row.NullInt64Field(field).Int64)
}
// Int64 returns the int64 value of the expression.
func (row *Row) Int64(format string, values ...any) int64 {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
return int64(value)
case float64:
return int64(value)
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not int64", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
n, err := strconv.ParseInt(string(value), 10, 64)
if err != nil {
panic(fmt.Errorf(callsite(1)+"%d is []byte, not int64", value))
}
return n
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not int64", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not int64", value))
case nil:
return 0
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not int64", value))
}
}
return row.NullInt64Field(Expr(format, values...)).Int64
}
// Int64Field returns the int64 value of the field.
func (row *Row) Int64Field(field Number) int64 {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call Int64Field for static queries"))
}
return row.NullInt64Field(field).Int64
}
// NullInt64 returns the sql.NullInt64 value of the expression.
func (row *Row) NullInt64(format string, values ...any) sql.NullInt64 {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
return sql.NullInt64{Int64: value, Valid: true}
case float64:
return sql.NullInt64{Int64: int64(value), Valid: true}
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not int64", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
n, err := strconv.ParseInt(string(value), 10, 64)
if err != nil {
panic(fmt.Errorf(callsite(1)+"%d is []byte, not int64", value))
}
return sql.NullInt64{Int64: n, Valid: true}
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not int64", value))
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not int64", value))
case nil:
return sql.NullInt64{}
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not int64", value))
}
}
return row.NullInt64Field(Expr(format, values...))
}
// NullInt64Field returns the sql.NullInt64 value of the field.
func (row *Row) NullInt64Field(field Number) sql.NullInt64 {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call NullInt64Field for static queries"))
}
if row.sqlRows == nil {
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &sql.NullInt64{})
return sql.NullInt64{}
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*sql.NullInt64)
return *scanDest
}
// JSON scans the JSON expression into destPtr.
func (row *Row) JSON(destPtr any, format string, values ...any) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call JSON for static queries"))
}
row.json(destPtr, Expr(format, values...), 1)
}
// JSONField scans the JSON field into destPtr.
func (row *Row) JSONField(destPtr any, field JSON) {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call JSONField for static queries"))
}
row.json(destPtr, field, 1)
}
func (row *Row) json(destPtr any, field JSON, skip int) {
if row.sqlRows == nil {
if reflect.TypeOf(destPtr).Kind() != reflect.Ptr {
panic(fmt.Errorf(callsite(skip+1)+"cannot pass in non pointer value (%#v) as destPtr", destPtr))
}
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &nullBytes{
dialect: row.dialect,
displayType: displayTypeString,
})
return
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*nullBytes)
if scanDest.valid {
err := json.Unmarshal(scanDest.bytes, destPtr)
if err != nil {
_, file, line, _ := runtime.Caller(skip + 1)
panic(fmt.Errorf(callsite(skip+1)+"unmarshaling json %q into %T: %w", file, line, string(scanDest.bytes), destPtr, err))
}
}
}
// String returns the string value of the expression.
func (row *Row) String(format string, values ...any) string {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
panic(fmt.Errorf(callsite(1)+"%d is int64, not string", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not string", value))
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not string", value))
case []byte:
return string(value)
case string:
return value
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not string", value))
case nil:
return ""
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not string", value))
}
}
return row.NullStringField(Expr(format, values...)).String
}
// String returns the string value of the field.
func (row *Row) StringField(field String) string {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call StringField for static queries"))
}
return row.NullStringField(field).String
}
// NullString returns the sql.NullString value of the expression.
func (row *Row) NullString(format string, values ...any) sql.NullString {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
panic(fmt.Errorf(callsite(1)+"%d is int64, not string", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not string", value))
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not string", value))
case []byte:
return sql.NullString{String: string(value), Valid: true}
case string:
return sql.NullString{String: value, Valid: true}
case time.Time:
panic(fmt.Errorf(callsite(1)+"%v is time.Time, not string", value))
case nil:
return sql.NullString{}
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not string", value))
}
}
return row.NullStringField(Expr(format, values...))
}
// NullStringField returns the sql.NullString value of the field.
func (row *Row) NullStringField(field String) sql.NullString {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call NullStringField for static queries"))
}
if row.sqlRows == nil {
row.fields = append(row.fields, field)
row.scanDest = append(row.scanDest, &sql.NullString{})
return sql.NullString{}
}
defer func() {
row.runningIndex++
}()
scanDest := row.scanDest[row.runningIndex].(*sql.NullString)
return *scanDest
}
// https://github.com/mattn/go-sqlite3/blob/4396a38886da660e403409e35ef4a37906bf0975/sqlite3.go#L209
var sqliteTimestampFormats = []string{
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02T15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02T15:04:05.999999999",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
"2006-01-02 15:04",
"2006-01-02T15:04",
"2006-01-02",
}
// Time returns the time.Time value of the expression.
func (row *Row) Time(format string, values ...any) time.Time {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
panic(fmt.Errorf(callsite(1)+"%d is int64, not time.Time", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not time.Time", value))
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not time.Time", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
s := strings.TrimSuffix(string(value), "Z")
for _, format := range sqliteTimestampFormats {
if t, err := time.ParseInLocation(format, s, time.UTC); err == nil {
return t
}
}
panic(fmt.Errorf(callsite(1)+"%d is []byte, not time.Time", value))
case string:
panic(fmt.Errorf(callsite(1)+"%q is string, not time.Time", value))
case time.Time:
return value
case nil:
return time.Time{}
default:
panic(fmt.Errorf(callsite(1)+"%[1]v is %[1]T, not time.Time", value))
}
}
return row.NullTimeField(Expr(format, values...)).Time
}
// Time returns the time.Time value of the field.
func (row *Row) TimeField(field Time) time.Time {
if row.queryIsStatic {
panic(fmt.Errorf(callsite(1) + "cannot call TimeField for static queries"))
}
return row.NullTimeField(field).Time
}
// NullTime returns the sql.NullTime value of the expression.
func (row *Row) NullTime(format string, values ...any) sql.NullTime {
if row.queryIsStatic {
index, ok := row.columnIndex[format]
if !ok {
panic(fmt.Errorf(callsite(1)+"column %s does not exist (available columns: %s)", format, strings.Join(row.columns, ", ")))
}
value := row.values[index]
switch value := value.(type) {
case int64:
panic(fmt.Errorf(callsite(1)+"%d is int64, not time.Time", value))
case float64:
panic(fmt.Errorf(callsite(1)+"%d is float64, not time.Time", value))
case bool:
panic(fmt.Errorf(callsite(1)+"%v is bool, not time.Time", value))
case []byte:
// Special case: go-mysql-driver returns everything as []byte.
s := strings.TrimSuffix(string(value), "Z")
for _, format := range sqliteTimestampFormats {
if t, err := time.ParseInLocation(format, s, time.UTC); err == nil {
return sql.NullTime{Time: t, Valid: true}