diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-06-15 18:06:50 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-06-15 20:38:39 -0400 |
commit | 009acc95b8804b5b62fbd43c6fdd61d6fd407ef7 (patch) | |
tree | b6e8d799be87d73b3f981c7a876b8c22a18809d4 /lib/sqlalchemy/sql/elements.py | |
parent | de08740d7c21fa9dcef453bfd07a3defa428e88f (diff) | |
download | sqlalchemy-009acc95b8804b5b62fbd43c6fdd61d6fd407ef7.tar.gz |
Turn off the is_literal flag when proxying literal_column() to Label
Fixed a series of quoting issues which all stemmed from the concept of the
:func:`.literal_column` construct, which when being "proxied" through a
subquery to be referred towards by a label that matches its text, the label
would not have quoting rules applied to it, even if the string in the
:class:`.Label` were set up as a :class:`.quoted_name` construct. Not
applying quoting to the text of the :class:`.Label` is a bug because this
text is strictly a SQL identifier name and not a SQL expression, and the
string should not have quotes embedded into it already unlike the
:func:`.literal_column` which it may be applied towards. The existing
behavior of a non-labeled :func:`.literal_column` being propagated as is on
the outside of a subquery is maintained in order to help with manual
quoting schemes, although it's not clear if valid SQL can be generated for
such a construct in any case.
Fixes: #4730
Change-Id: I300941f27872fc4298c74a1d1ed65aef1a5cdd82
Diffstat (limited to 'lib/sqlalchemy/sql/elements.py')
-rw-r--r-- | lib/sqlalchemy/sql/elements.py | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index a333303ec..cc57e58e5 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -3895,7 +3895,9 @@ class Label(roles.LabeledColumnExprRole, ColumnElement): def _make_proxy(self, selectable, name=None, **kw): e = self.element._make_proxy( - selectable, name=name if name else self.name + selectable, + name=name if name else self.name, + disallow_is_literal=True, ) e._proxies.append(self) if self._type is not None: @@ -4029,7 +4031,6 @@ class ColumnClause(roles.LabeledColumnExprRole, Immutable, ColumnElement): :ref:`sqlexpression_literal_column` """ - self.key = self.name = text self.table = _selectable self.type = type_api.to_instance(type_) @@ -4158,11 +4159,25 @@ class ColumnClause(roles.LabeledColumnExprRole, Immutable, ColumnElement): name=None, attach=True, name_is_truncatable=False, + disallow_is_literal=False, **kw ): - # propagate the "is_literal" flag only if we are keeping our name, - # otherwise its considered to be a label - is_literal = self.is_literal and (name is None or name == self.name) + # the "is_literal" flag normally should never be propagated; a proxied + # column is always a SQL identifier and never the actual expression + # being evaluated. however, there is a case where the "is_literal" flag + # might be used to allow the given identifier to have a fixed quoting + # pattern already, so maintain the flag for the proxy unless a + # :class:`.Label` object is creating the proxy. See [ticket:4730]. + is_literal = ( + not disallow_is_literal + and self.is_literal + and ( + # note this does not accommodate for quoted_name differences + # right now + name is None + or name == self.name + ) + ) c = self._constructor( coercions.expect(roles.TruncatedLabelRole, name or self.name) if name_is_truncatable |