Commits

Mike Bayer committed 93ecec3 Merge

merge tip

Comments (0)

Files changed (4)

     and act the same way as Query.join("propname", from_joinpoint=True)
     in that regard.
     
+- sql
+   - Fixed bug introduced in 0.6beta2 where column labels would 
+     render inside of column expressions already assigned a label.
+     [ticket:1747]
+     
 0.6beta2
 ========
 

lib/sqlalchemy/sql/compiler.py

     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
 
                         (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):

test/orm/test_query.py

         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):

test/sql/test_compiler.py

         )
 
         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")