From 296c84313ab29bf9599634f38caaf7dd092e4e23 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 17 Oct 2020 11:39:56 -0400 Subject: Ensure escaping of percent signs in columns, parameters Improved support for column names that contain percent signs in the string, including repaired issues involving anoymous labels that also embedded a column name with a percent sign in it, as well as re-established support for bound parameter names with percent signs embedded on the psycopg2 dialect, using a late-escaping process similar to that used by the cx_Oracle dialect. * Added new constructor for _anonymous_label() that ensures incoming string tokens based on column or table names will have percent signs escaped; abstracts away the format of the label. * generalized cx_Oracle's quoted_bind_names facility into the compiler itself, and leveraged this for the psycopg2 dialect's issue with percent signs in names as well. the parameter substitution is now integrated with compiler.construct_parameters() as well as the recently reworked set_input_sizes(), reducing verbosity in the cx_Oracle dialect. Fixes: #5653 Change-Id: Ia2ad13ea68b4b0558d410026e5a33f5cb3fbab2c --- lib/sqlalchemy/dialects/postgresql/psycopg2.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql/psycopg2.py') diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 2446604ba..72c36b4a8 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -644,7 +644,14 @@ class PGExecutionContext_psycopg2(PGExecutionContext): class PGCompiler_psycopg2(PGCompiler): - pass + def bindparam_string(self, name, **kw): + if "%" in name and not kw.get("post_compile", False): + # psycopg2 will not allow a percent sign in a + # pyformat parameter name even if it is doubled + kw["escaped_from"] = name + name = name.replace("%", "P") + + return PGCompiler.bindparam_string(self, name, **kw) class PGIdentifierPreparer_psycopg2(PGIdentifierPreparer): -- cgit v1.2.1