| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
…re ? When a SQLite query uses both sqlc.arg() named parameters and bare ? placeholders, the generated SQL has numbered ?N for named params but leaves bare ? unnumbered. SQLite's auto-numbering for bare ? then conflicts with the explicit ?N values, silently binding arguments to wrong columns. Fix by numbering all placeholders sequentially in text order when the mixed case is detected, ensuring positional argument passing matches the generated ?N values.
|
can i please get a review for this? |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
fix(sqlite): correct parameter binding when mixing sqlc.arg() with bare ?
What happened?
When a SQLite query mixes sqlc.arg() named parameters with bare positional ? placeholders, sqlc generates SQL with mismatched parameter numbering that silently binds values to wrong columns.
Given this query:
Before (broken): sqlc generates SET name = ?2 ... WHERE org_id = ? AND name = ?3 — the bare ? gets auto-assigned index 1 by SQLite, but the Go function passes NewName as the 1st positional arg. Result: SET name = OrgID, WHERE org_id = NewName.
After (fixed): all placeholders are numbered in text order: SET name = ?1 ... WHERE org_id = ?2 AND name = ?3, matching the positional argument order.
Root cause
Commit c2dcd56 ("Allow for mixed parameters types ($1 or ?) and sqlc.arg()") added mixed-param support for PostgreSQL and MySQL but did not cover SQLite. PostgreSQL doesn't have this issue because $N is inherently numbered and args are sorted by number. MySQL doesn't have it because all params emit bare ? (no numbering, purely positional). SQLite is the only engine that generates numbered ?N for named params but also accepts unnumbered ? — and those two styles have incompatible binding semantics when mixed in the same query.
The NamedParameters rewriter assigns numbered ?N to sqlc.arg() params while skipping positions pre-reserved for bare ? — but it never renumbers the bare ? itself. Since SQLite's auto-numbering for bare ? is independent of explicit ?N, the two schemes conflict.
Fix
When SQLite has both named params and bare ? in the same query:
This ensures the numbered placeholders in the output SQL match the positional argument order in the generated Go function.
Test plan