Skip to content

fix(engine/mysql): Case-insensitive identifiers #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/identifier_case_sensitivity/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions internal/endtoend/testdata/identifier_case_sensitivity/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CREATE TABLE Authors (
ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name text NOT NULL,
Bio text
);

-- name: GetAuthor :one
SELECT * FROM Authors
WHERE ID = ? LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM Authors
ORDER BY Name;

-- name: CreateAuthor :execresult
INSERT INTO Authors (
Name, Bio
) VALUES (
?, ?
);

-- name: DeleteAuthor :exec
DELETE FROM Authors
WHERE ID = ?;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/identifier_case_sensitivity/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "db",
"engine": "mysql",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
39 changes: 23 additions & 16 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func todo(n pcast.Node) *ast.TODO {
return &ast.TODO{}
}

func identifier(id string) string {
return strings.ToLower(id)
}

func NewIdentifer(t string) *ast.String {
return &ast.String{Str: identifier(t)}
}

func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
alt := &ast.AlterTableStmt{
Table: parseTableName(n.Table),
Expand Down Expand Up @@ -119,7 +127,7 @@ func (c *cc) convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
}

func (c *cc) convertAssignment(n *pcast.Assignment) *ast.ResTarget {
name := n.Column.Name.String()
name := identifier(n.Column.Name.String())
return &ast.ResTarget{
Name: &name,
Val: c.convert(n.Expr),
Expand Down Expand Up @@ -259,12 +267,12 @@ func (c *cc) convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
var items []ast.Node
if schema := n.Name.Schema.String(); schema != "" {
items = append(items, &ast.String{Str: schema})
items = append(items, NewIdentifer(schema))
}
if table := n.Name.Table.String(); table != "" {
items = append(items, &ast.String{Str: table})
items = append(items, NewIdentifer(table))
}
items = append(items, &ast.String{Str: n.Name.Name.String()})
items = append(items, NewIdentifer(n.Name.Name.String()))
return &ast.ColumnRef{
Fields: &ast.List{
Items: items,
Expand All @@ -275,7 +283,7 @@ func (c *cc) convertColumnNameExpr(n *pcast.ColumnNameExpr) *ast.ColumnRef {
func (c *cc) convertColumnNames(cols []*pcast.ColumnName) *ast.List {
list := &ast.List{Items: []ast.Node{}}
for i := range cols {
name := cols[i].Name.String()
name := identifier(cols[i].Name.String())
list.Items = append(list.Items, &ast.ResTarget{
Name: &name,
})
Expand Down Expand Up @@ -344,9 +352,9 @@ func (c *cc) convertFuncCallExpr(n *pcast.FuncCallExpr) ast.Node {
// TODO: Deprecate the usage of Funcname
items := []ast.Node{}
if schema != "" {
items = append(items, &ast.String{Str: schema})
items = append(items, NewIdentifer(schema))
}
items = append(items, &ast.String{Str: name})
items = append(items, NewIdentifer(name))

args := &ast.List{}
for _, arg := range n.Args {
Expand Down Expand Up @@ -432,7 +440,8 @@ func (c *cc) convertSelectField(n *pcast.SelectField) *ast.ResTarget {
}
var name *string
if n.AsName.O != "" {
name = &n.AsName.O
asname := identifier(n.AsName.O)
name = &asname
}
return &ast.ResTarget{
// TODO: Populate Indirection field
Expand Down Expand Up @@ -479,7 +488,7 @@ func (c *cc) convertCommonTableExpression(n *pcast.CommonTableExpression) *ast.C

columns := &ast.List{}
for _, col := range n.ColNameList {
columns.Items = append(columns.Items, &ast.String{Str: col.String()})
columns.Items = append(columns.Items, NewIdentifer(col.String()))
}

return &ast.CommonTableExpr{
Expand Down Expand Up @@ -556,7 +565,7 @@ func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {
func (c *cc) convertWildCardField(n *pcast.WildCardField) *ast.ColumnRef {
items := []ast.Node{}
if t := n.Table.String(); t != "" {
items = append(items, &ast.String{Str: t})
items = append(items, NewIdentifer(t))
}
items = append(items, &ast.A_Star{})

Expand All @@ -579,9 +588,7 @@ func (c *cc) convertAggregateFuncExpr(n *pcast.AggregateFuncExpr) *ast.FuncCall
},
Funcname: &ast.List{
Items: []ast.Node{
&ast.String{
Str: name,
},
NewIdentifer(name),
},
},
Args: &ast.List{},
Expand Down Expand Up @@ -740,7 +747,7 @@ func (c *cc) convertDropDatabaseStmt(n *pcast.DropDatabaseStmt) ast.Node {
return &ast.DropSchemaStmt{
MissingOk: !n.IfExists,
Schemas: []*ast.String{
{Str: n.Name},
NewIdentifer(n.Name),
},
}
}
Expand Down Expand Up @@ -1138,8 +1145,8 @@ func (c *cc) convertSplitRegionStmt(n *pcast.SplitRegionStmt) ast.Node {
}

func (c *cc) convertTableName(n *pcast.TableName) *ast.RangeVar {
schema := n.Schema.String()
rel := n.Name.String()
schema := identifier(n.Schema.String())
rel := identifier(n.Name.String())
return &ast.RangeVar{
Schemaname: &schema,
Relname: &rel,
Expand Down
8 changes: 4 additions & 4 deletions internal/engine/dolphin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func text(nodes []pcast.Node) []string {

func parseTableName(n *pcast.TableName) *ast.TableName {
return &ast.TableName{
Schema: n.Schema.String(),
Name: n.Name.String(),
Schema: identifier(n.Schema.String()),
Name: identifier(n.Name.String()),
}
}

Expand All @@ -76,9 +76,9 @@ func toList(node pcast.Node) *ast.List {
switch n := node.(type) {
case *pcast.TableName:
if schema := n.Schema.String(); schema != "" {
items = append(items, &ast.String{Str: schema})
items = append(items, NewIdentifer(schema))
}
items = append(items, &ast.String{Str: n.Name.String()})
items = append(items, NewIdentifer(n.Name.String()))
default:
return nil
}
Expand Down