From 80722647af8b577f9026c695c55c5b4cd3d83bae Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 21 Jun 2012 16:49:29 -0400 Subject: [PATCH] - [bug] quoting is applied to the column names inside the WITH RECURSIVE clause of a common table expression according to the quoting rules for the originating Column. [ticket:2512] --- CHANGES | 6 ++++++ lib/sqlalchemy/sql/compiler.py | 7 +++++-- test/sql/test_compiler.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 0a8e90f1f7..6ab25ac748 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,12 @@ CHANGES 0.7.9 ===== - sql + - [bug] quoting is applied to the column names + inside the WITH RECURSIVE clause of a + common table expression according to the + quoting rules for the originating Column. + [ticket:2512] + - [bug] Fixed regression introduced in 0.7.6 whereby the FROM list of a SELECT statement could be incorrect in certain "clone+replace" diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index f34277a962..0ed4be1ba5 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -780,10 +780,13 @@ class SQLCompiler(engine.Compiled): col_source = cte.original.selects[0] else: assert False - recur_cols = [c.key for c in util.unique_list(col_source.inner_columns) + recur_cols = [c for c in + util.unique_list(col_source.inner_columns) if c is not None] - text += "(%s)" % (", ".join(recur_cols)) + text += "(%s)" % (", ".join( + self.preparer.format_column(ident) + for ident in recur_cols)) text += " AS \n" + \ cte.original._compiler_dispatch( self, asfrom=True, **kwargs diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index ca041ea9c2..f58b006dff 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -2490,6 +2490,20 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): "FROM regional_sales WHERE " "regional_sales.amount < :amount_2") + def test_cte_reserved_quote(self): + orders = table('orders', + column('order'), + ) + s = select([orders.c.order]).cte("regional_sales", recursive=True) + s = select([s.c.order]) + self.assert_compile(s, + 'WITH RECURSIVE regional_sales("order") AS ' + '(SELECT orders."order" AS "order" ' + "FROM orders)" + ' SELECT regional_sales."order" ' + "FROM regional_sales" + ) + def test_date_between(self): import datetime table = Table('dt', metadata, -- 2.47.2