Skip to content

Commit 2ffe686

Browse files
committed
Replace static functions with references to global interafaces
1 parent 4f15584 commit 2ffe686

File tree

2 files changed

+7
-118
lines changed

2 files changed

+7
-118
lines changed

sql/parser.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,4 @@ func (m *MySqlSchemaFormatter) GenerateCreateTableCheckConstraintClause(checkNam
266266
// identifier by replacing them with double backticks.
267267
func (m *MySqlSchemaFormatter) QuoteIdentifier(id string) string {
268268
return fmt.Sprintf("`%s`", strings.ReplaceAll(id, "`", "``"))
269-
}
270-
271-
// QuoteIdentifiers wraps each of the specified identifiers in backticks, escapes all occurrences of backticks in
272-
// the identifier, and returns a slice of the quoted identifiers.
273-
func (m *MySqlSchemaFormatter) QuoteIdentifiers(ids []string) []string {
274-
quoted := make([]string, len(ids))
275-
for i, id := range ids {
276-
quoted[i] = QuoteIdentifier(id)
277-
}
278-
return quoted
279-
}
269+
}

sql/sqlfmt.go

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414

1515
package sql
1616

17-
import (
18-
"fmt"
19-
"strings"
20-
)
21-
2217
// All functions here are used together to generate 'CREATE TABLE' statement. Each function takes what it requires
2318
// to build the definition, which are mostly exact names or values (e.g. columns, indexes names, types, etc.)
2419
// These functions allow creating the compatible 'CREATE TABLE' statement from both GMS and Dolt, which use different
@@ -27,133 +22,37 @@ import (
2722
// GenerateCreateTableStatement returns 'CREATE TABLE' statement with given table names
2823
// and column definition statements in order and the collation and character set names for the table
2924
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)
5026
}
5127

5228
// GenerateCreateTableColumnDefinition returns column definition string for 'CREATE TABLE' statement for given column.
5329
// This part comes first in the 'CREATE TABLE' statement.
5430
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)
9632
}
9733

9834
// GenerateCreateTablePrimaryKeyDefinition returns primary key definition string for 'CREATE TABLE' statement
9935
// for given column(s). This part comes after each column definitions.
10036
func GenerateCreateTablePrimaryKeyDefinition(pkCols []string) string {
101-
return fmt.Sprintf(" PRIMARY KEY (%s)", strings.Join(QuoteIdentifiers(pkCols), ","))
37+
return GlobalSchemaFormatter.GenerateCreateTablePrimaryKeyDefinition(pkCols)
10238
}
10339

10440
// GenerateCreateTableIndexDefinition returns index definition string for 'CREATE TABLE' statement
10541
// for given index. This part comes after primary key definition if there is any.
10642
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)
13244
}
13345

13446
// GenerateCreateTableForiegnKeyDefinition returns foreign key constraint definition string for 'CREATE TABLE' statement
13547
// for given foreign key. This part comes after index definitions if there are any.
13648
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)
14750
}
14851

14952
// GenerateCreateTableCheckConstraintClause returns check constraint clause string for 'CREATE TABLE' statement
15053
// for given check constraint. This part comes the last and after foreign key definitions if there are any.
15154
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)
15756
}
15857

15958
// QuoteIdentifier wraps the specified identifier in backticks and escapes all occurrences of backticks in the

0 commit comments

Comments
 (0)