Skip to content

fix(sqlite): disable positional indexing when sqlc.slice is present#4330

Open
noxymon wants to merge 2 commits intosqlc-dev:mainfrom
noxymon:fix/named-parameters-and-slices
Open

fix(sqlite): disable positional indexing when sqlc.slice is present#4330
noxymon wants to merge 2 commits intosqlc-dev:mainfrom
noxymon:fix/named-parameters-and-slices

Conversation

@noxymon
Copy link

@noxymon noxymon commented Mar 7, 2026

hopefully fixes #4213

Problem:

In SQLite, sqlc uses positional parameters (e.g., ?1, ?2) to support named parameter reuse. However, sqlc.slice expands at runtime into a variable number of placeholders via strings.Replace. If a named parameter follows a slice, its compile-time
index (e.g., ?2) becomes invalid at runtime because the slice expansion shifted the actual argument positions in the underlying driver call. This leads to runtime data corruption as parameters pick up the wrong values.

Solution:

This PR modifies the SQLite rewriter to detect if a sqlc.slice is present in the query. If a slice is detected, named parameter support (positional indexing) is disabled for that specific query. This forces the rewriter to use simple ? placeholders
and the Go generator to append every parameter occurrence to the queryParams slice. This approach is inherently safe against dynamic placeholder expansion and matches the proven behavior of the MySQL generator.

Verification:

  • Reproduction: Created internal/endtoend/testdata/issue_4213_repro to confirm the bug.
  • Regression: Verified that named parameter reuse still works (via duplication in the arg list) in internal/endtoend/testdata/issue_4213_reuse.
  • Existing Tests: Updated sqlc_slice and sqlc_slice_prepared end-to-end tests for SQLite which were previously expecting the broken positional behavior.
  • Test Suite: All SQLite end-to-end tests are passing.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlc generator bug in sqlc-go when using named parameters and slices

1 participant