Skip to content

Commit ee40f72

Browse files
committed
feat(sqlite): support numbered parameter
1 parent dfa42b5 commit ee40f72

File tree

21 files changed

+1147
-1180
lines changed

21 files changed

+1147
-1180
lines changed

examples/booktest/sqlite/query.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ RETURNING *;
5151

5252
/* name: UpdateBook :exec */
5353
UPDATE books
54-
SET title = ?, tag = ?
55-
WHERE book_id = ?;
54+
SET title = ?1, tag = ?2
55+
WHERE book_id = ?3;
5656

5757
/* name: UpdateBookISBN :exec */
5858
UPDATE books
59-
SET title = ?, tag = ?, isbn = ?
60-
WHERE book_id = ?;
59+
SET title = ?1, tag = ?2, isbn = ?4
60+
WHERE book_id = ?3;
6161

6262
/* name: DeleteAuthorBeforeYear :exec */
6363
DELETE FROM books

examples/booktest/sqlite/query.sql.go

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

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

internal/endtoend/testdata/sqlc_arg/postgresql/stdlib/go/query.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE TABLE foo (name text not null);
22

33
-- name: FuncParamIdent :many
4-
SELECT name FROM foo WHERE name = sqlc.arg(slug);
4+
SELECT name FROM foo WHERE name = sqlc.arg(slug) and name = sqlc.arg(slug);
55

66
-- name: FuncParamString :many
77
SELECT name FROM foo WHERE name = sqlc.arg('slug');

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

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

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

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

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

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

internal/engine/sqlite/convert.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,26 +588,30 @@ func (c *cc) convertBinaryNode(n *parser.Expr_binaryContext) ast.Node {
588588
}
589589

590590
func (c *cc) convertParam(n *parser.Expr_bindContext) ast.Node {
591-
if n.BIND_PARAMETER() != nil {
591+
if n.NUMBERED_BIND_PARAMETER() != nil {
592592
// Parameter numbers start at one
593593
c.paramCount += 1
594+
595+
text := n.GetText()
596+
number := c.paramCount
597+
if len(text) > 1 {
598+
number, _ = strconv.Atoi(text[1:])
599+
}
594600
return &ast.ParamRef{
595-
Number: c.paramCount,
601+
Number: number,
596602
Location: n.GetStart().GetStart(),
603+
Dollar: len(text) > 1,
597604
}
598605
}
599-
return todo(n)
600-
}
601606

602-
func (c *cc) convertNamedParam(n *parser.Expr_named_bindContext) ast.Node {
603607
if n.NAMED_BIND_PARAMETER() != nil {
604-
c.paramCount += 1
605608
return &ast.A_Expr{
606609
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "@"}}},
607610
Rexpr: &ast.String{Str: n.GetText()[1:]},
608611
Location: n.GetStart().GetStart(),
609612
}
610613
}
614+
611615
return todo(n)
612616
}
613617

@@ -894,9 +898,6 @@ func (c *cc) convert(node node) ast.Node {
894898
case *parser.Expr_bindContext:
895899
return c.convertParam(n)
896900

897-
case *parser.Expr_named_bindContext:
898-
return c.convertNamedParam(n)
899-
900901
case *parser.Expr_literalContext:
901902
return c.convertLiteral(n)
902903

internal/engine/sqlite/parser/SQLiteLexer.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ IDENTIFIER:
224224

225225
NUMERIC_LITERAL: ((DIGIT+ ('.' DIGIT*)?) | ('.' DIGIT+)) (E [-+]? DIGIT+)? | '0x' HEX_DIGIT+;
226226

227-
BIND_PARAMETER: '?' DIGIT* | [:$] IDENTIFIER;
227+
NUMBERED_BIND_PARAMETER: '?' DIGIT*;
228228

229-
NAMED_BIND_PARAMETER: '@' IDENTIFIER;
229+
NAMED_BIND_PARAMETER: [:@$] IDENTIFIER;
230230

231231
STRING_LITERAL: '\'' ( ~'\'' | '\'\'')* '\'';
232232

internal/engine/sqlite/parser/SQLiteLexer.interp

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

internal/engine/sqlite/parser/SQLiteLexer.tokens

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ DO_=184
185185
NOTHING_=185
186186
IDENTIFIER=186
187187
NUMERIC_LITERAL=187
188-
BIND_PARAMETER=188
188+
NUMBERED_BIND_PARAMETER=188
189189
NAMED_BIND_PARAMETER=189
190190
STRING_LITERAL=190
191191
BLOB_LITERAL=191

internal/engine/sqlite/parser/SQLiteParser.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ drop_stmt:
272272
*/
273273
expr:
274274
literal_value #expr_literal
275-
| BIND_PARAMETER #expr_bind
276-
| NAMED_BIND_PARAMETER #expr_named_bind
275+
| NUMBERED_BIND_PARAMETER #expr_bind
276+
| NAMED_BIND_PARAMETER #expr_bind
277277
| ((schema_name DOT)? table_name DOT)? column_name #expr_qualified_column_name
278278
| unary_operator expr #expr_unary
279279
| expr PIPE2 expr #expr_binary

internal/engine/sqlite/parser/SQLiteParser.interp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ DO_
385385
NOTHING_
386386
IDENTIFIER
387387
NUMERIC_LITERAL
388-
BIND_PARAMETER
388+
NUMBERED_BIND_PARAMETER
389389
NAMED_BIND_PARAMETER
390390
STRING_LITERAL
391391
BLOB_LITERAL

internal/engine/sqlite/parser/SQLiteParser.tokens

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ DO_=184
185185
NOTHING_=185
186186
IDENTIFIER=186
187187
NUMERIC_LITERAL=187
188-
BIND_PARAMETER=188
188+
NUMBERED_BIND_PARAMETER=188
189189
NAMED_BIND_PARAMETER=189
190190
STRING_LITERAL=190
191191
BLOB_LITERAL=191

0 commit comments

Comments
 (0)