14
14
15
15
package sql
16
16
17
- import (
18
- "fmt"
19
- "strings"
20
- )
21
-
22
17
// All functions here are used together to generate 'CREATE TABLE' statement. Each function takes what it requires
23
18
// to build the definition, which are mostly exact names or values (e.g. columns, indexes names, types, etc.)
24
19
// These functions allow creating the compatible 'CREATE TABLE' statement from both GMS and Dolt, which use different
@@ -27,133 +22,37 @@ import (
27
22
// GenerateCreateTableStatement returns 'CREATE TABLE' statement with given table names
28
23
// and column definition statements in order and the collation and character set names for the table
29
24
func GenerateCreateTableStatement (tblName string , colStmts []string , temp , autoInc , tblCharsetName , tblCollName , comment string ) string {
30
- if comment != "" {
31
- // Escape any single quotes in the comment and add the COMMENT keyword
32
- comment = strings .ReplaceAll (comment , "'" , "''" )
33
- comment = fmt .Sprintf (" COMMENT='%s'" , comment )
34
- }
35
-
36
- if autoInc != "" {
37
- autoInc = fmt .Sprintf (" AUTO_INCREMENT=%s" , autoInc )
38
- }
39
-
40
- return fmt .Sprintf (
41
- "CREATE%s TABLE %s (\n %s\n ) ENGINE=InnoDB%s DEFAULT CHARSET=%s COLLATE=%s%s" ,
42
- temp ,
43
- QuoteIdentifier (tblName ),
44
- strings .Join (colStmts , ",\n " ),
45
- autoInc ,
46
- tblCharsetName ,
47
- tblCollName ,
48
- comment ,
49
- )
25
+ return GlobalSchemaFormatter .GenerateCreateTableStatement (tblName , colStmts , temp , autoInc , tblCharsetName , tblCollName , comment )
50
26
}
51
27
52
28
// GenerateCreateTableColumnDefinition returns column definition string for 'CREATE TABLE' statement for given column.
53
29
// This part comes first in the 'CREATE TABLE' statement.
54
30
func GenerateCreateTableColumnDefinition (col * Column , colDefault , onUpdate string , tableCollation CollationID ) string {
55
- var colTypeString string
56
- if collationType , ok := col .Type .(TypeWithCollation ); ok {
57
- colTypeString = collationType .StringWithTableCollation (tableCollation )
58
- } else {
59
- colTypeString = col .Type .String ()
60
- }
61
- stmt := fmt .Sprintf (" %s %s" , QuoteIdentifier (col .Name ), colTypeString )
62
- if ! col .Nullable {
63
- stmt = fmt .Sprintf ("%s NOT NULL" , stmt )
64
- }
65
-
66
- if col .AutoIncrement {
67
- stmt = fmt .Sprintf ("%s AUTO_INCREMENT" , stmt )
68
- }
69
-
70
- if c , ok := col .Type .(SpatialColumnType ); ok {
71
- if s , d := c .GetSpatialTypeSRID (); d {
72
- stmt = fmt .Sprintf ("%s /*!80003 SRID %v */" , stmt , s )
73
- }
74
- }
75
-
76
- if col .Generated != nil {
77
- storedStr := ""
78
- if ! col .Virtual {
79
- storedStr = " STORED"
80
- }
81
- stmt = fmt .Sprintf ("%s GENERATED ALWAYS AS %s%s" , stmt , col .Generated .String (), storedStr )
82
- }
83
-
84
- if col .Default != nil && col .Generated == nil {
85
- stmt = fmt .Sprintf ("%s DEFAULT %s" , stmt , colDefault )
86
- }
87
-
88
- if col .OnUpdate != nil {
89
- stmt = fmt .Sprintf ("%s ON UPDATE %s" , stmt , onUpdate )
90
- }
91
-
92
- if col .Comment != "" {
93
- stmt = fmt .Sprintf ("%s COMMENT '%s'" , stmt , col .Comment )
94
- }
95
- return stmt
31
+ return GlobalSchemaFormatter .GenerateCreateTableColumnDefinition (col , colDefault , onUpdate , tableCollation )
96
32
}
97
33
98
34
// GenerateCreateTablePrimaryKeyDefinition returns primary key definition string for 'CREATE TABLE' statement
99
35
// for given column(s). This part comes after each column definitions.
100
36
func GenerateCreateTablePrimaryKeyDefinition (pkCols []string ) string {
101
- return fmt . Sprintf ( " PRIMARY KEY (%s)" , strings . Join ( QuoteIdentifiers ( pkCols ), "," ) )
37
+ return GlobalSchemaFormatter . GenerateCreateTablePrimaryKeyDefinition ( pkCols )
102
38
}
103
39
104
40
// GenerateCreateTableIndexDefinition returns index definition string for 'CREATE TABLE' statement
105
41
// for given index. This part comes after primary key definition if there is any.
106
42
func GenerateCreateTableIndexDefinition (isUnique , isSpatial , isFullText , isVector bool , indexID string , indexCols []string , comment string ) string {
107
- unique := ""
108
- if isUnique {
109
- unique = "UNIQUE "
110
- }
111
-
112
- spatial := ""
113
- if isSpatial {
114
- unique = "SPATIAL "
115
- }
116
-
117
- fulltext := ""
118
- if isFullText {
119
- fulltext = "FULLTEXT "
120
- }
121
-
122
- vector := ""
123
- if isVector {
124
- vector = "VECTOR "
125
- }
126
-
127
- key := fmt .Sprintf (" %s%s%s%sKEY %s (%s)" , unique , spatial , fulltext , vector , QuoteIdentifier (indexID ), strings .Join (indexCols , "," ))
128
- if comment != "" {
129
- key = fmt .Sprintf ("%s COMMENT '%s'" , key , comment )
130
- }
131
- return key
43
+ return GlobalSchemaFormatter .GenerateCreateTableIndexDefinition (isUnique , isSpatial , isFullText , isVector , indexID , indexCols , comment )
132
44
}
133
45
134
46
// GenerateCreateTableForiegnKeyDefinition returns foreign key constraint definition string for 'CREATE TABLE' statement
135
47
// for given foreign key. This part comes after index definitions if there are any.
136
48
func GenerateCreateTableForiegnKeyDefinition (fkName string , fkCols []string , parentTbl string , parentCols []string , onDelete , onUpdate string ) string {
137
- keyCols := strings .Join (QuoteIdentifiers (fkCols ), "," )
138
- refCols := strings .Join (QuoteIdentifiers (parentCols ), "," )
139
- fkey := fmt .Sprintf (" CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)" , QuoteIdentifier (fkName ), keyCols , QuoteIdentifier (parentTbl ), refCols )
140
- if onDelete != "" {
141
- fkey = fmt .Sprintf ("%s ON DELETE %s" , fkey , onDelete )
142
- }
143
- if onUpdate != "" {
144
- fkey = fmt .Sprintf ("%s ON UPDATE %s" , fkey , onUpdate )
145
- }
146
- return fkey
49
+ return GlobalSchemaFormatter .GenerateCreateTableForiegnKeyDefinition (fkName , fkCols , parentTbl , parentCols , onDelete , onUpdate )
147
50
}
148
51
149
52
// GenerateCreateTableCheckConstraintClause returns check constraint clause string for 'CREATE TABLE' statement
150
53
// for given check constraint. This part comes the last and after foreign key definitions if there are any.
151
54
func GenerateCreateTableCheckConstraintClause (checkName , checkExpr string , enforced bool ) string {
152
- cc := fmt .Sprintf (" CONSTRAINT %s CHECK (%s)" , QuoteIdentifier (checkName ), checkExpr )
153
- if ! enforced {
154
- cc = fmt .Sprintf ("%s /*!80016 NOT ENFORCED */" , cc )
155
- }
156
- return cc
55
+ return GlobalSchemaFormatter .GenerateCreateTableCheckConstraintClause (checkName , checkExpr , enforced )
157
56
}
158
57
159
58
// QuoteIdentifier wraps the specified identifier in backticks and escapes all occurrences of backticks in the
0 commit comments