Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "analyze",
"args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"],
"contexts": ["base"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: ByIDs :many
SELECT id, name FROM items WHERE id IN (sqlc.slice(ids));
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE items (id INTEGER NOT NULL, name VARCHAR NOT NULL);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: error parsing queries: query.sql:2:41: sqlc.slice is not supported by this engine; bind a list parameter instead
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"contexts": ["base"]}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/duckdb_named_params/duckdb/go/db.go

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

10 changes: 10 additions & 0 deletions internal/endtoend/testdata/duckdb_named_params/duckdb/go/models.go

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

124 changes: 124 additions & 0 deletions internal/endtoend/testdata/duckdb_named_params/duckdb/go/query.sql.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/duckdb_named_params/duckdb/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- name: ByID :many
SELECT id, name FROM items WHERE id = sqlc.arg(want_id);

-- name: ByNullableID :many
SELECT id, name FROM items WHERE id = sqlc.narg(want_id);

-- name: ByAt :many
SELECT id, name FROM items WHERE id = @want_id;

-- name: Range :many
SELECT id, name FROM items WHERE id > sqlc.arg(min_id) AND id < sqlc.arg(max_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE items (id INTEGER NOT NULL, name VARCHAR NOT NULL);
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/duckdb_named_params/duckdb/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "duckdb"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "database/sql"
1 change: 1 addition & 0 deletions internal/sql/preprocess/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Native placeholders by dialect:
| postgresql | `$1` | sqlc syntax |
| mysql | `?` | user variable, left alone |
| sqlite | `?1` | sqlc syntax |
| duckdb | `$1` | sqlc syntax; `sqlc.slice` is rejected |

GoogleSQL and ClickHouse are deliberately absent. They handle their own
parameter syntax, so `File` returns their source unchanged and sqlc syntax is
Expand Down
15 changes: 15 additions & 0 deletions internal/sql/preprocess/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ type Dialect struct {
// identifiers. It decides the case of a parameter named by a bare
// reference, e.g. sqlc.arg(FooBar).
FoldIdentifier bool

// NoSlice reports whether sqlc.slice is rejected. A dialect that numbers
// its parameters ($1) binds a list as one value, and the Go code that
// sqlc.slice generates only expands "?" placeholders, so it would bind
// the first element and return wrong rows without an error.
NoSlice bool
}

var dialects = map[config.Engine]Dialect{
Expand Down Expand Up @@ -88,6 +94,15 @@ var dialects = map[config.Engine]Dialect{
Backtick: true,
Backslash: true,
},
// DuckDB binds with $1 like PostgreSQL and accepts $$ ... $$ strings. A
// bare ? is left to the engine, as it was before DuckDB had an entry.
config.EngineDuckDB: {
Style: StyleDollar,
AtSign: true,
DollarQuote: true,
DollarNumber: true,
NoSlice: true,
},
// ClickHouse binds with an unnumbered ?, like MySQL, and its identifiers
// keep their case.
config.EngineClickHouse: {
Expand Down
7 changes: 7 additions & 0 deletions internal/sql/preprocess/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func (l *lexer) sqlcCall(start, afterSqlc int) (occurrence, int, bool) {
case "narg":
occ.kind = kindNarg
case "slice":
if l.d.NoSlice {
occ.err = &sqlerr.Error{
Message: "sqlc.slice is not supported by this engine; bind a list parameter instead",
Location: start,
}
return occ, occ.end, true
}
occ.kind = kindSlice
case "embed":
occ.kind = kindEmbed
Expand Down
Loading