fix limited_rql w/ UNION query stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 18 Mar 2010 15:51:53 +0100
branchstable
changeset 4939 349af486f5ed
parent 4937 4e08eba12851
child 4942 8f4002f41b26
fix limited_rql w/ UNION query
rset.py
test/unittest_rset.py
--- a/rset.py	Thu Mar 18 09:12:54 2010 +0100
+++ b/rset.py	Thu Mar 18 15:51:53 2010 +0100
@@ -286,12 +286,14 @@
             newselect = stmts.Select()
             newselect.limit = limit
             newselect.offset = offset
-            aliases = [nodes.VariableRef(newselect.get_variable(vref.name, i))
-                       for i, vref in enumerate(rqlst.selection)]
+            aliases = [nodes.VariableRef(newselect.get_variable(chr(65+i), i))
+                       for i in xrange(len(rqlst.children[0].selection))]
+            for vref in aliases:
+                newselect.append_selected(nodes.VariableRef(vref.variable))
             newselect.set_with([nodes.SubQuery(aliases, rqlst)], check=False)
             newunion = stmts.Union()
             newunion.append(newselect)
-            rql = rqlst.as_string(kwargs=self.args)
+            rql = newunion.as_string(kwargs=self.args)
             rqlst.parent = None
         return rql
 
--- a/test/unittest_rset.py	Thu Mar 18 09:12:54 2010 +0100
+++ b/test/unittest_rset.py	Thu Mar 18 15:51:53 2010 +0100
@@ -371,6 +371,18 @@
         rset = self.execute(u'Any X WHERE X has_text %(text)s', {'text' : 'foo'})
         self.assertEquals(rset.searched_text(), 'foo')
 
+    def test_union_limited_rql(self):
+        rset = self.execute('(Any X,N WHERE X is Bookmark, X title N)'
+                            ' UNION '
+                            '(Any X,N WHERE X is CWGroup, X name N)')
+        rset.limit(2, 10, inplace=True)
+        self.assertEquals(rset.limited_rql(),
+                          'Any A,B LIMIT 2 OFFSET 10 '
+                          'WITH A,B BEING ('
+                          '(Any X,N WHERE X is Bookmark, X title N) '
+                          'UNION '
+                          '(Any X,N WHERE X is CWGroup, X name N)'
+                          ')')
 
 if __name__ == '__main__':
     unittest_main()