]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
implemented Class.any(criterion)
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Jul 2007 17:00:53 +0000 (17:00 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Jul 2007 17:00:53 +0000 (17:00 +0000)
lib/sqlalchemy/orm/interfaces.py
lib/sqlalchemy/orm/properties.py
test/orm/query.py

index 79b575cc2f3939af269858a147b0070a2e06c86d..4cad68e346528cbbf4f24c73db2076d31ebcda35 100644 (file)
@@ -344,12 +344,20 @@ class PropComparator(sql.ColumnOperators):
         return a.contains(b)
     contains_op = staticmethod(contains_op)
     
+    def any_op(a, b):
+        return a.any(b)
+    any_op = staticmethod(any_op)
+    
     def __init__(self, prop):
         self.prop = prop
 
     def contains(self, other):
         """return true if this collection contains other"""
         return self.operate(PropComparator.contains_op, other)
+
+    def any(self, criterion):
+        """return true if this collection contains any member that meets the given criterion"""
+        return self.operate(PropComparator.any_op, criterion)
         
 class StrategizedProperty(MapperProperty):
     """A MapperProperty which uses selectable strategies to affect
index 4635b8e4b719b49595a07c1c0ba62373749b098f..d259270580127386dc3418e44f82c398dd1871ed 100644 (file)
@@ -182,6 +182,14 @@ class PropertyLoader(StrategizedProperty):
             else:  
                 return self.prop._optimized_compare(other)
         
+        def any(self, criterion):
+            if not self.prop.uselist:
+                raise exceptions.InvalidRequestError("'any' not implemented for scalar attributes")
+            j = self.prop.primaryjoin
+            if self.prop.secondaryjoin:
+                j = j & self.prop.secondaryjoin
+            return sql.exists([1], j & criterion)
+                
         def contains(self, other):
             if not self.prop.uselist:
                 raise exceptions.InvalidRequestError("'contains' not implemented for scalar attributes")
index df4187eb4dc0e5e1e35a59ca672df1e1ea2ff57d..01815374da2779329ebdd2369d81eb49eb47f24d 100644 (file)
@@ -221,6 +221,11 @@ class FilterTest(QueryTest):
             assert True
             
         #assert [User(id=7), User(id=9), User(id=10)] == sess.query(User).filter(User.addresses!=address).all()
+    
+    def test_any(self):
+        sess = create_session()
+        address = sess.query(Address).get(3)
+        assert [User(id=8), User(id=9)] == sess.query(User).filter(User.addresses.any(Address.email_address.like('%ed%'))).all()
         
     def test_contains_m2m(self):
         sess = create_session()