]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
-Fixed bug in common table expressions whereby positional bound
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Jul 2014 22:34:32 +0000 (18:34 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Jul 2014 22:34:52 +0000 (18:34 -0400)
parameters could be expressed in the wrong final order
when CTEs were nested in certain ways.
fixes #3090

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/sql/compiler.py
test/sql/test_cte.py

index 6168b3b6869b58f87c4d6a280322424174b5eaf9..a081b67a44a6648ab877192d31aa3c758d8eb7e0 100644 (file)
     :version: 0.9.7
     :released:
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 3090
+        :versions: 1.0.0
+
+        Fixed bug in common table expressions whereby positional bound
+        parameters could be expressed in the wrong final order
+        when CTEs were nested in certain ways.
+
     .. change::
         :tags: bug, sql
         :tickets: 3069
index 1295874b9755d2154546f6349bb80e733c5d05d1..fc68510509f7af4534f5cb1836f7199ca4d119fa 100644 (file)
@@ -405,7 +405,7 @@ class SQLCompiler(Compiled):
         self.ctes_by_name = {}
         self.ctes_recursive = False
         if self.positional:
-            self.cte_positional = []
+            self.cte_positional = {}
 
     def _apply_numbered_params(self):
         poscount = itertools.count(1)
@@ -1087,8 +1087,6 @@ class SQLCompiler(Compiled):
                                 fromhints=None,
                                 **kwargs):
         self._init_cte_state()
-        if self.positional:
-            kwargs['positional_names'] = self.cte_positional
 
         if isinstance(cte.name, elements._truncated_label):
             cte_name = self._truncated_identifier("alias", cte.name)
@@ -1142,10 +1140,15 @@ class SQLCompiler(Compiled):
                 text += "(%s)" % (", ".join(
                                     self.preparer.format_column(ident)
                                     for ident in recur_cols))
+
+            if self.positional:
+                kwargs['positional_names'] = self.cte_positional[cte] = []
+
             text += " AS \n" + \
                         cte.original._compiler_dispatch(
                                 self, asfrom=True, **kwargs
                             )
+
             self.ctes[cte] = text
 
         if asfrom:
@@ -1414,7 +1417,6 @@ class SQLCompiler(Compiled):
                             iswrapper=False, fromhints=None,
                             compound_index=0,
                             force_result_map=False,
-                            positional_names=None,
                             nested_join_translation=False,
                             **kwargs):
 
@@ -1431,7 +1433,6 @@ class SQLCompiler(Compiled):
                             iswrapper=iswrapper, fromhints=fromhints,
                             compound_index=compound_index,
                             force_result_map=force_result_map,
-                            positional_names=positional_names,
                             nested_join_translation=True, **kwargs
                         )
 
@@ -1477,7 +1478,6 @@ class SQLCompiler(Compiled):
 
         column_clause_args = kwargs.copy()
         column_clause_args.update({
-                'positional_names': positional_names,
                 'within_label_clause': False,
                 'within_columns_clause': False
             })
@@ -1588,7 +1588,10 @@ class SQLCompiler(Compiled):
 
     def _render_cte_clause(self):
         if self.positional:
-            self.positiontup = self.cte_positional + self.positiontup
+            self.positiontup = sum([
+                                    self.cte_positional[cte]
+                                    for cte in self.ctes], []) + \
+                                    self.positiontup
         cte_text = self.get_cte_preamble(self.ctes_recursive) + " "
         cte_text += ", \n".join(
             [txt for txt in self.ctes.values()]
index 887d567107b7b15fbde835237659b6f57ee3d9d2..18c85f9e6b8ae109845dd2c2e1a7a2db1ce8480a 100644 (file)
@@ -367,6 +367,42 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL):
             dialect=dialect
         )
 
+    def test_positional_binds_2(self):
+        orders = table('orders',
+            column('order'),
+        )
+        s = select([orders.c.order, literal("x")]).cte("regional_sales")
+        s = select([s.c.order, literal("y")])
+        dialect = default.DefaultDialect()
+        dialect.positional = True
+        dialect.paramstyle = 'numeric'
+        s1 = select([orders.c.order]).where(orders.c.order == 'x').\
+                    cte("regional_sales_1")
+
+        s1a = s1.alias()
+
+        s2 = select([orders.c.order == 'y', s1a.c.order,
+                    orders.c.order, s1.c.order]).\
+                    where(orders.c.order == 'z').\
+                    cte("regional_sales_2")
+
+
+        s3 = select([s2])
+
+        self.assert_compile(
+            s3,
+            'WITH regional_sales_1 AS (SELECT orders."order" AS "order" '
+            'FROM orders WHERE orders."order" = :1), regional_sales_2 AS '
+            '(SELECT orders."order" = :2 AS anon_1, '
+            'anon_2."order" AS "order", '
+            'orders."order" AS "order", '
+            'regional_sales_1."order" AS "order" FROM orders, '
+            'regional_sales_1 '
+            'AS anon_2, regional_sales_1 '
+            'WHERE orders."order" = :3) SELECT regional_sales_2.anon_1, '
+            'regional_sales_2."order" FROM regional_sales_2',
+            checkpositional=('x', 'y', 'z'), dialect=dialect)
+
 
     def test_all_aliases(self):
         orders = table('order', column('order'))