@@ -64,8 +64,9 @@ type SchemaFormatter interface {
64
64
// for given column(s). This part comes after each column definitions.
65
65
GenerateCreateTablePrimaryKeyDefinition (pkCols []string ) string
66
66
// GenerateCreateTableIndexDefinition returns index definition string for 'CREATE TABLE' statement
67
- // for given index. This part comes after primary key definition if there is any.
68
- GenerateCreateTableIndexDefinition (isUnique , isSpatial , isFullText , isVector bool , indexID string , indexCols []string , comment string ) string
67
+ // for given index. This part comes after primary key definition if there is any. Implementors can signal that the
68
+ // index definition provided cannot be included with the second return param
69
+ GenerateCreateTableIndexDefinition (isUnique , isSpatial , isFullText , isVector bool , indexID string , indexCols []string , comment string ) (string , bool )
69
70
// GenerateCreateTableForiegnKeyDefinition returns foreign key constraint definition string for 'CREATE TABLE' statement
70
71
// for given foreign key. This part comes after index definitions if there are any.
71
72
GenerateCreateTableForiegnKeyDefinition (fkName string , fkCols []string , parentTbl string , parentCols []string , onDelete , onUpdate string ) string
@@ -151,7 +152,7 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableStatement(tblName string, colS
151
152
return fmt .Sprintf (
152
153
"CREATE%s TABLE %s (\n %s\n ) ENGINE=InnoDB%s DEFAULT CHARSET=%s COLLATE=%s%s" ,
153
154
temp ,
154
- QuoteIdentifier (tblName ),
155
+ m . QuoteIdentifier (tblName ),
155
156
strings .Join (colStmts , ",\n " ),
156
157
autoInc ,
157
158
tblCharsetName ,
@@ -168,7 +169,7 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableColumnDefinition(col *Column,
168
169
} else {
169
170
colTypeString = col .Type .String ()
170
171
}
171
- stmt := fmt .Sprintf (" %s %s" , QuoteIdentifier (col .Name ), colTypeString )
172
+ stmt := fmt .Sprintf (" %s %s" , m . QuoteIdentifier (col .Name ), colTypeString )
172
173
if ! col .Nullable {
173
174
stmt = fmt .Sprintf ("%s NOT NULL" , stmt )
174
175
}
@@ -207,11 +208,11 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableColumnDefinition(col *Column,
207
208
208
209
// GenerateCreateTablePrimaryKeyDefinition implements the SchemaFormatter interface.
209
210
func (m * MySqlSchemaFormatter ) GenerateCreateTablePrimaryKeyDefinition (pkCols []string ) string {
210
- return fmt .Sprintf (" PRIMARY KEY (%s)" , strings .Join (QuoteIdentifiers (pkCols ), "," ))
211
+ return fmt .Sprintf (" PRIMARY KEY (%s)" , strings .Join (m . QuoteIdentifiers (pkCols ), "," ))
211
212
}
212
213
213
214
// GenerateCreateTableIndexDefinition implements the SchemaFormatter interface.
214
- func (m * MySqlSchemaFormatter ) GenerateCreateTableIndexDefinition (isUnique , isSpatial , isFullText , isVector bool , indexID string , indexCols []string , comment string ) string {
215
+ func (m * MySqlSchemaFormatter ) GenerateCreateTableIndexDefinition (isUnique , isSpatial , isFullText , isVector bool , indexID string , indexCols []string , comment string ) ( string , bool ) {
215
216
unique := ""
216
217
if isUnique {
217
218
unique = "UNIQUE "
@@ -232,18 +233,18 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableIndexDefinition(isUnique, isSp
232
233
vector = "VECTOR "
233
234
}
234
235
235
- key := fmt .Sprintf (" %s%s%s%sKEY %s (%s)" , unique , spatial , fulltext , vector , QuoteIdentifier (indexID ), strings .Join (indexCols , "," ))
236
+ key := fmt .Sprintf (" %s%s%s%sKEY %s (%s)" , unique , spatial , fulltext , vector , m . QuoteIdentifier (indexID ), strings .Join (indexCols , "," ))
236
237
if comment != "" {
237
238
key = fmt .Sprintf ("%s COMMENT '%s'" , key , comment )
238
239
}
239
- return key
240
+ return key , true
240
241
}
241
242
242
243
// GenerateCreateTableForiegnKeyDefinition implements the SchemaFormatter interface.
243
244
func (m * MySqlSchemaFormatter ) GenerateCreateTableForiegnKeyDefinition (fkName string , fkCols []string , parentTbl string , parentCols []string , onDelete , onUpdate string ) string {
244
- keyCols := strings .Join (QuoteIdentifiers (fkCols ), "," )
245
- refCols := strings .Join (QuoteIdentifiers (parentCols ), "," )
246
- fkey := fmt .Sprintf (" CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)" , QuoteIdentifier (fkName ), keyCols , QuoteIdentifier (parentTbl ), refCols )
245
+ keyCols := strings .Join (m . QuoteIdentifiers (fkCols ), "," )
246
+ refCols := strings .Join (m . QuoteIdentifiers (parentCols ), "," )
247
+ fkey := fmt .Sprintf (" CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)" , m . QuoteIdentifier (fkName ), keyCols , m . QuoteIdentifier (parentTbl ), refCols )
247
248
if onDelete != "" {
248
249
fkey = fmt .Sprintf ("%s ON DELETE %s" , fkey , onDelete )
249
250
}
@@ -255,7 +256,7 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableForiegnKeyDefinition(fkName st
255
256
256
257
// GenerateCreateTableCheckConstraintClause implements the SchemaFormatter interface.
257
258
func (m * MySqlSchemaFormatter ) GenerateCreateTableCheckConstraintClause (checkName , checkExpr string , enforced bool ) string {
258
- cc := fmt .Sprintf (" CONSTRAINT %s CHECK (%s)" , QuoteIdentifier (checkName ), checkExpr )
259
+ cc := fmt .Sprintf (" CONSTRAINT %s CHECK (%s)" , m . QuoteIdentifier (checkName ), checkExpr )
259
260
if ! enforced {
260
261
cc = fmt .Sprintf ("%s /*!80016 NOT ENFORCED */" , cc )
261
262
}
@@ -266,4 +267,14 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableCheckConstraintClause(checkNam
266
267
// identifier by replacing them with double backticks.
267
268
func (m * MySqlSchemaFormatter ) QuoteIdentifier (id string ) string {
268
269
return fmt .Sprintf ("`%s`" , strings .ReplaceAll (id , "`" , "``" ))
270
+ }
271
+
272
+ // QuoteIdentifiers wraps each of the specified identifiers in backticks, escapes all occurrences of backticks in
273
+ // the identifier, and returns a slice of the quoted identifiers.
274
+ func (m * MySqlSchemaFormatter ) QuoteIdentifiers (ids []string ) []string {
275
+ quoted := make ([]string , len (ids ))
276
+ for i , id := range ids {
277
+ quoted [i ] = m .QuoteIdentifier (id )
278
+ }
279
+ return quoted
269
280
}
0 commit comments