- def as_sql(self, qn=None, connection=None):
- bits = []
- for item in self.children:
- if hasattr(item, 'as_sql'):
- sql, params = item.as_sql(qn=qn, connection=connection)
- bits.append(sql)
- continue
-
- constraint, lookup_type, y, values = item
- comp = get_lookup_operator(lookup_type)
- if hasattr(constraint, "col"):
- # django 1.2
- column = constraint.col
- if lookup_type == 'in':
- equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
- clause = '(|%s)' % ''.join(equal_bits)
- else:
- clause = "(%s%s%s)" % (constraint.col, comp, values)
- else:
- # django 1.1
- (table, column, db_type) = constraint
- equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
- if len(equal_bits) == 1:
- clause = equal_bits[0]
- else:
- clause = '(|%s)' % ''.join(equal_bits)
-
- if self.negated:
- bits.append('(!%s)' % clause)
- else:
- bits.append(clause)
- if len(bits) == 1:
- sql_string = bits[0]
- elif self.connector == AND:
- sql_string = '(&%s)' % ''.join(bits)
- elif self.connector == OR:
- sql_string = '(|%s)' % ''.join(bits)
- else:
- raise Exception("Unhandled WHERE connector: %s" % self.connector)
- return sql_string, []
-
-class Query(BaseQuery):
- def __init__(self, *args, **kwargs):
- super(Query, self).__init__(*args, **kwargs)
- self.connection = ldapdb.connection
-
- def get_count(self):
- filterstr = ''.join(['(objectClass=%s)' % cls for cls in self.model.object_classes])
- sql, params = self.where.as_sql()
- filterstr += sql
- filterstr = '(&%s)' % filterstr
-
- try:
- vals = self.connection.search_s(
- self.model.base_dn,
- ldap.SCOPE_SUBTREE,
- filterstr=filterstr,
- attrlist=[],
- )
- except:
- raise self.model.DoesNotExist
-
- return len(vals)
-
- def get_compiler(self, using=None, connection=None):
- return Compiler(self, ldapdb.connection, using)
-
- def results_iter(self):
- "For django 1.1 compatibility"
- return self.get_compiler().results_iter()
-