-
Notifications
You must be signed in to change notification settings - Fork 4
/
ddl.go
1114 lines (942 loc) · 33.5 KB
/
ddl.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 ddl
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
// The various keyword constants used by ddl.
const (
PRIMARY_KEY = "PRIMARY KEY"
FOREIGN_KEY = "FOREIGN KEY"
UNIQUE = "UNIQUE"
CHECK = "CHECK"
EXCLUDE = "EXCLUDE"
INDEX = "INDEX"
DEFAULT_IDENTITY = "GENERATED BY DEFAULT AS IDENTITY"
ALWAYS_IDENTITY = "GENERATED ALWAYS AS IDENTITY"
IDENTITY = "IDENTITY"
RESTRICT = "RESTRICT"
CASCADE = "CASCADE"
NO_ACTION = "NO ACTION"
SET_NULL = "SET NULL"
SET_DEFAULT = "SET DEFAULT"
)
// The dialects supported by ddl.
const (
DialectSQLite = "sqlite"
DialectPostgres = "postgres"
DialectMySQL = "mysql"
DialectSQLServer = "sqlserver"
DialectOracle = "oracle"
)
var bufpool = sync.Pool{
New: func() any { return &bytes.Buffer{} },
}
// Catalog represents a database catalog i.e. a database instance.
type Catalog struct {
// Dialect is the dialect of the database. Possible values: "sqlite",
// "postgres", "mysql", "sqlserver".
Dialect string `json:",omitempty"`
// VersionNums holds the database's version numbers.
//
// Example: Postgres 14.2 would be represented as []int{14, 2}.
VersionNums VersionNums `json:",omitempty"`
// Database name.
CatalogName string `json:",omitempty"`
// CurrentSchema is the current schema of the database. For Postgres it
// is usually "public", for MySQL this is the database name, for SQL Server
// it is usually "dbo". It is always empty for SQLite.
CurrentSchema string `json:",omitempty"`
// DefaultCollation is the default collation of the database.
DefaultCollation string `json:",omitempty"`
// If DefaultCollationValid is false, the database's default collation is
// unknown.
DefaultCollationValid bool `json:",omitempty"`
// The extensions in the database. Postgres only.
Extensions []string `json:",omitempty"`
// If ExtensionsValid is false, the database's extensions are unknown.
ExtensionsValid bool `json:",omitempty"`
// The list of schemas within the database.
Schemas []Schema `json:",omitempty"`
}
// Schema represents a database schema.
type Schema struct {
// SchemaName is the name of the schema.
SchemaName string `json:",omitempty"`
// Tables is the list of tables within the schema.
Tables []Table `json:",omitempty"`
// Views is the list of views within the schema.
Views []View `json:",omitempty"`
// If ViewsValid is false, the schema's views are unknown.
ViewsValid bool `json:",omitempty"`
// Routines is the list of routines (stored procedures and functions)
// within the schema.
Routines []Routine `json:",omitempty"`
// If RoutinesValid is false, the schema's routines are unknown.
RoutinesValid bool `json:",omitempty"`
// The list of enum types within the schema. Postgres only.
Enums []Enum `json:",omitempty"`
// If EnumsValid is false, the schema's enum types are unknown.
EnumsValid bool `json:",omitempty"`
// The list of domain types within the schema. Postgres only.
Domains []Domain `json:",omitempty"`
// If DomainsValid is false, the schema's domain types are unknown.
DomainsValid bool `json:",omitempty"`
// Comment stores the comment on the schema object.
Comment string `json:",omitempty"`
// If Ignore is true, the schema should be treated like it doesn't exist (a
// soft delete flag).
Ignore bool `json:",omitempty"`
}
// Enum represents a database enum type. Postgres only.
type Enum struct {
// EnumSchema is the name of schema that the enum type belongs to.
EnumSchema string `json:",omitempty"`
// EnumName is the name of the enum type.
EnumName string `json:",omitempty"`
// EnumLabels contains the list of labels associated with the enum type.
EnumLabels []string `json:",omitempty"`
// Comment stores the comment on the enum type.
Comment string `json:",omitempty"`
// If Ignore is true, the enum type should be treated like it doesn't
// exist (a soft delete flag).
Ignore bool `json:",omitempty"`
}
// Domain represents a database domain type. Postgres only.
type Domain struct {
// DomainSchema is the name of schema that the domain type belongs to.
DomainSchema string `json:",omitempty"`
// DomainName is the name of the domain type.
DomainName string `json:",omitempty"`
// UnderlyingType is the underlying type of the domain.
UnderlyingType string `json:",omitempty"`
// CollationName is the collation of the domain type.
CollationName string `json:",omitempty"`
// IsNotNull indicates if the domain type is NOT NULL.
IsNotNull bool `json:",omitempty"`
// ColumnDefault is the default value of the domain type.
ColumnDefault string `json:",omitempty"`
// CheckNames is the list of check constraint names on the domain type.
CheckNames []string `json:",omitempty"`
// CheckExprs is the list of check constraints expressions on the domain
// type.
CheckExprs []string `json:",omitempty"`
// Comment stores the comment on the domain type.
Comment string `json:",omitempty"`
// If Ignore is true, the domain type should be treated like it doesn't
// exist (a soft delete flag).
Ignore bool `json:",omitempty"`
}
// Routine represents a database routine (either a stored procedure or a
// function).
type Routine struct {
// RoutineSchema is the name of schema that the routine belongs to.
RoutineSchema string `json:",omitempty"`
// RoutineName is the name of the routine.
RoutineName string `json:",omitempty"`
// IdentityArguments is a string containing the identity arguments that
// uniquely identify routines sharing the same name. Postgres only.
IdentityArguments string `json:",omitempty"`
// RoutineType identifies the type of the routine. Possible values:
// "PROCEDURE", "FUNCTION".
RoutineType string `json:",omitempty"`
// SQL is the SQL definition of the routine.
SQL string `json:",omitempty"`
// Attrs stores additional metadata about the routine.
Attrs map[string]string `json:",omitempty"`
// Comment stores the comment on the routine.
Comment string `json:",omitempty"`
// If Ignore is true, the routine should be treated like it doesn't exist
// (a soft delete flag).
Ignore bool `json:",omitempty"`
}
// View represents a database view.
type View struct {
// ViewSchema is the name of schema that the view belongs to.
ViewSchema string `json:",omitempty"`
// ViewName is the name of the view.
ViewName string `json:",omitempty"`
// IsMaterialized indicates if the view is a materialized view.
IsMaterialized bool `json:",omitempty"`
// SQL is the SQL definition of the view.
SQL string `json:",omitempty"`
// Columns is the list of columns in the view.
Columns []string `json:",omitempty"`
// ColumnTypes is the list of column types in the view.
ColumnTypes []string `json:",omitempty"`
// EnumColumns is the list of columns in the view whose column type is an
// enum.
EnumColumns []string `json:",omitempty"`
// Indexes is the list of indexes belonging to the view.
Indexes []Index `json:",omitempty"`
// Triggers is the list of triggers belonging to the view.
Triggers []Trigger `json:",omitempty"`
// Comment stores the comment on the view.
Comment string `json:",omitempty"`
// If Ignore is true, the view should be treated like it doesn't exist (a
// soft delete flag).
Ignore bool `json:",omitempty"`
}
// Table represents a database table.
type Table struct {
// TableSchema is the name of schema that the table belongs to.
TableSchema string `json:",omitempty"`
// TableName is the name of the table.
TableName string `json:",omitempty"`
// SQL is the SQL definition of the table.
SQL string `json:",omitempty"`
// IsVirtual indicates if the table is a virtual table. SQLite only.
IsVirtual bool `json:",omitempty"`
// Columns is the list of columns within the table.
Columns []Column `json:",omitempty"`
// Constraints is the list of constraints within the table.
Constraints []Constraint `json:",omitempty"`
// Indexes is the list of indexes within the table.
Indexes []Index `json:",omitempty"`
// Triggers is the list of triggers within the table.
Triggers []Trigger `json:",omitempty"`
// Comment stores the comment on the table.
Comment string `json:",omitempty"`
// If Ignore is true, the table should be treated like it doesn't exist (a
// soft delete flag).
Ignore bool `json:",omitempty"`
}
// Column represents a database column.
type Column struct {
// TableSchema is the name of the schema that the table and column belong to.
TableSchema string `json:",omitempty"`
// TableName is the name of the table that the column belongs to.
TableName string `json:",omitempty"`
// ColumnName is the name of the column.
ColumnName string `json:",omitempty"`
// ColumnType is the type of the column.
ColumnType string `json:",omitempty"`
// CharacterLength stores the character length of the column (as a string)
// if applicable.
CharacterLength string `json:",omitempty"`
// NumericPrecision stores the numeric precision of the column (as a
// string) if applicable.
NumericPrecision string `json:",omitempty"`
// NumericScale stores the numeric scale of the column (as a string) if
// applicable.
NumericScale string `json:",omitempty"`
// DomainName stores the name of the domain if the column is a domain type.
// In which case the ColumnType of the column is the underlying type of the
// domain. Postgres only.
DomainName string `json:",omitempty"`
// IsEnum indicates if the column is an enum type. If true, the ColumnType
// of the column is the name of the enum. Postgres only.
IsEnum bool `json:",omitempty"`
// IsNotNull indicates if the column is NOT NULL.
IsNotNull bool `json:",omitempty"`
// IsPrimaryKey indicates if the column is the primary key. It is true only
// if the column is the only column participating in the primary key
// constraint.
IsPrimaryKey bool `json:",omitempty"`
// IsUnique indicates if the column is unique. It is true only if the
// column is the only column participating in the unique constraint.
IsUnique bool `json:",omitempty"`
// IsAutoincrement indicates if the column is AUTO_INCREMENT (MySQL) or
// AUTOINCREMENT (SQLite).
IsAutoincrement bool `json:",omitempty"`
// ReferencesSchema stores the name of the referenced schema if the column
// is a foreign key. It is filled in only if the column is the only column
// participating in the foreign key constraint.
ReferencesSchema string `json:",omitempty"`
// ReferencesTable stores the name of the referenced table if the column is
// a foreign key. It is filled in only if the column is the only column
// participating in the foreign key constraint.
ReferencesTable string `json:",omitempty"`
// ReferencesTable stores the name of the referenced column if the column
// is a foreign key. It is filled in only if the column is the only column
// participating in the foreign key constraint.
ReferencesColumn string `json:",omitempty"`
// UpdateRule stores the ON UPDATE rule of the column's foreign key (if
// applicable). Possible values: "RESTRICT", "CASCADE", "NO ACTION", "SET
// NULL", "SET DEFAULT".
UpdateRule string `json:",omitempty"`
// DeleteRule stores the ON DELETE of the column's foreign key (if
// applicable). Possible values: "RESTRICT", "CASCADE", "NO ACTION", "SET
// NULL", "SET DEFAULT".
DeleteRule string `json:",omitempty"`
// IsDeferrable indicates if the column's foreign key is deferrable (if
// applicable). Postgres only.
IsDeferrable bool `json:",omitempty"`
// IsInitiallyDeferred indicates if the column's foreign key is initially
// deferred (if applicable). Postgres only.
IsInitiallyDeferred bool `json:",omitempty"`
// ColumnIdentity stores the identity definition of the column. Possible
// values: "GENERATED BY DEFAULT AS IDENTITY" (Postgres), "GENERATED ALWAYS
// AS IDENTITY" (Postgres), "IDENTITY" (SQLServer).
ColumnIdentity string `json:",omitempty"`
// ColumnDefault stores the default value of the column as it is literally
// represented in SQL. So if the default value is a string, the value
// should be surrounded by 'single quotes'. If the default value is a
// function call, the default value should be surrounded by brackets e.g.
// (uuid()).
ColumnDefault string `json:",omitempty"`
// OnUpdateCurrentTimestamp indicates if the column is updated with the
// CURRENT_TIMESTAMP whenever the row is updated. MySQL only.
OnUpdateCurrentTimestamp bool `json:",omitempty"`
// IsGenerated indicates if the column is a generated column. It does not
// have be set to true if the GeneratedExpr field is already non-empty.
IsGenerated bool `json:",omitempty"`
// GeneratedExpr holds the generated expression of the column if the column
// is generated.
GeneratedExpr string `json:",omitempty"`
// GeneratedExprStored indicates if the generated column is STORED. If
// false, the generated column is assumed to be VIRTUAL.
GeneratedExprStored bool `json:",omitempty"`
// CollationName stores the collation of the column. If empty, the column
// collation is assumed to follow the DefaultCollation of the Catalog.
CollationName string `json:",omitempty"`
// Comment stores the comment on the column.
Comment string `json:",omitempty"`
// If Ignore is true, the column should be treated like it doesn't exist (a
// soft delete flag).
Ignore bool `json:",omitempty"`
}
// Constraint represents a database constraint.
type Constraint struct {
// TableSchema is the name of the schema that the table and constraint belong to.
TableSchema string `json:",omitempty"`
// TableName is the name of the table that the constraint belongs to.
TableName string `json:",omitempty"`
// ConstraintName is the name of the constraint.
ConstraintName string `json:",omitempty"`
// ConstraintType is the type of the constraint. Possible values: "PRIMARY
// KEY", "UNIQUE", "FOREIGN KEY", "CHECK", "EXCLUDE".
ConstraintType string `json:",omitempty"`
// Columns holds the name of the columns participating in the constraint.
Columns []string `json:",omitempty"`
// ReferencesSchema stores the name of the referenced schema if the constraint
// is a foreign key.
ReferencesSchema string `json:",omitempty"`
// ReferencesSchema stores the name of the referenced table if the constraint
// is a foreign key.
ReferencesTable string `json:",omitempty"`
// ReferencesSchema stores the name of the referenced columns if the constraint
// is a foreign key.
ReferencesColumns []string `json:",omitempty"`
// UpdateRule stores the ON UPDATE rule if the constraint is a foreign key.
// Possible values: "RESTRICT", "CASCADE", "NO ACTION", "SET NULL", "SET
// DEFAULT".
UpdateRule string `json:",omitempty"`
// DeleteRule stores the ON DELETE rule if the constraint is a foreign key.
// Possible values: "RESTRICT", "CASCADE", "NO ACTION", "SET NULL", "SET
// DEFAULT".
DeleteRule string `json:",omitempty"`
//MatchOption stores the MATCH option if the constraint is a foreign key.
MatchOption string `json:",omitempty"`
// CheckExpr stores the CHECK expression if the constraint is a CHECK constraint.
CheckExpr string `json:",omitempty"`
// ExclusionOperators stores the list of exclusion operators if the
// constraint is an EXCLUDE constraint. Postgres only.
ExclusionOperators []string `json:",omitempty"`
// ExclusionIndexType stores the exclusion index type if the constraint is
// an EXCLUDE constraint. Postgres only.
ExclusionIndexType string `json:",omitempty"`
// ExclusionPredicate stores the exclusion predicate if the constraint is
// an EXCLUDE constraint. Postgres only.
ExclusionPredicate string `json:",omitempty"`
// IsDeferrable indicates if the constraint is deferrable. Postgres only.
IsDeferrable bool `json:",omitempty"`
// IsDeferrable indicates if the constraint is initially deferred. Postgres
// only.
IsInitiallyDeferred bool `json:",omitempty"`
// IsClustered indicates if the constraint is the clustered index of the
// table. SQLServer only.
IsClustered bool `json:",omitempty"`
// IsNotValid indicates if the constraint exists but is not valid e.g. if
// it was constructed with the NOT VALID (Postgres) or WITH NOCHECK
// (SQLServer).
IsNotValid bool `json:",omitempty"`
// Comment stores the comment on the constraint.
Comment string `json:",omitempty"`
// If Ignore is true, the constraint should be treated like it doesn't
// exist (a soft delete flag).
Ignore bool `json:",omitempty"`
}
// Index represents a database index.
type Index struct {
// TableSchema is the name of the schema that the table and index belong to.
TableSchema string `json:",omitempty"`
// TableName is the name of the table (or view) that the index belongs to.
TableName string `json:",omitempty"`
// IndexName is the name of the index.
IndexName string `json:",omitempty"`
// IndexType is the type of the index.
IndexType string `json:",omitempty"`
// IsViewIndex indicates if the index is for a view.
IsViewIndex bool `json:",omitempty"`
// IsUnique indicates if the index is a unique index.
IsUnique bool `json:",omitempty"`
// Columns holds the names of the columns participating in the index.
Columns []string `json:",omitempty"`
// IncludeColumns holds the names of the columns that are included by the
// index (the INCLUDE clause).
IncludeColumns []string `json:",omitempty"`
// Descending indicates if each column of the index is descending.
Descending []bool `json:",omitempty"`
// Opclasses holds the opclass of each column of the index. Postgres only.
Opclasses []string `json:",omitempty"`
// Predicate stores the index predicate i.e. the index is a partial index.
Predicate string `json:",omitempty"`
// SQL is the SQL definition of the index.
SQL string `json:",omitempty"`
// Comment stores the comment on the index.
Comment string `json:",omitempty"`
// If Ignore is true, the index should be treated like it doesn't exist (a
// soft delete flag).
Ignore bool `json:",omitempty"`
}
// Trigger represents a database trigger.
type Trigger struct {
// TableSchema is the name of the schema that the table and trigger belong to.
TableSchema string `json:",omitempty"`
// TableName is the name of the table (or view) that the trigger belongs to.
TableName string `json:",omitempty"`
// TriggerName is the name of the trigger.
TriggerName string `json:",omitempty"`
// IsViewTrigger indicates if the trigger belongs to a view.
IsViewTrigger bool `json:",omitempty"`
// SQL is the SQL definition of the trigger.
SQL string `json:",omitempty"`
// Attrs stores additional metadata about the trigger.
Attrs map[string]string `json:",omitempty"`
// Comment stores the comment on the trigger.
Comment string `json:",omitempty"`
// If Ignore is true, the trigger should be treated like it doesn't exist
// (a soft delete flag).
Ignore bool `json:",omitempty"`
}
// GenerateName generates the appropriate constraint/index name for a given
// table and columns. The nameType should be one of "PRIMARY KEY", "FOREIGN
// KEY", "UNIQUE" or "INDEX".
func GenerateName(nameType string, tableName string, columnNames []string) string {
var b strings.Builder
n := len(tableName) + len(nameType)
for _, columnName := range columnNames {
n += len(columnName) + 1
}
b.Grow(n)
for _, char := range tableName {
if char == ' ' {
char = '_'
}
b.WriteRune(char)
}
for _, columnName := range columnNames {
b.WriteString("_")
for _, char := range columnName {
if char == ' ' {
char = '_'
}
b.WriteRune(char)
}
}
var suffix string
switch nameType {
case PRIMARY_KEY:
suffix = "_pkey"
case FOREIGN_KEY:
suffix = "_fkey"
case UNIQUE:
suffix = "_key"
case INDEX:
suffix = "_idx"
case CHECK:
suffix = "_check"
case EXCLUDE:
suffix = "_excl"
}
// Cap length to 63 chars (Postgres' limitation).
excessLength := b.Len() + len(suffix) - 63
if excessLength > 0 {
trimmedPrefix := b.String()
trimmedPrefix = trimmedPrefix[:len(trimmedPrefix)-excessLength]
return trimmedPrefix + suffix
}
b.WriteString(suffix)
return b.String()
}
func isLiteral(s string) bool {
// is string literal?
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
return true
}
// is known literal?
if strings.EqualFold(s, "TRUE") ||
strings.EqualFold(s, "FALSE") ||
strings.EqualFold(s, "CURRENT_DATE") ||
strings.EqualFold(s, "CURRENT_TIME") ||
strings.EqualFold(s, "CURRENT_TIMESTAMP") ||
strings.EqualFold(s, "NULL") {
return true
}
// is int literal?
if _, err := strconv.ParseInt(s, 10, 64); err == nil {
return true
}
// is float literal?
if _, err := strconv.ParseFloat(s, 64); err == nil {
return true
}
return false
}
func wrapBrackets(s string) string {
if s == "" {
return ""
}
if s[0] == '(' && s[len(s)-1] == ')' {
return s
}
return "(" + s + ")"
}
func unwrapBrackets(s string) string {
if s == "" {
return ""
}
last := len(s) - 1
if s[0] == '(' && s[last] == ')' {
return s[1:last]
}
return s
}
func wrappedInBrackets(s string) bool {
return s != "" && s[0] == '(' && s[len(s)-1] == ')'
}
// splitArgs works like strings.Split with commas except it ignores commas inside
// 'strings', (brackets) and [square brackets].
func splitArgs(s string) []string {
if s == "" {
return nil
}
var args []string
var splitAt, skipCharAt, arrayLevel, bracketLevel int
var insideString bool
for {
splitAt, skipCharAt, arrayLevel, bracketLevel = -1, -1, 0, 0
insideString = false
for i, char := range s {
// do we unconditionally skip the current char?
if skipCharAt == i {
continue
}
// are we currently inside an array literal?
if arrayLevel > 0 {
switch char {
// does the current char close an array literal?
case ']':
arrayLevel--
// does the current char start a new array literal?
case '[':
arrayLevel++
}
continue
}
// are we currently inside a bracket expression?
if bracketLevel > 0 {
switch char {
// does the current char close a bracket expression?
case ')':
bracketLevel--
// does the current char start a new bracket expression?
case '(':
bracketLevel++
}
continue
}
// are we currently inside a string?
if insideString {
nextIndex := i + 1
// does the current char terminate the current string?
if char == '\'' {
// is the next char the same as the current char, which
// escapes it and prevents it from terminating the current
// string?
if nextIndex < len(s) && s[nextIndex] == '\'' {
skipCharAt = nextIndex
} else {
insideString = false
}
}
continue
}
// does the current char mark the start of a new array literal?
if char == '[' {
arrayLevel++
continue
}
// does the current char mark the start of a new bracket expression?
if char == '(' {
bracketLevel++
continue
}
// does the current char mark the start of a new string?
if char == '\'' {
insideString = true
continue
}
// is the current char an argument delimiter?
if char == ',' {
splitAt = i
break
}
}
// did we find an argument delimiter?
if splitAt >= 0 {
args, s = append(args, s[:splitAt]), s[splitAt+1:]
} else {
args = append(args, s)
break
}
}
return args
}
// normalizeColumnType will normalize column types so that they can be
// meaningfully compared.
func normalizeColumnType(dialect string, columnType string) (normalizedType, arg1, arg2 string) {
columnType = strings.ToUpper(strings.TrimSpace(columnType))
normalizedType = columnType
var args, suffix string
i := strings.Index(columnType, "(")
j := strings.LastIndex(columnType, ")")
if j > i {
normalizedType = strings.TrimSpace(columnType[:i])
args = strings.TrimSpace(columnType[i+1 : j])
suffix = strings.TrimSpace(columnType[j+1:])
k := strings.Index(args, ",")
if k >= 0 {
arg1 = strings.TrimSpace(args[:k])
arg2 = strings.TrimSpace(args[k+1:])
} else {
arg1 = strings.TrimSpace(args)
}
}
isPostgresArray := false
if dialect == DialectPostgres && strings.HasSuffix(normalizedType, "[]") {
isPostgresArray = true
normalizedType = normalizedType[:len(normalizedType)-2]
}
switch dialect {
case DialectPostgres:
// https://www.postgresql.org/docs/current/datatype.html
switch strings.ReplaceAll(normalizedType, " ", "") {
// Numeric
case "INTEGER", "SERIAL", "SERIAL4", "INT4":
normalizedType = "INT"
case "BIGSERIAL", "SERIAL8", "INT8":
normalizedType = "BIGINT"
case "SMALLSERIAL", "SERIAL2", "INT2":
normalizedType = "SMALLINT"
case "DECIMAL":
normalizedType = "NUMERIC"
case "FLOAT4":
normalizedType = "REAL"
case "FLOAT8":
normalizedType = "DOUBLE PRECISION"
// Character
case "CHARACTERVARYING":
normalizedType = "VARCHAR"
case "CHARACTER":
normalizedType = "CHAR"
// TimeField
case "TIMESTAMPWITHOUTTIMEZONE":
normalizedType = "TIMESTAMP"
case "TIMESTAMPWITHTIMEZONE":
normalizedType = "TIMESTAMPTZ"
case "TIMESTAMP":
if suffix != "" && strings.ReplaceAll(suffix, " ", "") == "WITHTIMEZONE" {
normalizedType = "TIMESTAMPTZ"
}
case "TIMEWITHOUTTIMEZONE":
normalizedType = "TIME"
case "TIMEWITHTIMEZONE":
normalizedType = "TIMETZ"
case "TIME":
if suffix != "" && strings.ReplaceAll(suffix, " ", "") == "WITHTIMEZONE" {
normalizedType = "TIMETZ"
}
// Binary
case "BITVARYING":
normalizedType = "VARBIT"
// Boolean
case "BOOL":
normalizedType = "BOOLEAN"
}
case DialectMySQL:
isUnsigned := strings.HasSuffix(normalizedType, " UNSIGNED")
if isUnsigned {
normalizedType = strings.TrimSuffix(normalizedType, " UNSIGNED")
} else {
normalizedType = strings.TrimSuffix(normalizedType, " SIGNED")
}
switch strings.ReplaceAll(normalizedType, " ", "") {
case "INTEGER":
normalizedType, arg1, arg2 = "INT", "", ""
case "DEC", "DECIMAL":
normalizedType = "NUMERIC"
case "BOOL", "BOOLEAN":
normalizedType, arg1, arg2 = "TINYINT", "1", ""
case "TINYINT":
if arg1 != "1" {
arg1 = ""
}
arg2 = ""
case "SMALLINT", "MEDIUMINT", "INT", "BIGINT":
// MySQL display width is deprecated
// https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html
arg1, arg2 = "", ""
}
if isUnsigned {
normalizedType += " UNSIGNED"
}
case DialectSQLServer:
switch strings.ReplaceAll(normalizedType, " ", "") {
case "BINARYVARYING":
normalizedType = "VARBINARY"
case "INTEGER":
normalizedType = "INT"
case "NATIONALCHARACTERVARYING":
normalizedType = "NVARCHAR"
case "CHARACTERVARYING":
normalizedType = "VARCHAR"
case "CHARACTER":
normalizedType = "CHAR"
case "DEC", "DECIMAL":
normalizedType = "NUMERIC"
}
}
if isPostgresArray {
normalizedType = normalizedType + "[]"
}
return normalizedType, arg1, arg2
}
// normalizeColumnType will normalize column defaults so that they can be
// meaningfully compared.
func normalizeColumnDefault(dialect string, columnDefault string) (normalizedDefault string) {
columnDefault = strings.TrimSpace(columnDefault)
if columnDefault == "" {
return ""
}
upperDefault := strings.ToUpper(columnDefault)
switch upperDefault {
case "1", "TRUE":
return "'1'"
case "0", "FALSE":
return "'0'"
case "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "NULL":
return upperDefault
}
switch dialect {
case DialectSQLite:
if upperDefault == "DATETIME()" || upperDefault == "DATETIME('NOW')" {
return "CURRENT_TIMESTAMP"
}
case DialectPostgres:
if upperDefault == "NOW()" {
return "CURRENT_TIMESTAMP"
}
if before, _, found := strings.Cut(columnDefault, "::"); found {
return before
}
case DialectMySQL:
if upperDefault == "NOW()" {
return "CURRENT_TIMESTAMP"
}
case DialectSQLServer:
if upperDefault == "GETDATE()" {
return "CURRENT_TIMESTAMP"
}
}
return columnDefault
}
// dirFS is like os.DirFS without the restriction of banning filenames like
// '../../somefile.sql'.
type dirFS string
var _ fs.FS = (*dirFS)(nil)
// Open implements fs.FS.
func (d dirFS) Open(name string) (fs.File, error) {
dir := string(d)
if dir == "." {
dir = ""
}
return os.Open(filepath.Join(dir, name))
}
var (
driversMu sync.RWMutex
drivers = make(map[string]Driver)
)
// Driver represents the capabilities of the underlying database driver for a
// particular dialect. It is not necessary to implement all fields.
type Driver struct {
// (Required) Dialect is the database dialect. Possible values: "sqlite", "postgres",
// "mysql", "sqlserver".
Dialect string
// (Required) DriverName is the driverName to be used with sql.Open().
DriverName string
// If not nil, IsLockTimeout is used to check if an error is a
// database-specific lock timeout error.
IsLockTimeout func(error) bool
// If not nil, PreprocessDSN will be called on a dataSourceName right
// before it is passed in to sql.Open().
PreprocessDSN func(string) string
// If not nil, AnnotateError will be called on an error returned by the
// database to display to the user. The primary purpose is to annotate the
// error with useful information like line number where an error occurred.
AnnotateError func(originalErr error, query string) error
}
// Registers registers a driver for a particular dialect. It is safe to call
// Register for a dialect multiple times, the last one wins.
func Register(driver Driver) {
driversMu.Lock()
defer driversMu.Unlock()
if driver.Dialect == "" {
panic("ddl: driver dialect cannot be empty")
}
drivers[driver.Dialect] = driver
}
func getDriver(dialect string) (driver Driver, ok bool) {
driversMu.RLock()
defer driversMu.RUnlock()
driver, ok = drivers[dialect]
return driver, ok
}
// NormalizeDSN normalizes an input DSN (Data Source Name), using a heuristic
// to detect the dialect of the DSN as well as providing an appropriate
// driverName to be used with sql.Open().
func NormalizeDSN(dsn string) (dialect, driverName, normalizedDSN string) {
if strings.HasPrefix(dsn, "file:") {
filename, _, _ := strings.Cut(strings.TrimPrefix(strings.TrimPrefix(dsn, "file:"), "//"), "?")
file, err := os.Open(filename)
if errors.Is(err, os.ErrNotExist) && (strings.HasSuffix(filename, ".sqlite") ||
strings.HasSuffix(filename, ".sqlite3") ||
strings.HasSuffix(filename, ".db") ||
strings.HasSuffix(filename, ".db3")) {
return DialectSQLite, "sqlite3", dsn
}
if err != nil {
return "", "", ""
}
defer file.Close()
r := bufio.NewReader(file)
// SQLite databases may also start with a 'file:' prefix. Treat the
// contents of the file as a dsn only if the file isn't already an
// SQLite database i.e. the first 16 bytes isn't the SQLite file
// header. https://www.sqlite.org/fileformat.html#the_database_header
header, err := r.Peek(16)