support "OR" connector in queries
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 29 Jun 2009 11:34:39 +0000 (11:34 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 29 Jun 2009 11:34:39 +0000 (11:34 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@441 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/query.py

index 938abd5904aac6cf79230c134fd7b7f8d8958b04..7346608505d888c759e0ace78beb0f8734cc612f 100644 (file)
@@ -26,28 +26,36 @@ import ldap
 from django.db.models.query import QuerySet as BaseQuerySet
 from django.db.models.query_utils import Q
 from django.db.models.sql import BaseQuery
-from django.db.models.sql.where import WhereNode
+from django.db.models.sql.where import WhereNode as BaseWhereNode, AND, OR
 
 import ldapdb
 
-def compile(q):
-    filterstr = ""
-    for item in q.children:
-        if isinstance(item, WhereNode):
-            filterstr += compile(item)
-            continue
-        table, column, type, x, y, values = item
-        if q.negated:
-            filterstr += "(!(%s=%s))" % (column,values[0])
+class WhereNode(BaseWhereNode):
+    def as_sql(self):
+        bits = []
+        for item in self.children:
+            if isinstance(item, WhereNode):
+                bits.append(item.as_sql())
+                continue
+            table, column, type, x, y, values = item
+            if self.negated:
+                bits.append('(!(%s=%s))' % (column,values[0]))
+            else:
+                bits.append('(%s=%s)' % (column,values[0]))
+        if len(bits) == 1:
+            return bits[0]
+        elif self.connector == AND:
+            return '(&%s)' % ''.join(bits)
+        elif self.connector == OR:
+            return '(|%s)' % ''.join(bits)
         else:
-            filterstr += "(%s=%s)" % (column,values[0])
-    return filterstr
+            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._meta.object_classes[0]
-        filterstr += compile(self.where)
+        filterstr += self.where.as_sql()
         filterstr = '(&%s)' % filterstr
         attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ]
 
@@ -86,6 +94,6 @@ class Query(BaseQuery):
 class QuerySet(BaseQuerySet):
     def __init__(self, model=None, query=None):
         if not query:
-            query = Query(model, None)
+            query = Query(model, None, WhereNode)
         super(QuerySet, self).__init__(model, query)