Skip to content

In: skip ? inside SQL comments and string literals#984

Open
c-tonneslan wants to merge 1 commit into
jmoiron:masterfrom
c-tonneslan:fix/in-skip-sql-comments
Open

In: skip ? inside SQL comments and string literals#984
c-tonneslan wants to merge 1 commit into
jmoiron:masterfrom
c-tonneslan:fix/in-skip-sql-comments

Conversation

@c-tonneslan
Copy link
Copy Markdown

Fixes #961.

sqlx.In() scans for ? bind variables using strings.IndexByte, which has no concept of SQL syntax. A ? inside a -- line comment, a /* block comment */, or a single-quoted string literal gets counted as a placeholder, causing a "number of bindVars exceeds arguments" error even when the query is correct.

The fix adds a nextBindVar helper that walks the string while tracking comment and string state, returning only positions where ? is an actual bind variable. The In function uses it in place of IndexByte.

Added tests covering line comments, block comments, string literals with ?, and a combined case to make sure real bind variables still expand correctly.

To reproduce before this fix:

// Errored with "number of bindVars exceeds arguments"
q, args, err := sqlx.In("SELECT * -- filter by id?\nFROM foo WHERE id IN (?)", []int{1, 2, 3})

sqlx.In() was using strings.IndexByte to find ? placeholders, which
has no concept of SQL syntax. A ? inside a -- line comment, a /* block
comment */, or a single-quoted string literal would be counted as a
bind variable, causing "number of bindVars exceeds arguments" errors
even when the query was correct.

Fix adds a nextBindVar helper that walks the query string while tracking
comment and string state, returning only the index of a ? that's actually
a bind variable. The In function now uses it in place of IndexByte.

Closes jmoiron#961
c-tonneslan added a commit to c-tonneslan/portfolio that referenced this pull request May 12, 2026
c-tonneslan added a commit to c-tonneslan/c-tonneslan that referenced this pull request May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline comment with a question mark ? results in a "number of bindVars exceeds arguments" error within sqlx.In

1 participant