do not define queryset.delete(), the base implementation works
[matthijs/upstream/django-ldapdb.git] / ldapdb / models / query.py
index 35df5df8329da532e84736cbf67751162acf3ae3..e84748c4ee00a35475d34e913e4824ed70a88cbe 100644 (file)
 from copy import deepcopy
 import ldap
 
+from django.db import connections
 from django.db.models.query import QuerySet as BaseQuerySet
 from django.db.models.query_utils import Q
-from django.db.models.sql import Query as BaseQuery
+from django.db.models.sql import Query
 from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as BaseConstraint, AND, OR
 
-import ldapdb
 from ldapdb.backends.ldap import compiler
 from ldapdb.models.fields import CharField
 
-def get_lookup_operator(lookup_type):
-    if lookup_type == 'gte':
-        return '>='
-    elif lookup_type == 'lte':
-        return '<='
-    else:
-        return '='
-
 class Constraint(BaseConstraint):
     """
     An object that can be passed to WhereNode.add() and knows how to
@@ -95,97 +87,9 @@ class WhereNode(BaseWhereNode):
             obj = Constraint(obj.alias, obj.col, obj.field)
         super(WhereNode, self).add((obj, lookup_type, value), connector)
 
-    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 lookup_type == 'in':
-                equal_bits = [ "(%s%s%s)" % (constraint.col, comp, value) for value in values ]
-                clause = '(|%s)' % ''.join(equal_bits)
-            else:
-                clause = "(%s%s%s)" % (constraint.col, comp, values)
-
-            bits.append(clause)
-
-        if not len(bits):
-            return '', []
-
-        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)
-
-        if self.negated:
-            sql_string = ('(!%s)' % sql_string)
-
-        return sql_string, []
-
-class Query(BaseQuery):
-    def __init__(self, *args, **kwargs):
-        super(Query, self).__init__(*args, **kwargs)
-        self.connection = ldapdb.connection
-
-    def _ldap_filter(self):
-        filterstr = ''.join(['(objectClass=%s)' % cls for cls in self.model.object_classes])
-        sql, params = self.where.as_sql()
-        filterstr += sql
-        return '(&%s)' % filterstr
-
-    def get_count(self, using):
-        try:
-            vals = ldapdb.connection.search_s(
-                self.model.base_dn,
-                self.model.search_scope,
-                filterstr=self._ldap_filter(),
-                attrlist=[],
-            )
-        except ldap.NO_SUCH_OBJECT:
-            return 0
-
-        number = len(vals)
-
-        # apply limit and offset
-        number = max(0, number - self.low_mark)
-        if self.high_mark is not None:
-            number = min(number, self.high_mark - self.low_mark)
-
-        return number
-
-    def get_compiler(self, using=None, connection=None):
-        return compiler.SQLCompiler(self, ldapdb.connection, using)
-
-    def has_results(self, using):
-        return self.get_count(using) != 0
-
 class QuerySet(BaseQuerySet):
     def __init__(self, model=None, query=None, using=None):
         if not query:
             query = Query(model, WhereNode)
         super(QuerySet, self).__init__(model=model, query=query, using=using)
 
-    def delete(self):
-        "Bulk deletion."
-        try:
-            vals = ldapdb.connection.search_s(
-                self.model.base_dn,
-                self.model.search_scope,
-                filterstr=self.query._ldap_filter(),
-                attrlist=[],
-            )
-        except ldap.NO_SUCH_OBJECT:
-            return
-
-        # FIXME : there is probably a more efficient way to do this 
-        for dn, attrs in vals:
-            ldapdb.connection.delete_s(dn)
-