author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 28 Jul 2009 11:04:41 +0200 | |
changeset 2526 | 40e41eb34a7d |
parent 1977 | 606923dff11b |
child 3689 | deb13e88e037 |
permissions | -rw-r--r-- |
0 | 1 |
"""provide a minimal RQL support for google appengine dbmodel |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from datetime import datetime |
|
11 |
||
12 |
from rql import RQLHelper, nodes |
|
13 |
||
14 |
from cubicweb import Binary |
|
15 |
from cubicweb.rset import ResultSet |
|
16 |
from cubicweb.server import SQL_CONNECT_HOOKS |
|
17 |
||
18 |
from google.appengine.api.datastore import Key, Get, Query, Entity |
|
19 |
from google.appengine.api.datastore_types import Text, Blob |
|
20 |
from google.appengine.api.datastore_errors import EntityNotFoundError, BadKeyError |
|
21 |
||
22 |
||
23 |
def etype_from_key(key): |
|
24 |
return Key(key).kind() |
|
25 |
||
26 |
def poss_var_types(myvar, ovar, kind, solutions): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
27 |
return frozenset(etypes[myvar] for etypes in solutions |
0 | 28 |
if etypes[ovar] == kind) |
29 |
||
30 |
def expand_result(results, result, myvar, values, dsget=None): |
|
31 |
values = map(dsget, values) |
|
32 |
if values: |
|
33 |
result[myvar] = values.pop(0) |
|
34 |
for value in values: |
|
35 |
newresult = result.copy() |
|
36 |
newresult[myvar] = value |
|
37 |
results.append(newresult) |
|
38 |
else: |
|
39 |
results.remove(result) |
|
40 |
||
41 |
def _resolve(restrictions, solutions, fixed): |
|
42 |
varname = restrictions[0].searched_var |
|
43 |
objs = [] |
|
44 |
for etype in frozenset(etypes[varname] for etypes in solutions): |
|
45 |
gqlargs = {} |
|
46 |
query = Query(etype) |
|
47 |
for restriction in restrictions: |
|
48 |
restriction.fill_query(fixed, query) |
|
49 |
pobjs = query.Run() |
|
50 |
if varname in fixed: |
|
51 |
value = fixed[varname] |
|
52 |
objs += (x for x in pobjs if x == value) |
|
53 |
else: |
|
54 |
objs += pobjs |
|
55 |
if varname in fixed and not objs: |
|
56 |
raise EidMismatch(varname, value) |
|
57 |
return objs |
|
58 |
||
59 |
def _resolve_not(restrictions, solutions, fixed): |
|
60 |
restr = restrictions[0] |
|
61 |
constrvarname = restr.constraint_var |
|
62 |
if len(restrictions) > 1 or not constrvarname in fixed: |
|
63 |
raise NotImplementedError() |
|
64 |
varname = restr.searched_var |
|
65 |
objs = [] |
|
66 |
for etype in frozenset(etypes[varname] for etypes in solutions): |
|
67 |
gqlargs = {} |
|
68 |
for operator in ('<', '>'): |
|
69 |
query = Query(etype) |
|
70 |
restr.fill_query(fixed, query, operator) |
|
71 |
objs += query.Run() |
|
72 |
return objs |
|
73 |
||
74 |
def _print_results(rlist): |
|
75 |
return '[%s]' % ', '.join(_print_result(r) for r in rlist) |
|
76 |
||
77 |
def _print_result(rdict): |
|
78 |
string = [] |
|
79 |
for k, v in rdict.iteritems(): |
|
80 |
if isinstance(v, Entity): |
|
81 |
string.append('%s: %s' % (k, v.key()))#_print_result(v))) |
|
82 |
elif isinstance(v, list): |
|
83 |
string.append('%s: [%s]' % (k, ', '.join(str(i) for i in v))) |
|
84 |
else: |
|
85 |
string.append('%s: %s' % (k, v)) |
|
86 |
return '{%s}' % ', '.join(string) |
|
87 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
88 |
|
0 | 89 |
class EidMismatch(Exception): |
90 |
def __init__(self, varname, value): |
|
91 |
self.varname = varname |
|
92 |
self.value = value |
|
93 |
||
94 |
||
95 |
class Restriction(object): |
|
96 |
supported_operators = ('=',) |
|
97 |
def __init__(self, rel): |
|
98 |
operator = rel.children[1].operator |
|
99 |
if not operator in self.supported_operators: |
|
100 |
raise NotImplementedError('unsupported operator') |
|
101 |
self.rel = rel |
|
102 |
self.operator = operator |
|
103 |
self.rtype = rel.r_type |
|
104 |
self.var = rel.children[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
105 |
|
0 | 106 |
def __repr__(self): |
107 |
return '<%s for %s>' % (self.__class__.__name__, self.rel) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
108 |
|
0 | 109 |
@property |
110 |
def rhs(self): |
|
111 |
return self.rel.children[1].children[0] |
|
112 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
113 |
|
0 | 114 |
class MultipleRestriction(object): |
115 |
def __init__(self, restrictions): |
|
116 |
self.restrictions = restrictions |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
117 |
|
0 | 118 |
def resolve(self, solutions, fixed): |
119 |
return _resolve(self.restrictions, solutions, fixed) |
|
120 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
121 |
|
0 | 122 |
class VariableSelection(Restriction): |
123 |
def __init__(self, rel, dsget, prefix='s'): |
|
124 |
Restriction.__init__(self, rel) |
|
125 |
self._dsget = dsget |
|
126 |
self._not = self.rel.neged(strict=True) |
|
127 |
self._prefix = prefix + '_' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
128 |
|
0 | 129 |
def __repr__(self): |
130 |
return '<%s%s for %s>' % (self._prefix[0], self.__class__.__name__, self.rel) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
131 |
|
0 | 132 |
@property |
133 |
def searched_var(self): |
|
134 |
if self._prefix == 's_': |
|
135 |
return self.var.name |
|
136 |
return self.rhs.name |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
137 |
|
0 | 138 |
@property |
139 |
def constraint_var(self): |
|
140 |
if self._prefix == 's_': |
|
141 |
return self.rhs.name |
|
142 |
return self.var.name |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
143 |
|
0 | 144 |
def _possible_values(self, myvar, ovar, entity, solutions, dsprefix): |
145 |
if self.rtype == 'identity': |
|
146 |
return (entity.key(),) |
|
147 |
value = entity.get(dsprefix + self.rtype) |
|
148 |
if value is None: |
|
149 |
return [] |
|
150 |
if not isinstance(value, list): |
|
151 |
value = [value] |
|
152 |
vartypes = poss_var_types(myvar, ovar, entity.kind(), solutions) |
|
153 |
return (v for v in value if v.kind() in vartypes) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
154 |
|
0 | 155 |
def complete_and_filter(self, solutions, results): |
156 |
myvar = self.rhs.name |
|
157 |
ovar = self.var.name |
|
158 |
rtype = self.rtype |
|
159 |
if self.schema.rschema(rtype).is_final(): |
|
160 |
# should be detected by rql.stcheck: "Any C WHERE NOT X attr C" doesn't make sense |
|
161 |
#if self._not: |
|
162 |
# raise NotImplementedError() |
|
163 |
for result in results: |
|
164 |
result[myvar] = result[ovar].get('s_'+rtype) |
|
165 |
elif self.var.name in results[0]: |
|
166 |
if self.rhs.name in results[0]: |
|
167 |
self.filter(solutions, results) |
|
168 |
else: |
|
169 |
if self._not: |
|
170 |
raise NotImplementedError() |
|
171 |
for result in results[:]: |
|
172 |
values = self._possible_values(myvar, ovar, result[ovar], |
|
173 |
solutions, 's_') |
|
174 |
expand_result(results, result, myvar, values, self._dsget) |
|
175 |
else: |
|
176 |
assert self.rhs.name in results[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
177 |
self.object_complete_and_filter(solutions, results) |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
178 |
|
0 | 179 |
def filter(self, solutions, results): |
180 |
myvar = self.rhs.name |
|
181 |
ovar = self.var.name |
|
182 |
newsols = {} |
|
183 |
for result in results[:]: |
|
184 |
entity = result[ovar] |
|
185 |
key = entity.key() |
|
186 |
if not key in newsols: |
|
187 |
values = self._possible_values(myvar, ovar, entity, solutions, 's_') |
|
188 |
newsols[key] = frozenset(v for v in values) |
|
189 |
if self._not: |
|
190 |
if result[myvar].key() in newsols[key]: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
191 |
results.remove(result) |
0 | 192 |
elif not result[myvar].key() in newsols[key]: |
193 |
results.remove(result) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
194 |
|
0 | 195 |
def object_complete_and_filter(self, solutions, results): |
196 |
if self._not: |
|
197 |
raise NotImplementedError() |
|
198 |
myvar = self.var.name |
|
199 |
ovar = self.rhs.name |
|
200 |
for result in results[:]: |
|
201 |
values = self._possible_values(myvar, ovar, result[ovar], |
|
202 |
solutions, 'o_') |
|
203 |
expand_result(results, result, myvar, values, self._dsget) |
|
204 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
205 |
|
0 | 206 |
class EidRestriction(Restriction): |
207 |
def __init__(self, rel, dsget): |
|
208 |
Restriction.__init__(self, rel) |
|
209 |
self._dsget = dsget |
|
210 |
||
211 |
def resolve(self, kwargs): |
|
212 |
value = self.rel.children[1].children[0].eval(kwargs) |
|
213 |
return self._dsget(value) |
|
214 |
||
215 |
||
216 |
class RelationRestriction(VariableSelection): |
|
217 |
||
218 |
def _get_value(self, fixed): |
|
219 |
return fixed[self.constraint_var].key() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
220 |
|
0 | 221 |
def fill_query(self, fixed, query, operator=None): |
222 |
restr = '%s%s %s' % (self._prefix, self.rtype, operator or self.operator) |
|
223 |
query[restr] = self._get_value(fixed) |
|
224 |
||
225 |
def resolve(self, solutions, fixed): |
|
226 |
if self.rtype == 'identity': |
|
227 |
if self._not: |
|
228 |
raise NotImplementedError() |
|
229 |
return [fixed[self.constraint_var]] |
|
230 |
if self._not: |
|
231 |
return _resolve_not([self], solutions, fixed) |
|
232 |
return _resolve([self], solutions, fixed) |
|
233 |
||
234 |
||
235 |
class NotRelationRestriction(RelationRestriction): |
|
236 |
||
237 |
def _get_value(self, fixed): |
|
238 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
239 |
|
0 | 240 |
def resolve(self, solutions, fixed): |
241 |
if self.rtype == 'identity': |
|
242 |
raise NotImplementedError() |
|
243 |
return _resolve([self], solutions, fixed) |
|
244 |
||
245 |
||
246 |
class AttributeRestriction(RelationRestriction): |
|
247 |
supported_operators = ('=', '>', '>=', '<', '<=', 'ILIKE') |
|
248 |
def __init__(self, rel, kwargs): |
|
249 |
RelationRestriction.__init__(self, rel, None) |
|
250 |
value = self.rhs.eval(kwargs) |
|
251 |
self.value = value |
|
252 |
if self.operator == 'ILIKE': |
|
253 |
if value.startswith('%'): |
|
254 |
raise NotImplementedError('LIKE is only supported for prefix search') |
|
255 |
if not value.endswith('%'): |
|
256 |
raise NotImplementedError('LIKE is only supported for prefix search') |
|
257 |
self.operator = '>' |
|
258 |
self.value = value[:-1] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
259 |
|
0 | 260 |
def complete_and_filter(self, solutions, results): |
261 |
# check lhs var first in case this is a restriction |
|
262 |
assert self._not |
|
263 |
myvar, rtype, value = self.var.name, self.rtype, self.value |
|
264 |
for result in results[:]: |
|
265 |
if result[myvar].get('s_'+rtype) == value: |
|
266 |
results.remove(result) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
267 |
|
0 | 268 |
def _get_value(self, fixed): |
269 |
return self.value |
|
270 |
||
271 |
||
272 |
class DateAttributeRestriction(AttributeRestriction): |
|
273 |
"""just a thin layer on top af `AttributeRestriction` that |
|
274 |
tries to convert date strings such as in : |
|
275 |
Any X WHERE X creation_date >= '2008-03-04' |
|
276 |
""" |
|
277 |
def __init__(self, rel, kwargs): |
|
278 |
super(DateAttributeRestriction, self).__init__(rel, kwargs) |
|
279 |
if isinstance(self.value, basestring): |
|
280 |
# try: |
|
281 |
self.value = datetime.strptime(self.value, '%Y-%m-%d') |
|
282 |
# except Exception, exc: |
|
283 |
# from logging import error |
|
284 |
# error('unable to parse date %s with format %%Y-%%m-%%d (exc=%s)', value, exc) |
|
285 |
||
286 |
||
287 |
class AttributeInRestriction(AttributeRestriction): |
|
288 |
def __init__(self, rel, kwargs): |
|
289 |
RelationRestriction.__init__(self, rel, None) |
|
290 |
values = [] |
|
291 |
for c in self.rel.children[1].iget_nodes(nodes.Constant): |
|
1131
544609e83317
pylint cleanup, no more need for mx datetime conversion
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
292 |
values.append(c.eval(kwargs)) |
0 | 293 |
self.value = values |
294 |
||
295 |
@property |
|
296 |
def operator(self): |
|
297 |
return 'in' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
298 |
|
0 | 299 |
|
300 |
class TypeRestriction(AttributeRestriction): |
|
301 |
def __init__(self, var): |
|
302 |
self.var = var |
|
303 |
||
304 |
def __repr__(self): |
|
305 |
return '<%s for %s>' % (self.__class__.__name__, self.var) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
306 |
|
0 | 307 |
def resolve(self, solutions, fixed): |
308 |
objs = [] |
|
309 |
for etype in frozenset(etypes[self.var.name] for etypes in solutions): |
|
310 |
objs += Query(etype).Run() |
|
311 |
return objs |
|
312 |
||
313 |
||
314 |
def append_result(res, descr, i, j, value, etype): |
|
315 |
if value is not None: |
|
1131
544609e83317
pylint cleanup, no more need for mx datetime conversion
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
316 |
if isinstance(value, Text): |
0 | 317 |
value = unicode(value) |
318 |
elif isinstance(value, Blob): |
|
319 |
value = Binary(str(value)) |
|
320 |
if j == 0: |
|
321 |
res.append([value]) |
|
322 |
descr.append([etype]) |
|
323 |
else: |
|
324 |
res[i].append(value) |
|
325 |
descr[i].append(etype) |
|
326 |
||
327 |
||
328 |
class ValueResolver(object): |
|
329 |
def __init__(self, functions, args, term): |
|
330 |
self.functions = functions |
|
331 |
self.args = args |
|
332 |
self.term = term |
|
333 |
self._solution = self.term.stmt.solutions[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
334 |
|
0 | 335 |
def compute(self, result): |
336 |
"""return (entity type, value) to which self.term is evaluated according |
|
337 |
to the given result dictionnary and to query arguments (self.args) |
|
338 |
""" |
|
339 |
return self.term.accept(self, result) |
|
340 |
||
341 |
def visit_function(self, node, result): |
|
342 |
args = tuple(n.accept(self, result)[1] for n in node.children) |
|
343 |
value = self.functions[node.name](*args) |
|
344 |
return node.get_type(self._solution, self.args), value |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
345 |
|
0 | 346 |
def visit_variableref(self, node, result): |
347 |
value = result[node.name] |
|
348 |
try: |
|
349 |
etype = value.kind() |
|
350 |
value = str(value.key()) |
|
351 |
except AttributeError: |
|
352 |
etype = self._solution[node.name] |
|
353 |
return etype, value |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
354 |
|
0 | 355 |
def visit_constant(self, node, result): |
356 |
return node.get_type(kwargs=self.args), node.eval(self.args) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
357 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
358 |
|
0 | 359 |
class RQLInterpreter(object): |
360 |
"""algorithm: |
|
361 |
1. visit the restriction clauses and collect restriction for each subject |
|
362 |
of a relation. Different restriction types are: |
|
363 |
* EidRestriction |
|
364 |
* AttributeRestriction |
|
365 |
* RelationRestriction |
|
366 |
* VariableSelection (not really a restriction) |
|
367 |
-> dictionary {<variable>: [restriction...], ...} |
|
368 |
2. resolve eid restrictions |
|
369 |
3. for each select in union: |
|
370 |
for each solution in select'solutions: |
|
371 |
1. resolve variables which have attribute restriction |
|
372 |
2. resolve relation restriction |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
373 |
3. resolve selection and add to global results |
0 | 374 |
""" |
375 |
def __init__(self, schema): |
|
376 |
self.schema = schema |
|
377 |
Restriction.schema = schema # yalta! |
|
378 |
self.rqlhelper = RQLHelper(schema, {'eid': etype_from_key}) |
|
379 |
self._stored_proc = {'LOWER': lambda x: x.lower(), |
|
380 |
'UPPER': lambda x: x.upper()} |
|
381 |
for cb in SQL_CONNECT_HOOKS.get('sqlite', []): |
|
382 |
cb(self) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
383 |
|
0 | 384 |
# emulate sqlite connection interface so we can reuse stored procedures |
385 |
def create_function(self, name, nbargs, func): |
|
386 |
self._stored_proc[name] = func |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
387 |
|
0 | 388 |
def create_aggregate(self, name, nbargs, func): |
389 |
self._stored_proc[name] = func |
|
390 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
391 |
|
0 | 392 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
393 |
rqlst = self.rqlhelper.parse(operation, annotate=True) |
|
394 |
try: |
|
395 |
self.rqlhelper.compute_solutions(rqlst, kwargs=parameters) |
|
396 |
except BadKeyError: |
|
397 |
results, description = [], [] |
|
398 |
else: |
|
399 |
results, description = self.interpret(rqlst, parameters) |
|
400 |
return ResultSet(results, operation, parameters, description, rqlst=rqlst) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
401 |
|
0 | 402 |
def interpret(self, node, kwargs, dsget=None): |
403 |
if dsget is None: |
|
404 |
self._dsget = Get |
|
405 |
else: |
|
406 |
self._dsget = dsget |
|
407 |
try: |
|
408 |
return node.accept(self, kwargs) |
|
409 |
except NotImplementedError: |
|
410 |
self.critical('support for query not implemented: %s', node) |
|
411 |
raise |
|
412 |
||
413 |
def visit_union(self, node, kwargs): |
|
414 |
results, description = [], [] |
|
415 |
extra = {'kwargs': kwargs} |
|
416 |
for child in node.children: |
|
417 |
pres, pdescr = self.visit_select(child, extra) |
|
418 |
results += pres |
|
419 |
description += pdescr |
|
420 |
return results, description |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
421 |
|
0 | 422 |
def visit_select(self, node, extra): |
423 |
constraints = {} |
|
424 |
if node.where is not None: |
|
425 |
node.where.accept(self, constraints, extra) |
|
426 |
fixed, toresolve, postresolve, postfilters = {}, {}, {}, [] |
|
427 |
# extract NOT filters |
|
428 |
for vname, restrictions in constraints.items(): |
|
429 |
for restr in restrictions[:]: |
|
430 |
if isinstance(restr, AttributeRestriction) and restr._not: |
|
431 |
postfilters.append(restr) |
|
432 |
restrictions.remove(restr) |
|
433 |
if not restrictions: |
|
434 |
del constraints[vname] |
|
435 |
# add TypeRestriction for variable which have no restrictions at all |
|
436 |
for varname, var in node.defined_vars.iteritems(): |
|
437 |
if not varname in constraints: |
|
438 |
constraints[varname] = [TypeRestriction(var)] |
|
439 |
#print node, constraints |
|
440 |
# compute eid restrictions |
|
441 |
kwargs = extra['kwargs'] |
|
442 |
for varname, restrictions in constraints.iteritems(): |
|
443 |
for restr in restrictions[:]: |
|
444 |
if isinstance(restr, EidRestriction): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
445 |
assert not varname in fixed |
0 | 446 |
try: |
447 |
value = restr.resolve(kwargs) |
|
448 |
fixed[varname] = value |
|
449 |
except EntityNotFoundError: |
|
450 |
return [], [] |
|
451 |
restrictions.remove(restr) |
|
452 |
#print 'fixed', fixed.keys() |
|
453 |
# combine remaining restrictions |
|
454 |
for varname, restrictions in constraints.iteritems(): |
|
455 |
for restr in restrictions: |
|
456 |
if isinstance(restr, AttributeRestriction): |
|
457 |
toresolve.setdefault(varname, []).append(restr) |
|
458 |
elif isinstance(restr, NotRelationRestriction) or ( |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
459 |
isinstance(restr, RelationRestriction) and |
0 | 460 |
not restr.searched_var in fixed and restr.constraint_var in fixed): |
461 |
toresolve.setdefault(varname, []).append(restr) |
|
462 |
else: |
|
463 |
postresolve.setdefault(varname, []).append(restr) |
|
464 |
try: |
|
465 |
if len(toresolve[varname]) > 1: |
|
466 |
toresolve[varname] = MultipleRestriction(toresolve[varname]) |
|
467 |
else: |
|
468 |
toresolve[varname] = toresolve[varname][0] |
|
469 |
except KeyError: |
|
470 |
pass |
|
471 |
#print 'toresolve %s' % toresolve |
|
472 |
#print 'postresolve %s' % postresolve |
|
473 |
# resolve additional restrictions |
|
474 |
if fixed: |
|
475 |
partres = [fixed.copy()] |
|
476 |
else: |
|
477 |
partres = [] |
|
478 |
for varname, restr in toresolve.iteritems(): |
|
479 |
varpartres = partres[:] |
|
480 |
try: |
|
481 |
values = tuple(restr.resolve(node.solutions, fixed)) |
|
482 |
except EidMismatch, ex: |
|
483 |
varname = ex.varname |
|
484 |
value = ex.value |
|
485 |
partres = [res for res in partres if res[varname] != value] |
|
486 |
if partres: |
|
487 |
continue |
|
488 |
# some join failed, no possible results |
|
489 |
return [], [] |
|
490 |
if not values: |
|
491 |
# some join failed, no possible results |
|
492 |
return [], [] |
|
493 |
if not varpartres: |
|
494 |
# init results |
|
495 |
for value in values: |
|
496 |
partres.append({varname: value}) |
|
497 |
elif not varname in partres[0]: |
|
498 |
# cartesian product |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
499 |
for res in partres: |
0 | 500 |
res[varname] = values[0] |
501 |
for res in partres[:]: |
|
502 |
for value in values[1:]: |
|
503 |
res = res.copy() |
|
504 |
res[varname] = value |
|
505 |
partres.append(res) |
|
506 |
else: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
507 |
# union |
0 | 508 |
for res in varpartres: |
509 |
for value in values: |
|
510 |
res = res.copy() |
|
511 |
res[varname] = value |
|
512 |
partres.append(res) |
|
513 |
#print 'partres', len(partres) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
514 |
#print partres |
0 | 515 |
# Note: don't check for empty partres since constant selection may still |
516 |
# produce result at this point |
|
517 |
# sort to get RelationRestriction before AttributeSelection |
|
518 |
restrictions = sorted((restr for restrictions in postresolve.itervalues() |
|
519 |
for restr in restrictions), |
|
520 |
key=lambda x: not isinstance(x, RelationRestriction)) |
|
521 |
# compute stuff not doable in the previous step using datastore queries |
|
522 |
for restr in restrictions + postfilters: |
|
523 |
restr.complete_and_filter(node.solutions, partres) |
|
524 |
if not partres: |
|
525 |
# some join failed, no possible results |
|
526 |
return [], [] |
|
527 |
if extra.pop('has_exists', False): |
|
528 |
# remove potential duplicates introduced by exists |
|
529 |
toremovevars = [v.name for v in node.defined_vars.itervalues() |
|
530 |
if not v.scope is node] |
|
531 |
if toremovevars: |
|
532 |
newpartres = [] |
|
533 |
for result in partres: |
|
534 |
for var in toremovevars: |
|
535 |
del result[var] |
|
536 |
if not result in newpartres: |
|
537 |
newpartres.append(result) |
|
538 |
if not newpartres: |
|
539 |
# some join failed, no possible results |
|
540 |
return [], [] |
|
541 |
partres = newpartres |
|
542 |
if node.orderby: |
|
543 |
for sortterm in reversed(node.orderby): |
|
544 |
resolver = ValueResolver(self._stored_proc, kwargs, sortterm.term) |
|
545 |
partres.sort(reverse=not sortterm.asc, |
|
546 |
key=lambda x: resolver.compute(x)[1]) |
|
547 |
if partres: |
|
548 |
if node.offset: |
|
549 |
partres = partres[node.offset:] |
|
550 |
if node.limit: |
|
551 |
partres = partres[:node.limit] |
|
552 |
if not partres: |
|
553 |
return [], [] |
|
554 |
#print 'completed partres', _print_results(partres) |
|
555 |
# compute results |
|
556 |
res, descr = [], [] |
|
557 |
for j, term in enumerate(node.selection): |
|
558 |
resolver = ValueResolver(self._stored_proc, kwargs, term) |
|
559 |
if not partres: |
|
560 |
etype, value = resolver.compute({}) |
|
561 |
# only constant selected |
|
562 |
if not res: |
|
563 |
res.append([]) |
|
564 |
descr.append([]) |
|
565 |
res[0].append(value) |
|
566 |
descr[0].append(etype) |
|
567 |
else: |
|
568 |
for i, sol in enumerate(partres): |
|
569 |
etype, value = resolver.compute(sol) |
|
570 |
append_result(res, descr, i, j, value, etype) |
|
571 |
#print '--------->', res |
|
572 |
return res, descr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
573 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
574 |
def visit_and(self, node, constraints, extra): |
0 | 575 |
for child in node.children: |
576 |
child.accept(self, constraints, extra) |
|
577 |
def visit_exists(self, node, constraints, extra): |
|
578 |
extra['has_exists'] = True |
|
579 |
self.visit_and(node, constraints, extra) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
580 |
|
0 | 581 |
def visit_not(self, node, constraints, extra): |
582 |
for child in node.children: |
|
583 |
child.accept(self, constraints, extra) |
|
584 |
try: |
|
585 |
extra.pop(node) |
|
586 |
except KeyError: |
|
587 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
588 |
|
0 | 589 |
def visit_relation(self, node, constraints, extra): |
590 |
if node.is_types_restriction(): |
|
591 |
return |
|
592 |
rschema = self.schema.rschema(node.r_type) |
|
593 |
neged = node.neged(strict=True) |
|
594 |
if neged: |
|
595 |
# ok, we *may* process this Not node (not implemented error will be |
|
596 |
# raised later if we can't) |
|
597 |
extra[node.parent] = True |
|
598 |
if rschema.is_final(): |
|
599 |
self._visit_final_relation(rschema, node, constraints, extra) |
|
600 |
elif neged: |
|
601 |
self._visit_non_final_neged_relation(rschema, node, constraints) |
|
602 |
else: |
|
603 |
self._visit_non_final_relation(rschema, node, constraints) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
604 |
|
0 | 605 |
def _visit_non_final_relation(self, rschema, node, constraints, not_=False): |
606 |
lhs, rhs = node.get_variable_parts() |
|
607 |
for v1, v2, prefix in ((lhs, rhs, 's'), (rhs, lhs, 'o')): |
|
608 |
#if not_: |
|
609 |
nbrels = len(v2.variable.stinfo['relations']) |
|
610 |
#else: |
|
611 |
# nbrels = len(v2.variable.stinfo['relations']) - len(v2.variable.stinfo['uidrels']) |
|
612 |
if nbrels > 1: |
|
613 |
constraints.setdefault(v1.name, []).append( |
|
614 |
RelationRestriction(node, self._dsget, prefix)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
615 |
# just init an empty list for v2 variable to avoid a |
0 | 616 |
# TypeRestriction being added for it |
617 |
constraints.setdefault(v2.name, []) |
|
618 |
break |
|
619 |
else: |
|
620 |
constraints.setdefault(rhs.name, []).append( |
|
621 |
VariableSelection(node, self._dsget, 's')) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
622 |
|
0 | 623 |
def _visit_non_final_neged_relation(self, rschema, node, constraints): |
624 |
lhs, rhs = node.get_variable_parts() |
|
625 |
for v1, v2, prefix in ((lhs, rhs, 's'), (rhs, lhs, 'o')): |
|
626 |
stinfo = v2.variable.stinfo |
|
627 |
if not stinfo['selected'] and len(stinfo['relations']) == 1: |
|
628 |
constraints.setdefault(v1.name, []).append( |
|
629 |
NotRelationRestriction(node, self._dsget, prefix)) |
|
630 |
constraints.setdefault(v2.name, []) |
|
631 |
break |
|
632 |
else: |
|
633 |
self._visit_non_final_relation(rschema, node, constraints, True) |
|
634 |
||
635 |
def _visit_final_relation(self, rschema, node, constraints, extra): |
|
636 |
varname = node.children[0].name |
|
637 |
if rschema.type == 'eid': |
|
638 |
constraints.setdefault(varname, []).append( |
|
639 |
EidRestriction(node, self._dsget)) |
|
640 |
else: |
|
641 |
rhs = node.children[1].children[0] |
|
642 |
if isinstance(rhs, nodes.VariableRef): |
|
643 |
constraints.setdefault(rhs.name, []).append( |
|
644 |
VariableSelection(node, self._dsget)) |
|
645 |
elif isinstance(rhs, nodes.Constant): |
|
646 |
if rschema.objects()[0] in ('Datetime', 'Date'): # XXX |
|
647 |
constraints.setdefault(varname, []).append( |
|
648 |
DateAttributeRestriction(node, extra['kwargs'])) |
|
649 |
else: |
|
650 |
constraints.setdefault(varname, []).append( |
|
651 |
AttributeRestriction(node, extra['kwargs'])) |
|
652 |
elif isinstance(rhs, nodes.Function) and rhs.name == 'IN': |
|
653 |
constraints.setdefault(varname, []).append( |
|
654 |
AttributeInRestriction(node, extra['kwargs'])) |
|
655 |
else: |
|
656 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
657 |
|
0 | 658 |
def _not_implemented(self, *args, **kwargs): |
659 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
660 |
|
0 | 661 |
visit_or = _not_implemented |
662 |
# shouldn't occurs |
|
663 |
visit_set = _not_implemented |
|
664 |
visit_insert = _not_implemented |
|
665 |
visit_delete = _not_implemented |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
666 |
|
0 | 667 |
|
668 |
from logging import getLogger |
|
669 |
from cubicweb import set_log_methods |
|
670 |
set_log_methods(RQLInterpreter, getLogger('cubicweb.goa.rqlinterpreter')) |
|
671 |
set_log_methods(Restriction, getLogger('cubicweb.goa.rqlinterpreter')) |