Skip to content

Commit 5696f2f

Browse files
authored
fix(engine/sqlite): Support quoted identifier (#2556)
* fix(engine/sqlite): support quoted identifier close #1817 * test: add endtoend
1 parent e43c45f commit 5696f2f

File tree

6 files changed

+112
-9
lines changed

6 files changed

+112
-9
lines changed

internal/endtoend/testdata/quoted_colname/sqlite/go/db.go

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

internal/endtoend/testdata/quoted_colname/sqlite/go/models.go

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

internal/endtoend/testdata/quoted_colname/sqlite/go/query.sql.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE "test"
3+
(
4+
"id" TEXT NOT NULL
5+
);
6+
7+
-- name: TestList :many
8+
SELECT * FROM "test";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "sqlite",
7+
"schema": "query.sql",
8+
"queries": "query.sql",
9+
"name": "querytest"
10+
}
11+
]
12+
}

internal/engine/sqlite/convert.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ func todo(funcname string, n node) *ast.TODO {
2929
}
3030

3131
func identifier(id string) string {
32+
if len(id) >= 2 && id[0] == '"' && id[len(id)-1] == '"' {
33+
unquoted, _ := strconv.Unquote(id)
34+
return unquoted
35+
}
3236
return strings.ToLower(id)
3337
}
3438

35-
func NewIdentifer(t string) *ast.String {
39+
func NewIdentifier(t string) *ast.String {
3640
return &ast.String{Str: identifier(t)}
3741
}
3842

@@ -293,7 +297,7 @@ func (c *cc) convertFuncContext(n *parser.Expr_functionContext) ast.Node {
293297
},
294298
Funcname: &ast.List{
295299
Items: []ast.Node{
296-
NewIdentifer(funcName),
300+
NewIdentifier(funcName),
297301
},
298302
},
299303
AggStar: n.STAR() != nil,
@@ -317,16 +321,16 @@ func (c *cc) convertColumnNameExpr(n *parser.Expr_qualified_column_nameContext)
317321
if schema, ok := n.Schema_name().(*parser.Schema_nameContext); ok {
318322
schemaText := schema.GetText()
319323
if schemaText != "" {
320-
items = append(items, NewIdentifer(schemaText))
324+
items = append(items, NewIdentifier(schemaText))
321325
}
322326
}
323327
if table, ok := n.Table_name().(*parser.Table_nameContext); ok {
324328
tableName := table.GetText()
325329
if tableName != "" {
326-
items = append(items, NewIdentifer(tableName))
330+
items = append(items, NewIdentifier(tableName))
327331
}
328332
}
329-
items = append(items, NewIdentifer(n.Column_name().GetText()))
333+
items = append(items, NewIdentifier(n.Column_name().GetText()))
330334
return &ast.ColumnRef{
331335
Fields: &ast.List{
332336
Items: items,
@@ -384,7 +388,7 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
384388
tableName := identifier(cte.Table_name().GetText())
385389
var cteCols ast.List
386390
for _, col := range cte.AllColumn_name() {
387-
cteCols.Items = append(cteCols.Items, NewIdentifer(col.GetText()))
391+
cteCols.Items = append(cteCols.Items, NewIdentifier(col.GetText()))
388392
}
389393
ctes = append(ctes, &ast.CommonTableExpr{
390394
Ctename: &tableName,
@@ -506,7 +510,7 @@ func (c *cc) getCols(core *parser.Select_coreContext) []ast.Node {
506510
func (c *cc) convertWildCardField(n *parser.Result_columnContext) *ast.ColumnRef {
507511
items := []ast.Node{}
508512
if n.Table_name() != nil {
509-
items = append(items, NewIdentifer(n.Table_name().GetText()))
513+
items = append(items, NewIdentifier(n.Table_name().GetText()))
510514
}
511515
items = append(items, &ast.A_Star{})
512516

@@ -853,7 +857,7 @@ func (c *cc) convertTablesOrSubquery(n []parser.ITable_or_subqueryContext) []ast
853857
},
854858
Funcname: &ast.List{
855859
Items: []ast.Node{
856-
NewIdentifer(rel),
860+
NewIdentifier(rel),
857861
},
858862
},
859863
Args: &ast.List{
@@ -965,7 +969,7 @@ func (c *cc) convertCastExpr(n *parser.Expr_castContext) ast.Node {
965969
TypeName: &ast.TypeName{
966970
Name: name,
967971
Names: &ast.List{Items: []ast.Node{
968-
NewIdentifer(name),
972+
NewIdentifier(name),
969973
}},
970974
ArrayBounds: &ast.List{},
971975
},

0 commit comments

Comments
 (0)