From 0e1049600dc88f0f52ff23493ca3aff83a87818f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 13 Feb 2021 17:13:51 -0500 Subject: expand and further generalize bound parameter translate Continued with the improvement made as part of :ticket:`5653` to further support bound parameter names, including those generated against column names, for names that include colons, parenthesis, and question marks, as well as improved test support, so that bound parameter names even if they are auto-derived from column names should have no problem including for parenthesis in psycopg2's "pyformat" style. As part of this change, the format used by the asyncpg DBAPI adapter (which is local to SQLAlchemy's asyncpg diaelct) has been changed from using "qmark" paramstyle to "format", as there is a standard and internally supported SQL string escaping style for names that use percent signs with "format" style (i.e. to double percent signs), as opposed to names that use question marks with "qmark" style (where an escaping system is not defined by pep-249 or Python). Fixes: #5941 Change-Id: Id86f5af81903d7063a8e3505e60df56490f85358 --- lib/sqlalchemy/sql/compiler.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index f22e8614b..763b4cabb 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -166,6 +166,11 @@ BIND_TEMPLATES = { "named": ":%(name)s", } +BIND_TRANSLATE = { + "pyformat": re.compile(r"[%\(\)]"), + "named": re.compile(r"[\:]"), +} +_BIND_TRANSLATE_CHARS = {"%": "P", "(": "A", ")": "Z", ":": "C"} OPERATORS = { # binary @@ -765,6 +770,7 @@ class SQLCompiler(Compiled): self.positiontup = [] self._numeric_binds = dialect.paramstyle == "numeric" self.bindtemplate = BIND_TEMPLATES[dialect.paramstyle] + self._bind_translate = BIND_TRANSLATE.get(dialect.paramstyle, None) self.ctes = None @@ -2417,11 +2423,23 @@ class SQLCompiler(Compiled): escaped_from=None, **kw ): + if self.positional: if positional_names is not None: positional_names.append(name) else: self.positiontup.append(name) + elif not post_compile and not escaped_from: + tr_reg = self._bind_translate + if tr_reg.search(name): + # i'd rather use translate() here but I can't get it to work + # in all cases under Python 2, not worth it right now + new_name = tr_reg.sub( + lambda m: _BIND_TRANSLATE_CHARS[m.group(0)], + name, + ) + escaped_from = name + name = new_name if escaped_from: if not self.escaped_bind_names: -- cgit v1.2.1