1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of Bolloré telecom nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 class SQLCompiler(object):
36 def __init__(self, query, connection, using):
38 self.connection = connection
41 def results_iter(self):
42 if self.query.select_fields:
43 fields = self.query.select_fields
45 fields = self.query.model._meta.fields
47 attrlist = [ x.db_column for x in fields if x.db_column ]
50 vals = self.connection.search_s(
51 self.query.model.base_dn,
52 self.query.model.search_scope,
53 filterstr=self.query._ldap_filter(),
56 except ldap.NO_SUCH_OBJECT:
60 if self.query.extra_order_by:
61 ordering = self.query.extra_order_by
62 elif not self.query.default_ordering:
63 ordering = self.query.order_by
65 ordering = self.query.order_by or self.query.model._meta.ordering
67 for fieldname in ordering:
68 if fieldname.startswith('-'):
69 fieldname = fieldname[1:]
73 field = self.query.model._meta.get_field(fieldname)
74 attr_x = field.from_ldap(x[1].get(field.db_column, []), connection=self.connection)
75 attr_y = field.from_ldap(y[1].get(field.db_column, []), connection=self.connection)
76 # perform case insensitive comparison
77 if hasattr(attr_x, 'lower'):
78 attr_x = attr_x.lower()
79 if hasattr(attr_y, 'lower'):
80 attr_y = attr_y.lower()
81 val = negate and cmp(attr_y, attr_x) or cmp(attr_x, attr_y)