X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=91a3f62e2058bf8587342559c04ffefee3c58487;hb=5c3683a37e2afc87654d4b44c3f8e9fbe7872076;hp=ab5f19ec727b574cc845d44995d1b4371611ead7;hpb=d148d9497b40bbfe0a1056e73e72415281f9ef9f;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index ab5f19e..91a3f62 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -56,8 +56,13 @@ class Constraint(BaseConstraint): """ An object that can be passed to WhereNode.add() and knows how to pre-process itself prior to including in the WhereNode. + + NOTES: + - we redefine this class, because when self.field is None calls + Field().get_db_prep_lookup(), which short-circuits our LDAP-specific code. + - the connection argument defaults to None for django 1.1 compatibility """ - def process(self, lookup_type, value): + def process(self, lookup_type, value, connection=None): """ Returns a tuple of data suitable for inclusion in a WhereNode instance. @@ -67,10 +72,12 @@ class Constraint(BaseConstraint): try: if self.field: - params = self.field.get_db_prep_lookup(lookup_type, value) + params = self.field.get_db_prep_lookup(lookup_type, value, + connection=connection, prepared=True) db_type = self.field.db_type() else: - params = CharField().get_db_prep_lookup(lookup_type, value) + params = CharField().get_db_prep_lookup(lookup_type, value, + connection=connection, prepared=True) db_type = None except ObjectDoesNotExist: raise EmptyShortCircuit @@ -190,10 +197,11 @@ class WhereNode(BaseWhereNode): else: clause = '(|%s)' % ''.join(equal_bits) - if self.negated: - bits.append('(!%s)' % clause) - else: - bits.append(clause) + bits.append(clause) + + if not len(bits): + return '', [] + if len(bits) == 1: sql_string = bits[0] elif self.connector == AND: @@ -202,6 +210,10 @@ class WhereNode(BaseWhereNode): sql_string = '(|%s)' % ''.join(bits) else: raise Exception("Unhandled WHERE connector: %s" % self.connector) + + if self.negated: + sql_string = ('(!%s)' % sql_string) + return sql_string, [] class Query(BaseQuery):