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