author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 16 Sep 2009 18:14:43 +0200 | |
branch | 3.5 |
changeset 3269 | 02a918f108a7 |
parent 2921 | 8e2544e78a5e |
child 3437 | a30b5b5138a4 |
permissions | -rw-r--r-- |
0 | 1 |
"""plan execution of rql queries on a single source |
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: 2001-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 copy import copy |
|
11 |
||
12 |
from rql.stmts import Union, Select |
|
13 |
from rql.nodes import Constant |
|
14 |
||
15 |
from cubicweb import QueryError, typed_eid |
|
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
16 |
from cubicweb.schema import VIRTUAL_RTYPES |
0 | 17 |
|
18 |
def add_types_restriction(schema, rqlst, newroot=None, solutions=None): |
|
19 |
if newroot is None: |
|
20 |
assert solutions is None |
|
21 |
if hasattr(rqlst, '_types_restr_added'): |
|
22 |
return |
|
23 |
solutions = rqlst.solutions |
|
24 |
newroot = rqlst |
|
25 |
rqlst._types_restr_added = True |
|
26 |
else: |
|
27 |
assert solutions is not None |
|
28 |
rqlst = rqlst.stmt |
|
29 |
eschema = schema.eschema |
|
30 |
allpossibletypes = {} |
|
31 |
for solution in solutions: |
|
32 |
for varname, etype in solution.iteritems(): |
|
33 |
if not varname in newroot.defined_vars or eschema(etype).is_final(): |
|
34 |
continue |
|
35 |
allpossibletypes.setdefault(varname, set()).add(etype) |
|
36 |
for varname in sorted(allpossibletypes): |
|
37 |
try: |
|
38 |
var = newroot.defined_vars[varname] |
|
39 |
except KeyError: |
|
40 |
continue |
|
41 |
stinfo = var.stinfo |
|
42 |
if stinfo.get('uidrels'): |
|
43 |
continue # eid specified, no need for additional type specification |
|
44 |
try: |
|
45 |
typerels = rqlst.defined_vars[varname].stinfo.get('typerels') |
|
46 |
except KeyError: |
|
47 |
assert varname in rqlst.aliases |
|
48 |
continue |
|
49 |
if newroot is rqlst and typerels: |
|
50 |
mytyperel = iter(typerels).next() |
|
51 |
else: |
|
52 |
for vref in newroot.defined_vars[varname].references(): |
|
53 |
rel = vref.relation() |
|
54 |
if rel and rel.is_types_restriction(): |
|
55 |
mytyperel = rel |
|
56 |
break |
|
57 |
else: |
|
58 |
mytyperel = None |
|
59 |
possibletypes = allpossibletypes[varname] |
|
60 |
if mytyperel is not None: |
|
61 |
# variable as already some types restriction. new possible types |
|
62 |
# can only be a subset of existing ones, so only remove no more |
|
63 |
# possible types |
|
64 |
for cst in mytyperel.get_nodes(Constant): |
|
65 |
if not cst.value in possibletypes: |
|
66 |
cst.parent.remove(cst) |
|
67 |
try: |
|
68 |
stinfo['possibletypes'].remove(cst.value) |
|
69 |
except KeyError: |
|
70 |
# restriction on a type not used by this query, may |
|
71 |
# occurs with X is IN(...) |
|
72 |
pass |
|
73 |
else: |
|
74 |
# we have to add types restriction |
|
75 |
if stinfo.get('scope') is not None: |
|
76 |
rel = var.scope.add_type_restriction(var, possibletypes) |
|
77 |
else: |
|
78 |
# tree is not annotated yet, no scope set so add the restriction |
|
79 |
# to the root |
|
80 |
rel = newroot.add_type_restriction(var, possibletypes) |
|
81 |
stinfo['typerels'] = frozenset((rel,)) |
|
82 |
stinfo['possibletypes'] = possibletypes |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
83 |
|
0 | 84 |
class SSPlanner(object): |
85 |
"""SingleSourcePlanner: build execution plan for rql queries |
|
86 |
||
87 |
optimized for single source repositories |
|
88 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
89 |
|
0 | 90 |
def __init__(self, schema, rqlhelper): |
91 |
self.schema = schema |
|
92 |
self.rqlhelper = rqlhelper |
|
93 |
||
94 |
def build_plan(self, plan): |
|
95 |
"""build an execution plan from a RQL query |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
96 |
|
0 | 97 |
do nothing here, dispatch according to the statement type |
98 |
""" |
|
99 |
build_plan = getattr(self, 'build_%s_plan' % plan.rqlst.TYPE) |
|
100 |
for step in build_plan(plan, plan.rqlst): |
|
101 |
plan.add_step(step) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
102 |
|
0 | 103 |
def build_select_plan(self, plan, rqlst): |
104 |
"""build execution plan for a SELECT RQL query. Suppose only one source |
|
105 |
is available and so avoid work need for query decomposition among sources |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
106 |
|
0 | 107 |
the rqlst should not be tagged at this point. |
108 |
""" |
|
109 |
plan.preprocess(rqlst) |
|
110 |
return (OneFetchStep(plan, rqlst, plan.session.repo.sources),) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
111 |
|
0 | 112 |
def build_insert_plan(self, plan, rqlst): |
113 |
"""get an execution plan from an INSERT RQL query""" |
|
114 |
# each variable in main variables is a new entity to insert |
|
115 |
to_build = {} |
|
116 |
session = plan.session |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
117 |
etype_class = session.vreg['etypes'].etype_class |
0 | 118 |
for etype, var in rqlst.main_variables: |
119 |
# need to do this since entity class is shared w. web client code ! |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
120 |
to_build[var.name] = etype_class(etype)(session) |
0 | 121 |
plan.add_entity_def(to_build[var.name]) |
122 |
# add constant values to entity def, mark variables to be selected |
|
123 |
to_select = plan.relation_definitions(rqlst, to_build) |
|
124 |
# add necessary steps to add relations and update attributes |
|
125 |
step = InsertStep(plan) # insert each entity and its relations |
|
126 |
step.children += self._compute_relation_steps(plan, rqlst.solutions, |
|
127 |
rqlst.where, to_select) |
|
128 |
return (step,) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
129 |
|
0 | 130 |
def _compute_relation_steps(self, plan, solutions, restriction, to_select): |
131 |
"""handle the selection of relations for an insert query""" |
|
132 |
for edef, rdefs in to_select.items(): |
|
133 |
# create a select rql st to fetch needed data |
|
134 |
select = Select() |
|
135 |
eschema = edef.e_schema |
|
136 |
for i in range(len(rdefs)): |
|
137 |
rtype, term, reverse = rdefs[i] |
|
138 |
select.append_selected(term.copy(select)) |
|
139 |
if reverse: |
|
140 |
rdefs[i] = rtype, RelationsStep.REVERSE_RELATION |
|
141 |
else: |
|
142 |
rschema = eschema.subject_relation(rtype) |
|
143 |
if rschema.is_final() or rschema.inlined: |
|
144 |
rdefs[i] = rtype, RelationsStep.FINAL |
|
145 |
else: |
|
146 |
rdefs[i] = rtype, RelationsStep.RELATION |
|
147 |
if restriction is not None: |
|
148 |
select.set_where(restriction.copy(select)) |
|
149 |
step = RelationsStep(plan, edef, rdefs) |
|
150 |
step.children += self._select_plan(plan, select, solutions) |
|
151 |
yield step |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
152 |
|
0 | 153 |
def build_delete_plan(self, plan, rqlst): |
154 |
"""get an execution plan from a DELETE RQL query""" |
|
155 |
# build a select query to fetch entities to delete |
|
156 |
steps = [] |
|
157 |
for etype, var in rqlst.main_variables: |
|
158 |
step = DeleteEntitiesStep(plan) |
|
159 |
step.children += self._sel_variable_step(plan, rqlst.solutions, |
|
160 |
rqlst.where, etype, var) |
|
161 |
steps.append(step) |
|
162 |
for relation in rqlst.main_relations: |
|
163 |
step = DeleteRelationsStep(plan, relation.r_type) |
|
164 |
step.children += self._sel_relation_steps(plan, rqlst.solutions, |
|
165 |
rqlst.where, relation) |
|
166 |
steps.append(step) |
|
167 |
return steps |
|
168 |
||
169 |
def _sel_variable_step(self, plan, solutions, restriction, etype, varref): |
|
170 |
"""handle the selection of variables for a delete query""" |
|
171 |
select = Select() |
|
172 |
varref = varref.copy(select) |
|
173 |
select.defined_vars = {varref.name: varref.variable} |
|
174 |
select.append_selected(varref) |
|
175 |
if restriction is not None: |
|
176 |
select.set_where(restriction.copy(select)) |
|
177 |
if etype != 'Any': |
|
178 |
select.add_type_restriction(varref.variable, etype) |
|
179 |
return self._select_plan(plan, select, solutions) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
180 |
|
0 | 181 |
def _sel_relation_steps(self, plan, solutions, restriction, relation): |
182 |
"""handle the selection of relations for a delete query""" |
|
183 |
select = Select() |
|
184 |
lhs, rhs = relation.get_variable_parts() |
|
185 |
select.append_selected(lhs.copy(select)) |
|
186 |
select.append_selected(rhs.copy(select)) |
|
187 |
select.set_where(relation.copy(select)) |
|
188 |
if restriction is not None: |
|
189 |
select.add_restriction(restriction.copy(select)) |
|
190 |
return self._select_plan(plan, select, solutions) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
191 |
|
0 | 192 |
def build_set_plan(self, plan, rqlst): |
193 |
"""get an execution plan from an SET RQL query""" |
|
194 |
select = Select() |
|
195 |
# extract variables to add to the selection |
|
196 |
selected_index = {} |
|
197 |
index = 0 |
|
198 |
relations, attrrelations = [], [] |
|
199 |
getrschema = self.schema.rschema |
|
200 |
for relation in rqlst.main_relations: |
|
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
201 |
if relation.r_type in VIRTUAL_RTYPES: |
0 | 202 |
raise QueryError('can not assign to %r relation' |
203 |
% relation.r_type) |
|
204 |
lhs, rhs = relation.get_variable_parts() |
|
205 |
if not lhs.as_string('utf-8') in selected_index: |
|
206 |
select.append_selected(lhs.copy(select)) |
|
207 |
selected_index[lhs.as_string('utf-8')] = index |
|
208 |
index += 1 |
|
209 |
if not rhs.as_string('utf-8') in selected_index: |
|
210 |
select.append_selected(rhs.copy(select)) |
|
211 |
selected_index[rhs.as_string('utf-8')] = index |
|
212 |
index += 1 |
|
213 |
rschema = getrschema(relation.r_type) |
|
214 |
if rschema.is_final() or rschema.inlined: |
|
215 |
attrrelations.append(relation) |
|
216 |
else: |
|
217 |
relations.append(relation) |
|
218 |
# add step necessary to fetch all selected variables values |
|
219 |
if rqlst.where is not None: |
|
220 |
select.set_where(rqlst.where.copy(select)) |
|
221 |
# set distinct to avoid potential duplicate key error |
|
222 |
select.distinct = True |
|
223 |
step = UpdateStep(plan, attrrelations, relations, selected_index) |
|
224 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
|
225 |
return (step,) |
|
226 |
||
227 |
# internal methods ######################################################## |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
228 |
|
0 | 229 |
def _select_plan(self, plan, select, solutions): |
230 |
union = Union() |
|
231 |
union.append(select) |
|
232 |
select.clean_solutions(solutions) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
233 |
add_types_restriction(self.schema, select) |
0 | 234 |
self.rqlhelper.annotate(union) |
235 |
return self.build_select_plan(plan, union) |
|
236 |
||
237 |
||
238 |
# execution steps and helper functions ######################################## |
|
239 |
||
240 |
def varmap_test_repr(varmap, tablesinorder): |
|
241 |
if varmap is None: |
|
242 |
return varmap |
|
243 |
maprepr = {} |
|
244 |
for var, sql in varmap.iteritems(): |
|
245 |
table, col = sql.split('.') |
|
246 |
maprepr[var] = '%s.%s' % (tablesinorder[table], col) |
|
247 |
return maprepr |
|
248 |
||
249 |
def offset_result(offset, result): |
|
250 |
offset -= len(result) |
|
251 |
if offset < 0: |
|
252 |
result = result[offset:] |
|
253 |
offset = None |
|
254 |
elif offset == 0: |
|
255 |
offset = None |
|
256 |
result = () |
|
257 |
return offset, result |
|
258 |
||
259 |
||
260 |
class LimitOffsetMixIn(object): |
|
261 |
limit = offset = None |
|
262 |
def set_limit_offset(self, limit, offset): |
|
263 |
self.limit = limit |
|
264 |
self.offset = offset or None |
|
265 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
266 |
|
0 | 267 |
class Step(object): |
268 |
"""base abstract class for execution step""" |
|
269 |
def __init__(self, plan): |
|
270 |
self.plan = plan |
|
271 |
self.children = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
272 |
|
0 | 273 |
def execute_child(self): |
274 |
assert len(self.children) == 1 |
|
275 |
return self.children[0].execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
276 |
|
0 | 277 |
def execute_children(self): |
278 |
for step in self.children: |
|
279 |
step.execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
280 |
|
0 | 281 |
def execute(self): |
282 |
"""execute this step and store partial (eg this step) results""" |
|
283 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
284 |
|
0 | 285 |
def mytest_repr(self): |
286 |
"""return a representation of this step suitable for test""" |
|
287 |
return (self.__class__.__name__,) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
288 |
|
0 | 289 |
def test_repr(self): |
290 |
"""return a representation of this step suitable for test""" |
|
291 |
return self.mytest_repr() + ( |
|
292 |
[step.test_repr() for step in self.children],) |
|
293 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
294 |
|
0 | 295 |
class OneFetchStep(LimitOffsetMixIn, Step): |
296 |
"""step consisting in fetching data from sources and directly returning |
|
297 |
results |
|
298 |
""" |
|
299 |
def __init__(self, plan, union, sources, inputmap=None): |
|
300 |
Step.__init__(self, plan) |
|
301 |
self.union = union |
|
302 |
self.sources = sources |
|
303 |
self.inputmap = inputmap |
|
304 |
self.set_limit_offset(union.children[-1].limit, union.children[-1].offset) |
|
305 |
||
306 |
def set_limit_offset(self, limit, offset): |
|
307 |
LimitOffsetMixIn.set_limit_offset(self, limit, offset) |
|
308 |
for select in self.union.children: |
|
309 |
select.limit = limit |
|
310 |
select.offset = offset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
311 |
|
0 | 312 |
def execute(self): |
313 |
"""call .syntax_tree_search with the given syntax tree on each |
|
314 |
source for each solution |
|
315 |
""" |
|
316 |
self.execute_children() |
|
317 |
session = self.plan.session |
|
318 |
args = self.plan.args |
|
319 |
inputmap = self.inputmap |
|
320 |
union = self.union |
|
321 |
# do we have to use a inputmap from a previous step ? If so disable |
|
322 |
# cachekey |
|
323 |
if inputmap or self.plan.cache_key is None: |
|
324 |
cachekey = None |
|
325 |
# union may have been splited into subqueries, rebuild a cache key |
|
326 |
elif isinstance(self.plan.cache_key, tuple): |
|
327 |
cachekey = list(self.plan.cache_key) |
|
328 |
cachekey[0] = union.as_string() |
|
329 |
cachekey = tuple(cachekey) |
|
330 |
else: |
|
331 |
cachekey = union.as_string() |
|
332 |
result = [] |
|
333 |
# limit / offset processing |
|
334 |
limit = self.limit |
|
335 |
offset = self.offset |
|
336 |
if offset is not None: |
|
337 |
if len(self.sources) > 1: |
|
338 |
# we'll have to deal with limit/offset by ourself |
|
339 |
if union.children[-1].limit: |
|
340 |
union.children[-1].limit = limit + offset |
|
341 |
union.children[-1].offset = None |
|
342 |
else: |
|
343 |
offset, limit = None, None |
|
344 |
for source in self.sources: |
|
345 |
if offset is None and limit is not None: |
|
346 |
# modifying the sample rqlst is enough since sql generation |
|
347 |
# will pick it here as well |
|
348 |
union.children[-1].limit = limit - len(result) |
|
349 |
result_ = source.syntax_tree_search(session, union, args, cachekey, |
|
350 |
inputmap) |
|
351 |
if offset is not None: |
|
352 |
offset, result_ = offset_result(offset, result_) |
|
353 |
result += result_ |
|
354 |
if limit is not None: |
|
355 |
if len(result) >= limit: |
|
356 |
return result[:limit] |
|
357 |
#print 'ONEFETCH RESULT %s' % (result) |
|
358 |
return result |
|
359 |
||
360 |
def mytest_repr(self): |
|
361 |
"""return a representation of this step suitable for test""" |
|
362 |
try: |
|
363 |
inputmap = varmap_test_repr(self.inputmap, self.plan.tablesinorder) |
|
364 |
except AttributeError: |
|
365 |
inputmap = self.inputmap |
|
366 |
return (self.__class__.__name__, |
|
367 |
sorted((r.as_string(kwargs=self.plan.args), r.solutions) |
|
368 |
for r in self.union.children), |
|
369 |
self.limit, self.offset, |
|
370 |
sorted(self.sources), inputmap) |
|
371 |
||
372 |
||
373 |
# UPDATE/INSERT/DELETE steps ################################################## |
|
374 |
||
375 |
class RelationsStep(Step): |
|
376 |
"""step consisting in adding attributes/relations to entity defs from a |
|
377 |
previous FetchStep |
|
378 |
||
379 |
relations values comes from the latest result, with one columns for |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
380 |
each relation defined in self.rdefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
381 |
|
0 | 382 |
for one entity definition, we'll construct N entity, where N is the |
383 |
number of the latest result |
|
384 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
385 |
|
0 | 386 |
FINAL = 0 |
387 |
RELATION = 1 |
|
388 |
REVERSE_RELATION = 2 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
389 |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
390 |
def __init__(self, plan, edef, rdefs): |
0 | 391 |
Step.__init__(self, plan) |
392 |
# partial entity definition to expand |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
393 |
self.edef = edef |
0 | 394 |
# definition of relations to complete |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
395 |
self.rdefs = rdefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
396 |
|
0 | 397 |
def execute(self): |
398 |
"""execute this step""" |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
399 |
base_edef = self.edef |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
400 |
edefs = [] |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
401 |
result = self.execute_child() |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
402 |
for row in result: |
0 | 403 |
# get a new entity definition for this row |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
404 |
edef = copy(base_edef) |
0 | 405 |
# complete this entity def using row values |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
406 |
for i in range(len(self.rdefs)): |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
407 |
rtype, rorder = self.rdefs[i] |
0 | 408 |
if rorder == RelationsStep.FINAL: |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
409 |
edef[rtype] = row[i] |
0 | 410 |
elif rorder == RelationsStep.RELATION: |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
411 |
self.plan.add_relation_def( (edef, rtype, row[i]) ) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
412 |
edef.querier_pending_relations[(rtype, 'subject')] = row[i] |
0 | 413 |
else: |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
414 |
self.plan.add_relation_def( (row[i], rtype, edef) ) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
415 |
edef.querier_pending_relations[(rtype, 'object')] = row[i] |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
416 |
edefs.append(edef) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
417 |
self.plan.substitute_entity_def(base_edef, edefs) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
418 |
return result |
0 | 419 |
|
420 |
||
421 |
class InsertStep(Step): |
|
422 |
"""step consisting in inserting new entities / relations""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
423 |
|
0 | 424 |
def execute(self): |
425 |
"""execute this step""" |
|
426 |
for step in self.children: |
|
427 |
assert isinstance(step, RelationsStep) |
|
428 |
step.plan = self.plan |
|
429 |
step.execute() |
|
430 |
# insert entities first |
|
431 |
result = self.plan.insert_entity_defs() |
|
432 |
# then relation |
|
433 |
self.plan.insert_relation_defs() |
|
434 |
# return eids of inserted entities |
|
435 |
return result |
|
436 |
||
437 |
||
438 |
class DeleteEntitiesStep(Step): |
|
439 |
"""step consisting in deleting entities""" |
|
440 |
||
441 |
def execute(self): |
|
442 |
"""execute this step""" |
|
443 |
todelete = frozenset(typed_eid(eid) for eid, in self.execute_child()) |
|
444 |
session = self.plan.session |
|
445 |
delete = session.repo.glob_delete_entity |
|
446 |
# register pending eids first to avoid multiple deletion |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
447 |
pending = session.transaction_data.setdefault('pendingeids', set()) |
0 | 448 |
actual = todelete - pending |
449 |
pending |= actual |
|
450 |
for eid in actual: |
|
451 |
delete(session, eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
452 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
453 |
|
0 | 454 |
class DeleteRelationsStep(Step): |
455 |
"""step consisting in deleting relations""" |
|
456 |
||
457 |
def __init__(self, plan, rtype): |
|
458 |
Step.__init__(self, plan) |
|
459 |
self.rtype = rtype |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
460 |
|
0 | 461 |
def execute(self): |
462 |
"""execute this step""" |
|
463 |
session = self.plan.session |
|
464 |
delete = session.repo.glob_delete_relation |
|
465 |
for subj, obj in self.execute_child(): |
|
466 |
delete(session, subj, self.rtype, obj) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
467 |
|
0 | 468 |
|
469 |
class UpdateStep(Step): |
|
470 |
"""step consisting in updating entities / adding relations from relations |
|
471 |
definitions and from results fetched in previous step |
|
472 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
473 |
|
0 | 474 |
def __init__(self, plan, attribute_relations, relations, selected_index): |
475 |
Step.__init__(self, plan) |
|
476 |
self.attribute_relations = attribute_relations |
|
477 |
self.relations = relations |
|
478 |
self.selected_index = selected_index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
479 |
|
0 | 480 |
def execute(self): |
481 |
"""execute this step""" |
|
482 |
plan = self.plan |
|
483 |
session = self.plan.session |
|
484 |
repo = session.repo |
|
485 |
edefs = {} |
|
486 |
# insert relations |
|
2875
b7399ef8b3e0
towards better edited attributes handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
487 |
attributes = set([relation.r_type for relation in self.attribute_relations]) |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
488 |
result = self.execute_child() |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
489 |
for row in result: |
0 | 490 |
for relation in self.attribute_relations: |
491 |
lhs, rhs = relation.get_variable_parts() |
|
492 |
eid = typed_eid(row[self.selected_index[str(lhs)]]) |
|
493 |
try: |
|
494 |
edef = edefs[eid] |
|
495 |
except KeyError: |
|
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
496 |
edefs[eid] = edef = session.entity_from_eid(eid) |
0 | 497 |
if isinstance(rhs, Constant): |
498 |
# add constant values to entity def |
|
499 |
value = rhs.eval(plan.args) |
|
500 |
edef[relation.r_type] = value |
|
501 |
else: |
|
502 |
edef[relation.r_type] = row[self.selected_index[str(rhs)]] |
|
503 |
for relation in self.relations: |
|
504 |
subj = row[self.selected_index[str(relation.children[0])]] |
|
505 |
obj = row[self.selected_index[str(relation.children[1])]] |
|
506 |
repo.glob_add_relation(session, subj, relation.r_type, obj) |
|
507 |
# update entities |
|
508 |
for eid, edef in edefs.iteritems(): |
|
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
509 |
repo.glob_update_entity(session, edef, attributes) |
0 | 510 |
return result |