forked from huandu/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
815 lines (659 loc) · 22.4 KB
/
struct.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
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"database/sql/driver"
"math"
"reflect"
"regexp"
"sort"
"strings"
)
var (
// DBTag is the struct tag to describe the name for a field in struct.
DBTag = "db"
// FieldTag is the struct tag to describe the tag name for a field in struct.
// Use "," to separate different tags.
FieldTag = "fieldtag"
// FieldOpt is the options for a struct field.
// As db column can contain "," in theory, field options should be provided in a separated tag.
FieldOpt = "fieldopt"
// FieldAs is the column alias (AS) for a struct field.
FieldAs = "fieldas"
)
const (
fieldOptWithQuote = "withquote"
fieldOptOmitEmpty = "omitempty"
optName = "optName"
optParams = "optParams"
)
var optRegex = regexp.MustCompile(`(?P<` + optName + `>\w+)(\((?P<` + optParams + `>.*)\))?`)
var typeOfSQLDriverValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
// Struct represents a struct type.
//
// All methods in Struct are thread-safe.
// We can define a global variable to hold a Struct and use it in any goroutine.
type Struct struct {
Flavor Flavor
structType reflect.Type
structFieldsParser structFieldsParser
withTags []string
withoutTags []string
}
var emptyStruct Struct
// NewStruct analyzes type information in structValue
// and creates a new Struct with all structValue fields.
// If structValue is not a struct, NewStruct returns a dummy Struct.
func NewStruct(structValue interface{}) *Struct {
t := reflect.TypeOf(structValue)
t = dereferencedType(t)
if t.Kind() != reflect.Struct {
return &emptyStruct
}
return &Struct{
Flavor: DefaultFlavor,
structType: t,
structFieldsParser: makeDefaultFieldsParser(t),
}
}
// For sets the default flavor of s and returns a shadow copy of s.
// The original s.Flavor is not changed.
func (s *Struct) For(flavor Flavor) *Struct {
c := *s
c.Flavor = flavor
return &c
}
// WithFieldMapper returns a new Struct based on s with custom field mapper.
// The original s is not changed.
func (s *Struct) WithFieldMapper(mapper FieldMapperFunc) *Struct {
if s.structType == nil {
return &emptyStruct
}
c := *s
c.structFieldsParser = makeCustomFieldsParser(s.structType, mapper)
return &c
}
// WithTag sets included tag(s) for all builder methods.
// For instance, calling s.WithTag("tag").SelectFrom("t") is to select all fields tagged with "tag" from table "t".
//
// If multiple tags are provided, fields tagged with any of them are included.
// That is, s.WithTag("tag1", "tag2").SelectFrom("t") is to select all fields tagged with "tag1" or "tag2" from table "t".
func (s *Struct) WithTag(tags ...string) *Struct {
if len(tags) == 0 {
return s
}
c := *s
c.mergeWithTags(tags)
return &c
}
func (s *Struct) mergeWithTags(with []string) {
newTags := make([]int, 0, len(with))
withTags := s.withTags
withoutTags := s.withoutTags
if len(withoutTags) == 0 {
for i, tag := range with {
if tag == "" {
continue
}
if !hasTag(withTags, tag) {
newTags = append(newTags, i)
}
}
} else {
for i, tag := range with {
if tag == "" {
continue
}
if !hasTag(withTags, tag) {
if !hasTag(withoutTags, tag) {
newTags = append(newTags, i)
}
}
}
}
if len(newTags) == 0 {
return
}
// Merge with tags.
withTags = make([]string, 0, len(s.withTags)+len(newTags))
withTags = append(withTags, s.withTags...)
for _, idx := range newTags {
withTags = append(withTags, with[idx])
}
sort.Strings(withTags)
withTags = removeDuplicatedTags(withTags)
s.withTags = withTags
}
// WithoutTag sets excluded tag(s) for all builder methods.
// For instance, calling s.WithoutTag("tag").SelectFrom("t") is to select all fields except those tagged with "tag" from table "t".
//
// If multiple tags are provided, fields tagged with any of them are excluded.
// That is, s.WithoutTag("tag1", "tag2").SelectFrom("t") is to exclude any field tagged with "tag1" or "tag2" from table "t".
func (s *Struct) WithoutTag(tags ...string) *Struct {
if len(tags) == 0 {
return s
}
c := *s
c.mergeWithoutTags(tags)
return &c
}
func (s *Struct) mergeWithoutTags(without []string) {
withTags := s.withTags
withoutTags := s.withoutTags
if len(withoutTags) == 0 {
withoutTags = make([]string, len(without))
copy(withoutTags, without)
} else {
newTags := make([]int, 0, len(without))
for i, tag := range without {
if tag == "" {
continue
}
if !hasTag(withoutTags, tag) {
newTags = append(newTags, i)
}
}
if len(newTags) == 0 {
return
}
// Merge without tags.
tags := make([]string, 0, len(withoutTags)+len(newTags))
tags = append(tags, withoutTags...)
for _, idx := range newTags {
tags = append(tags, without[idx])
}
withoutTags = tags
}
sort.Strings(withoutTags)
withoutTags = removeDuplicatedTags(withoutTags)
// Filter out useless tags in s.withTags.
kept := make([]int, 0, len(withTags))
for i, tag := range withTags {
if !hasTag(withoutTags, tag) {
kept = append(kept, i)
}
}
if len(kept) > 0 {
filteredTags := make([]string, 0, len(kept))
for _, i := range kept {
filteredTags = append(filteredTags, withTags[i])
}
withTags = filteredTags
} else {
withTags = nil
}
// Update with and without tags.
s.withTags = withTags
s.withoutTags = withoutTags
}
func hasTag(tags []string, tag string) bool {
if len(tags) == 0 {
return false
}
i := sort.SearchStrings(tags, tag)
return i < len(tags) && tags[i] == tag
}
func removeDuplicatedTags(tags []string) []string {
if len(tags) <= 1 {
return tags
}
// Unlikely to find any duplicates.
hasDupes := false
for i := 1; i < len(tags); i++ {
if tags[i] == tags[i-1] {
hasDupes = true
break
}
}
if !hasDupes {
return tags
}
unique := make([]string, 0, len(tags))
unique = append(unique, tags[0])
for i := 1; i < len(tags); i++ {
if tags[i] != tags[i-1] {
unique = append(unique, tags[i])
}
}
return unique
}
// SelectFrom creates a new `SelectBuilder` with table name.
// By default, all exported fields of the s are listed as columns in SELECT.
//
// Caller is responsible to set WHERE condition to find right record.
func (s *Struct) SelectFrom(table string) *SelectBuilder {
return s.selectFromWithTags(table, s.withTags, s.withoutTags)
}
// SelectFromForTag creates a new `SelectBuilder` with table name for a specified tag.
// By default, all fields of the s tagged with tag are listed as columns in SELECT.
//
// Caller is responsible to set WHERE condition to find right record.
//
// Deprecated: It's recommended to use s.WithTag(tag).SelectFrom(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) SelectFromForTag(table string, tag string) (sb *SelectBuilder) {
return s.selectFromWithTags(table, []string{tag}, nil)
}
func (s *Struct) selectFromWithTags(table string, with, without []string) (sb *SelectBuilder) {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
sb = s.Flavor.NewSelectBuilder()
sb.From(table)
if tagged == nil {
sb.Select("*")
return
}
buf := newStringBuilder()
cols := make([]string, 0, len(tagged.ForRead))
tableAlias := parseTableAlias(table)
for _, sf := range tagged.ForRead {
if s.Flavor != CQL && !strings.ContainsRune(sf.Alias, '.') {
buf.WriteString(tableAlias)
buf.WriteRune('.')
}
buf.WriteString(sf.NameForSelect(s.Flavor))
cols = append(cols, buf.String())
buf.Reset()
}
sb.Select(cols...)
return sb
}
func parseTableAlias(table string) string {
idx := strings.LastIndex(table, " ")
if idx == -1 {
return table
}
return table[idx+1:]
}
// Update creates a new `UpdateBuilder` with table name.
// By default, all exported fields of the s is assigned in UPDATE with the field values from value.
// If value's type is not the same as that of s, Update returns a dummy `UpdateBuilder` with table name.
//
// Caller is responsible to set WHERE condition to match right record.
func (s *Struct) Update(table string, value interface{}) *UpdateBuilder {
return s.updateWithTags(table, s.withTags, s.withoutTags, value)
}
// UpdateForTag creates a new `UpdateBuilder` with table name.
// By default, all fields of the s tagged with tag is assigned in UPDATE with the field values from value.
// If value's type is not the same as that of s, UpdateForTag returns a dummy `UpdateBuilder` with table name.
//
// Caller is responsible to set WHERE condition to match right record.
//
// Deprecated: It's recommended to use s.WithTag(tag).Update(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) UpdateForTag(table string, tag string, value interface{}) *UpdateBuilder {
return s.updateWithTags(table, []string{tag}, nil, value)
}
func (s *Struct) updateWithTags(table string, with, without []string, value interface{}) *UpdateBuilder {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
ub := s.Flavor.NewUpdateBuilder()
ub.Update(table)
if tagged == nil {
return ub
}
v := reflect.ValueOf(value)
v = dereferencedValue(v)
if v.Type() != s.structType {
return ub
}
assignments := make([]string, 0, len(tagged.ForWrite))
for _, sf := range tagged.ForWrite {
name := sf.Name
val := v.FieldByName(name)
if isEmptyValue(val) {
if sf.ShouldOmitEmpty(with...) {
continue
}
} else {
val = dereferencedFieldValue(val)
}
data := val.Interface()
assignments = append(assignments, ub.Assign(sf.Quote(s.Flavor), data))
}
ub.Set(assignments...)
return ub
}
// InsertInto creates a new `InsertBuilder` with table name using verb INSERT INTO.
// By default, all exported fields of s are set as columns by calling `InsertBuilder#Cols`,
// and value is added as a list of values by calling `InsertBuilder#Values`.
//
// InsertInto never returns any error.
// If the type of any item in value is not expected, it will be ignored.
// If value is an empty slice, `InsertBuilder#Values` will not be called.
func (s *Struct) InsertInto(table string, value ...interface{}) *InsertBuilder {
ib := s.Flavor.NewInsertBuilder()
ib.InsertInto(table)
s.buildColsAndValuesForTag(ib, s.withTags, s.withoutTags, value...)
return ib
}
// InsertIgnoreInto creates a new `InsertBuilder` with table name using verb INSERT IGNORE INTO.
// By default, all exported fields of s are set as columns by calling `InsertBuilder#Cols`,
// and value is added as a list of values by calling `InsertBuilder#Values`.
//
// InsertIgnoreInto never returns any error.
// If the type of any item in value is not expected, it will be ignored.
// If value is an empty slice, `InsertBuilder#Values` will not be called.
func (s *Struct) InsertIgnoreInto(table string, value ...interface{}) *InsertBuilder {
ib := s.Flavor.NewInsertBuilder()
ib.InsertIgnoreInto(table)
s.buildColsAndValuesForTag(ib, s.withTags, s.withoutTags, value...)
return ib
}
// ReplaceInto creates a new `InsertBuilder` with table name using verb REPLACE INTO.
// By default, all exported fields of s are set as columns by calling `InsertBuilder#Cols`,
// and value is added as a list of values by calling `InsertBuilder#Values`.
//
// ReplaceInto never returns any error.
// If the type of any item in value is not expected, it will be ignored.
// If value is an empty slice, `InsertBuilder#Values` will not be called.
func (s *Struct) ReplaceInto(table string, value ...interface{}) *InsertBuilder {
ib := s.Flavor.NewInsertBuilder()
ib.ReplaceInto(table)
s.buildColsAndValuesForTag(ib, s.withTags, s.withoutTags, value...)
return ib
}
// buildColsAndValuesForTag uses ib to set exported fields tagged with tag as columns
// and add value as a list of values.
func (s *Struct) buildColsAndValuesForTag(ib *InsertBuilder, with, without []string, value ...interface{}) {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
if tagged == nil {
return
}
vs := make([]reflect.Value, 0, len(value))
for _, item := range value {
v := reflect.ValueOf(item)
v = dereferencedFieldValue(v)
if v.Type() == s.structType {
vs = append(vs, v)
}
}
if len(vs) == 0 {
return
}
cols := make([]string, 0, len(tagged.ForWrite))
values := make([][]interface{}, len(vs))
nilCols := make([]int, 0, len(tagged.ForWrite))
for _, sf := range tagged.ForWrite {
cols = append(cols, sf.Quote(s.Flavor))
name := sf.Name
shouldOmitEmpty := sf.ShouldOmitEmpty(with...)
nilCnt := 0
for i, v := range vs {
val := v.FieldByName(name)
if isEmptyValue(val) && shouldOmitEmpty {
nilCnt++
}
val = dereferencedFieldValue(val)
if val.IsValid() {
values[i] = append(values[i], val.Interface())
} else {
values[i] = append(values[i], nil)
}
}
nilCols = append(nilCols, nilCnt)
}
// Try to filter out nil values if possible.
filteredCols := make([]string, 0, len(cols))
filteredValues := make([][]interface{}, len(values))
for i, cnt := range nilCols {
// If all values are nil in a column, ignore the column completely.
if cnt == len(values) {
continue
}
filteredCols = append(filteredCols, cols[i])
for n, value := range values {
filteredValues[n] = append(filteredValues[n], value[i])
}
}
ib.Cols(filteredCols...)
for _, value := range filteredValues {
ib.Values(value...)
}
}
// InsertIntoForTag creates a new `InsertBuilder` with table name using verb INSERT INTO.
// By default, exported fields tagged with tag are set as columns by calling `InsertBuilder#Cols`,
// and value is added as a list of values by calling `InsertBuilder#Values`.
//
// InsertIntoForTag never returns any error.
// If the type of any item in value is not expected, it will be ignored.
// If value is an empty slice, `InsertBuilder#Values` will not be called.
//
// Deprecated: It's recommended to use s.WithTag(tag).InsertInto(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) InsertIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder {
ib := s.Flavor.NewInsertBuilder()
ib.InsertInto(table)
s.buildColsAndValuesForTag(ib, []string{tag}, nil, value...)
return ib
}
// InsertIgnoreIntoForTag creates a new `InsertBuilder` with table name using verb INSERT IGNORE INTO.
// By default, exported fields tagged with tag are set as columns by calling `InsertBuilder#Cols`,
// and value is added as a list of values by calling `InsertBuilder#Values`.
//
// InsertIgnoreIntoForTag never returns any error.
// If the type of any item in value is not expected, it will be ignored.
// If value is an empty slice, `InsertBuilder#Values` will not be called.
//
// Deprecated: It's recommended to use s.WithTag(tag).InsertIgnoreInto(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) InsertIgnoreIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder {
ib := s.Flavor.NewInsertBuilder()
ib.InsertIgnoreInto(table)
s.buildColsAndValuesForTag(ib, []string{tag}, nil, value...)
return ib
}
// ReplaceIntoForTag creates a new `InsertBuilder` with table name using verb REPLACE INTO.
// By default, exported fields tagged with tag are set as columns by calling `InsertBuilder#Cols`,
// and value is added as a list of values by calling `InsertBuilder#Values`.
//
// ReplaceIntoForTag never returns any error.
// If the type of any item in value is not expected, it will be ignored.
// If value is an empty slice, `InsertBuilder#Values` will not be called.
//
// Deprecated: It's recommended to use s.WithTag(tag).ReplaceInto(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) ReplaceIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder {
ib := s.Flavor.NewInsertBuilder()
ib.ReplaceInto(table)
s.buildColsAndValuesForTag(ib, []string{tag}, nil, value...)
return ib
}
// DeleteFrom creates a new `DeleteBuilder` with table name.
//
// Caller is responsible to set WHERE condition to match right record.
func (s *Struct) DeleteFrom(table string) *DeleteBuilder {
db := s.Flavor.NewDeleteBuilder()
db.DeleteFrom(table)
return db
}
// Addr takes address of all exported fields of the s from the st.
// The returned result can be used in `Row#Scan` directly.
func (s *Struct) Addr(st interface{}) []interface{} {
return s.addrWithTags(s.withTags, s.withoutTags, st)
}
// AddrForTag takes address of all fields of the s tagged with tag from the st.
// The returned value can be used in `Row#Scan` directly.
//
// If tag is not defined in s in advance, returns nil.
//
// Deprecated: It's recommended to use s.WithTag(tag).Addr(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) AddrForTag(tag string, st interface{}) []interface{} {
return s.addrWithTags([]string{tag}, nil, st)
}
func (s *Struct) addrWithTags(with, without []string, st interface{}) []interface{} {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
if tagged == nil {
return nil
}
return s.addrWithFields(tagged.ForRead, st)
}
// AddrWithCols takes address of all columns defined in cols from the st.
// The returned value can be used in `Row#Scan` directly.
func (s *Struct) AddrWithCols(cols []string, st interface{}) []interface{} {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(s.withTags, s.withoutTags)
if tagged == nil {
return nil
}
fields := tagged.Cols(cols)
if fields == nil {
return nil
}
return s.addrWithFields(fields, st)
}
func (s *Struct) addrWithFields(fields []*structField, st interface{}) []interface{} {
v := reflect.ValueOf(st)
v = dereferencedValue(v)
if v.Type() != s.structType {
return nil
}
addrs := make([]interface{}, 0, len(fields))
for _, sf := range fields {
name := sf.Name
data := v.FieldByName(name).Addr().Interface()
addrs = append(addrs, data)
}
return addrs
}
// Columns returns column names of s for all exported struct fields.
func (s *Struct) Columns() []string {
return s.columnsWithTags(s.withTags, s.withoutTags)
}
// ColumnsForTag returns column names of the s tagged with tag.
//
// Deprecated: It's recommended to use s.WithTag(tag).Columns(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) ColumnsForTag(tag string) (cols []string) {
return s.columnsWithTags([]string{tag}, nil)
}
func (s *Struct) columnsWithTags(with, without []string) (cols []string) {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
if tagged == nil {
return
}
cols = make([]string, 0, len(tagged.ForWrite))
for _, sf := range tagged.ForWrite {
cols = append(cols, sf.Alias)
}
return
}
// Values returns a shadow copy of all exported fields in st.
func (s *Struct) Values(st interface{}) []interface{} {
return s.valuesWithTags(s.withTags, s.withoutTags, st)
}
// ValuesForTag returns a shadow copy of all fields tagged with tag in st.
//
// Deprecated: It's recommended to use s.WithTag(tag).Values(...) instead of calling this method.
// The former one is more readable and can be chained with other methods.
func (s *Struct) ValuesForTag(tag string, value interface{}) (values []interface{}) {
return s.valuesWithTags([]string{tag}, nil, value)
}
func (s *Struct) valuesWithTags(with, without []string, value interface{}) (values []interface{}) {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
if tagged == nil {
return
}
v := reflect.ValueOf(value)
v = dereferencedValue(v)
if v.Type() != s.structType {
return
}
values = make([]interface{}, 0, len(tagged.ForWrite))
for _, sf := range tagged.ForWrite {
name := sf.Name
data := v.FieldByName(name).Interface()
values = append(values, data)
}
return
}
// ForeachRead foreach tags.
func (s *Struct) ForeachRead(trans func(dbtag string, isQuoted bool, field reflect.StructField)) {
s.foreachReadWithTags(s.withTags, s.withoutTags, trans)
}
func (s *Struct) foreachReadWithTags(with, without []string, trans func(dbtag string, isQuoted bool, field reflect.StructField)) {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
if tagged == nil {
return
}
for _, sf := range tagged.ForRead {
trans(sf.DBTag, sf.IsQuoted, sf.Field)
}
}
// ForeachWrite foreach tags.
func (s *Struct) ForeachWrite(trans func(dbtag string, isQuoted bool, field reflect.StructField)) {
s.foreachWriteWithTags(s.withTags, s.withoutTags, trans)
}
func (s *Struct) foreachWriteWithTags(with, without []string, trans func(dbtag string, isQuoted bool, field reflect.StructField)) {
sfs := s.structFieldsParser()
tagged := sfs.FilterTags(with, without)
if tagged == nil {
return
}
for _, sf := range tagged.ForWrite {
trans(sf.DBTag, sf.IsQuoted, sf.Field)
}
}
func dereferencedType(t reflect.Type) reflect.Type {
for k := t.Kind(); k == reflect.Ptr || k == reflect.Interface; k = t.Kind() {
t = t.Elem()
}
return t
}
func dereferencedValue(v reflect.Value) reflect.Value {
for k := v.Kind(); k == reflect.Ptr || k == reflect.Interface; k = v.Kind() {
v = v.Elem()
}
return v
}
func dereferencedFieldValue(v reflect.Value) reflect.Value {
for k := v.Kind(); k == reflect.Ptr || k == reflect.Interface; k = v.Kind() {
if v.Type().Implements(typeOfSQLDriverValuer) {
break
}
v = v.Elem()
}
return v
}
// isEmptyValue checks if v is zero.
// Following code is borrowed from `IsZero` method in `reflect.Value` since Go 1.13.
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return math.Float64bits(v.Float()) == 0
case reflect.Complex64, reflect.Complex128:
c := v.Complex()
return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
case reflect.Array:
for i := 0; i < v.Len(); i++ {
if !isEmptyValue(v.Index(i)) {
return false
}
}
return true
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return v.IsNil()
case reflect.String:
return v.Len() == 0
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if !isEmptyValue(v.Field(i)) {
return false
}
}
return true
}
return false
}