Skip to content

fix(branching): escape literal percent patterns when parameters are named - #17

Open
konstde00 wants to merge 1 commit into
mitdbg:mainfrom
konstde00:fix/7-named-params-literal-percent
Open

konstde00 wants to merge 1 commit into
mitdbg:mainfrom
konstde00:fix/7-named-params-literal-percent

Conversation

@konstde00

Copy link
Copy Markdown
Contributor

Hi! Thanks for maintaining Chronos.

This fixes the named-parameter case from #7. With positional (?) parameters a literal %s, %b or %t inside a string is still read as a placeholder, so the issue's expected behaviour is met only for named parameters.

Currently PostgresDatabaseAdapter.execute translates :name to %(name)s and then calls _escape_pyformat_percents, which keeps every bare %s, %b or %t as a positional placeholder. So with named parameters valid SQL such as LIKE '%status%' fails in psycopg with "positional and named placeholders cannot be mixed". Now _escape_pyformat_percents takes named, which execute sets when the parameters are a mapping. In that mode only %(name)s with a valid identifier is kept and every other percent sign is escaped, as in the OKG workaround from the issue. Positional parameters and executemany behave 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 3 test_postgres_orpheus_* tests that fail on both.

Relates to #7.

Thank you!

…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.
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.

1 participant