summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-09-07 00:01:34 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-09-07 00:01:34 -0400
commit7950270cf2b12807acd7c330b11dae36e50c3a28 (patch)
treef1ec50aa6fc604d7a2dadf1b41aff73952a05dcc /lib/sqlalchemy/sql
parente80c7cc5c103788a4c7e1c479af2c37cd9c958b3 (diff)
downloadsqlalchemy-7950270cf2b12807acd7c330b11dae36e50c3a28.tar.gz
- enhance ClauseAdapter / ColumnAdapter to have new behaviors with labels.
The "anonymize label" logic is now generalized to ClauseAdapter, and takes place when the anonymize_labels flag is sent, taking effect for all .columns lookups as well as within traverse() calls against the label directly. - traverse() will also memoize what it gets in columns, so that calling upon traverse() / .columns against the same Label will produce the same anonymized label. This is so that AliasedClass produces the same anonymized label when it is accessed per-column (e.g. SomeAlias.some_column) as well as when it is applied to a Query, and within column loader strategies (e.g. query(SomeAlias)); the former uses traverse() while the latter uses .columns - AliasedClass now calls onto ColumnAdapter - Query also makes sure to use that same ColumnAdapter from the AliasedClass in all cases - update the logic from 0.9 in #1068 to make use of the same _label_resolve_dict we use for #2992, simplifying how that works and adding support for new scenarios that were pretty broken (see #3148, #3188)
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/compiler.py10
-rw-r--r--lib/sqlalchemy/sql/elements.py16
-rw-r--r--lib/sqlalchemy/sql/util.py28
3 files changed, 33 insertions, 21 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 4349c97f4..72dd11eaf 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -701,13 +701,7 @@ class SQLCompiler(Compiled):
# here; we can only add a label in the ORDER BY for an individual
# label expression in the columns clause.
- # TODO: we should see if we can bring _resolve_label
- # into this
-
-
- raw_col = set(l._order_by_label_element.name
- for l in order_by_select._raw_columns
- if l._order_by_label_element is not None)
+ raw_col = set(order_by_select._label_resolve_dict.keys())
return ", ".join(
s for s in
@@ -716,7 +710,7 @@ class SQLCompiler(Compiled):
self,
render_label_as_label=c._order_by_label_element if
c._order_by_label_element is not None and
- c._order_by_label_element.name in raw_col
+ c._order_by_label_element._label in raw_col
else None,
**kw)
for c in clauselist.clauses)
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index c8504f21f..ece6bce9e 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -688,6 +688,10 @@ class ColumnElement(operators.ColumnOperators, ClauseElement):
"""
+ _allow_label_resolve = True
+ """A flag that can be flipped to prevent a column from being resolvable
+ by string label name."""
+
_alt_names = ()
def self_group(self, against=None):
@@ -704,8 +708,6 @@ class ColumnElement(operators.ColumnOperators, ClauseElement):
else:
return super(ColumnElement, self)._negate()
- _allow_label_resolve = True
-
@util.memoized_property
def type(self):
return type_api.NULLTYPE
@@ -1248,6 +1250,8 @@ class TextClause(Executable, ClauseElement):
# interpreted in a column expression situation
key = _label = _resolve_label = None
+ _allow_label_resolve = False
+
def __init__(
self,
text,
@@ -2943,8 +2947,14 @@ class Label(ColumnElement):
def get_children(self, **kwargs):
return self.element,
- def _copy_internals(self, clone=_clone, **kw):
+ def _copy_internals(self, clone=_clone, anonymize_labels=False, **kw):
self.element = clone(self.element, **kw)
+ if anonymize_labels:
+ self.name = _anonymous_label(
+ '%%(%d %s)s' % (
+ id(self), getattr(self.element, 'name', 'anon'))
+ )
+ self.key = self._label = self._key_label = self.name
@property
def _from_objects(self):
diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py
index 47ab61fdd..f630f9e93 100644
--- a/lib/sqlalchemy/sql/util.py
+++ b/lib/sqlalchemy/sql/util.py
@@ -488,8 +488,10 @@ class ClauseAdapter(visitors.ReplacingCloningVisitor):
def __init__(self, selectable, equivalents=None,
include=None, exclude=None,
include_fn=None, exclude_fn=None,
- adapt_on_names=False):
- self.__traverse_options__ = {'stop_on': [selectable]}
+ adapt_on_names=False, anonymize_labels=False):
+ self.__traverse_options__ = {
+ 'stop_on': [selectable],
+ 'anonymize_labels': anonymize_labels}
self.selectable = selectable
if include:
assert not include_fn
@@ -549,9 +551,14 @@ class ColumnAdapter(ClauseAdapter):
def __init__(self, selectable, equivalents=None,
chain_to=None, include=None,
exclude=None, adapt_required=False,
- allow_label_resolve=True):
+ adapt_on_names=False,
+ allow_label_resolve=True,
+ anonymize_labels=False):
ClauseAdapter.__init__(self, selectable, equivalents,
- include, exclude)
+ include, exclude,
+ adapt_on_names=adapt_on_names,
+ anonymize_labels=anonymize_labels)
+
if chain_to:
self.chain(chain_to)
self.columns = util.populate_column_dict(self._locate_col)
@@ -567,7 +574,13 @@ class ColumnAdapter(ClauseAdapter):
ac.columns = util.populate_column_dict(ac._locate_col)
return ac
- adapt_clause = ClauseAdapter.traverse
+ def traverse(self, obj):
+ new_obj = ClauseAdapter.traverse(self, obj)
+ if new_obj is not obj:
+ self.columns[obj] = new_obj
+ return new_obj
+
+ adapt_clause = traverse
adapt_list = ClauseAdapter.copy_and_process
def _wrap(self, local, wrapped):
@@ -581,11 +594,6 @@ class ColumnAdapter(ClauseAdapter):
if c is None:
c = self.adapt_clause(col)
- # anonymize labels in case they have a hardcoded name
- # see test_eager_relations.py -> SubqueryTest.test_label_anonymizing
- if isinstance(c, Label):
- c = c.label(None)
-
# adapt_required used by eager loading to indicate that
# we don't trust a result row column that is not translated.
# this is to prevent a column from being interpreted as that