-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels_sql_where.py
271 lines (223 loc) · 8.41 KB
/
models_sql_where.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
# Copyright 2005,2006,2007,2008 Spike^ekipS <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import datetime, traceback
from django.conf import settings
from django.core.exceptions import FieldError
from django.db import models, connection
from django.db.models.query_utils import QueryWrapper
from django.db.models.sql.datastructures import EmptyResultSet, FullResultSet
from django.db.models.sql import where
from django.utils import tree
from django.utils.tree import Node
import pylucene, constant, utils
LOOKUP_TYPE_SINGLE_VALUE = (
"contains",
"day",
"endswith",
"exact",
"gt",
"gte",
"icontains",
"iendswith",
"iexact",
"iregex",
"isnull",
"istartswith",
"lt",
"lte",
"month",
"regex",
"search",
"startswith",
"year",
)
class WhereNode (where.WhereNode, tree.Node) :
model = None
def set_model (self, index_model) :
self.index_model = index_model
def as_sql(self, query, node=None, qn=None) :
if node is None:
node = self
if not qn:
qn = connection.ops.quote_name
if not node.children:
return None
subquery = None
queries = list()
empty = True
if node.connector == where.OR :
subquery = pylucene.BooleanQuery()
for child in node.children:
try:
if hasattr(child, "as_sql") :
child.set_model(self.index_model)
sql = child.as_sql(query, qn=qn)
elif isinstance(child, tree.Node) :
sql = self.as_sql(query, node=child, qn=qn)
else:
sql = self.make_atom(child, qn)
except EmptyResultSet, e :
if node.connector == where.AND and not node.negated:
raise
elif node.negated:
empty = False
continue
except FullResultSet:
if self.connector == where.OR:
if node.negated:
empty = True
break
return None
if node.negated:
empty = True
continue
except Exception, e :
if settings.DEBUG :
traceback.print_exc()
raise
empty = False
if sql :
if query == sql :
continue
if node.connector == where.OR :
connector = where.OR
elif node.negated == False :
connector = False
else :
connector = True
if subquery :
subquery.add(sql, constant.QUERY_BOOLEANS.get(where.OR))
else :
query.add(sql, constant.QUERY_BOOLEANS.get(connector))
if subquery :
query.add(subquery, constant.QUERY_BOOLEANS.get(where.AND))
if empty:
raise EmptyResultSet
return query
def get_query (self) :
return query
def get_term (self, field, value) :
return pylucene.Term.new(field, value)
def add(self, data, connector):
if not isinstance(data, (list, tuple)):
super(WhereNode, self).add(data, connector)
return
alias, col, field, lookup_type, value = data
if isinstance(value, datetime.datetime):
annotation = datetime.datetime
else:
annotation = bool(value)
tree.Node.add(self, (alias, col, field, lookup_type, annotation, value), connector)
def make_atom (self, child, qn) :
table_alias, name, _field, lookup_type, value_annot, value = child
field = self.index_model._meta.get_field(name)
subquery = None
if lookup_type in LOOKUP_TYPE_SINGLE_VALUE :
if type(value) in (list, tuple, ) :
value = value[0]
if field is None :
raise FieldError("Invalid field name: '%s'" % name)
value = field.to_query(value)
######################################################################
# value is <str>
if lookup_type in ("search", "contains", "icontains", ) :
subquery = pylucene.RegexQuery(
self.get_term(
field.name,
"%s" % (lookup_type == "icontains" and value.lower() or value),
)
)
elif lookup_type in ("regex", "iregex", ) :
subquery = pylucene.RegexQuery(
self.get_term(
field.name,
lookup_type == "iregex" and value.lower() or value
)
)
elif lookup_type in ("startswith", "istartswith", ) :
subquery = pylucene.RegexQuery(
self.get_term(
field.name,
"^(%s)" % (lookup_type == "istartswith" and value.lower() or value),
)
)
elif lookup_type in ("endswith", "iendswith", ) :
subquery = pylucene.RegexQuery(
self.get_term(
field.name,
"%s$" % (lookup_type == "iendswith" and value.lower() or value),
)
)
elif lookup_type in ("lt", "lte", ) :
subquery = pylucene.RangeQuery(
None,
self.get_term(field.name, value),
False,
)
elif lookup_type in ("gt", "gte", ) :
subquery = pylucene.RangeQuery(
self.get_term(field.name, value),
None,
False,
)
elif lookup_type in ("exact", "iexact", ) :
value = lookup_type == "iexact" and value.lower() or value
subquery = pylucene.TermQuery(self.get_term(field.name, value))
elif lookup_type == "year" :
subquery = pylucene.RegexQuery(
self.get_term(field.name, "^%s" % value),
)
elif lookup_type in ("month", "day") :
value = "%02d" % int(value)
subquery = pylucene.RegexQuery(
self.get_term(field.name, "^[\d]{4}[\d]{2}%s" % value),
)
elif lookup_type == "isnull":
subquery = pylucene.RegexQuery(
self.get_term(field.name, "^$")
)
elif type(value) in (list, tuple, ) :
values = [self.index_model._meta.get_field(field.name).to_query(i) for i in value]
if lookup_type == "range" :
values.sort()
subquery = pylucene.RangeQuery(
self.get_term(field.name, values[0]),
self.get_term(field.name, values[1]),
False,
)
elif lookup_type == "in" :
subquery = pylucene.BooleanQuery()
for i in values :
subquery.add(
pylucene.TermQuery(self.get_term(field.name, i)),
constant.QUERY_BOOLEANS.get("OR"),
)
if subquery :
return subquery
raise TypeError("Invalid lookup_type: %r" % lookup_type)
def negate (self) :
self.children = [Node(self.children, self.connector, not self.negated)]
self.connector = self.default
"""
Description
-----------
ChangeLog
---------
Usage
-----
"""
__author__ = "Spike^ekipS <[email protected]>"