]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- implemented from_obj argument for query.count, improves count function
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 2 Nov 2006 21:31:56 +0000 (21:31 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 2 Nov 2006 21:31:56 +0000 (21:31 +0000)
on selectresults [ticket:325]

CHANGES
lib/sqlalchemy/orm/query.py
test/ext/selectresults.py

diff --git a/CHANGES b/CHANGES
index fb0334cfdca4b0f0ba48ebfd3fc9a04ad23d51f8..e4abe15650978878de84a41c689627e905c06bc0 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -17,6 +17,8 @@ contained a cyclical many-to-one relationship to object B, and object B
 was just attached to object A, *but* object B itself wasnt changed,
 the many-to-one synchronize of B's primary key attribute to A's foreign key
 attribute wouldnt occur.  [ticket:360]
+- implemented from_obj argument for query.count, improves count function
+on selectresults [ticket:325]
 
 0.3.0
 - General:
index 9c76c6a176550247e436f8f40e041ef9daa01ee6..e257a1cbe140a281816c56ca6eea8bf3ae5758f3 100644 (file)
@@ -255,10 +255,19 @@ class Query(object):
 
     def count(self, whereclause=None, params=None, **kwargs):
         """given a WHERE criterion, create a SELECT COUNT statement, execute and return the resulting count value."""
+
+        from_obj = kwargs.pop('from_obj', [])
+        alltables = []
+        for l in [sql_util.TableFinder(x) for x in from_obj]:
+            alltables += l
+            
+        if self.table not in alltables:
+            from_obj.append(self.table)
+
         if self._nestable(**kwargs):
-            s = self.table.select(whereclause, **kwargs).alias('getcount').count()
+            s = sql.select([self.table], whereclause, **kwargs).alias('getcount').count()
         else:
-            s = self.table.count(whereclause)
+            s = sql.select([sql.func.count(list(self.table.primary_key)[0])], whereclause, from_obj=from_obj, **kwargs)
         return self.session.scalar(self.mapper, s, params=params)
 
     def select_statement(self, statement, **params):
index d82ad96fb6c74af1164bc4d92e06284595243143..ebb0e69e2d49d304bca78311a3913ce78c935923 100644 (file)
@@ -155,6 +155,17 @@ class RelationsTest(AssertMixin):
         x = query.outerjoin_to('orders').outerjoin_to('items').select(or_(tables.Order.c.order_id==None,tables.Item.c.item_id==2))
         print x.compile()
         self.assert_result(list(x), tables.User, *tables.user_result[1:3])
+    def test_outerjointo_count(self):
+        """test the join_to and outerjoin_to functions on SelectResults"""
+        mapper(tables.User, tables.users, properties={
+            'orders':relation(mapper(tables.Order, tables.orders, properties={
+                'items':relation(mapper(tables.Item, tables.orderitems))
+            }))
+        })
+        session = create_session()
+        query = SelectResults(session.query(tables.User))
+        x = query.outerjoin_to('orders').outerjoin_to('items').select(or_(tables.Order.c.order_id==None,tables.Item.c.item_id==2)).count()
+        assert x==2
     def test_from(self):
         mapper(tables.User, tables.users, properties={
             'orders':relation(mapper(tables.Order, tables.orders, properties={