]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug introduced in 0.6beta2 where column labels would
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 24 Mar 2010 00:41:40 +0000 (20:41 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 24 Mar 2010 00:41:40 +0000 (20:41 -0400)
render inside of column expressions already assigned a label.
[ticket:1747]

CHANGES
lib/sqlalchemy/sql/compiler.py
test/orm/test_query.py
test/sql/test_compiler.py

diff --git a/CHANGES b/CHANGES
index 1cb4625d0e4c4eaec49a08bf6a36b05628de784c..d3442458125a80ca35b9cb0fab102194919fdca8 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -17,7 +17,12 @@ CHANGES
     would fail if the underlying table (but not the actual alias) 
     were referenced inside the subquery generated by 
     q.from_self() or q.select_from().
-    
+
+- sql
+   - Fixed bug introduced in 0.6beta2 where column labels would 
+     render inside of column expressions already assigned a label.
+     [ticket:1747]
+     
 0.6beta2
 ========
 
index 4e9175ae868f1a0a1bfaef0858d413d9b49c70bc..75b3f79f0670506eb68fe29878fc274e28f410b6 100644 (file)
@@ -305,11 +305,13 @@ class SQLCompiler(engine.Compiled):
     def visit_grouping(self, grouping, asfrom=False, **kwargs):
         return "(" + self.process(grouping.element, **kwargs) + ")"
 
-    def visit_label(self, label, result_map=None, within_columns_clause=False, **kw):
+    def visit_label(self, label, result_map=None, 
+                            within_label_clause=False, 
+                            within_columns_clause=False, **kw):
         # only render labels within the columns clause
         # or ORDER BY clause of a select.  dialect-specific compilers
         # can modify this behavior.
-        if within_columns_clause:
+        if within_columns_clause and not within_label_clause:
             labelname = isinstance(label.name, sql._generated_label) and \
                     self._truncated_identifier("colident", label.name) or label.name
 
@@ -318,13 +320,14 @@ class SQLCompiler(engine.Compiled):
                         (label.name, (label, label.element, labelname), label.element.type)
 
             return self.process(label.element, 
-                                    within_columns_clause=within_columns_clause, 
+                                    within_columns_clause=True,
+                                    within_label_clause=True, 
                                     **kw) + \
                         OPERATORS[operators.as_] + \
                         self.preparer.format_label(label, labelname)
         else:
             return self.process(label.element, 
-                                    within_columns_clause=within_columns_clause, 
+                                    within_columns_clause=False, 
                                     **kw)
             
     def visit_column(self, column, result_map=None, **kwargs):
index 0ca196a56ec89a6d6b6c94fa9dc543e361e9b6bf..cf64bac848e789bbe3163f8de03a6e6d2b1107a0 100644 (file)
@@ -563,6 +563,9 @@ class RawSelectTest(QueryTest, AssertsCompiledSQL):
         self.assert_compile(sess.query(x).filter(x==5).statement, 
             "SELECT lala(users.id) AS foo FROM users WHERE lala(users.id) = :param_1", dialect=default.DefaultDialect())
 
+        self.assert_compile(sess.query(func.sum(x).label('bar')).statement,  
+            "SELECT sum(lala(users.id)) AS bar FROM users", dialect=default.DefaultDialect()) 
+
 class ExpressionTest(QueryTest, AssertsCompiledSQL):
         
     def test_deferred_instances(self):
index d6a3804be478fd4bcc391c01bc98c16ef7ccf05e..f567d4a5abbaf545795849f0d186ce457c893a7f 100644 (file)
@@ -171,9 +171,14 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
         )
 
         self.assert_compile(
-            select([cast("data", Integer)], use_labels=True),      # this will work with plain Integer in 0.6
+            select([cast("data", Integer)], use_labels=True),
             "SELECT CAST(:param_1 AS INTEGER) AS anon_1"
         )
+        
+        self.assert_compile(
+            select([func.sum(func.lala(table1.c.myid).label('foo')).label('bar')]),
+            "SELECT sum(lala(mytable.myid)) AS bar FROM mytable"
+        )
     
     def test_paramstyles(self):
         stmt = text("select :foo, :bar, :bat from sometable")