In: skip ? inside SQL comments and string literals#984
Open
c-tonneslan wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #961.
sqlx.In()scans for?bind variables usingstrings.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
nextBindVarhelper that walks the string while tracking comment and string state, returning only positions where?is an actual bind variable. TheInfunction uses it in place ofIndexByte.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: