From: Mike Bayer Date: Fri, 20 Jul 2007 17:00:53 +0000 (+0000) Subject: implemented Class.any(criterion) X-Git-Tag: rel_0_4_6~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=172ba372294038c199e34182d88da32fdd63a2bc;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git implemented Class.any(criterion) --- diff --git a/lib/sqlalchemy/orm/interfaces.py b/lib/sqlalchemy/orm/interfaces.py index 79b575cc2f..4cad68e346 100644 --- a/lib/sqlalchemy/orm/interfaces.py +++ b/lib/sqlalchemy/orm/interfaces.py @@ -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 diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 4635b8e4b7..d259270580 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -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") diff --git a/test/orm/query.py b/test/orm/query.py index df4187eb4d..01815374da 100644 --- a/test/orm/query.py +++ b/test/orm/query.py @@ -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()