Skip to content

Commit de4dbfe

Browse files
committed
feat: changed to interpret functions starting with "sqlc_" as "sqlc." in SQLite
because the "sqlc.arg" function causes a syntax error in SQLite.
1 parent 2637260 commit de4dbfe

File tree

6 files changed

+136
-2
lines changed

6 files changed

+136
-2
lines changed

internal/endtoend/testdata/sqlc_arg/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/sqlc_arg/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/sqlc_arg/sqlite/go/query.sql.go

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE foo (name text not null);
2+
3+
/* name: FuncParamIdent :many */
4+
SELECT name FROM foo WHERE name = sqlc_arg(slug);
5+
6+
/* name: FuncParamString :many */
7+
SELECT name FROM foo WHERE name = sqlc_arg('slug');
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+
"engine": "sqlite",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/sqlite/convert.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,16 @@ func (c *cc) convertFuncContext(n *parser.Expr_functionContext) ast.Node {
210210
Args: args,
211211
}
212212
} else {
213+
schema := ""
214+
const sqlcPrefix = "sqlc_"
215+
if strings.HasPrefix(funcName, sqlcPrefix) {
216+
schema = "sqlc"
217+
funcName = funcName[len(sqlcPrefix):]
218+
}
213219
return &ast.FuncCall{
214220
Func: &ast.FuncName{
215-
Name: funcName,
221+
Schema: schema,
222+
Name: funcName,
216223
},
217224
Funcname: &ast.List{
218225
Items: []ast.Node{
@@ -511,8 +518,10 @@ func (c *cc) convertLiteral(n *parser.Expr_literalContext) ast.Node {
511518
}
512519

513520
if literal.STRING_LITERAL() != nil {
521+
text := literal.GetText()
522+
unquoted := text[1 : len(text)-1]
514523
return &ast.A_Const{
515-
Val: &ast.String{Str: literal.GetText()},
524+
Val: &ast.String{Str: unquoted},
516525
}
517526
}
518527

0 commit comments

Comments
 (0)