summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-09-04 18:46:53 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-09-05 09:53:37 -0400
commitd7aa017d83b416187b54ad38400475fd86d80671 (patch)
tree61e864530882f36733c12dadcb135af5a9d7d676 /lib/sqlalchemy
parentc3dcdbd21de44b23e527f5580c318e47ea6930a7 (diff)
downloadsqlalchemy-d7aa017d83b416187b54ad38400475fd86d80671.tar.gz
Strip special chars in anonymized bind names
Characters that interfere with "pyformat" or "named" formats in bound parameters, namely ``%, (, )`` and the space character, as well as a few other typically undesirable characters, are stripped early for a :func:`.bindparam` that is using an anonymized name, which is typically generated automatically from a named column which itself includes these characters in its name and does not use a ``.key``, so that they do not interfere either with the SQLAlchemy compiler's use of string formatting or with the driver-level parsing of the parameter, both of which could be demonstrated before the fix. The change only applies to anonymized parameter names that are generated and consumed internally, not end-user defined names, so the change should have no impact on any existing code. Applies in particular to the psycopg2 driver which does not otherwise quote special parameter names, but also strips leading underscores to suit Oracle (but not yet leading numbers, as some anon parameters are currently entirely numeric/underscore based); Oracle in any case continues to quote parameter names that include special characters. Fixes: #4837 Change-Id: I21cb654c3e4ef786114160b8b4295242720bf3f9
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/sql/elements.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index bc6f51b8c..3caa380ee 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -1227,7 +1227,13 @@ class BindParameter(roles.InElementRole, ColumnElement):
if unique:
self.key = _anonymous_label(
- "%%(%d %s)s" % (id(self), key or "param")
+ "%%(%d %s)s"
+ % (
+ id(self),
+ re.sub(r"[%\(\) \$]+", "_", key).strip("_")
+ if key is not None
+ else "param",
+ )
)
else:
self.key = key or _anonymous_label("%%(%d param)s" % id(self))