Skip to content

Commit ce95370

Browse files
committed
Made inline index definitions optional
1 parent 2ffe686 commit ce95370

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

sql/parser.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
209210
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), ","))
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.
243244
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)
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.
257258
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)
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.
267268
func (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
}

sql/rowexec/show_iters.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,11 @@ func (i *showCreateTablesIter) produceCreateTableStatement(ctx *sql.Context, tab
452452
}
453453
}
454454

455-
colStmts = append(colStmts, sql.GenerateCreateTableIndexDefinition(index.IsUnique(), index.IsSpatial(),
456-
index.IsFullText(), index.IsVector(), index.ID(), indexCols, index.Comment()))
455+
indexDefn, shouldInclude := sql.GenerateCreateTableIndexDefinition(index.IsUnique(), index.IsSpatial(),
456+
index.IsFullText(), index.IsVector(), index.ID(), indexCols, index.Comment())
457+
if shouldInclude {
458+
colStmts = append(colStmts, indexDefn)
459+
}
457460
}
458461

459462
fkt, err := getForeignKeyTable(table)

sql/sqlfmt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func GenerateCreateTablePrimaryKeyDefinition(pkCols []string) string {
3939

4040
// GenerateCreateTableIndexDefinition returns index definition string for 'CREATE TABLE' statement
4141
// for given index. This part comes after primary key definition if there is any.
42-
func GenerateCreateTableIndexDefinition(isUnique, isSpatial, isFullText, isVector bool, indexID string, indexCols []string, comment string) string {
42+
func GenerateCreateTableIndexDefinition(isUnique, isSpatial, isFullText, isVector bool, indexID string, indexCols []string, comment string) (string, bool) {
4343
return GlobalSchemaFormatter.GenerateCreateTableIndexDefinition(isUnique, isSpatial, isFullText, isVector, indexID, indexCols, comment)
4444
}
4545

@@ -66,7 +66,7 @@ func QuoteIdentifier(id string) string {
6666
func QuoteIdentifiers(ids []string) []string {
6767
quoted := make([]string, len(ids))
6868
for i, id := range ids {
69-
quoted[i] = QuoteIdentifier(id)
69+
quoted[i] = GlobalSchemaFormatter.QuoteIdentifier(id)
7070
}
7171
return quoted
7272
}

0 commit comments

Comments
 (0)