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