From: Mike Bayer Date: Wed, 24 Mar 2010 00:41:40 +0000 (-0400) Subject: - Fixed bug introduced in 0.6beta2 where column labels would X-Git-Tag: rel_0_6beta3~12^2~14^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1321db6473a45517bbb86203e9cbdd4c02fa8ac0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug introduced in 0.6beta2 where column labels would render inside of column expressions already assigned a label. [ticket:1747] --- diff --git a/CHANGES b/CHANGES index 1cb4625d0e..d344245812 100644 --- 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 ======== diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 4e9175ae86..75b3f79f06 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -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): diff --git a/test/orm/test_query.py b/test/orm/test_query.py index 0ca196a56e..cf64bac848 100644 --- a/test/orm/test_query.py +++ b/test/orm/test_query.py @@ -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): diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index d6a3804be4..f567d4a5ab 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -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")