author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 04 Aug 2009 17:39:02 +0200 | |
changeset 2682 | 0188a4a02403 |
parent 2667 | c8aa82538d8e |
child 2875 | b7399ef8b3e0 |
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 |
|
380 |
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
|
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 |
|
0 | 390 |
def __init__(self, plan, e_def, r_defs): |
391 |
Step.__init__(self, plan) |
|
392 |
# partial entity definition to expand |
|
393 |
self.e_def = e_def |
|
394 |
# definition of relations to complete |
|
395 |
self.r_defs = r_defs |
|
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""" |
|
399 |
base_e_def = self.e_def |
|
400 |
result = [] |
|
401 |
for row in self.execute_child(): |
|
402 |
# get a new entity definition for this row |
|
403 |
e_def = copy(base_e_def) |
|
404 |
# complete this entity def using row values |
|
405 |
for i in range(len(self.r_defs)): |
|
406 |
rtype, rorder = self.r_defs[i] |
|
407 |
if rorder == RelationsStep.FINAL: |
|
408 |
e_def[rtype] = row[i] |
|
409 |
elif rorder == RelationsStep.RELATION: |
|
410 |
self.plan.add_relation_def( (e_def, rtype, row[i]) ) |
|
411 |
e_def.querier_pending_relations[(rtype, 'subject')] = row[i] |
|
412 |
else: |
|
413 |
self.plan.add_relation_def( (row[i], rtype, e_def) ) |
|
414 |
e_def.querier_pending_relations[(rtype, 'object')] = row[i] |
|
415 |
result.append(e_def) |
|
416 |
self.plan.substitute_entity_def(base_e_def, result) |
|
417 |
||
418 |
||
419 |
class InsertStep(Step): |
|
420 |
"""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
|
421 |
|
0 | 422 |
def execute(self): |
423 |
"""execute this step""" |
|
424 |
for step in self.children: |
|
425 |
assert isinstance(step, RelationsStep) |
|
426 |
step.plan = self.plan |
|
427 |
step.execute() |
|
428 |
# insert entities first |
|
429 |
result = self.plan.insert_entity_defs() |
|
430 |
# then relation |
|
431 |
self.plan.insert_relation_defs() |
|
432 |
# return eids of inserted entities |
|
433 |
return result |
|
434 |
||
435 |
||
436 |
class DeleteEntitiesStep(Step): |
|
437 |
"""step consisting in deleting entities""" |
|
438 |
||
439 |
def execute(self): |
|
440 |
"""execute this step""" |
|
441 |
todelete = frozenset(typed_eid(eid) for eid, in self.execute_child()) |
|
442 |
session = self.plan.session |
|
443 |
delete = session.repo.glob_delete_entity |
|
444 |
# 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
|
445 |
pending = session.transaction_data.setdefault('pendingeids', set()) |
0 | 446 |
actual = todelete - pending |
447 |
pending |= actual |
|
448 |
for eid in actual: |
|
449 |
delete(session, eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
450 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
451 |
|
0 | 452 |
class DeleteRelationsStep(Step): |
453 |
"""step consisting in deleting relations""" |
|
454 |
||
455 |
def __init__(self, plan, rtype): |
|
456 |
Step.__init__(self, plan) |
|
457 |
self.rtype = rtype |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
458 |
|
0 | 459 |
def execute(self): |
460 |
"""execute this step""" |
|
461 |
session = self.plan.session |
|
462 |
delete = session.repo.glob_delete_relation |
|
463 |
for subj, obj in self.execute_child(): |
|
464 |
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
|
465 |
|
0 | 466 |
|
467 |
class UpdateStep(Step): |
|
468 |
"""step consisting in updating entities / adding relations from relations |
|
469 |
definitions and from results fetched in previous step |
|
470 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
471 |
|
0 | 472 |
def __init__(self, plan, attribute_relations, relations, selected_index): |
473 |
Step.__init__(self, plan) |
|
474 |
self.attribute_relations = attribute_relations |
|
475 |
self.relations = relations |
|
476 |
self.selected_index = selected_index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
477 |
|
0 | 478 |
def execute(self): |
479 |
"""execute this step""" |
|
480 |
plan = self.plan |
|
481 |
session = self.plan.session |
|
482 |
repo = session.repo |
|
483 |
edefs = {} |
|
484 |
# insert relations |
|
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
485 |
attributes = [relation.r_type for relation in self.attribute_relations] |
0 | 486 |
for row in self.execute_child(): |
487 |
for relation in self.attribute_relations: |
|
488 |
lhs, rhs = relation.get_variable_parts() |
|
489 |
eid = typed_eid(row[self.selected_index[str(lhs)]]) |
|
490 |
try: |
|
491 |
edef = edefs[eid] |
|
492 |
except KeyError: |
|
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
493 |
edefs[eid] = edef = session.entity_from_eid(eid) |
0 | 494 |
if isinstance(rhs, Constant): |
495 |
# add constant values to entity def |
|
496 |
value = rhs.eval(plan.args) |
|
497 |
edef[relation.r_type] = value |
|
498 |
else: |
|
499 |
edef[relation.r_type] = row[self.selected_index[str(rhs)]] |
|
500 |
for relation in self.relations: |
|
501 |
subj = row[self.selected_index[str(relation.children[0])]] |
|
502 |
obj = row[self.selected_index[str(relation.children[1])]] |
|
503 |
repo.glob_add_relation(session, subj, relation.r_type, obj) |
|
504 |
# update entities |
|
505 |
result = [] |
|
506 |
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
|
507 |
repo.glob_update_entity(session, edef, attributes) |
0 | 508 |
result.append( (eid,) ) |
509 |
return result |