- def as_sql(self):
- bits = []
- for item in self.children:
- if isinstance(item, WhereNode):
- bits.append(item.as_sql())
- continue
- constraint, x, y, values = item
- if hasattr(constraint, "col"):
- # django 1.2
- clause = "(%s=%s)" % (constraint.col, values)
- else:
- # django 1.1
- (table, column, type) = constraint
- equal_bits = [ "(%s=%s)" % (column, 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:
- return bits[0]
- elif self.connector == AND:
- return '(&%s)' % ''.join(bits)
- elif self.connector == OR:
- return '(|%s)' % ''.join(bits)
- else:
- raise Exception("Unhandled WHERE connector: %s" % self.connector)
-
-class Query(BaseQuery):
- def results_iter(self):
- # FIXME: use all object classes
- filterstr = '(objectClass=%s)' % self.model.object_classes[0]
- filterstr += self.where.as_sql()
- filterstr = '(&%s)' % filterstr
- attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ]
-
- try:
- vals = ldapdb.connection.search_s(
- self.model.base_dn,
- ldap.SCOPE_SUBTREE,
- filterstr=filterstr,
- attrlist=attrlist,
- )
- except:
- raise self.model.DoesNotExist
-
- # perform sorting
- if self.extra_order_by:
- ordering = self.extra_order_by
- elif not self.default_ordering:
- ordering = self.order_by
- else:
- ordering = self.order_by or self.model._meta.ordering
- def getkey(x):
- keys = []
- for k in ordering:
- attr = self.model._meta.get_field(k).db_column
- keys.append(x[1].get(attr, '').lower())
- return keys
- vals = sorted(vals, key=lambda x: getkey(x))
-
- # process results
- for dn, attrs in vals:
- row = []
- for field in iter(self.model._meta.fields):
- if field.attname == 'dn':
- row.append(dn)
- else:
- row.append(attrs.get(field.db_column, None))
- yield row
-