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