Skip to content

Commit 508e8f2

Browse files
authored
fix(engine/mysql): Case-insensitive identifiers (#1216)
1 parent 9513155 commit 508e8f2

File tree

7 files changed

+180
-20
lines changed

7 files changed

+180
-20
lines changed

internal/endtoend/testdata/identifier_case_sensitivity/db/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/identifier_case_sensitivity/db/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/identifier_case_sensitivity/db/query.sql.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CREATE TABLE Authors (
2+
ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
3+
Name text NOT NULL,
4+
Bio text
5+
);
6+
7+
-- name: GetAuthor :one
8+
SELECT * FROM Authors
9+
WHERE ID = ? LIMIT 1;
10+
11+
-- name: ListAuthors :many
12+
SELECT * FROM Authors
13+
ORDER BY Name;
14+
15+
-- name: CreateAuthor :execresult
16+
INSERT INTO Authors (
17+
Name, Bio
18+
) VALUES (
19+
?, ?
20+
);
21+
22+
-- name: DeleteAuthor :exec
23+
DELETE FROM Authors
24+
WHERE ID = ?;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "db",
6+
"engine": "mysql",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}

internal/engine/dolphin/convert.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ func todo(n pcast.Node) *ast.TODO {
2525
return &ast.TODO{}
2626
}
2727

28+
func identifier(id string) string {
29+
return strings.ToLower(id)
30+
}
31+
32+
func NewIdentifer(t string) *ast.String {
33+
return &ast.String{Str: identifier(t)}
34+
}
35+
2836
func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
2937
alt := &ast.AlterTableStmt{
3038
Table: parseTableName(n.Table),
@@ -119,7 +127,7 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
119127
}
120128

121129
func (c *cc) convertAssignment(n *pcast.Assignment) *ast.ResTarget {
122-
name := n.Column.Name.String()
130+
name := identifier(n.Column.Name.String())
123131
return &ast.ResTarget{
124132
Name: &name,
125133
Val: c.convert(n.Expr),
@@ -259,12 +267,12 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
259267
func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
260268
var items []ast.Node
261269
if schema := n.Name.Schema.String(); schema != "" {
262-
items = append(items, &ast.String{Str: schema})
270+
items = append(items, NewIdentifer(schema))
263271
}
264272
if table := n.Name.Table.String(); table != "" {
265-
items = append(items, &ast.String{Str: table})
273+
items = append(items, NewIdentifer(table))
266274
}
267-
items = append(items, &ast.String{Str: n.Name.Name.String()})
275+
items = append(items, NewIdentifer(n.Name.Name.String()))
268276
return &ast.ColumnRef{
269277
Fields: &ast.List{
270278
Items: items,
@@ -275,7 +283,7 @@ func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
275283
func (c *cc) convertColumnNames(cols []*pcast.ColumnName) *ast.List {
276284
list := &ast.List{Items: []ast.Node{}}
277285
for i := range cols {
278-
name := cols[i].Name.String()
286+
name := identifier(cols[i].Name.String())
279287
list.Items = append(list.Items, &ast.ResTarget{
280288
Name: &name,
281289
})
@@ -344,9 +352,9 @@ func (c *cc) convertFuncCallExpr(n *pcast.FuncCallExpr) ast.Node {
344352
// TODO: Deprecate the usage of Funcname
345353
items := []ast.Node{}
346354
if schema != "" {
347-
items = append(items, &ast.String{Str: schema})
355+
items = append(items, NewIdentifer(schema))
348356
}
349-
items = append(items, &ast.String{Str: name})
357+
items = append(items, NewIdentifer(name))
350358

351359
args := &ast.List{}
352360
for _, arg := range n.Args {
@@ -432,7 +440,8 @@ func (c *cc) convertSelectField(n *pcast.SelectField) *ast.ResTarget {
432440
}
433441
var name *string
434442
if n.AsName.O != "" {
435-
name = &n.AsName.O
443+
asname := identifier(n.AsName.O)
444+
name = &asname
436445
}
437446
return &ast.ResTarget{
438447
// TODO: Populate Indirection field
@@ -479,7 +488,7 @@ func (c *cc) convertCommonTableExpression(n *pcast.CommonTableExpression) *ast.C
479488

480489
columns := &ast.List{}
481490
for _, col := range n.ColNameList {
482-
columns.Items = append(columns.Items, &ast.String{Str: col.String()})
491+
columns.Items = append(columns.Items, NewIdentifer(col.String()))
483492
}
484493

485494
return &ast.CommonTableExpr{
@@ -556,7 +565,7 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
556565
func (c *cc) convertWildCardField(n *pcast.WildCardField) *ast.ColumnRef {
557566
items := []ast.Node{}
558567
if t := n.Table.String(); t != "" {
559-
items = append(items, &ast.String{Str: t})
568+
items = append(items, NewIdentifer(t))
560569
}
561570
items = append(items, &ast.A_Star{})
562571

@@ -579,9 +588,7 @@ func (c *cc) convertAggregateFuncExpr(n *pcast.AggregateFuncExpr) *ast.FuncCall
579588
},
580589
Funcname: &ast.List{
581590
Items: []ast.Node{
582-
&ast.String{
583-
Str: name,
584-
},
591+
NewIdentifer(name),
585592
},
586593
},
587594
Args: &ast.List{},
@@ -740,7 +747,7 @@ func (c *cc) convertDropDatabaseStmt(n *pcast.DropDatabaseStmt) ast.Node {
740747
return &ast.DropSchemaStmt{
741748
MissingOk: !n.IfExists,
742749
Schemas: []*ast.String{
743-
{Str: n.Name},
750+
NewIdentifer(n.Name),
744751
},
745752
}
746753
}
@@ -1138,8 +1145,8 @@ func (c *cc) convertSplitRegionStmt(n *pcast.SplitRegionStmt) ast.Node {
11381145
}
11391146

11401147
func (c *cc) convertTableName(n *pcast.TableName) *ast.RangeVar {
1141-
schema := n.Schema.String()
1142-
rel := n.Name.String()
1148+
schema := identifier(n.Schema.String())
1149+
rel := identifier(n.Name.String())
11431150
return &ast.RangeVar{
11441151
Schemaname: &schema,
11451152
Relname: &rel,

internal/engine/dolphin/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func text(nodes []pcast.Node) []string {
6666

6767
func parseTableName(n *pcast.TableName) *ast.TableName {
6868
return &ast.TableName{
69-
Schema: n.Schema.String(),
70-
Name: n.Name.String(),
69+
Schema: identifier(n.Schema.String()),
70+
Name: identifier(n.Name.String()),
7171
}
7272
}
7373

@@ -76,9 +76,9 @@ func toList(node pcast.Node) *ast.List {
7676
switch n := node.(type) {
7777
case *pcast.TableName:
7878
if schema := n.Schema.String(); schema != "" {
79-
items = append(items, &ast.String{Str: schema})
79+
items = append(items, NewIdentifer(schema))
8080
}
81-
items = append(items, &ast.String{Str: n.Name.String()})
81+
items = append(items, NewIdentifer(n.Name.String()))
8282
default:
8383
return nil
8484
}

0 commit comments

Comments
 (0)