Skip to content

Fix named_parameters when string literals contain comment markers#2783

Open
JSap0914 wants to merge 1 commit into
simonw:mainfrom
JSap0914:fix-named-parameters-string-literals
Open

Fix named_parameters when string literals contain comment markers#2783
JSap0914 wants to merge 1 commit into
simonw:mainfrom
JSap0914:fix-named-parameters-string-literals

Conversation

@JSap0914

Copy link
Copy Markdown

Bug

named_parameters() (and therefore derive_named_parameters()) stripped SQL comments before string literals, in separate regex passes. A string literal that contains a comment marker is then mistaken for the start of a comment.

For example:

select * from t where note = '-- TODO' and id = :id

returned [] instead of ["id"] — the -- inside the '-- TODO' string literal was treated as the start of a line comment, which swallowed the rest of the line and hid the :id parameter. In the query UI this means the parameter input form silently loses fields for any query whose string literals contain -- or /* */.

Fix

Match comments and string literals in a single left-to-right pass so that whichever construct starts first wins — this is how SQL is actually tokenized, so a -- inside a string is part of the string, and a quote inside a comment is part of the comment. This also keeps the existing behaviour of ignoring parameters that appear inside comments or string literals.

Verification

uv run pytest tests/test_utils.py -k test_named_parameters

Result: 22 passed. New parametrized cases cover string literals containing -- / /* */, parameters inside comments, and parameters inside string literals. The full tests/test_utils.py (213 tests) and the query-page HTML tests still pass.

named_parameters stripped SQL comments before string literals in
separate passes. A string literal such as '-- TODO' would be treated
as the start of a line comment, swallowing the rest of the line and
hiding any named parameters that followed it. For example:

    select * from t where note = '-- TODO' and id = :id

returned [] instead of ['id'], so the query parameter input form
would be missing the :id field.

Match comments and string literals in a single left-to-right pass so
that whichever construct starts first wins, matching how SQL is
actually tokenized.
Copilot AI review requested due to automatic review settings June 16, 2026 06:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants