@@ -64,8 +64,9 @@ type SchemaFormatter interface {
6464 // for given column(s). This part comes after each column definitions.
6565 GenerateCreateTablePrimaryKeyDefinition (pkCols []string ) string
6666 // 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 )
6970 // GenerateCreateTableForiegnKeyDefinition returns foreign key constraint definition string for 'CREATE TABLE' statement
7071 // for given foreign key. This part comes after index definitions if there are any.
7172 GenerateCreateTableForiegnKeyDefinition (fkName string , fkCols []string , parentTbl string , parentCols []string , onDelete , onUpdate string ) string
@@ -151,7 +152,7 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableStatement(tblName string, colS
151152 return fmt .Sprintf (
152153 "CREATE%s TABLE %s (\n %s\n ) ENGINE=InnoDB%s DEFAULT CHARSET=%s COLLATE=%s%s" ,
153154 temp ,
154- QuoteIdentifier (tblName ),
155+ m . QuoteIdentifier (tblName ),
155156 strings .Join (colStmts , ",\n " ),
156157 autoInc ,
157158 tblCharsetName ,
@@ -168,7 +169,7 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableColumnDefinition(col *Column,
168169 } else {
169170 colTypeString = col .Type .String ()
170171 }
171- stmt := fmt .Sprintf (" %s %s" , QuoteIdentifier (col .Name ), colTypeString )
172+ stmt := fmt .Sprintf (" %s %s" , m . QuoteIdentifier (col .Name ), colTypeString )
172173 if ! col .Nullable {
173174 stmt = fmt .Sprintf ("%s NOT NULL" , stmt )
174175 }
@@ -207,11 +208,11 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableColumnDefinition(col *Column,
207208
208209// GenerateCreateTablePrimaryKeyDefinition implements the SchemaFormatter interface.
209210func (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 ), "," ))
211212}
212213
213214// 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 ) {
215216 unique := ""
216217 if isUnique {
217218 unique = "UNIQUE "
@@ -232,18 +233,18 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableIndexDefinition(isUnique, isSp
232233 vector = "VECTOR "
233234 }
234235
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 , "," ))
236237 if comment != "" {
237238 key = fmt .Sprintf ("%s COMMENT '%s'" , key , comment )
238239 }
239- return key
240+ return key , true
240241}
241242
242243// GenerateCreateTableForiegnKeyDefinition implements the SchemaFormatter interface.
243244func (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 )
247248 if onDelete != "" {
248249 fkey = fmt .Sprintf ("%s ON DELETE %s" , fkey , onDelete )
249250 }
@@ -255,7 +256,7 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableForiegnKeyDefinition(fkName st
255256
256257// GenerateCreateTableCheckConstraintClause implements the SchemaFormatter interface.
257258func (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 )
259260 if ! enforced {
260261 cc = fmt .Sprintf ("%s /*!80016 NOT ENFORCED */" , cc )
261262 }
@@ -266,4 +267,14 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableCheckConstraintClause(checkNam
266267// identifier by replacing them with double backticks.
267268func (m * MySqlSchemaFormatter ) QuoteIdentifier (id string ) string {
268269 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
269280}
0 commit comments