This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
compiler.py
269 lines (230 loc) · 9.61 KB
/
compiler.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
268
269
import datetime
import sys
import pymongo
from pymongo.objectid import ObjectId
from pymongo.errors import InvalidId
from django.db import models
from django.db.models.sql import aggregates as sqlaggregates
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql import aggregates as sqlaggregates
from django.db.models.sql.constants import LOOKUP_SEP, MULTI, SINGLE
from django.db.models.sql.where import AND, OR
from django.db.utils import DatabaseError, IntegrityError
from django.utils.tree import Node
from django.db.models.sql.where import WhereNode
import re
from django.conf import settings
TYPE_MAPPING = {
'unicode': lambda val: unicode(val),
# mongodb doesn't handle number anyway
#'int': lambda val: int(val),
'float': lambda val: float(val),
'bool': lambda val: bool(val),
'objectid': lambda val: val,
}
OPERATORS_MAP = {
'exact': lambda val: val,
'iexact': lambda val: re.compile(r'^%s$' % val, re.IGNORECASE),
'startswith': lambda val: re.compile(r'%s' % val),
'istartswith': lambda val: re.compile(r'^%s' % val, re.IGNORECASE),
'endswith': lambda val: re.compile(r'%s$' % val),
'iendswith': lambda val: re.compile(r'%s$' % val, re.IGNORECASE),
'contains': lambda val: re.compile(r'%s' % val),
'icontains': lambda val: re.compile(r'%s' % val, re.IGNORECASE),
'regex': lambda val: re.compile(val),
'iregex': lambda val: re.compile(val, re.IGNORECASE),
'gt': lambda val: {"$gt": val},
'gte': lambda val: {"$gte": val},
'lt': lambda val: {"$lt": val},
'lte': lambda val: {"$lte": val},
'in': lambda val: {"$in": val},
}
def _get_mapping(db_type, value):
# TODO - comments. lotsa comments
if db_type in TYPE_MAPPING:
_func = TYPE_MAPPING[db_type]
else:
_func = lambda val: val
# TODO - what if the data is represented as list on the python side?
if isinstance(value, list):
return map(_func, value)
return _func(value)
def python2db(db_type, value):
# mongodb only handle 8 bit number
if isinstance(value, (int, float, long)):
return str(value)
return _get_mapping(db_type, value)
def db2python(db_type, value):
return _get_mapping(db_type, value)
def _parse_constraint(where_child, connection):
_constraint, lookup_type, _annotation, value = where_child
(table_alias, column, db_type), value = _constraint.process(lookup_type, value, connection)
if lookup_type not in ('in', 'range') and isinstance(value, (tuple, list)):
# Django fields always return a list (see Field.get_db_prep_lookup)
# except if get_db_prep_lookup got overridden by a subclass
if len(value) > 1:
# TODO... - when can we get here?
raise Exception("blah!")
if lookup_type == 'isnull':
value = annotation
else:
value = value[0]
#fix managing of some specific "string manipulation in standard sql field"
# line 287 in where.py params = self.field.get_db_prep_lookup(lookup_type, value, connection=connection, prepared=True)
if lookup_type in ['startswith', 'istartswith']:
if value.endswith(u"%"):
value = value[:-1]
elif lookup_type in ['endswith', 'iendswith']:
if value.startswith(u"%"):
value = value[1:]
elif lookup_type in ['icontains', 'contains']:
value = value[1:-1]
return (lookup_type, table_alias, column, db_type, value)
class SQLCompiler(SQLCompiler):
"""
A simple query: no joins, no distinct, etc.
Internal attributes of interest:
x connection - DatabaseWrapper instance
x query - query object, which is to be
executed
"""
def __init__(self, *args, **kw):
super(SQLCompiler, self).__init__(*args, **kw)
self.cursor = self.connection._cursor
"""
Private API
"""
def _execute_aggregate_query(self, aggregates, result_type):
if len(aggregates) == 1 and isinstance(aggregates[0], sqlaggregates.Count):
count = self.get_count()
if result_type is SINGLE:
return [count]
elif result_type is MULTI:
return [[count]]
def _get_query(self):
query = {}
where = self.query.where
pk_column = self.query.get_meta().pk.column
query = self._get_query_recursif(query=query, where=where, pk_column=pk_column)
# if we have a _id, we need to put back the value into the pk_column
if query.has_key(pk_column):
query['_id'] = query[pk_column]
del query[pk_column]
return query
def _get_query_recursif(self, query, where, pk_column):
if where.connector == OR:
raise NotImplementedError("OR queries not supported yet.")
for child in where.children:
if isinstance(child, (list, tuple)):
lookup_type, collection, column, db_type, value = \
_parse_constraint(child, self.connection)
if column == pk_column:
if lookup_type=='exact':
query["_id"] = value
elif lookup_type=='in':
query["_id"] = {"$in":value}
else:
query[column] = OPERATORS_MAP[lookup_type](python2db(db_type, value))
elif isinstance(child, WhereNode):
query = self._get_query_recursif(query=query, where=child,
pk_column=pk_column)
return query
def _get_collection(self):
_collection = self.query.get_meta().db_table
return self.cursor()[_collection]
"""
Public API
"""
def get_count(self):
return self.get_results().count()
def get_results(self):
"""
@returns: pymongo iterator over results
defined by self.query
"""
_high_limit = self.query.high_mark or 0
_low_limit = self.query.low_mark or 0
query = self._get_query()
results = self._get_collection().find(query).skip(_low_limit).limit(
_high_limit - _low_limit)
if self.query.order_by:
sort_list = []
for order in self.query.order_by:
if order.startswith('-'):
sort_list.append((order[1:], pymongo.DESCENDING))
else:
sort_list.append((order, pymongo.ASCENDING))
results = results.sort(sort_list)
return results
"""
API used by Django
"""
def results_iter(self):
"""
Returns an iterator over the results from executing this query.
self.query - the query created by the ORM
self.query.where - conditions imposed by the query
"""
pk_column = str(self.query.get_meta().pk.column)
for document in self.get_results():
# remove the_id at the last moment if present
if document.has_key('_id'):
document[pk_column] = document['_id']
del document['_id']
result = []
for field in self.query.get_meta().local_fields:
result.append(db2python(field.db_type(
connection=self.connection), document.get(field.column, field.default)))
yield result
def execute_sql(self, result_type=MULTI):
# let's catch aggregate call
aggregates = self.query.aggregate_select.values()
if aggregates:
return self._execute_aggregate_query(aggregates, result_type)
return
def has_results(self):
return self.get_count() > 0
class SQLInsertCompiler(SQLCompiler):
def execute_sql(self, return_id=False):
"""
self.query - the data that should be inserted
"""
dat = {}
for (field, value), column in zip(self.query.values, self.query.columns):
dat[column] = python2db(field.db_type(connection=self.connection), value)
# every object should have a unique pk, (is it always the case in Django?)
pk_field = self.query.get_meta().pk
pk_name = pk_field.attname
if pk_name in dat:
pk = dat.pop(pk_name)
if isinstance(pk, (str, unicode)):
try:
pk = ObjectId(pk)
except InvalidId:
# http://www.mongodb.org/display/DOCS/Object+IDs
# Users are welcome to use their own conventions
# for creating ids; the _id value may be of any type
# so long as it is a unique.
pass
dat['_id'] = pk
res = self.connection._cursor()[self.query.get_meta().db_table].save(dat)
if return_id:
return unicode(res)
class SQLUpdateCompiler(SQLCompiler):
def execute_sql(self, return_id=False):
"""
self.query - the data that should be inserted
"""
dat = {}
for (field, value), column in zip(self.query.values, self.query.columns):
dat[column] = python2db(field.db_type(connection=self.connection), value)
# every object should have a unique pk
pk_field = self.query.get_meta().pk
pk_name = pk_field.attname
res = self.connection._cursor()[self.query.get_meta().db_table].save(dat)
if return_id:
return unicode(res)
class SQLDeleteCompiler(SQLCompiler):
def execute_sql(self, result_type=MULTI):
query = self._get_query()
return self._get_collection().remove(query)