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