summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-09-02 10:49:46 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-09-02 10:49:46 -0400
commitc0af2ee6c992cf6574d8eea052d7cd3b0fabd337 (patch)
tree200b9ee5795847754481e1eec52da24c6c6b46f8 /lib/sqlalchemy/sql
parentb621d232519bd84321853087b5ab21b3d8ef1dd9 (diff)
downloadsqlalchemy-c0af2ee6c992cf6574d8eea052d7cd3b0fabd337.tar.gz
- add logic to compiler such that if stack is empty, we just
stringify a _label_reference() as is. - add .key to _label_reference(), so that when _make_proxy() is called, we don't call str() on it anyway. - add a test to exercise Query's behavior of adding all the order_by expressions to the columns list of the select, assert that things work out when we have a _label_reference there, that it gets sucked into the columns list and spit out on the other side, it's referred to appropriately, etc. _label_reference() could theoretically be resolved at the point we iterate _raw_columns() but it's better to just let things work as they already do (except nicer, since we get "tablename.colname" instead of just "somename" in the columns list) so that we aren't adding a ton of overhead to _columns_plus_names in the common case.
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/compiler.py8
-rw-r--r--lib/sqlalchemy/sql/elements.py6
-rw-r--r--lib/sqlalchemy/sql/selectable.py1
3 files changed, 13 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index e4597dcd8..23e5456a7 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -495,6 +495,12 @@ class SQLCompiler(Compiled):
return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
def visit_label_reference(self, element, **kwargs):
+ if not self.stack:
+ # compiling the element outside of the context of a SELECT
+ return self.process(
+ element._text_clause
+ )
+
selectable = self.stack[-1]['selectable']
try:
col = selectable._inner_column_dict[element.text]
@@ -504,7 +510,7 @@ class SQLCompiler(Compiled):
"Can't resolve label reference %r; converting to text()",
util.ellipses_string(element.text))
return self.process(
- elements.TextClause._create_text(element.text)
+ element._text_clause
)
else:
kwargs['render_label_as_label'] = col
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index 0ea05fa0e..984cfe0ee 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -2292,7 +2292,11 @@ class _label_reference(ColumnElement):
__visit_name__ = 'label_reference'
def __init__(self, text):
- self.text = text
+ self.text = self.key = text
+
+ @util.memoized_property
+ def _text_clause(self):
+ return TextClause._create_text(self.text)
class UnaryExpression(ColumnElement):
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index cf2c213d2..a49493995 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -2976,6 +2976,7 @@ class Select(HasPrefixes, GenerativeSelect):
def name_for_col(c):
if c._columns_clause_label is None:
return (None, c)
+
name = c._columns_clause_label
if name in names:
name = c.anon_label