author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 04 Feb 2013 14:39:55 +0100 | |
branch | oldstable |
changeset 8858 | 51fdbbbd07b2 |
parent 8127 | 96d343a5e01b |
child 8562 | 0d2fb4604265 |
child 8853 | f61755c05c89 |
permissions | -rw-r--r-- |
7118
e094b3d4eb95
[server] move EditedEntity class to its own module, to avoid cyclic dependency when needed from e.g. session.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6893
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5213
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5821
656c974961c4
[rql2sql] #1089207: do not encode Unicode strings from RQL statements to db encoding
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
18 |
"""plan execution of rql queries on a single source""" |
0 | 19 |
|
4869
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
20 |
from __future__ import with_statement |
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
21 |
|
0 | 22 |
__docformat__ = "restructuredtext en" |
23 |
||
24 |
from rql.stmts import Union, Select |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
25 |
from rql.nodes import Constant, Relation |
0 | 26 |
|
27 |
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
|
28 |
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
|
29 |
from cubicweb.rqlrewrite import add_types_restriction |
4869
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
30 |
from cubicweb.server.session import security_enabled |
7118
e094b3d4eb95
[server] move EditedEntity class to its own module, to avoid cyclic dependency when needed from e.g. session.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6893
diff
changeset
|
31 |
from cubicweb.server.edition import EditedEntity |
0 | 32 |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
33 |
READ_ONLY_RTYPES = set(('eid', 'has_text', 'is', 'is_instance_of', 'identity')) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
34 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
35 |
_CONSTANT = object() |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
36 |
_FROM_SUBSTEP = object() |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
37 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
38 |
def _extract_const_attributes(plan, rqlst, to_build): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
39 |
"""add constant values to entity def, mark variables to be selected |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
40 |
""" |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
41 |
to_select = {} |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
42 |
for relation in rqlst.main_relations: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
43 |
lhs, rhs = relation.get_variable_parts() |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
44 |
rtype = relation.r_type |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
45 |
if rtype in READ_ONLY_RTYPES: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
46 |
raise QueryError("can't assign to %s" % rtype) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
47 |
try: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
48 |
edef = to_build[str(lhs)] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
49 |
except KeyError: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
50 |
# lhs var is not to build, should be selected and added as an |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
51 |
# object relation |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
52 |
edef = to_build[str(rhs)] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
53 |
to_select.setdefault(edef, []).append((rtype, lhs, 1)) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
54 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
55 |
if isinstance(rhs, Constant) and not rhs.uid: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
56 |
# add constant values to entity def |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
57 |
value = rhs.eval(plan.args) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
58 |
eschema = edef.entity.e_schema |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
59 |
attrtype = eschema.subjrels[rtype].objects(eschema)[0] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
60 |
if attrtype == 'Password' and isinstance(value, unicode): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
61 |
value = value.encode('UTF8') |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
62 |
edef.edited_attribute(rtype, value) |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
63 |
elif to_build.has_key(str(rhs)): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
64 |
# create a relation between two newly created variables |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
65 |
plan.add_relation_def((edef, rtype, to_build[rhs.name])) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
66 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
67 |
to_select.setdefault(edef, []).append( (rtype, rhs, 0) ) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
68 |
return to_select |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
69 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
70 |
def _extract_eid_consts(plan, rqlst): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
71 |
"""return a dict mapping rqlst variable object to their eid if specified in |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
72 |
the syntax tree |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
73 |
""" |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
74 |
session = plan.session |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
75 |
if rqlst.where is None: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
76 |
return {} |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
77 |
eidconsts = {} |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
78 |
neweids = session.transaction_data.get('neweids', ()) |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4795
diff
changeset
|
79 |
checkread = session.read_security |
4869
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
80 |
eschema = session.vreg.schema.eschema |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
81 |
for rel in rqlst.where.get_nodes(Relation): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
82 |
if rel.r_type == 'eid' and not rel.neged(strict=True): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
83 |
lhs, rhs = rel.get_variable_parts() |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
84 |
if isinstance(rhs, Constant): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
85 |
eid = typed_eid(rhs.eval(plan.args)) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
86 |
# check read permission here since it may not be done by |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
87 |
# the generated select substep if not emited (eg nothing |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
88 |
# to be selected) |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4795
diff
changeset
|
89 |
if checkread and eid not in neweids: |
4869
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
90 |
with security_enabled(session, read=False): |
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
91 |
eschema(session.describe(eid)[0]).check_perm( |
230ace4d68c0
[write security] we must check perm with read security disabled + add missing eid argument to check_perm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
92 |
session, 'read', eid=eid) |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
93 |
eidconsts[lhs.variable] = eid |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
94 |
return eidconsts |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
95 |
|
4795
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
96 |
def _build_substep_query(select, origrqlst): |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
97 |
"""Finalize substep select query that should be executed to get proper |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
98 |
selection of stuff to insert/update. |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
99 |
|
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
100 |
Return None when no query actually needed, else the given select node that |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
101 |
will be used as substep query. |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
102 |
|
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
103 |
When select has nothing selected, search in origrqlst for restriction that |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
104 |
should be considered. |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
105 |
""" |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
106 |
if origrqlst.where is not None and not select.selection: |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
107 |
# no selection, append one randomly by searching for a relation which is |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
108 |
# neither a type restriction (is) nor an eid specification (not neged |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
109 |
# eid with constant node) |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
110 |
for rel in origrqlst.where.iget_nodes(Relation): |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
111 |
if rel.neged(strict=True) or not ( |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
112 |
rel.is_types_restriction() or |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
113 |
(rel.r_type == 'eid' |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
114 |
and isinstance(rel.get_variable_parts()[1], Constant))): |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
115 |
select.append_selected(rel.children[0].copy(select)) |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
116 |
break |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
117 |
else: |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
118 |
return |
4795
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
119 |
if select.selection: |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
120 |
if origrqlst.where is not None: |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
121 |
select.set_where(origrqlst.where.copy(select)) |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
122 |
if getattr(origrqlst, 'having', None): |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
123 |
select.set_having([sq.copy(select) for sq in origrqlst.having]) |
4795
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
124 |
return select |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
125 |
return None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
126 |
|
0 | 127 |
class SSPlanner(object): |
128 |
"""SingleSourcePlanner: build execution plan for rql queries |
|
129 |
||
130 |
optimized for single source repositories |
|
131 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
132 |
|
0 | 133 |
def __init__(self, schema, rqlhelper): |
134 |
self.schema = schema |
|
135 |
self.rqlhelper = rqlhelper |
|
136 |
||
137 |
def build_plan(self, plan): |
|
138 |
"""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
|
139 |
|
0 | 140 |
do nothing here, dispatch according to the statement type |
141 |
""" |
|
142 |
build_plan = getattr(self, 'build_%s_plan' % plan.rqlst.TYPE) |
|
143 |
for step in build_plan(plan, plan.rqlst): |
|
144 |
plan.add_step(step) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
145 |
|
0 | 146 |
def build_select_plan(self, plan, rqlst): |
147 |
"""build execution plan for a SELECT RQL query. Suppose only one source |
|
148 |
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
|
149 |
|
0 | 150 |
the rqlst should not be tagged at this point. |
151 |
""" |
|
152 |
plan.preprocess(rqlst) |
|
153 |
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
|
154 |
|
0 | 155 |
def build_insert_plan(self, plan, rqlst): |
156 |
"""get an execution plan from an INSERT RQL query""" |
|
157 |
# each variable in main variables is a new entity to insert |
|
158 |
to_build = {} |
|
159 |
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
|
160 |
etype_class = session.vreg['etypes'].etype_class |
0 | 161 |
for etype, var in rqlst.main_variables: |
162 |
# need to do this since entity class is shared w. web client code ! |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
163 |
to_build[var.name] = EditedEntity(etype_class(etype)(session)) |
0 | 164 |
plan.add_entity_def(to_build[var.name]) |
165 |
# add constant values to entity def, mark variables to be selected |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
166 |
to_select = _extract_const_attributes(plan, rqlst, to_build) |
0 | 167 |
# add necessary steps to add relations and update attributes |
168 |
step = InsertStep(plan) # insert each entity and its relations |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
169 |
step.children += self._compute_relation_steps(plan, rqlst, to_select) |
0 | 170 |
return (step,) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
171 |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
172 |
def _compute_relation_steps(self, plan, rqlst, to_select): |
0 | 173 |
"""handle the selection of relations for an insert query""" |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
174 |
eidconsts = _extract_eid_consts(plan, rqlst) |
0 | 175 |
for edef, rdefs in to_select.items(): |
176 |
# create a select rql st to fetch needed data |
|
177 |
select = Select() |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
178 |
eschema = edef.entity.e_schema |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
179 |
for i, (rtype, term, reverse) in enumerate(rdefs): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
180 |
if getattr(term, 'variable', None) in eidconsts: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
181 |
value = eidconsts[term.variable] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
182 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
183 |
select.append_selected(term.copy(select)) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
184 |
value = _FROM_SUBSTEP |
0 | 185 |
if reverse: |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
186 |
rdefs[i] = (rtype, InsertRelationsStep.REVERSE_RELATION, value) |
0 | 187 |
else: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
188 |
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
|
189 |
if rschema.final or rschema.inlined: |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
190 |
rdefs[i] = (rtype, InsertRelationsStep.FINAL, value) |
0 | 191 |
else: |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
192 |
rdefs[i] = (rtype, InsertRelationsStep.RELATION, value) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
193 |
step = InsertRelationsStep(plan, edef, rdefs) |
4795
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
194 |
select = _build_substep_query(select, rqlst) |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
195 |
if select is not None: |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
196 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
0 | 197 |
yield step |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
198 |
|
0 | 199 |
def build_delete_plan(self, plan, rqlst): |
200 |
"""get an execution plan from a DELETE RQL query""" |
|
201 |
# build a select query to fetch entities to delete |
|
202 |
steps = [] |
|
203 |
for etype, var in rqlst.main_variables: |
|
204 |
step = DeleteEntitiesStep(plan) |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
205 |
step.children += self._sel_variable_step(plan, rqlst, etype, var) |
0 | 206 |
steps.append(step) |
207 |
for relation in rqlst.main_relations: |
|
208 |
step = DeleteRelationsStep(plan, relation.r_type) |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
209 |
step.children += self._sel_relation_steps(plan, rqlst, relation) |
0 | 210 |
steps.append(step) |
211 |
return steps |
|
212 |
||
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
213 |
def _sel_variable_step(self, plan, rqlst, etype, varref): |
0 | 214 |
"""handle the selection of variables for a delete query""" |
215 |
select = Select() |
|
216 |
varref = varref.copy(select) |
|
217 |
select.defined_vars = {varref.name: varref.variable} |
|
218 |
select.append_selected(varref) |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
219 |
if rqlst.where is not None: |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
220 |
select.set_where(rqlst.where.copy(select)) |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
221 |
if getattr(rqlst, 'having', None): |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
222 |
select.set_having([x.copy(select) for x in rqlst.having]) |
0 | 223 |
if etype != 'Any': |
224 |
select.add_type_restriction(varref.variable, etype) |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
225 |
return self._select_plan(plan, select, rqlst.solutions) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
226 |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
227 |
def _sel_relation_steps(self, plan, rqlst, relation): |
0 | 228 |
"""handle the selection of relations for a delete query""" |
229 |
select = Select() |
|
230 |
lhs, rhs = relation.get_variable_parts() |
|
231 |
select.append_selected(lhs.copy(select)) |
|
232 |
select.append_selected(rhs.copy(select)) |
|
233 |
select.set_where(relation.copy(select)) |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
234 |
if rqlst.where is not None: |
8020
52e250f307b6
[rql2sql]Â fix regression introduced in 8018:f01c80513274 leading to bad execution of DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8018
diff
changeset
|
235 |
select.add_restriction(rqlst.where.copy(select)) |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
236 |
if getattr(rqlst, 'having', None): |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
237 |
select.set_having([x.copy(select) for x in rqlst.having]) |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
238 |
return self._select_plan(plan, select, rqlst.solutions) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
239 |
|
0 | 240 |
def build_set_plan(self, plan, rqlst): |
241 |
"""get an execution plan from an SET RQL query""" |
|
242 |
getrschema = self.schema.rschema |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
243 |
select = Select() # potential substep query |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
244 |
selectedidx = {} # local state |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
245 |
attributes = set() # edited attributes |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
246 |
updatedefs = [] # definition of update attributes/relations |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
247 |
selidx = residx = 0 # substep selection / resulting rset indexes |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
248 |
# search for eid const in the WHERE clause |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
249 |
eidconsts = _extract_eid_consts(plan, rqlst) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
250 |
# build `updatedefs` describing things to update and add necessary |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
251 |
# variables to the substep selection |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
252 |
for i, relation in enumerate(rqlst.main_relations): |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
253 |
if relation.r_type in VIRTUAL_RTYPES: |
0 | 254 |
raise QueryError('can not assign to %r relation' |
255 |
% relation.r_type) |
|
256 |
lhs, rhs = relation.get_variable_parts() |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
257 |
lhskey = lhs.as_string('utf-8') |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
258 |
if not lhskey in selectedidx: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
259 |
if lhs.variable in eidconsts: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
260 |
eid = eidconsts[lhs.variable] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
261 |
lhsinfo = (_CONSTANT, eid, residx) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
262 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
263 |
select.append_selected(lhs.copy(select)) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
264 |
lhsinfo = (_FROM_SUBSTEP, selidx, residx) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
265 |
selidx += 1 |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
266 |
residx += 1 |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
267 |
selectedidx[lhskey] = lhsinfo |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
268 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
269 |
lhsinfo = selectedidx[lhskey][:-1] + (None,) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
270 |
rhskey = rhs.as_string('utf-8') |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
271 |
if not rhskey in selectedidx: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
272 |
if isinstance(rhs, Constant): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
273 |
rhsinfo = (_CONSTANT, rhs.eval(plan.args), residx) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
274 |
elif getattr(rhs, 'variable', None) in eidconsts: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
275 |
eid = eidconsts[rhs.variable] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
276 |
rhsinfo = (_CONSTANT, eid, residx) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
277 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
278 |
select.append_selected(rhs.copy(select)) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
279 |
rhsinfo = (_FROM_SUBSTEP, selidx, residx) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
280 |
selidx += 1 |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
281 |
residx += 1 |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
282 |
selectedidx[rhskey] = rhsinfo |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
283 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
284 |
rhsinfo = selectedidx[rhskey][:-1] + (None,) |
0 | 285 |
rschema = getrschema(relation.r_type) |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
286 |
updatedefs.append( (lhsinfo, rhsinfo, rschema) ) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
287 |
# the update step |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
288 |
step = UpdateStep(plan, updatedefs) |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
289 |
# when necessary add substep to fetch yet unknown values |
4795
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
290 |
select = _build_substep_query(select, rqlst) |
f1c8bc628b45
[repo] fix bug introduced by 4757:ec9c20c6b9f7, testing for select.selection is not enough to avoid the substep query, we should check there is no interesting restriction (test added)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
291 |
if select is not None: |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
292 |
# set distinct to avoid potential duplicate key error |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
293 |
select.distinct = True |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
294 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
0 | 295 |
return (step,) |
296 |
||
297 |
# internal methods ######################################################## |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
298 |
|
0 | 299 |
def _select_plan(self, plan, select, solutions): |
300 |
union = Union() |
|
301 |
union.append(select) |
|
302 |
select.clean_solutions(solutions) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
303 |
add_types_restriction(self.schema, select) |
0 | 304 |
self.rqlhelper.annotate(union) |
305 |
return self.build_select_plan(plan, union) |
|
306 |
||
307 |
||
308 |
# execution steps and helper functions ######################################## |
|
309 |
||
310 |
def varmap_test_repr(varmap, tablesinorder): |
|
311 |
if varmap is None: |
|
312 |
return varmap |
|
313 |
maprepr = {} |
|
314 |
for var, sql in varmap.iteritems(): |
|
315 |
table, col = sql.split('.') |
|
316 |
maprepr[var] = '%s.%s' % (tablesinorder[table], col) |
|
317 |
return maprepr |
|
318 |
||
319 |
def offset_result(offset, result): |
|
320 |
offset -= len(result) |
|
321 |
if offset < 0: |
|
322 |
result = result[offset:] |
|
323 |
offset = None |
|
324 |
elif offset == 0: |
|
325 |
offset = None |
|
326 |
result = () |
|
327 |
return offset, result |
|
328 |
||
329 |
||
330 |
class LimitOffsetMixIn(object): |
|
331 |
limit = offset = None |
|
332 |
def set_limit_offset(self, limit, offset): |
|
333 |
self.limit = limit |
|
334 |
self.offset = offset or None |
|
335 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
336 |
|
0 | 337 |
class Step(object): |
338 |
"""base abstract class for execution step""" |
|
339 |
def __init__(self, plan): |
|
340 |
self.plan = plan |
|
341 |
self.children = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
342 |
|
0 | 343 |
def execute_child(self): |
344 |
assert len(self.children) == 1 |
|
345 |
return self.children[0].execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
346 |
|
0 | 347 |
def execute_children(self): |
348 |
for step in self.children: |
|
349 |
step.execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
350 |
|
0 | 351 |
def execute(self): |
352 |
"""execute this step and store partial (eg this step) results""" |
|
353 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
354 |
|
0 | 355 |
def mytest_repr(self): |
356 |
"""return a representation of this step suitable for test""" |
|
357 |
return (self.__class__.__name__,) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
358 |
|
0 | 359 |
def test_repr(self): |
360 |
"""return a representation of this step suitable for test""" |
|
361 |
return self.mytest_repr() + ( |
|
362 |
[step.test_repr() for step in self.children],) |
|
363 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
364 |
|
0 | 365 |
class OneFetchStep(LimitOffsetMixIn, Step): |
366 |
"""step consisting in fetching data from sources and directly returning |
|
367 |
results |
|
368 |
""" |
|
369 |
def __init__(self, plan, union, sources, inputmap=None): |
|
370 |
Step.__init__(self, plan) |
|
371 |
self.union = union |
|
372 |
self.sources = sources |
|
373 |
self.inputmap = inputmap |
|
374 |
self.set_limit_offset(union.children[-1].limit, union.children[-1].offset) |
|
375 |
||
376 |
def set_limit_offset(self, limit, offset): |
|
377 |
LimitOffsetMixIn.set_limit_offset(self, limit, offset) |
|
378 |
for select in self.union.children: |
|
379 |
select.limit = limit |
|
380 |
select.offset = offset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
381 |
|
0 | 382 |
def execute(self): |
383 |
"""call .syntax_tree_search with the given syntax tree on each |
|
384 |
source for each solution |
|
385 |
""" |
|
386 |
self.execute_children() |
|
387 |
session = self.plan.session |
|
388 |
args = self.plan.args |
|
389 |
inputmap = self.inputmap |
|
390 |
union = self.union |
|
391 |
# do we have to use a inputmap from a previous step ? If so disable |
|
392 |
# cachekey |
|
393 |
if inputmap or self.plan.cache_key is None: |
|
394 |
cachekey = None |
|
8127
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8020
diff
changeset
|
395 |
# union may have been splited into subqueries, in which case we can't |
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8020
diff
changeset
|
396 |
# use plan.cache_key, rebuild a cache key |
0 | 397 |
elif isinstance(self.plan.cache_key, tuple): |
398 |
cachekey = list(self.plan.cache_key) |
|
399 |
cachekey[0] = union.as_string() |
|
400 |
cachekey = tuple(cachekey) |
|
401 |
else: |
|
402 |
cachekey = union.as_string() |
|
403 |
result = [] |
|
404 |
# limit / offset processing |
|
405 |
limit = self.limit |
|
406 |
offset = self.offset |
|
407 |
if offset is not None: |
|
408 |
if len(self.sources) > 1: |
|
409 |
# we'll have to deal with limit/offset by ourself |
|
410 |
if union.children[-1].limit: |
|
411 |
union.children[-1].limit = limit + offset |
|
412 |
union.children[-1].offset = None |
|
413 |
else: |
|
414 |
offset, limit = None, None |
|
415 |
for source in self.sources: |
|
416 |
if offset is None and limit is not None: |
|
417 |
# modifying the sample rqlst is enough since sql generation |
|
418 |
# will pick it here as well |
|
419 |
union.children[-1].limit = limit - len(result) |
|
420 |
result_ = source.syntax_tree_search(session, union, args, cachekey, |
|
421 |
inputmap) |
|
422 |
if offset is not None: |
|
423 |
offset, result_ = offset_result(offset, result_) |
|
424 |
result += result_ |
|
425 |
if limit is not None: |
|
426 |
if len(result) >= limit: |
|
427 |
return result[:limit] |
|
428 |
#print 'ONEFETCH RESULT %s' % (result) |
|
429 |
return result |
|
430 |
||
431 |
def mytest_repr(self): |
|
432 |
"""return a representation of this step suitable for test""" |
|
433 |
try: |
|
434 |
inputmap = varmap_test_repr(self.inputmap, self.plan.tablesinorder) |
|
435 |
except AttributeError: |
|
436 |
inputmap = self.inputmap |
|
437 |
return (self.__class__.__name__, |
|
438 |
sorted((r.as_string(kwargs=self.plan.args), r.solutions) |
|
439 |
for r in self.union.children), |
|
440 |
self.limit, self.offset, |
|
441 |
sorted(self.sources), inputmap) |
|
442 |
||
443 |
||
444 |
# UPDATE/INSERT/DELETE steps ################################################## |
|
445 |
||
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
446 |
class InsertRelationsStep(Step): |
0 | 447 |
"""step consisting in adding attributes/relations to entity defs from a |
448 |
previous FetchStep |
|
449 |
||
450 |
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
|
451 |
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
|
452 |
|
0 | 453 |
for one entity definition, we'll construct N entity, where N is the |
454 |
number of the latest result |
|
455 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
456 |
|
0 | 457 |
FINAL = 0 |
458 |
RELATION = 1 |
|
459 |
REVERSE_RELATION = 2 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
460 |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
461 |
def __init__(self, plan, edef, rdefs): |
0 | 462 |
Step.__init__(self, plan) |
463 |
# 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
|
464 |
self.edef = edef |
0 | 465 |
# 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
|
466 |
self.rdefs = rdefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
467 |
|
0 | 468 |
def execute(self): |
469 |
"""execute this step""" |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
470 |
base_edef = self.edef |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
471 |
edefs = [] |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
472 |
if self.children: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
473 |
result = self.execute_child() |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
474 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
475 |
result = [[]] |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
476 |
for row in result: |
0 | 477 |
# get a new entity definition for this row |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
478 |
edef = base_edef.clone() |
0 | 479 |
# complete this entity def using row values |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
480 |
index = 0 |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
481 |
for rtype, rorder, value in self.rdefs: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
482 |
if value is _FROM_SUBSTEP: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
483 |
value = row[index] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
484 |
index += 1 |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
485 |
if rorder == InsertRelationsStep.FINAL: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
486 |
edef.edited_attribute(rtype, value) |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
487 |
elif rorder == InsertRelationsStep.RELATION: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
488 |
self.plan.add_relation_def( (edef, rtype, value) ) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
489 |
edef.querier_pending_relations[(rtype, 'subject')] = value |
0 | 490 |
else: |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
491 |
self.plan.add_relation_def( (value, rtype, edef) ) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
492 |
edef.querier_pending_relations[(rtype, 'object')] = value |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
493 |
edefs.append(edef) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
494 |
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
|
495 |
return result |
0 | 496 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
497 |
|
0 | 498 |
class InsertStep(Step): |
499 |
"""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
|
500 |
|
0 | 501 |
def execute(self): |
502 |
"""execute this step""" |
|
503 |
for step in self.children: |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
504 |
assert isinstance(step, InsertRelationsStep) |
0 | 505 |
step.plan = self.plan |
506 |
step.execute() |
|
507 |
# insert entities first |
|
508 |
result = self.plan.insert_entity_defs() |
|
509 |
# then relation |
|
510 |
self.plan.insert_relation_defs() |
|
511 |
# return eids of inserted entities |
|
512 |
return result |
|
513 |
||
514 |
||
515 |
class DeleteEntitiesStep(Step): |
|
516 |
"""step consisting in deleting entities""" |
|
517 |
||
518 |
def execute(self): |
|
519 |
"""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
|
520 |
results = self.execute_child() |
6893
2e10337c9c2c
avoid creating a new instance of CleanupDeletedEidsCacheOp if nothing was deleted
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6889
diff
changeset
|
521 |
if results: |
2e10337c9c2c
avoid creating a new instance of CleanupDeletedEidsCacheOp if nothing was deleted
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6889
diff
changeset
|
522 |
todelete = frozenset(typed_eid(eid) for eid, in results) |
2e10337c9c2c
avoid creating a new instance of CleanupDeletedEidsCacheOp if nothing was deleted
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6889
diff
changeset
|
523 |
session = self.plan.session |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
524 |
session.repo.glob_delete_entities(session, todelete) |
3648
665c37544060
on entity deletion query, return eids of deleted entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
525 |
return results |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
526 |
|
0 | 527 |
class DeleteRelationsStep(Step): |
528 |
"""step consisting in deleting relations""" |
|
529 |
||
530 |
def __init__(self, plan, rtype): |
|
531 |
Step.__init__(self, plan) |
|
532 |
self.rtype = rtype |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
533 |
|
0 | 534 |
def execute(self): |
535 |
"""execute this step""" |
|
536 |
session = self.plan.session |
|
537 |
delete = session.repo.glob_delete_relation |
|
538 |
for subj, obj in self.execute_child(): |
|
539 |
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
|
540 |
|
0 | 541 |
|
542 |
class UpdateStep(Step): |
|
543 |
"""step consisting in updating entities / adding relations from relations |
|
544 |
definitions and from results fetched in previous step |
|
545 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
546 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
547 |
def __init__(self, plan, updatedefs): |
0 | 548 |
Step.__init__(self, plan) |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
549 |
self.updatedefs = updatedefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
550 |
|
0 | 551 |
def execute(self): |
552 |
"""execute this step""" |
|
553 |
session = self.plan.session |
|
554 |
repo = session.repo |
|
555 |
edefs = {} |
|
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
556 |
relations = {} |
0 | 557 |
# insert relations |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
558 |
if self.children: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
559 |
result = self.execute_child() |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
560 |
else: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
561 |
result = [[]] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
562 |
for i, row in enumerate(result): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
563 |
newrow = [] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
564 |
for (lhsinfo, rhsinfo, rschema) in self.updatedefs: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
565 |
lhsval = _handle_relterm(lhsinfo, row, newrow) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
566 |
rhsval = _handle_relterm(rhsinfo, row, newrow) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
567 |
if rschema.final or rschema.inlined: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
568 |
eid = typed_eid(lhsval) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
569 |
try: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
570 |
edited = edefs[eid] |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
571 |
except KeyError: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
572 |
edef = session.entity_from_eid(eid) |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
573 |
edefs[eid] = edited = EditedEntity(edef) |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
574 |
edited.edited_attribute(str(rschema), rhsval) |
0 | 575 |
else: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
576 |
str_rschema = str(rschema) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
577 |
if str_rschema in relations: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
578 |
relations[str_rschema].append((lhsval, rhsval)) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
579 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
580 |
relations[str_rschema] = [(lhsval, rhsval)] |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
581 |
result[i] = newrow |
0 | 582 |
# update entities |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
583 |
repo.glob_add_relations(session, relations) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
584 |
for eid, edited in edefs.iteritems(): |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
585 |
repo.glob_update_entity(session, edited) |
0 | 586 |
return result |
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
587 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
588 |
def _handle_relterm(info, row, newrow): |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
589 |
if info[0] is _CONSTANT: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
590 |
val = info[1] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
591 |
else: # _FROM_SUBSTEP |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
592 |
val = row[info[1]] |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
593 |
if info[-1] is not None: |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
594 |
newrow.append(val) |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
595 |
return val |