Fix named_parameters when string literals contain comment markers (#2783)

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.

Co-authored-by: JSap0914 <JSap0914@users.noreply.github.com>
This commit is contained in:
JSap0914 2026-07-08 06:23:31 +09:00 committed by GitHub
commit bf3e277c98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 8 deletions

View file

@ -756,6 +756,16 @@ def test_parse_metadata(content, expected):
("select 1 + :one + :two", ["one", "two"]),
("select 'bob' || '0:00' || :cat", ["cat"]),
("select this is invalid :one, :two, :three", ["one", "two", "three"]),
# A string literal containing a comment marker should not hide
# parameters that come after it
("select * from t where note = '-- TODO' and id = :id", ["id"]),
("select '--' || :y", ["y"]),
("select * from t where note = '/* x */' and id = :id", ["id"]),
# Parameters that live inside a comment should be ignored
("select :x -- and :ignored", ["x"]),
("select :x /* and :ignored */ from t", ["x"]),
# Parameters inside a string literal should be ignored
("select ':ignored' || :real", ["real"]),
),
)
@pytest.mark.parametrize("use_async_version", (False, True))