1 # -*- coding: utf-8 -*-
4 # Copyright (C) 2009-2010 Bolloré telecom
5 # See AUTHORS file for a full list of contributors.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from copy import deepcopy
24 from django.db.models.query import QuerySet as BaseQuerySet
25 from django.db.models.query_utils import Q
26 from django.db.models.sql import Query as BaseQuery
27 from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as BaseConstraint, AND, OR
31 from ldapdb.models.fields import CharField
33 def get_lookup_operator(lookup_type):
34 if lookup_type == 'gte':
36 elif lookup_type == 'lte':
41 class Constraint(BaseConstraint):
43 An object that can be passed to WhereNode.add() and knows how to
44 pre-process itself prior to including in the WhereNode.
46 def process(self, lookup_type, value):
48 Returns a tuple of data suitable for inclusion in a WhereNode
51 # Because of circular imports, we need to import this here.
52 from django.db.models.base import ObjectDoesNotExist
56 params = self.field.get_db_prep_lookup(lookup_type, value)
57 db_type = self.field.db_type()
59 params = CharField().get_db_prep_lookup(lookup_type, value)
61 except ObjectDoesNotExist:
62 raise EmptyShortCircuit
64 return (self.alias, self.col, db_type), params
66 class Compiler(object):
67 def __init__(self, query, connection, using):
69 self.connection = connection
72 def results_iter(self):
74 attrlist = [ x.db_column for x in query.model._meta.local_fields if x.db_column ]
77 vals = self.connection.search_s(
80 filterstr=query._ldap_filter(),
83 except ldap.NO_SUCH_OBJECT:
85 raise query.model.DoesNotExist
88 if query.extra_order_by:
89 ordering = query.extra_order_by
90 elif not query.default_ordering:
91 ordering = query.order_by
93 ordering = query.order_by or query.model._meta.ordering
95 for fieldname in ordering:
96 if fieldname.startswith('-'):
97 fieldname = fieldname[1:]
101 field = query.model._meta.get_field(fieldname)
102 attr_x = field.from_ldap(x[1].get(field.db_column, []), connection=self.connection)
103 attr_y = field.from_ldap(y[1].get(field.db_column, []), connection=self.connection)
104 # perform case insensitive comparison
105 if hasattr(attr_x, 'lower'):
106 attr_x = attr_x.lower()
107 if hasattr(attr_y, 'lower'):
108 attr_y = attr_y.lower()
109 val = negate and cmp(attr_y, attr_x) or cmp(attr_x, attr_y)
113 vals = sorted(vals, cmp=cmpvals)
117 for dn, attrs in vals:
118 # FIXME : This is not optimal, we retrieve more results than we need
119 # but there is probably no other options as we can't perform ordering
121 if (self.query.low_mark and pos < self.query.low_mark) or \
122 (self.query.high_mark is not None and pos >= self.query.high_mark):
126 for field in iter(query.model._meta.fields):
127 if field.attname == 'dn':
129 elif hasattr(field, 'from_ldap'):
130 row.append(field.from_ldap(attrs.get(field.db_column, []), connection=self.connection))
137 class WhereNode(BaseWhereNode):
138 def add(self, data, connector):
139 if not isinstance(data, (list, tuple)):
140 super(WhereNode, self).add(data, connector)
143 # we replace the native Constraint by our own
144 obj, lookup_type, value = data
145 if hasattr(obj, "process"):
146 obj = Constraint(obj.alias, obj.col, obj.field)
147 super(WhereNode, self).add((obj, lookup_type, value), connector)
149 def as_sql(self, qn=None, connection=None):
151 for item in self.children:
152 if hasattr(item, 'as_sql'):
153 sql, params = item.as_sql(qn=qn, connection=connection)
157 constraint, lookup_type, y, values = item
158 comp = get_lookup_operator(lookup_type)
159 if hasattr(constraint, "col"):
161 column = constraint.col
162 if lookup_type == 'in':
163 equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
164 clause = '(|%s)' % ''.join(equal_bits)
166 clause = "(%s%s%s)" % (constraint.col, comp, values)
169 (table, column, db_type) = constraint
170 equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
171 if len(equal_bits) == 1:
172 clause = equal_bits[0]
174 clause = '(|%s)' % ''.join(equal_bits)
177 bits.append('(!%s)' % clause)
182 elif self.connector == AND:
183 sql_string = '(&%s)' % ''.join(bits)
184 elif self.connector == OR:
185 sql_string = '(|%s)' % ''.join(bits)
187 raise Exception("Unhandled WHERE connector: %s" % self.connector)
188 return sql_string, []
190 class Query(BaseQuery):
191 def __init__(self, *args, **kwargs):
192 super(Query, self).__init__(*args, **kwargs)
193 self.connection = ldapdb.connection
195 def _ldap_filter(self):
196 filterstr = ''.join(['(objectClass=%s)' % cls for cls in self.model.object_classes])
197 sql, params = self.where.as_sql()
199 return '(&%s)' % filterstr
201 def get_count(self, using=None):
203 vals = ldapdb.connection.search_s(
206 filterstr=self._ldap_filter(),
209 except ldap.NO_SUCH_OBJECT:
213 def get_compiler(self, using=None, connection=None):
214 return Compiler(self, ldapdb.connection, using)
216 def results_iter(self):
217 "For django 1.1 compatibility"
218 return self.get_compiler().results_iter()
220 class QuerySet(BaseQuerySet):
221 def __init__(self, model=None, query=None, using=None):
224 spec = inspect.getargspec(BaseQuery.__init__)
225 if len(spec[0]) == 3:
227 query = Query(model, WhereNode)
230 query = Query(model, None, WhereNode)
231 super(QuerySet, self).__init__(model=model, query=query)
236 vals = ldapdb.connection.search_s(
239 filterstr=self.query._ldap_filter(),
242 except ldap.NO_SUCH_OBJECT:
245 # FIXME : there is probably a more efficient way to do this
246 for dn, attrs in vals:
247 ldapdb.connection.delete_s(dn)