]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added limit/offset to union queries
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 26 Aug 2006 14:42:18 +0000 (14:42 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 26 Aug 2006 14:42:18 +0000 (14:42 +0000)
CHANGES
lib/sqlalchemy/ansisql.py
lib/sqlalchemy/sql.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index db4d15054cf2b0065fcb06d1543bee7ef1d0cdb3..405610760b05664798eb606e62110f3ca2e1e860 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -36,6 +36,7 @@ flag for use with table reflection to help with quoting rules
 - unit tests updated to run without any pysqlite installed; pool
 test uses a mock DBAPI
 - urls support escaped characters in passwords [ticket:281]
+- added limit/offset to UNION queries
 
 0.2.7
 - quoting facilities set up so that database-specific quoting can be
index f77e855e442364d7b2b221407d61e2d55cbfb87d..da6f4bf83285b75a9026860779865358c55f2c82 100644 (file)
@@ -244,6 +244,7 @@ class ANSICompiler(sql.Compiled):
         order_by = self.get_str(cs.order_by_clause)
         if order_by:
             text += " ORDER BY " + order_by
+        text += self.visit_select_postclauses(cs)
         if cs.parens:
             self.strings[cs] = "(" + text + ")"
         else:
@@ -409,12 +410,14 @@ class ANSICompiler(sql.Compiled):
         return (select.limit or select.offset) and self.limit_clause(select) or ""
 
     def limit_clause(self, select):
+        text = ""
         if select.limit is not None:
-            return  " \n LIMIT " + str(select.limit)
+            text +=  " \n LIMIT " + str(select.limit)
         if select.offset is not None:
             if select.limit is None:
-                return " \n LIMIT -1"
-            return " OFFSET " + str(select.offset)
+                text += " \n LIMIT -1"
+            text += " OFFSET " + str(select.offset)
+        return text
 
     def visit_table(self, table):
         self.froms[table] = self.preparer.format_table(table)
index 2aa1342ca07a3ec142994d747a70d6d864983f02..596e0e8eef61c2a2b7b2964b8f01af951b7866fd 100644 (file)
@@ -1347,6 +1347,8 @@ class CompoundSelect(SelectBaseMixin, FromClause):
         self.correlate = kwargs.pop('correlate', False)
         self.for_update = kwargs.pop('for_update', False)
         self.nowait = kwargs.pop('nowait', False)
+        self.limit = kwargs.get('limit', None)
+        self.offset = kwargs.get('offset', None)
         for s in self.selects:
             s.group_by(None)
             s.order_by(None)
index 57b123fc28dffd26058b2417ab40cfe6bf258b9f..e43d30c54e7534b47bf5fc5cb9517c8aa0e7b390 100644 (file)
@@ -461,6 +461,19 @@ FROM myothertable UNION SELECT thirdtable.userid, thirdtable.otherstuff FROM thi
             )
             assert u.corresponding_column(table2.c.otherid) is u.c.otherid
             
+            self.runtest(
+                union(
+                    select([table1]),
+                    select([table2]),
+                    order_by=['myid'],
+                    offset=10,
+                    limit=5
+                )
+            ,    "SELECT mytable.myid, mytable.name, mytable.description \
+FROM mytable UNION SELECT myothertable.otherid, myothertable.othername \
+FROM myothertable ORDER BY myid \
+ LIMIT 5 OFFSET 10"
+            )
             
     def testouterjoin(self):
         # test an outer join.  the oracle module should take the ON clause of the join and