Skip to content

Commit c42104e

Browse files
committed
fix(duckdb): rewrite sqlc.arg and @name to native placeholders
The preprocess dialect table had no entry for DuckDB, and an engine with no entry gets its source back unchanged. sqlc.arg(), sqlc.narg() and @name then reached the analyzer as function calls and column names. Add a DuckDB entry that uses $1 placeholders. Reject sqlc.slice for this dialect. With $1 placeholders the generated Go still calls strings.Replace on a "?" marker that the SQL does not contain, so it binds only the first element of the list and returns wrong rows without an error. A list parameter (list_contains or ANY) is the DuckDB way to do this. Adds the duckdb_named_params and analyze_slice_rejected cases.
1 parent 3ad8b20 commit c42104e

14 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: ByIDs :many
2+
SELECT id, name FROM items WHERE id IN (sqlc.slice(ids));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE items (id INTEGER NOT NULL, name VARCHAR NOT NULL);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Error: error parsing queries: query.sql:2:41: sqlc.slice is not supported by this engine; bind a list parameter instead
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"contexts": ["base"]}

‎internal/endtoend/testdata/duckdb_named_params/duckdb/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/duckdb_named_params/duckdb/go/models.go‎

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

‎internal/endtoend/testdata/duckdb_named_params/duckdb/go/query.sql.go‎

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: ByID :many
2+
SELECT id, name FROM items WHERE id = sqlc.arg(want_id);
3+
4+
-- name: ByNullableID :many
5+
SELECT id, name FROM items WHERE id = sqlc.narg(want_id);
6+
7+
-- name: ByAt :many
8+
SELECT id, name FROM items WHERE id = @want_id;
9+
10+
-- name: Range :many
11+
SELECT id, name FROM items WHERE id > sqlc.arg(min_id) AND id < sqlc.arg(max_id);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE items (id INTEGER NOT NULL, name VARCHAR NOT NULL);

0 commit comments

Comments
 (0)