Repository navigation
Conversation
…amed PostgresDatabaseAdapter.execute kept every bare %s, %b and %t as a positional placeholder, so SQL such as LIKE '%status%' with named parameters failed in psycopg with "positional and named placeholders cannot be mixed". When the parameters are a mapping, keep only valid %(name)s placeholders and escape every other percent sign.
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.
Hi! Thanks for maintaining Chronos.
This fixes the named-parameter case from #7. With positional (
?) parameters a literal%s,%bor%tinside a string is still read as a placeholder, so the issue's expected behaviour is met only for named parameters.Currently
PostgresDatabaseAdapter.executetranslates:nameto%(name)sand then calls_escape_pyformat_percents, which keeps every bare%s,%bor%tas a positional placeholder. So with named parameters valid SQL such asLIKE '%status%'fails in psycopg with "positional and named placeholders cannot be mixed". Now_escape_pyformat_percentstakesnamed, whichexecutesets when the parameters are a mapping. In that mode only%(name)swith a valid identifier is kept and every other percent sign is escaped, as in the OKG workaround from the issue. Positional parameters andexecutemanybehave as before.For the positional case I have a prototype that escapes percent signs inside quoted literals, identifiers, comments and dollar-quoted strings. Please let me know if you want it in this PR.
Two new tests in
tests/test_branching.py(the query above on PostgreSQL 16 and the escaping function) fail before the change and pass after. The 5 test files around it give the same results as the base commit, apart from one test that fails intermittently on the base too and the 3test_postgres_orpheus_*tests that fail on both.Relates to #7.
Thank you!