forked from latolukasz/beeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_schema.go
1177 lines (1104 loc) · 35.2 KB
/
table_schema.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 beeorm
import (
"crypto/sha256"
"database/sql"
"fmt"
"hash/fnv"
"reflect"
"regexp"
"strconv"
"strings"
)
type CachedQuery struct{}
type cachedQueryDefinition struct {
Max int
Query string
TrackedFields []string
QueryFields []string
OrderFields []string
}
type Enum interface {
GetFields() []string
GetDefault() string
Has(value string) bool
Index(value string) int
}
type enum struct {
fields []string
mapping map[string]int
defaultValue string
}
func (enum *enum) GetFields() []string {
return enum.fields
}
func (enum *enum) GetDefault() string {
return enum.defaultValue
}
func (enum *enum) Has(value string) bool {
_, has := enum.mapping[value]
return has
}
func (enum *enum) Index(value string) int {
return enum.mapping[value]
}
func initEnum(ref interface{}, defaultValue ...string) *enum {
enum := &enum{}
e := reflect.ValueOf(ref)
enum.mapping = make(map[string]int)
enum.fields = make([]string, 0)
for i := 0; i < e.Type().NumField(); i++ {
name := e.Field(i).String()
enum.fields = append(enum.fields, name)
enum.mapping[name] = i + 1
}
if len(defaultValue) > 0 {
enum.defaultValue = defaultValue[0]
} else {
enum.defaultValue = enum.fields[0]
}
return enum
}
type TableSchema interface {
GetTableName() string
GetType() reflect.Type
NewEntity() Entity
DropTable(engine Engine)
TruncateTable(engine Engine)
UpdateSchema(engine Engine)
UpdateSchemaAndTruncateTable(engine Engine)
GetMysql(engine Engine) *DB
GetLocalCache(engine Engine) (cache *LocalCache, has bool)
GetRedisCache(engine Engine) (cache *RedisCache, has bool)
GetReferences() []string
GetColumns() []string
GetUniqueIndexes() map[string][]string
GetSchemaChanges(engine Engine) (has bool, alters []Alter)
GetUsage(registry ValidatedRegistry) map[reflect.Type][]string
}
type tableSchema struct {
tableName string
mysqlPoolName string
t reflect.Type
fields *tableFields
registry *validatedRegistry
fieldsQuery string
tags map[string]map[string]string
cachedIndexes map[string]*cachedQueryDefinition
cachedIndexesOne map[string]*cachedQueryDefinition
cachedIndexesAll map[string]*cachedQueryDefinition
columnNames []string
columnMapping map[string]int
uniqueIndices map[string][]string
uniqueIndicesGlobal map[string][]string
dirtyFields map[string][]string
refOne []string
refMany []string
idIndex int
localCacheName string
hasLocalCache bool
redisCacheName string
hasRedisCache bool
searchCacheName string
cachePrefix string
structureHash uint64
hasFakeDelete bool
hasSearchableFakeDelete bool
hasLog bool
logPoolName string //name of redis
logTableName string
skipLogs []string
hasUUID bool
mapBindToScanPointer mapBindToScanPointer
mapPointerToValue mapPointerToValue
}
type mapBindToScanPointer map[string]func() interface{}
type mapPointerToValue map[string]func(val interface{}) interface{}
type tableFields struct {
t reflect.Type
fields map[int]reflect.StructField
prefix string
uintegers []int
integers []int
uintegersNullable []int
uintegersNullableSize []int
integersNullable []int
integersNullableSize []int
strings []int
stringsEnums []int
enums []Enum
sliceStringsSets []int
sets []Enum
bytes []int
fakeDelete int
booleans []int
booleansNullable []int
floats []int
floatsPrecision []int
floatsNullable []int
floatsNullablePrecision []int
floatsNullableSize []int
timesNullable []int
datesNullable []int
times []int
dates []int
jsons []int
structs []int
structsFields []*tableFields
refs []int
refsTypes []reflect.Type
refsMany []int
refsManyTypes []reflect.Type
}
func getTableSchema(registry *validatedRegistry, entityType reflect.Type) *tableSchema {
return registry.tableSchemas[entityType]
}
func (tableSchema *tableSchema) GetTableName() string {
return tableSchema.tableName
}
func (tableSchema *tableSchema) GetType() reflect.Type {
return tableSchema.t
}
func (tableSchema *tableSchema) DropTable(engine Engine) {
pool := tableSchema.GetMysql(engine)
pool.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`.`%s`;", pool.GetPoolConfig().GetDatabase(), tableSchema.tableName))
}
func (tableSchema *tableSchema) TruncateTable(engine Engine) {
pool := tableSchema.GetMysql(engine)
_ = pool.Exec(fmt.Sprintf("DELETE FROM `%s`.`%s`", pool.GetPoolConfig().GetDatabase(), tableSchema.tableName))
_ = pool.Exec(fmt.Sprintf("ALTER TABLE `%s`.`%s` AUTO_INCREMENT = 1", pool.GetPoolConfig().GetDatabase(), tableSchema.tableName))
}
func (tableSchema *tableSchema) UpdateSchema(engine Engine) {
pool := tableSchema.GetMysql(engine)
has, alters := tableSchema.GetSchemaChanges(engine)
if has {
for _, alter := range alters {
_ = pool.Exec(alter.SQL)
}
}
}
func (tableSchema *tableSchema) UpdateSchemaAndTruncateTable(engine Engine) {
tableSchema.UpdateSchema(engine)
pool := tableSchema.GetMysql(engine)
_ = pool.Exec(fmt.Sprintf("DELETE FROM `%s`.`%s`", pool.GetPoolConfig().GetDatabase(), tableSchema.tableName))
_ = pool.Exec(fmt.Sprintf("ALTER TABLE `%s`.`%s` AUTO_INCREMENT = 1", pool.GetPoolConfig().GetDatabase(), tableSchema.tableName))
}
func (tableSchema *tableSchema) GetMysql(engine Engine) *DB {
return engine.GetMysql(tableSchema.mysqlPoolName)
}
func (tableSchema *tableSchema) GetLocalCache(engine Engine) (cache *LocalCache, has bool) {
if !tableSchema.hasLocalCache {
return nil, false
}
return engine.GetLocalCache(tableSchema.localCacheName), true
}
func (tableSchema *tableSchema) GetRedisCache(engine Engine) (cache *RedisCache, has bool) {
if !tableSchema.hasRedisCache {
return nil, false
}
return engine.GetRedis(tableSchema.redisCacheName), true
}
func (tableSchema *tableSchema) GetReferences() []string {
return tableSchema.refOne
}
func (tableSchema *tableSchema) GetColumns() []string {
return tableSchema.columnNames
}
func (tableSchema *tableSchema) GetUniqueIndexes() map[string][]string {
data := make(map[string][]string)
for k, v := range tableSchema.uniqueIndices {
data[k] = v
}
for k, v := range tableSchema.uniqueIndicesGlobal {
data[k] = v
}
return data
}
func (tableSchema *tableSchema) GetSchemaChanges(engine Engine) (has bool, alters []Alter) {
return getSchemaChanges(engine.(*engineImplementation), tableSchema)
}
func (tableSchema *tableSchema) GetUsage(registry ValidatedRegistry) map[reflect.Type][]string {
vRegistry := registry.(*validatedRegistry)
results := make(map[reflect.Type][]string)
if vRegistry.entities != nil {
for _, t := range vRegistry.entities {
schema := getTableSchema(vRegistry, t)
tableSchema.getUsage(schema.fields, schema.t, "", results)
}
}
return results
}
func (tableSchema *tableSchema) getUsage(fields *tableFields, t reflect.Type, prefix string, results map[reflect.Type][]string) {
tName := tableSchema.t.String()
for i, fieldID := range fields.refs {
if fields.refsTypes[i].String() == tName {
results[t] = append(results[t], prefix+fields.t.Field(fieldID).Name)
}
}
for i, k := range fields.structs {
f := fields.t.Field(k)
subPrefix := prefix
if !f.Anonymous {
subPrefix += f.Name
}
tableSchema.getUsage(fields.structsFields[i], t, subPrefix, results)
}
}
func (tableSchema *tableSchema) init(registry *Registry, entityType reflect.Type) error {
tableSchema.t = entityType
tableSchema.tags = extractTags(registry, entityType, "")
oneRefs := make([]string, 0)
manyRefs := make([]string, 0)
tableSchema.mapBindToScanPointer = mapBindToScanPointer{}
tableSchema.mapPointerToValue = mapPointerToValue{}
tableSchema.mysqlPoolName = tableSchema.getTag("mysql", "default", "default")
_, has := registry.mysqlPools[tableSchema.mysqlPoolName]
if !has {
return fmt.Errorf("mysql pool '%s' not found", tableSchema.mysqlPoolName)
}
tableSchema.tableName = tableSchema.getTag("table", entityType.Name(), entityType.Name())
localCache := tableSchema.getTag("localCache", "default", "")
redisCache := tableSchema.getTag("redisCache", "default", "")
if localCache != "" {
_, has = registry.localCachePools[localCache]
if !has {
return fmt.Errorf("local cache pool '%s' not found", localCache)
}
}
if redisCache != "" {
_, has = registry.mysqlPools[redisCache]
if !has {
return fmt.Errorf("redis pool '%s' not found", redisCache)
}
}
cachePrefix := ""
if tableSchema.mysqlPoolName != "default" {
cachePrefix = tableSchema.mysqlPoolName
}
cachePrefix += tableSchema.tableName
cachedQueries := make(map[string]*cachedQueryDefinition)
cachedQueriesOne := make(map[string]*cachedQueryDefinition)
cachedQueriesAll := make(map[string]*cachedQueryDefinition)
dirtyFields := make(map[string][]string)
fakeDeleteField, has := entityType.FieldByName("FakeDelete")
if has && fakeDeleteField.Type.String() == "bool" {
tableSchema.hasFakeDelete = true
searchable := tableSchema.tags["FakeDelete"] != nil && tableSchema.tags["FakeDelete"]["searchable"] == "true"
tableSchema.hasSearchableFakeDelete = searchable
}
for key, values := range tableSchema.tags {
isOne := false
query, has := values["query"]
if !has {
query, has = values["queryOne"]
isOne = true
}
queryOrigin := query
fields := make([]string, 0)
fieldsTracked := make([]string, 0)
fieldsQuery := make([]string, 0)
fieldsOrder := make([]string, 0)
if has {
re := regexp.MustCompile(":([A-Za-z\\d])+")
variables := re.FindAllString(query, -1)
for _, variable := range variables {
fieldName := variable[1:]
has := false
for _, v := range fields {
if v == fieldName {
has = true
break
}
}
if !has {
fields = append(fields, fieldName)
}
query = strings.Replace(query, variable, fmt.Sprintf("`%s`", fieldName), 1)
}
if tableSchema.hasFakeDelete && len(variables) > 0 {
fields = append(fields, "FakeDelete")
}
if query == "" {
if tableSchema.hasFakeDelete {
query = "`FakeDelete` = 0 ORDER BY `ID`"
} else {
query = "1 ORDER BY `ID`"
}
} else if tableSchema.hasFakeDelete {
query = "`FakeDelete` = 0 AND " + query
}
queryLower := strings.ToLower(queryOrigin)
posOrderBy := strings.Index(queryLower, "order by")
for _, f := range fields {
if f != "ID" {
fieldsTracked = append(fieldsTracked, f)
}
pos := strings.Index(queryOrigin, ":"+f)
if pos < posOrderBy || posOrderBy == -1 {
fieldsQuery = append(fieldsQuery, f)
}
}
if posOrderBy > -1 {
variables = re.FindAllString(queryOrigin[posOrderBy:], -1)
for _, variable := range variables {
fieldName := variable[1:]
fieldsOrder = append(fieldsOrder, fieldName)
}
}
if !isOne {
def := &cachedQueryDefinition{50000, query, fieldsTracked, fieldsQuery, fieldsOrder}
cachedQueries[key] = def
cachedQueriesAll[key] = def
} else {
def := &cachedQueryDefinition{1, query, fieldsTracked, fieldsQuery, fieldsOrder}
cachedQueriesOne[key] = def
cachedQueriesAll[key] = def
}
}
_, has = values["ref"]
if has {
oneRefs = append(oneRefs, key)
}
_, has = values["refs"]
if has {
manyRefs = append(manyRefs, key)
}
dirtyValues, has := values["dirty"]
if has {
for _, v := range strings.Split(dirtyValues, ",") {
dirtyFields[v] = append(dirtyFields[v], key)
}
}
}
logPoolName := tableSchema.getTag("log", tableSchema.mysqlPoolName, "")
if logPoolName == "" && registry.forcedEntityLog != "" {
logPoolName = registry.forcedEntityLog
}
hasUUID := tableSchema.getTag("uuid", "true", "false") == "true"
uniqueIndices := make(map[string]map[int]string)
uniqueIndicesSimple := make(map[string][]string)
uniqueIndicesSimpleGlobal := make(map[string][]string)
indices := make(map[string]map[int]string)
skipLogs := make([]string, 0)
uniqueGlobal := tableSchema.getTag("unique", "", "")
if uniqueGlobal != "" {
parts := strings.Split(uniqueGlobal, "|")
for _, part := range parts {
def := strings.Split(part, ":")
uniqueIndices[def[0]] = make(map[int]string)
uniqueIndicesSimple[def[0]] = make([]string, 0)
uniqueIndicesSimpleGlobal[def[0]] = make([]string, 0)
for i, field := range strings.Split(def[1], ",") {
uniqueIndices[def[0]][i+1] = field
uniqueIndicesSimple[def[0]] = append(uniqueIndicesSimple[def[0]], field)
uniqueIndicesSimpleGlobal[def[0]] = append(uniqueIndicesSimpleGlobal[def[0]], field)
}
}
}
for k, v := range tableSchema.tags {
keys, has := v["unique"]
if has && k != "ORM" {
values := strings.Split(keys, ",")
for _, indexName := range values {
parts := strings.Split(indexName, ":")
id := int64(1)
if len(parts) > 1 {
id, _ = strconv.ParseInt(parts[1], 10, 64)
}
if uniqueIndices[parts[0]] == nil {
uniqueIndices[parts[0]] = make(map[int]string)
}
uniqueIndices[parts[0]][int(id)] = k
if uniqueIndicesSimple[parts[0]] == nil {
uniqueIndicesSimple[parts[0]] = make([]string, 0)
}
uniqueIndicesSimple[parts[0]] = append(uniqueIndicesSimple[parts[0]], k)
}
}
keys, has = v["index"]
if has {
values := strings.Split(keys, ",")
for _, indexName := range values {
parts := strings.Split(indexName, ":")
id := int64(1)
if len(parts) > 1 {
id, _ = strconv.ParseInt(parts[1], 10, 64)
}
if indices[parts[0]] == nil {
indices[parts[0]] = make(map[int]string)
}
indices[parts[0]][int(id)] = k
}
}
_, has = v["skip-log"]
if has {
skipLogs = append(skipLogs, k)
}
}
for _, ref := range oneRefs {
has := false
for _, v := range indices {
if v[1] == ref {
has = true
break
}
}
if !has {
for _, v := range uniqueIndices {
if v[1] == ref {
has = true
break
}
}
if !has {
indices["_"+ref] = map[int]string{1: ref}
}
}
}
tableSchema.fields = tableSchema.buildTableFields(entityType, registry, 1, "", tableSchema.tags)
tableSchema.columnNames, tableSchema.fieldsQuery = tableSchema.fields.buildColumnNames("")
columnMapping := make(map[string]int)
for i, name := range tableSchema.columnNames {
columnMapping[name] = i
}
tableSchema.idIndex = columnMapping["ID"]
cachePrefix = fmt.Sprintf("%x", sha256.Sum256([]byte(cachePrefix+tableSchema.fieldsQuery)))
cachePrefix = cachePrefix[0:5]
h := fnv.New32a()
_, _ = h.Write([]byte(cachePrefix))
tableSchema.structureHash = uint64(h.Sum32())
tableSchema.columnMapping = columnMapping
tableSchema.cachedIndexes = cachedQueries
tableSchema.cachedIndexesOne = cachedQueriesOne
tableSchema.cachedIndexesAll = cachedQueriesAll
tableSchema.dirtyFields = dirtyFields
tableSchema.localCacheName = localCache
tableSchema.hasLocalCache = localCache != ""
tableSchema.redisCacheName = redisCache
tableSchema.hasRedisCache = redisCache != ""
tableSchema.refOne = oneRefs
tableSchema.refMany = manyRefs
tableSchema.cachePrefix = cachePrefix
tableSchema.uniqueIndices = uniqueIndicesSimple
tableSchema.uniqueIndicesGlobal = uniqueIndicesSimpleGlobal
tableSchema.hasLog = logPoolName != ""
tableSchema.hasUUID = hasUUID
tableSchema.logPoolName = logPoolName
tableSchema.logTableName = fmt.Sprintf("_log_%s_%s", tableSchema.mysqlPoolName, tableSchema.tableName)
tableSchema.skipLogs = skipLogs
return tableSchema.validateIndexes(uniqueIndices, indices)
}
func (tableSchema *tableSchema) validateIndexes(uniqueIndices map[string]map[int]string, indices map[string]map[int]string) error {
all := make(map[string]map[int]string)
for k, v := range uniqueIndices {
all[k] = v
}
for k, v := range indices {
all[k] = v
}
for k, v := range all {
for k2, v2 := range all {
if k == k2 {
continue
}
same := 0
for i := 1; i <= len(v); i++ {
right, has := v2[i]
if has && right == v[i] {
same++
continue
}
break
}
if same == len(v) {
return fmt.Errorf("duplicated index %s with %s in %s", k, k2, tableSchema.t.String())
}
}
}
for k, v := range tableSchema.cachedIndexesOne {
ok := false
for _, columns := range uniqueIndices {
if len(columns) != len(v.QueryFields) {
continue
}
valid := 0
for _, field1 := range v.QueryFields {
for _, field2 := range columns {
if field1 == field2 {
valid++
}
}
}
if valid == len(columns) {
ok = true
}
}
if !ok {
return fmt.Errorf("missing unique index for cached query '%s' in %s", k, tableSchema.t.String())
}
}
for k, v := range tableSchema.cachedIndexes {
if v.Query == "1 ORDER BY `ID`" {
continue
}
//first do we have query fields
ok := false
for _, columns := range all {
valid := 0
for _, field1 := range v.QueryFields {
for _, field2 := range columns {
if field1 == field2 {
valid++
}
}
}
if valid == len(v.QueryFields) {
if len(v.OrderFields) == 0 {
ok = true
break
}
valid := 0
key := len(columns)
if columns[len(columns)] == "FakeDelete" {
key--
}
for i := len(v.OrderFields); i > 0; i-- {
if columns[key] == v.OrderFields[i-1] {
valid++
key--
continue
}
break
}
if valid == len(v.OrderFields) {
ok = true
}
}
}
if !ok {
return fmt.Errorf("missing index for cached query '%s' in %s", k, tableSchema.t.String())
}
}
return nil
}
func (tableSchema *tableSchema) getTag(key, trueValue, defaultValue string) string {
userValue, has := tableSchema.tags["ORM"][key]
if has {
if userValue == "true" {
return trueValue
}
return userValue
}
return defaultValue
}
func (tableSchema *tableSchema) buildTableFields(t reflect.Type, registry *Registry,
start int, prefix string, schemaTags map[string]map[string]string) *tableFields {
fields := &tableFields{t: t, prefix: prefix, fields: make(map[int]reflect.StructField)}
for i := start; i < t.NumField(); i++ {
f := t.Field(i)
tags := schemaTags[prefix+f.Name]
_, has := tags["ignore"]
if has {
continue
}
attributes := schemaFieldAttributes{
Fields: fields,
Tags: tags,
Index: i,
Prefix: prefix,
Field: f,
TypeName: f.Type.String(),
}
fields.fields[i] = f
switch attributes.TypeName {
case "uint",
"uint8",
"uint16",
"uint32",
"uint64":
tableSchema.buildUintField(attributes)
case "*uint",
"*uint8",
"*uint16",
"*uint32",
"*uint64":
tableSchema.buildUintPointerField(attributes)
case "int",
"int8",
"int16",
"int32",
"int64":
tableSchema.buildIntField(attributes)
case "*int",
"*int8",
"*int16",
"*int32",
"*int64":
tableSchema.buildIntPointerField(attributes)
case "string":
tableSchema.buildStringField(attributes, registry)
case "[]string":
tableSchema.buildStringSliceField(attributes, registry)
case "[]uint8":
fields.bytes = append(fields.bytes, i)
case "bool":
tableSchema.buildBoolField(attributes)
case "*bool":
tableSchema.buildBoolPointerField(attributes)
case "float32",
"float64":
tableSchema.buildFloatField(attributes)
case "*float32",
"*float64":
tableSchema.buildFloatPointerField(attributes)
case "*beeorm.CachedQuery":
continue
case "*time.Time":
tableSchema.buildTimePointerField(attributes)
case "time.Time":
tableSchema.buildTimeField(attributes)
default:
k := f.Type.Kind().String()
if k == "struct" {
tableSchema.buildStructField(attributes, registry, schemaTags)
} else if k == "ptr" {
tableSchema.buildPointerField(attributes)
} else {
tableSchema.buildPointersSliceField(attributes)
}
}
}
return fields
}
type schemaFieldAttributes struct {
Field reflect.StructField
TypeName string
Tags map[string]string
Fields *tableFields
Index int
Prefix string
}
func (attributes schemaFieldAttributes) GetColumnName() string {
return attributes.Prefix + attributes.Field.Name
}
func (tableSchema *tableSchema) buildUintField(attributes schemaFieldAttributes) {
attributes.Fields.uintegers = append(attributes.Fields.uintegers, attributes.Index)
columnName := attributes.GetColumnName()
tableSchema.mapBindToScanPointer[columnName] = func() interface{} {
v := uint64(0)
return &v
}
tableSchema.mapPointerToValue[columnName] = func(val interface{}) interface{} {
return *val.(*uint64)
}
}
func (tableSchema *tableSchema) buildUintPointerField(attributes schemaFieldAttributes) {
attributes.Fields.uintegersNullable = append(attributes.Fields.uintegersNullable, attributes.Index)
columnName := attributes.GetColumnName()
switch attributes.TypeName {
case "*uint":
attributes.Fields.uintegersNullableSize = append(attributes.Fields.uintegersNullableSize, 0)
case "*uint8":
attributes.Fields.uintegersNullableSize = append(attributes.Fields.uintegersNullableSize, 8)
case "*uint16":
attributes.Fields.uintegersNullableSize = append(attributes.Fields.uintegersNullableSize, 16)
case "*uint32":
attributes.Fields.uintegersNullableSize = append(attributes.Fields.uintegersNullableSize, 32)
case "*uint64":
attributes.Fields.uintegersNullableSize = append(attributes.Fields.uintegersNullableSize, 64)
}
tableSchema.mapBindToScanPointer[columnName] = scanIntNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerUintNullableScan
}
func (tableSchema *tableSchema) buildIntField(attributes schemaFieldAttributes) {
attributes.Fields.integers = append(attributes.Fields.integers, attributes.Index)
columnName := attributes.GetColumnName()
tableSchema.mapBindToScanPointer[columnName] = func() interface{} {
v := int64(0)
return &v
}
tableSchema.mapPointerToValue[columnName] = func(val interface{}) interface{} {
return *val.(*int64)
}
}
func (tableSchema *tableSchema) buildIntPointerField(attributes schemaFieldAttributes) {
attributes.Fields.integersNullable = append(attributes.Fields.integersNullable, attributes.Index)
columnName := attributes.GetColumnName()
switch attributes.TypeName {
case "*int":
attributes.Fields.integersNullableSize = append(attributes.Fields.integersNullableSize, 0)
case "*int8":
attributes.Fields.integersNullableSize = append(attributes.Fields.integersNullableSize, 8)
case "*int16":
attributes.Fields.integersNullableSize = append(attributes.Fields.integersNullableSize, 16)
case "*int32":
attributes.Fields.integersNullableSize = append(attributes.Fields.integersNullableSize, 32)
case "*int64":
attributes.Fields.integersNullableSize = append(attributes.Fields.integersNullableSize, 64)
}
tableSchema.mapBindToScanPointer[columnName] = scanIntNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerIntNullableScan
}
func (tableSchema *tableSchema) buildStringField(attributes schemaFieldAttributes, registry *Registry) {
enumCode, hasEnum := attributes.Tags["enum"]
columnName := attributes.GetColumnName()
if hasEnum {
attributes.Fields.stringsEnums = append(attributes.Fields.stringsEnums, attributes.Index)
attributes.Fields.enums = append(attributes.Fields.enums, registry.enums[enumCode])
} else {
attributes.Fields.strings = append(attributes.Fields.strings, attributes.Index)
}
tableSchema.mapBindToScanPointer[columnName] = func() interface{} {
return &sql.NullString{}
}
tableSchema.mapPointerToValue[columnName] = func(val interface{}) interface{} {
v := val.(*sql.NullString)
if v.Valid {
return v.String
}
return nil
}
}
func (tableSchema *tableSchema) buildStringSliceField(attributes schemaFieldAttributes, registry *Registry) {
setCode, hasSet := attributes.Tags["set"]
columnName := attributes.GetColumnName()
if hasSet {
attributes.Fields.sliceStringsSets = append(attributes.Fields.sliceStringsSets, attributes.Index)
attributes.Fields.sets = append(attributes.Fields.sets, registry.enums[setCode])
} else {
attributes.Fields.jsons = append(attributes.Fields.jsons, attributes.Index)
}
tableSchema.mapBindToScanPointer[columnName] = scanStringNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerStringNullableScan
}
func (tableSchema *tableSchema) buildBoolField(attributes schemaFieldAttributes) {
columnName := attributes.GetColumnName()
if attributes.GetColumnName() == "FakeDelete" {
attributes.Fields.fakeDelete = attributes.Index
} else {
attributes.Fields.booleans = append(attributes.Fields.booleans, attributes.Index)
tableSchema.mapBindToScanPointer[columnName] = scanBoolPointer
tableSchema.mapPointerToValue[columnName] = pointerBoolScan
}
}
func (tableSchema *tableSchema) buildBoolPointerField(attributes schemaFieldAttributes) {
attributes.Fields.booleansNullable = append(attributes.Fields.booleansNullable, attributes.Index)
columnName := attributes.GetColumnName()
tableSchema.mapBindToScanPointer[columnName] = scanBoolNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerBoolNullableScan
}
func (tableSchema *tableSchema) buildFloatField(attributes schemaFieldAttributes) {
columnName := attributes.GetColumnName()
precision := 8
if attributes.TypeName == "float32" {
precision = 4
}
precisionAttribute, has := attributes.Tags["precision"]
if has {
userPrecision, _ := strconv.Atoi(precisionAttribute)
precision = userPrecision
} else {
decimal, has := attributes.Tags["decimal"]
if has {
decimalArgs := strings.Split(decimal, ",")
precision, _ = strconv.Atoi(decimalArgs[1])
}
}
attributes.Fields.floats = append(attributes.Fields.floats, attributes.Index)
attributes.Fields.floatsPrecision = append(attributes.Fields.floatsPrecision, precision)
tableSchema.mapBindToScanPointer[columnName] = func() interface{} {
v := float64(0)
return &v
}
tableSchema.mapPointerToValue[columnName] = func(val interface{}) interface{} {
return *val.(*float64)
}
}
func (tableSchema *tableSchema) buildFloatPointerField(attributes schemaFieldAttributes) {
columnName := attributes.GetColumnName()
precision := 8
if attributes.TypeName == "*float32" {
precision = 4
attributes.Fields.floatsNullableSize = append(attributes.Fields.floatsNullableSize, 32)
} else {
attributes.Fields.floatsNullableSize = append(attributes.Fields.floatsNullableSize, 64)
}
precisionAttribute, has := attributes.Tags["precision"]
if has {
userPrecision, _ := strconv.Atoi(precisionAttribute)
precision = userPrecision
} else {
precisionAttribute, has := attributes.Tags["decimal"]
if has {
precision, _ = strconv.Atoi(strings.Split(precisionAttribute, ",")[1])
}
}
attributes.Fields.floatsNullable = append(attributes.Fields.floatsNullable, attributes.Index)
attributes.Fields.floatsNullablePrecision = append(attributes.Fields.floatsNullablePrecision, precision)
tableSchema.mapBindToScanPointer[columnName] = scanFloatNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerFloatNullableScan
}
func (tableSchema *tableSchema) buildTimePointerField(attributes schemaFieldAttributes) {
columnName := attributes.GetColumnName()
_, hasTime := attributes.Tags["time"]
if hasTime {
attributes.Fields.timesNullable = append(attributes.Fields.timesNullable, attributes.Index)
} else {
attributes.Fields.datesNullable = append(attributes.Fields.datesNullable, attributes.Index)
}
tableSchema.mapBindToScanPointer[columnName] = scanStringNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerStringNullableScan
}
func (tableSchema *tableSchema) buildTimeField(attributes schemaFieldAttributes) {
columnName := attributes.GetColumnName()
_, hasTime := attributes.Tags["time"]
if hasTime {
attributes.Fields.times = append(attributes.Fields.times, attributes.Index)
} else {
attributes.Fields.dates = append(attributes.Fields.dates, attributes.Index)
}
tableSchema.mapBindToScanPointer[columnName] = scanStringPointer
tableSchema.mapPointerToValue[columnName] = pointerStringScan
}
func (tableSchema *tableSchema) buildStructField(attributes schemaFieldAttributes, registry *Registry,
schemaTags map[string]map[string]string) {
attributes.Fields.structs = append(attributes.Fields.structs, attributes.Index)
subPrefix := ""
if !attributes.Field.Anonymous {
subPrefix = attributes.Field.Name
}
subFields := tableSchema.buildTableFields(attributes.Field.Type, registry, 0, subPrefix, schemaTags)
attributes.Fields.structsFields = append(attributes.Fields.structsFields, subFields)
}
func (tableSchema *tableSchema) buildPointerField(attributes schemaFieldAttributes) {
columnName := attributes.GetColumnName()
modelType := reflect.TypeOf((*Entity)(nil)).Elem()
if attributes.Field.Type.Implements(modelType) {
attributes.Fields.refs = append(attributes.Fields.refs, attributes.Index)
attributes.Fields.refsTypes = append(attributes.Fields.refsTypes, attributes.Field.Type.Elem())
tableSchema.mapBindToScanPointer[columnName] = scanIntNullablePointer
tableSchema.mapPointerToValue[columnName] = pointerUintNullableScan
} else {
attributes.Fields.jsons = append(attributes.Fields.jsons, attributes.Index)
}
}
func (tableSchema *tableSchema) buildPointersSliceField(attributes schemaFieldAttributes) {
if attributes.TypeName[0:3] == "[]*" {
modelType := reflect.TypeOf((*Entity)(nil)).Elem()
t := attributes.Field.Type.Elem()
if t.Implements(modelType) {
attributes.Fields.refsMany = append(attributes.Fields.refsMany, attributes.Index)
attributes.Fields.refsManyTypes = append(attributes.Fields.refsManyTypes, t.Elem())
return
}
}
attributes.Fields.jsons = append(attributes.Fields.jsons, attributes.Index)
}
func extractTags(registry *Registry, entityType reflect.Type, prefix string) (fields map[string]map[string]string) {
fields = make(map[string]map[string]string)
for i := 0; i < entityType.NumField(); i++ {
field := entityType.Field(i)
for k, v := range extractTag(registry, field) {
fields[prefix+k] = v
}
_, hasIgnore := fields[field.Name]["ignore"]
if hasIgnore {
continue
}
refOne := ""
refMany := ""
hasRef := false
hasRefMany := false
if field.Type.Kind().String() == "ptr" {
refName := field.Type.Elem().String()
_, hasRef = registry.entities[refName]
if hasRef {
refOne = refName
}
} else if field.Type.String()[0:3] == "[]*" {
refName := field.Type.String()[3:]
_, hasRefMany = registry.entities[refName]
if hasRefMany {
refMany = refName
}
}
query, hasQuery := field.Tag.Lookup("query")
queryOne, hasQueryOne := field.Tag.Lookup("queryOne")
if hasQuery {
if fields[field.Name] == nil {
fields[field.Name] = make(map[string]string)
}
fields[field.Name]["query"] = query
}
if hasQueryOne {
if fields[field.Name] == nil {
fields[field.Name] = make(map[string]string)
}
fields[field.Name]["queryOne"] = queryOne
}
if hasRef {
if fields[field.Name] == nil {
fields[field.Name] = make(map[string]string)
}
fields[field.Name]["ref"] = refOne
}
if hasRefMany {