-
Notifications
You must be signed in to change notification settings - Fork 3
/
struct_parser.go
975 lines (944 loc) · 28.3 KB
/
struct_parser.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
package ddl
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"reflect"
"strconv"
"strings"
"unicode"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// StructParser is used to parse Go source code into TableStructs.
type StructParser struct {
TableStructs TableStructs
parserDiagnostics *parserDiagnostics
dialect string
locations map[[2]string]location
columnExplicitType map[[3]string]struct{}
cache *CatalogCache
}
// NewStructParser creates a new StructParser. An existing token.Fileset can be
// passed in. If not, passing in nil is fine and a new token.FileSet will be
// instantiated.
func NewStructParser(fset *token.FileSet) *StructParser {
if fset == nil {
fset = token.NewFileSet()
}
return &StructParser{parserDiagnostics: &parserDiagnostics{
fset: fset,
}}
}
// VisitNode is a callback function that populates the TableStructs when passed
// to ast.Inspect().
func (p *StructParser) VisitNode(node ast.Node) bool {
switch node.(type) {
case *ast.File, *ast.GenDecl:
return true
}
p.VisitStruct(node)
return false
}
// VisitStruct is a callback function that populates the TableStructs when
// passed to inspect.Inspector.Preorder(). It expects the node to be of type
// *ast.TypeSpec.
func (p *StructParser) VisitStruct(node ast.Node) {
// Is it a type declaration?
typeSpec, ok := node.(*ast.TypeSpec)
if !ok {
return
}
// Is it a type declaration for a struct?
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
return
}
// Does the struct have fields?
if structType.Fields == nil {
return
}
tableStruct := TableStruct{
Name: typeSpec.Name.Name,
Fields: make([]StructField, 0, len(structType.Fields.List)),
}
for i, astField := range structType.Fields.List {
var structField StructField
// Name
if len(astField.Names) > 0 && astField.Names[0] != nil {
structField.Name = astField.Names[0].Name
}
// Type
if typ, ok := astField.Type.(*ast.SelectorExpr); ok {
if x, ok := typ.X.(*ast.Ident); ok {
structField.Type = x.Name + "." + typ.Sel.Name
}
} else if typ, ok := astField.Type.(*ast.Ident); ok {
structField.Type = typ.Name
}
// Tag
if astField.Tag != nil {
structField.tagPos = astField.Tag.Pos()
if tag, err := strconv.Unquote(astField.Tag.Value); err == nil {
structField.NameTag = reflect.StructTag(tag).Get("sq")
structField.Modifiers, err = NewModifiers(reflect.StructTag(tag).Get("ddl"))
if err != nil {
loc := location{
pos: structField.tagPos,
structName: tableStruct.Name,
fieldName: structField.Name,
}
p.report(loc, err.Error())
continue
}
}
}
// If the first field is not sq.TableStruct, skip this struct entirely.
if i == 0 {
if structField.Name != "" || structField.Type != "sq.TableStruct" {
structNameIsUppercase := true
for _, char := range tableStruct.Name {
if !unicode.IsUpper(char) {
structNameIsUppercase = false
break
}
}
if structNameIsUppercase && structField.Type != "sq.TableStruct" {
loc := location{pos: structField.tagPos}
p.report(loc, "struct "+tableStruct.Name+" is all uppercase but no sq.TableStruct field was found")
}
return
}
}
tableStruct.Fields = append(tableStruct.Fields, structField)
}
p.TableStructs = append(p.TableStructs, tableStruct)
}
// ParseFile parses an fs.File containing Go source code and populates the
// TableStructs.
func (p *StructParser) ParseFile(f fs.File) error {
fileinfo, err := f.Stat()
if err != nil {
return err
}
file, err := parser.ParseFile(p.parserDiagnostics.fset, fileinfo.Name(), f, 0)
if err != nil {
return err
}
ast.Inspect(file, p.VisitNode)
return p.Error()
}
// WriteCatalog populates the Catalog using the StructParser's TableStructs.
func (p *StructParser) WriteCatalog(catalog *Catalog) error {
p.dialect = catalog.Dialect
p.locations = make(map[[2]string]location)
p.columnExplicitType = make(map[[3]string]struct{})
p.cache = NewCatalogCache(catalog)
for _, tableStruct := range p.TableStructs {
if len(tableStruct.Fields) == 0 {
continue
}
var tableSchema string
tableName := strings.ToLower(tableStruct.Name)
if tableStruct.Fields[0].NameTag != "" {
tableName = tableStruct.Fields[0].NameTag
}
if i := strings.IndexByte(tableName, '.'); i >= 0 {
tableSchema, tableName = tableName[:i], tableName[i+1:]
}
if tableSchema == "" && catalog.CurrentSchema != "" {
tableSchema = catalog.CurrentSchema
}
schema := p.cache.GetOrCreateSchema(catalog, tableSchema)
table := p.cache.GetOrCreateTable(schema, tableName)
// The main loop.
for _, structField := range tableStruct.Fields {
loc := location{
pos: structField.tagPos,
structName: tableStruct.Name,
fieldName: structField.Name,
}
if (structField.Name == "" && structField.Type == "sq.TableStruct") || (structField.Name == "_" && structField.Type == "struct{}") {
p.parseTableModifiers(table, loc, structField.Modifiers)
continue
}
columnName := strings.ToLower(structField.Name)
if structField.NameTag != "" {
columnName = structField.NameTag
}
var columnType, characterLength string
switch structField.Type {
case "sq.AnyField":
case "sq.ArrayField":
switch p.dialect {
case DialectSQLite, DialectMySQL:
columnType = "JSON"
case DialectPostgres:
columnType = "TEXT[]"
case DialectSQLServer:
columnType, characterLength = "NVARCHAR(MAX)", "MAX"
default:
columnType, characterLength = "VARCHAR(255)", "255"
}
case "sq.BinaryField":
switch p.dialect {
case DialectSQLite:
columnType = "BLOB"
case DialectPostgres:
columnType = "BYTEA"
case DialectMySQL:
columnType = "MEDIUMBLOB"
case DialectSQLServer:
columnType, characterLength = "VARBINARY(MAX)", "MAX"
default:
columnType = "BINARY"
}
case "sq.BooleanField":
switch p.dialect {
case DialectSQLServer:
columnType = "BIT"
default:
columnType = "BOOLEAN"
}
case "sq.EnumField":
switch p.dialect {
case DialectSQLite, DialectPostgres:
columnType = "TEXT"
case DialectSQLServer:
columnType, characterLength = "NVARCHAR(255)", "255"
default:
columnType, characterLength = "VARCHAR(255)", "255"
}
case "sq.JSONField":
switch p.dialect {
case DialectSQLite, DialectMySQL:
columnType = "JSON"
case DialectPostgres:
columnType = "JSONB"
case DialectSQLServer:
columnType, characterLength = "NVARCHAR(MAX)", "MAX"
default:
columnType = "VARCHAR(255)"
}
case "sq.NumberField":
columnType = "INT"
case "sq.StringField":
switch p.dialect {
case DialectSQLite, DialectPostgres:
columnType = "TEXT"
case DialectSQLServer:
columnType, characterLength = "NVARCHAR(255)", "255"
default:
columnType, characterLength = "VARCHAR(255)", "255"
}
case "sq.TimeField":
switch p.dialect {
case DialectPostgres:
columnType = "TIMESTAMPTZ"
case DialectSQLServer:
columnType = "DATETIMEOFFSET"
default:
columnType = "DATETIME"
}
case "sq.UUIDField":
switch p.dialect {
case DialectSQLite, DialectPostgres:
columnType = "UUID"
default:
columnType = "BINARY(16)"
}
default:
continue
}
if characterLength != "" {
column := p.cache.GetOrCreateColumn(table, columnName, columnType)
column.CharacterLength = characterLength
}
p.parseColumnModifiers(table, columnName, columnType, loc, structField.Modifiers)
}
// Validate column existence for PRIMARY KEY and UNIQUE constraints.
for _, constraint := range table.Constraints {
if constraint.Ignore {
continue
}
hasInvalidColumn := false
for _, columnName := range constraint.Columns {
column := p.cache.GetColumn(table, columnName)
if column == nil {
hasInvalidColumn = true
loc := p.locations[[2]string{table.TableSchema, constraint.ConstraintName}]
p.report(loc, strings.Join(constraint.Columns, ",")+": "+columnName+" does not exist in the table")
}
}
if hasInvalidColumn {
continue
}
// set IsUnique and IsPrimaryKey for the corresponding columns
if len(constraint.Columns) != 1 || (constraint.ConstraintType != PRIMARY_KEY && constraint.ConstraintType != UNIQUE && constraint.ConstraintType != FOREIGN_KEY) {
continue
}
columnName := constraint.Columns[0]
column := p.cache.GetColumn(table, columnName)
switch constraint.ConstraintType {
case PRIMARY_KEY:
column.IsPrimaryKey = true
if catalog.Dialect == DialectSQLite && strings.EqualFold(column.ColumnType, "INT") {
if _, ok := p.columnExplicitType[[3]string{table.TableSchema, table.TableName, columnName}]; !ok {
column.ColumnType = "INTEGER"
}
}
case UNIQUE:
column.IsUnique = true
case FOREIGN_KEY:
column.ReferencesSchema = constraint.ReferencesSchema
column.ReferencesTable = constraint.ReferencesTable
column.ReferencesColumn = constraint.ReferencesColumns[0]
column.UpdateRule = constraint.UpdateRule
column.DeleteRule = constraint.DeleteRule
column.IsDeferrable = constraint.IsDeferrable
column.IsInitiallyDeferred = constraint.IsInitiallyDeferred
}
}
// Validate column existence for indexes.
for _, index := range table.Indexes {
if index.Ignore {
continue
}
for _, columnName := range index.Columns {
column := p.cache.GetColumn(table, columnName)
if column == nil {
loc := p.locations[[2]string{table.TableSchema, index.IndexName}]
p.report(loc, strings.Join(index.Columns, ",")+": "+columnName+" does not exist in the table")
}
}
}
// Set PRIMARY KEY columns to NOT NULL.
pkey := p.cache.GetPrimaryKey(table)
if pkey != nil && !pkey.Ignore {
for _, columnName := range pkey.Columns {
column := p.cache.GetColumn(table, columnName)
if column == nil {
continue
}
if catalog.Dialect == DialectSQLite && column.ColumnType == "INTEGER" && column.IsPrimaryKey {
// SQLite forbids INTEGER PRIMARY KEY (alias for ROWID) columns
// from being marked as NOT NULL (since they can never be NULL).
continue
}
column.IsNotNull = true
}
}
}
// Validate column existence for FOREIGN KEY constraints.
for _, schema := range catalog.Schemas {
for _, table := range schema.Tables {
for _, constraint := range table.Constraints {
if constraint.Ignore || constraint.ConstraintType != FOREIGN_KEY {
continue
}
// We need the location of the failing constraint so that we
// can inform the user where it is. If we can't find it,
// continue.
loc, ok := p.locations[[2]string{table.TableSchema, constraint.ConstraintName}]
if !ok {
continue
}
schemaName := constraint.ReferencesSchema
if schemaName == "" {
schemaName = catalog.CurrentSchema
}
refschema := p.cache.GetSchema(catalog, schemaName)
if refschema == nil {
p.report(loc, fmt.Sprintf("schema %s does not exist", schemaName))
continue
}
reftable := p.cache.GetTable(refschema, constraint.ReferencesTable)
tableName := constraint.ReferencesTable
if reftable == nil {
if schemaName != "" {
tableName = schemaName + "." + tableName
}
p.report(loc, fmt.Sprintf("table %s does not exist", tableName))
continue
}
for _, columnName := range constraint.ReferencesColumns {
refcolumn := p.cache.GetColumn(reftable, columnName)
if refcolumn == nil {
columnName = tableName + "." + columnName
p.report(loc, fmt.Sprintf("column %s does not exist", columnName))
continue
}
}
}
}
}
return p.Error()
}
func (p *StructParser) parseIndexModifier(table *Table, columnNames []string, loc location, m *Modifier) {
err := m.ParseRawValue()
if err != nil {
p.report(loc, err.Error())
return
}
if m.Value != "" && m.Value != "." {
columnNames = strings.Split(m.Value, ",")
}
if len(columnNames) == 0 {
p.report(loc, "no column provided")
}
indexName := GenerateName(INDEX, table.TableName, columnNames)
p.locations[[2]string{table.TableSchema, indexName}] = loc
index := p.cache.GetOrCreateIndex(table, indexName, columnNames)
index.TableSchema = table.TableSchema
index.TableName = table.TableName
index.Ignore = m.ExcludesDialect(p.dialect)
for i := range m.Submodifiers {
submodifier := &m.Submodifiers[i]
if submodifier.ExcludesDialect(p.dialect) {
continue
}
switch submodifier.Name {
case "unique":
index.IsUnique = true
case "using":
if p.dialect != DialectPostgres && p.dialect != DialectMySQL {
continue
}
index.IndexType = submodifier.RawValue
default:
p.report(loc, "unknown modifier "+strconv.Quote(submodifier.Name))
}
}
}
func (p *StructParser) parsePrimaryKeyUniqueModifier(table *Table, columnNames []string, loc location, m *Modifier) {
err := m.ParseRawValue()
if err != nil {
p.report(loc, err.Error())
return
}
constraintType := PRIMARY_KEY
if strings.EqualFold(UNIQUE, m.Name) {
constraintType = UNIQUE
}
if m.Value != "" && m.Value != "." {
columnNames = strings.Split(m.Value, ",")
}
if len(columnNames) == 0 {
p.report(loc, "no column provided")
}
constraintName := GenerateName(constraintType, table.TableName, columnNames)
if p.dialect == DialectMySQL && constraintType == PRIMARY_KEY {
constraintName = "PRIMARY"
}
p.locations[[2]string{table.TableSchema, constraintName}] = loc
constraint := p.cache.GetOrCreateConstraint(table, constraintName, constraintType, columnNames)
constraint.TableSchema = table.TableSchema
constraint.TableName = table.TableName
constraint.Ignore = m.ExcludesDialect(p.dialect)
for i := range m.Submodifiers {
submodifier := &m.Submodifiers[i]
if submodifier.ExcludesDialect(p.dialect) {
continue
}
switch submodifier.Name {
case "deferrable":
if p.dialect != DialectSQLite && p.dialect != DialectPostgres {
continue
}
constraint.IsDeferrable = true
case "deferred":
if p.dialect != DialectSQLite && p.dialect != DialectPostgres {
continue
}
constraint.IsDeferrable = true
constraint.IsInitiallyDeferred = true
default:
p.report(loc, "unknown modifier "+strconv.Quote(submodifier.Name))
}
}
}
func (p *StructParser) parseForeignKeyModifier(table *Table, loc location, m *Modifier) {
err := m.ParseRawValue()
if err != nil {
p.report(loc, err.Error())
return
}
if m.Value == "" || m.Value == "." {
p.report(loc, "no column(s) provided")
return
}
if len(m.Submodifiers) == 0 {
p.report(loc, "no referenced column(s) provided")
return
}
columnNames := strings.Split(m.Value, ",")
constraintName := GenerateName(FOREIGN_KEY, table.TableName, columnNames)
p.locations[[2]string{table.TableSchema, constraintName}] = loc
constraint := p.cache.GetOrCreateConstraint(table, constraintName, FOREIGN_KEY, columnNames)
constraint.TableSchema = table.TableSchema
constraint.TableName = table.TableName
constraint.Ignore = m.ExcludesDialect(p.dialect)
for i := range m.Submodifiers {
submodifier := &m.Submodifiers[i]
if submodifier.ExcludesDialect(p.dialect) {
continue
}
switch submodifier.Name {
case "references":
switch parts := strings.SplitN(submodifier.RawValue, ".", 3); len(parts) {
case 1:
constraint.ReferencesTable = parts[0]
constraint.ReferencesColumns = columnNames
case 2:
constraint.ReferencesTable = parts[0]
constraint.ReferencesColumns = strings.Split(parts[1], ",")
case 3:
constraint.ReferencesSchema = parts[0]
constraint.ReferencesTable = parts[1]
constraint.ReferencesColumns = strings.Split(parts[2], ",")
}
case "onupdate":
switch submodifier.RawValue {
case "cascade":
constraint.UpdateRule = CASCADE
case "restrict":
if p.dialect == DialectSQLServer {
constraint.UpdateRule = NO_ACTION
} else {
constraint.UpdateRule = RESTRICT
}
case "noaction":
constraint.UpdateRule = NO_ACTION
case "setnull":
constraint.UpdateRule = SET_NULL
case "setdefault":
constraint.UpdateRule = SET_DEFAULT
case "":
constraint.UpdateRule = ""
default:
loc.keys = append(loc.keys, submodifier.Name)
p.report(loc, "unknown value "+strconv.Quote(submodifier.RawValue))
}
case "ondelete":
switch submodifier.RawValue {
case "cascade":
constraint.DeleteRule = CASCADE
case "restrict":
if p.dialect == DialectSQLServer {
constraint.DeleteRule = NO_ACTION
} else {
constraint.DeleteRule = RESTRICT
}
case "noaction":
constraint.DeleteRule = NO_ACTION
case "setnull":
constraint.DeleteRule = SET_NULL
case "setdefault":
constraint.DeleteRule = SET_DEFAULT
case "":
constraint.DeleteRule = ""
default:
loc.keys = append(loc.keys, submodifier.Name)
p.report(loc, "unknown value "+strconv.Quote(submodifier.RawValue))
}
case "deferrable":
if p.dialect != DialectSQLite && p.dialect != DialectPostgres {
continue
}
constraint.IsDeferrable = true
case "deferred":
if p.dialect != DialectSQLite && p.dialect != DialectPostgres {
continue
}
constraint.IsDeferrable = true
constraint.IsInitiallyDeferred = true
case "index":
loc.keys = append(loc.keys, submodifier.Name)
p.parseIndexModifier(table, columnNames, loc, submodifier)
default:
p.report(loc, "unknown modifier "+strconv.Quote(submodifier.Name))
}
}
}
func (p *StructParser) parseReferencesModifier(table *Table, columnName string, loc location, m *Modifier) {
err := m.ParseRawValue()
if err != nil {
p.report(loc, err.Error())
return
}
if columnName == "" {
p.report(loc, "no column provided")
}
constraintName := GenerateName(FOREIGN_KEY, table.TableName, []string{columnName})
p.locations[[2]string{table.TableSchema, constraintName}] = loc
constraint := p.cache.GetOrCreateConstraint(table, constraintName, FOREIGN_KEY, []string{columnName})
constraint.TableSchema = table.TableSchema
constraint.TableName = table.TableName
constraint.Ignore = m.ExcludesDialect(p.dialect)
switch parts := strings.SplitN(m.Value, ".", 3); len(parts) {
case 1:
constraint.ReferencesTable = parts[0]
constraint.ReferencesColumns = []string{columnName}
case 2:
constraint.ReferencesTable = parts[0]
constraint.ReferencesColumns = strings.Split(parts[1], ",")
case 3:
constraint.ReferencesSchema = parts[0]
constraint.ReferencesTable = parts[1]
constraint.ReferencesColumns = strings.Split(parts[2], ",")
}
for i, submodifier := range m.Submodifiers {
if submodifier.ExcludesDialect(p.dialect) {
continue
}
switch submodifier.Name {
case "onupdate":
switch submodifier.RawValue {
case "cascade":
constraint.UpdateRule = CASCADE
case "restrict":
if p.dialect == DialectSQLServer {
constraint.UpdateRule = NO_ACTION
} else {
constraint.UpdateRule = RESTRICT
}
case "noaction":
constraint.UpdateRule = NO_ACTION
case "setnull":
constraint.UpdateRule = SET_NULL
case "setdefault":
constraint.UpdateRule = SET_DEFAULT
case "":
constraint.UpdateRule = ""
default:
loc.keys = append(loc.keys, submodifier.Name)
p.report(loc, "unknown value "+strconv.Quote(submodifier.RawValue))
}
case "ondelete":
switch submodifier.RawValue {
case "cascade":
constraint.DeleteRule = CASCADE
case "restrict":
if p.dialect == DialectSQLServer {
constraint.DeleteRule = NO_ACTION
} else {
constraint.DeleteRule = RESTRICT
}
case "noaction":
constraint.DeleteRule = NO_ACTION
case "setnull":
constraint.DeleteRule = SET_NULL
case "setdefault":
constraint.DeleteRule = SET_DEFAULT
case "":
constraint.DeleteRule = ""
default:
loc.keys = append(loc.keys, submodifier.Name)
p.report(loc, "unknown value "+strconv.Quote(submodifier.RawValue))
}
case "deferrable":
if p.dialect == DialectSQLite || p.dialect == DialectPostgres {
constraint.IsDeferrable = true
}
case "deferred":
if p.dialect == DialectSQLite || p.dialect == DialectPostgres {
constraint.IsDeferrable = true
constraint.IsInitiallyDeferred = true
}
case "index":
loc.keys = append(loc.keys, submodifier.Name)
p.parseIndexModifier(table, []string{columnName}, loc, &m.Submodifiers[i])
default:
p.report(loc, "unknown modifier "+strconv.Quote(submodifier.Name))
}
}
}
func (p *StructParser) parseColumnModifiers(table *Table, columnName, columnType string, loc location, modifiers []Modifier) {
column := p.cache.GetOrCreateColumn(table, columnName, columnType)
column.TableSchema = table.TableSchema
column.TableName = table.TableName
column.IsEnum = columnType == "sq.EnumField"
var dialects []string
for i := range modifiers {
modifier := &modifiers[i]
if len(modifier.Dialects) == 0 {
modifier.Dialects = dialects
}
if modifier.ExcludesDialect(p.dialect) {
continue
}
switch modifier.Name {
case "type":
p.columnExplicitType[[3]string{table.TableSchema, table.TableName, columnName}] = struct{}{}
column.ColumnType = modifier.RawValue
normalizedType, arg1, arg2 := normalizeColumnType(p.dialect, column.ColumnType)
switch normalizedType {
case "VARBINARY", "BINARY", "NVARCHAR", "VARCHAR", "CHAR":
if arg1 != "" {
column.CharacterLength = arg1
} else if column.CharacterLength != "" {
column.ColumnType = column.ColumnType + "(" + column.CharacterLength + ")"
}
column.NumericPrecision, column.NumericScale = "", ""
case "NUMERIC":
if arg1 != "" {
column.NumericPrecision = arg1
}
if arg2 != "" {
column.NumericScale = arg2
}
column.CharacterLength = ""
default:
column.CharacterLength, column.NumericPrecision, column.NumericScale = "", "", ""
}
case "len":
column.CharacterLength = modifier.RawValue
if column.CharacterLength != "" {
switch p.dialect {
case DialectPostgres, DialectMySQL:
column.ColumnType = "VARCHAR(" + column.CharacterLength + ")"
case DialectSQLServer:
column.ColumnType = "NVARCHAR(" + column.CharacterLength + ")"
}
}
case "auto_increment":
if p.dialect != DialectMySQL {
continue
}
column.IsAutoincrement = true
case "autoincrement":
if p.dialect != DialectSQLite {
continue
}
column.ColumnType = "INTEGER"
column.IsAutoincrement = true
case "identity":
switch p.dialect {
case DialectPostgres:
column.ColumnIdentity = DEFAULT_IDENTITY
case DialectSQLServer:
column.ColumnIdentity = IDENTITY
}
case "alwaysidentity":
switch p.dialect {
case DialectPostgres:
column.ColumnIdentity = ALWAYS_IDENTITY
case DialectSQLServer:
column.ColumnIdentity = IDENTITY
}
case "notnull":
column.IsNotNull = true
case "onupdatecurrenttimestamp":
if p.dialect != DialectMySQL {
continue
}
column.OnUpdateCurrentTimestamp = true
case "collate":
column.CollationName = modifier.RawValue
case "default":
if p.dialect != DialectPostgres && !isLiteral(modifier.RawValue) {
column.ColumnDefault = wrapBrackets(modifier.RawValue)
continue
}
column.ColumnDefault = modifier.RawValue
if p.dialect == DialectSQLServer {
if strings.EqualFold(column.ColumnDefault, "TRUE") {
column.ColumnDefault = "1"
} else if strings.EqualFold(column.ColumnDefault, "FALSE") {
column.ColumnDefault = "0"
}
}
case "generated":
column.IsGenerated = true
case "dialect":
if modifier.RawValue == "" {
loc.keys = []string{modifier.Name}
p.report(loc, "dialect value cannot be blank")
continue
}
column.Ignore = true
dialects = strings.Split(modifier.RawValue, ",")
for _, dialect := range dialects {
if p.dialect == dialect {
column.Ignore = false
break
}
}
case "index":
loc.keys = []string{modifier.Name}
p.parseIndexModifier(table, []string{columnName}, loc, modifier)
case "primarykey", "unique":
loc.keys = []string{modifier.Name}
p.parsePrimaryKeyUniqueModifier(table, []string{columnName}, loc, modifier)
case "foreignkey":
loc.keys = []string{modifier.Name}
p.parseForeignKeyModifier(table, loc, modifier)
case "references":
loc.keys = []string{modifier.Name}
p.parseReferencesModifier(table, columnName, loc, modifier)
default:
p.report(loc, "unknown modifier "+strconv.Quote(modifier.Name))
}
}
}
func (p *StructParser) parseTableModifiers(table *Table, loc location, modifiers []Modifier) {
var dialects []string
for i := range modifiers {
modifier := &modifiers[i]
if len(modifier.Dialects) == 0 {
modifier.Dialects = dialects
}
switch modifier.Name {
case "dialect":
if modifier.RawValue == "" {
loc.keys = []string{modifier.Name}
p.report(loc, "dialect value cannot be blank")
continue
}
table.Ignore = true
dialects = strings.Split(modifier.RawValue, ",")
for _, dialect := range dialects {
if p.dialect == dialect {
table.Ignore = false
break
}
}
case "index":
loc.keys = []string{modifier.Name}
p.parseIndexModifier(table, nil, loc, modifier)
case "primarykey", "unique":
loc.keys = []string{modifier.Name}
p.parsePrimaryKeyUniqueModifier(table, nil, loc, modifier)
case "foreignkey":
loc.keys = []string{modifier.Name}
p.parseForeignKeyModifier(table, loc, modifier)
case "virtual":
if p.dialect != DialectSQLite {
continue
}
table.IsVirtual = true
default:
p.report(loc, "unknown modifier "+strconv.Quote(modifier.Name))
}
}
}
func (p *StructParser) report(loc location, msg string) {
p.parserDiagnostics.locs = append(p.parserDiagnostics.locs, loc)
p.parserDiagnostics.msgs = append(p.parserDiagnostics.msgs, msg)
}
type location struct {
pos token.Pos
structName string
fieldName string
keys []string
}
type parserDiagnostics struct {
fset *token.FileSet
locs []location
msgs []string
}
// Diagnostics returns the errors encountered after calling WriteCatalog in a
// structured format.
func (p *StructParser) Diagnostics() ([]token.Pos, []string) {
positions := make([]token.Pos, 0, len(p.parserDiagnostics.msgs))
msgs := make([]string, 0, len(p.parserDiagnostics.msgs))
for i, msg := range p.parserDiagnostics.msgs {
var loc location
if i < len(p.parserDiagnostics.locs) {
loc = p.parserDiagnostics.locs[i]
}
n := len(loc.structName) + len(".") + len(loc.fieldName) + len(": ")
for _, key := range loc.keys {
n += len(".") + len(key)
}
n += len(": ") + len(msg)
var b strings.Builder
b.Grow(n)
if loc.structName != "" && loc.fieldName != "" {
b.WriteString(loc.structName + "." + loc.fieldName + ": ")
}
for j, key := range loc.keys {
if j > 0 {
b.WriteString(".")
}
b.WriteString(key)
}
b.WriteString(": " + msg)
positions = append(positions, loc.pos)
msgs = append(msgs, b.String())
}
return positions, msgs
}
// Error returns the errors encountered after calling WriteCatalog.
func (p *StructParser) Error() error {
if len(p.parserDiagnostics.msgs) > 0 {
return p.parserDiagnostics
}
return nil
}
// Error implements the error interface.
func (d *parserDiagnostics) Error() string {
buf := bufpool.Get().(*bytes.Buffer)
buf.Reset()
defer bufpool.Put(buf)
for i, msg := range d.msgs {
if i > 0 {
buf.WriteString("\n")
}
if i >= len(d.locs) {
buf.WriteString(msg)
continue
}
loc := d.locs[i]
if d.fset != nil {
pos := d.fset.Position(loc.pos)
if pos.IsValid() {
buf.WriteString(pos.String() + ": ")
}
}
if loc.structName != "" && loc.fieldName != "" {
buf.WriteString(loc.structName + "." + loc.fieldName + ": ")
}
if len(loc.keys) > 0 {
for j, key := range loc.keys {
if j > 0 {
buf.WriteByte('.')
}
buf.WriteString(key)
}
buf.WriteString(": ")
}
buf.WriteString(msg)
}
return buf.String()
}
// Analyzer is an &analysis.Analyzer which can be used in a custom linter.
var Analyzer = &analysis.Analyzer{
Name: "ddl",
Doc: "validates ddl structs",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: func(pass *analysis.Pass) (any, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{(*ast.TypeSpec)(nil)}
p := NewStructParser(pass.Fset)
inspect.Preorder(nodeFilter, p.VisitStruct)
var catalog Catalog
_ = p.WriteCatalog(&catalog)
positions, msgs := p.Diagnostics()
if len(msgs) == 0 {
return nil, nil
}
for i, msg := range msgs {
var pos token.Pos
if i < len(positions) {
pos = positions[i]
}
pass.Reportf(pos, msg)
}
return nil, nil
},
}