Skip to content

Commit

Permalink
fix(engine/sqlite): Support quoted identifier (#2556)
Browse files Browse the repository at this point in the history
* fix(engine/sqlite): support quoted identifier

close #1817

* test: add endtoend
  • Loading branch information
orisano committed Aug 2, 2023
1 parent e43c45f commit 5696f2f
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/quoted_colname/sqlite/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/quoted_colname/sqlite/go/models.go

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

37 changes: 37 additions & 0 deletions internal/endtoend/testdata/quoted_colname/sqlite/go/query.sql.go

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/quoted_colname/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Example queries for sqlc
CREATE TABLE "test"
(
"id" TEXT NOT NULL
);

-- name: TestList :many
SELECT * FROM "test";
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/quoted_colname/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql",
"name": "querytest"
}
]
}
22 changes: 13 additions & 9 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ func todo(funcname string, n node) *ast.TODO {
}

func identifier(id string) string {
if len(id) >= 2 && id[0] == '"' && id[len(id)-1] == '"' {
unquoted, _ := strconv.Unquote(id)
return unquoted
}
return strings.ToLower(id)
}

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

Expand Down Expand Up @@ -293,7 +297,7 @@ func (c *cc) convertFuncContext(n *parser.Expr_functionContext) ast.Node {
},
Funcname: &ast.List{
Items: []ast.Node{
NewIdentifer(funcName),
NewIdentifier(funcName),
},
},
AggStar: n.STAR() != nil,
Expand All @@ -317,16 +321,16 @@ func (c *cc) convertColumnNameExpr(n *parser.Expr_qualified_column_nameContext)
if schema, ok := n.Schema_name().(*parser.Schema_nameContext); ok {
schemaText := schema.GetText()
if schemaText != "" {
items = append(items, NewIdentifer(schemaText))
items = append(items, NewIdentifier(schemaText))
}
}
if table, ok := n.Table_name().(*parser.Table_nameContext); ok {
tableName := table.GetText()
if tableName != "" {
items = append(items, NewIdentifer(tableName))
items = append(items, NewIdentifier(tableName))
}
}
items = append(items, NewIdentifer(n.Column_name().GetText()))
items = append(items, NewIdentifier(n.Column_name().GetText()))
return &ast.ColumnRef{
Fields: &ast.List{
Items: items,
Expand Down Expand Up @@ -384,7 +388,7 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
tableName := identifier(cte.Table_name().GetText())
var cteCols ast.List
for _, col := range cte.AllColumn_name() {
cteCols.Items = append(cteCols.Items, NewIdentifer(col.GetText()))
cteCols.Items = append(cteCols.Items, NewIdentifier(col.GetText()))
}
ctes = append(ctes, &ast.CommonTableExpr{
Ctename: &tableName,
Expand Down Expand Up @@ -506,7 +510,7 @@ func (c *cc) getCols(core *parser.Select_coreContext) []ast.Node {
func (c *cc) convertWildCardField(n *parser.Result_columnContext) *ast.ColumnRef {
items := []ast.Node{}
if n.Table_name() != nil {
items = append(items, NewIdentifer(n.Table_name().GetText()))
items = append(items, NewIdentifier(n.Table_name().GetText()))
}
items = append(items, &ast.A_Star{})

Expand Down Expand Up @@ -853,7 +857,7 @@ func (c *cc) convertTablesOrSubquery(n []parser.ITable_or_subqueryContext) []ast
},
Funcname: &ast.List{
Items: []ast.Node{
NewIdentifer(rel),
NewIdentifier(rel),
},
},
Args: &ast.List{
Expand Down Expand Up @@ -965,7 +969,7 @@ func (c *cc) convertCastExpr(n *parser.Expr_castContext) ast.Node {
TypeName: &ast.TypeName{
Name: name,
Names: &ast.List{Items: []ast.Node{
NewIdentifer(name),
NewIdentifier(name),
}},
ArrayBounds: &ast.List{},
},
Expand Down

0 comments on commit 5696f2f

Please sign in to comment.