author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Thu, 27 Jun 2013 19:09:23 +0200 | |
changeset 9123 | c992811edec2 |
parent 8867 | 6ad000b91347 |
child 9206 | bf642b50135b |
permissions | -rw-r--r-- |
8562
0d2fb4604265
[session] fix arguments default value and promote usage of security_enabled as session method. Closes #2481820
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8127
diff
changeset
|
1 |
# copyright 2003-2012 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 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
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
|
23 |
from rql.nodes import Constant, Relation |
0 | 24 |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
25 |
from cubicweb import QueryError |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
26 |
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
|
27 |
from cubicweb.rqlrewrite import add_types_restriction |
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
|
28 |
from cubicweb.server.edition import EditedEntity |
0 | 29 |
|
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
|
30 |
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
|
31 |
|
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
|
32 |
_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
|
33 |
_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
|
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 |
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
|
36 |
"""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
|
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 |
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
|
39 |
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
|
40 |
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
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
# 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
|
48 |
# 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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
# 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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
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
|
58 |
value = value.encode('UTF8') |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
59 |
edef.edited_attribute(rtype, value) |
8697
574bb05e40a4
[toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8694
diff
changeset
|
60 |
elif str(rhs) in to_build: |
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
|
61 |
# 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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
|
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 |
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
|
68 |
"""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
|
69 |
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
|
70 |
""" |
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 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
for rel in rqlst.where.get_nodes(Relation): |
8853
f61755c05c89
[querier] fix eid relations handling in SET queries (closes #2797052)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8127
diff
changeset
|
79 |
# only care for 'eid' relations ... |
f61755c05c89
[querier] fix eid relations handling in SET queries (closes #2797052)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8127
diff
changeset
|
80 |
if (rel.r_type == 'eid' |
f61755c05c89
[querier] fix eid relations handling in SET queries (closes #2797052)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8127
diff
changeset
|
81 |
# ... that are not part of a NOT clause ... |
f61755c05c89
[querier] fix eid relations handling in SET queries (closes #2797052)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8127
diff
changeset
|
82 |
and not rel.neged(strict=True) |
f61755c05c89
[querier] fix eid relations handling in SET queries (closes #2797052)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8127
diff
changeset
|
83 |
# ... and where eid is specified by '=' operator. |
f61755c05c89
[querier] fix eid relations handling in SET queries (closes #2797052)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8127
diff
changeset
|
84 |
and rel.children[1].operator == '='): |
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
|
85 |
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
|
86 |
if isinstance(rhs, Constant): |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
87 |
eid = int(rhs.eval(plan.args)) |
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
|
88 |
# 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
|
89 |
# 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
|
90 |
# 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
|
91 |
if checkread and eid not in neweids: |
8562
0d2fb4604265
[session] fix arguments default value and promote usage of security_enabled as session method. Closes #2481820
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8127
diff
changeset
|
92 |
with session.security_enabled(read=False): |
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
|
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
|
98 |
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
|
99 |
"""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
|
100 |
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
|
101 |
|
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 |
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
|
103 |
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
|
104 |
|
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 |
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
|
106 |
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
|
107 |
""" |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
108 |
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
|
109 |
# 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
|
110 |
# 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
|
111 |
# 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
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
(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
|
116 |
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
|
117 |
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
|
118 |
break |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
119 |
else: |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
return None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
128 |
|
0 | 129 |
class SSPlanner(object): |
130 |
"""SingleSourcePlanner: build execution plan for rql queries |
|
131 |
||
132 |
optimized for single source repositories |
|
133 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
134 |
|
0 | 135 |
def __init__(self, schema, rqlhelper): |
136 |
self.schema = schema |
|
137 |
self.rqlhelper = rqlhelper |
|
138 |
||
139 |
def build_plan(self, plan): |
|
140 |
"""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
|
141 |
|
0 | 142 |
do nothing here, dispatch according to the statement type |
143 |
""" |
|
144 |
build_plan = getattr(self, 'build_%s_plan' % plan.rqlst.TYPE) |
|
145 |
for step in build_plan(plan, plan.rqlst): |
|
146 |
plan.add_step(step) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
147 |
|
0 | 148 |
def build_select_plan(self, plan, rqlst): |
149 |
"""build execution plan for a SELECT RQL query. Suppose only one source |
|
150 |
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
|
151 |
|
0 | 152 |
the rqlst should not be tagged at this point. |
153 |
""" |
|
154 |
plan.preprocess(rqlst) |
|
155 |
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
|
156 |
|
0 | 157 |
def build_insert_plan(self, plan, rqlst): |
158 |
"""get an execution plan from an INSERT RQL query""" |
|
159 |
# each variable in main variables is a new entity to insert |
|
160 |
to_build = {} |
|
161 |
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
|
162 |
etype_class = session.vreg['etypes'].etype_class |
0 | 163 |
for etype, var in rqlst.main_variables: |
164 |
# 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
|
165 |
to_build[var.name] = EditedEntity(etype_class(etype)(session)) |
0 | 166 |
plan.add_entity_def(to_build[var.name]) |
167 |
# 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
|
168 |
to_select = _extract_const_attributes(plan, rqlst, to_build) |
0 | 169 |
# add necessary steps to add relations and update attributes |
170 |
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
|
171 |
step.children += self._compute_relation_steps(plan, rqlst, to_select) |
0 | 172 |
return (step,) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
173 |
|
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 |
def _compute_relation_steps(self, plan, rqlst, to_select): |
0 | 175 |
"""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
|
176 |
eidconsts = _extract_eid_consts(plan, rqlst) |
0 | 177 |
for edef, rdefs in to_select.items(): |
178 |
# create a select rql st to fetch needed data |
|
179 |
select = Select() |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
value = _FROM_SUBSTEP |
0 | 187 |
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
|
188 |
rdefs[i] = (rtype, InsertRelationsStep.REVERSE_RELATION, value) |
0 | 189 |
else: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
rdefs[i] = (rtype, InsertRelationsStep.FINAL, value) |
0 | 193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
0 | 199 |
yield step |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
200 |
|
0 | 201 |
def build_delete_plan(self, plan, rqlst): |
202 |
"""get an execution plan from a DELETE RQL query""" |
|
203 |
# build a select query to fetch entities to delete |
|
204 |
steps = [] |
|
205 |
for etype, var in rqlst.main_variables: |
|
206 |
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
|
207 |
step.children += self._sel_variable_step(plan, rqlst, etype, var) |
0 | 208 |
steps.append(step) |
209 |
for relation in rqlst.main_relations: |
|
210 |
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
|
211 |
step.children += self._sel_relation_steps(plan, rqlst, relation) |
0 | 212 |
steps.append(step) |
213 |
return steps |
|
214 |
||
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
215 |
def _sel_variable_step(self, plan, rqlst, etype, varref): |
0 | 216 |
"""handle the selection of variables for a delete query""" |
217 |
select = Select() |
|
218 |
varref = varref.copy(select) |
|
219 |
select.defined_vars = {varref.name: varref.variable} |
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
select.set_having([x.copy(select) for x in rqlst.having]) |
0 | 225 |
if etype != 'Any': |
226 |
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
|
227 |
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
|
228 |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
229 |
def _sel_relation_steps(self, plan, rqlst, relation): |
0 | 230 |
"""handle the selection of relations for a delete query""" |
231 |
select = Select() |
|
232 |
lhs, rhs = relation.get_variable_parts() |
|
233 |
select.append_selected(lhs.copy(select)) |
|
234 |
select.append_selected(rhs.copy(select)) |
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
|
0 | 242 |
def build_set_plan(self, plan, rqlst): |
243 |
"""get an execution plan from an SET RQL query""" |
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
# 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
|
251 |
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
|
252 |
# 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
|
253 |
# 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
|
254 |
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
|
255 |
if relation.r_type in VIRTUAL_RTYPES: |
0 | 256 |
raise QueryError('can not assign to %r relation' |
257 |
% relation.r_type) |
|
258 |
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
|
259 |
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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
rhsinfo = selectedidx[rhskey][:-1] + (None,) |
0 | 287 |
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
|
288 |
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
|
289 |
# the update step |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
290 |
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
|
291 |
# 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
|
292 |
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
|
293 |
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
|
294 |
# 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
|
295 |
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
|
296 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
0 | 297 |
return (step,) |
298 |
||
299 |
# internal methods ######################################################## |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
300 |
|
0 | 301 |
def _select_plan(self, plan, select, solutions): |
302 |
union = Union() |
|
303 |
union.append(select) |
|
304 |
select.clean_solutions(solutions) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
305 |
add_types_restriction(self.schema, select) |
0 | 306 |
self.rqlhelper.annotate(union) |
307 |
return self.build_select_plan(plan, union) |
|
308 |
||
309 |
||
310 |
# execution steps and helper functions ######################################## |
|
311 |
||
312 |
def varmap_test_repr(varmap, tablesinorder): |
|
313 |
if varmap is None: |
|
314 |
return varmap |
|
315 |
maprepr = {} |
|
316 |
for var, sql in varmap.iteritems(): |
|
317 |
table, col = sql.split('.') |
|
318 |
maprepr[var] = '%s.%s' % (tablesinorder[table], col) |
|
319 |
return maprepr |
|
320 |
||
321 |
def offset_result(offset, result): |
|
322 |
offset -= len(result) |
|
323 |
if offset < 0: |
|
324 |
result = result[offset:] |
|
325 |
offset = None |
|
326 |
elif offset == 0: |
|
327 |
offset = None |
|
328 |
result = () |
|
329 |
return offset, result |
|
330 |
||
331 |
||
332 |
class LimitOffsetMixIn(object): |
|
333 |
limit = offset = None |
|
334 |
def set_limit_offset(self, limit, offset): |
|
335 |
self.limit = limit |
|
336 |
self.offset = offset or None |
|
337 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
338 |
|
0 | 339 |
class Step(object): |
340 |
"""base abstract class for execution step""" |
|
341 |
def __init__(self, plan): |
|
342 |
self.plan = plan |
|
343 |
self.children = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
344 |
|
0 | 345 |
def execute_child(self): |
346 |
assert len(self.children) == 1 |
|
347 |
return self.children[0].execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
348 |
|
0 | 349 |
def execute_children(self): |
350 |
for step in self.children: |
|
351 |
step.execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
352 |
|
0 | 353 |
def execute(self): |
354 |
"""execute this step and store partial (eg this step) results""" |
|
355 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
356 |
|
0 | 357 |
def mytest_repr(self): |
358 |
"""return a representation of this step suitable for test""" |
|
359 |
return (self.__class__.__name__,) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
360 |
|
0 | 361 |
def test_repr(self): |
362 |
"""return a representation of this step suitable for test""" |
|
363 |
return self.mytest_repr() + ( |
|
364 |
[step.test_repr() for step in self.children],) |
|
365 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
366 |
|
0 | 367 |
class OneFetchStep(LimitOffsetMixIn, Step): |
368 |
"""step consisting in fetching data from sources and directly returning |
|
369 |
results |
|
370 |
""" |
|
371 |
def __init__(self, plan, union, sources, inputmap=None): |
|
372 |
Step.__init__(self, plan) |
|
373 |
self.union = union |
|
374 |
self.sources = sources |
|
375 |
self.inputmap = inputmap |
|
376 |
self.set_limit_offset(union.children[-1].limit, union.children[-1].offset) |
|
377 |
||
378 |
def set_limit_offset(self, limit, offset): |
|
379 |
LimitOffsetMixIn.set_limit_offset(self, limit, offset) |
|
380 |
for select in self.union.children: |
|
381 |
select.limit = limit |
|
382 |
select.offset = offset |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
383 |
|
0 | 384 |
def execute(self): |
385 |
"""call .syntax_tree_search with the given syntax tree on each |
|
386 |
source for each solution |
|
387 |
""" |
|
388 |
self.execute_children() |
|
389 |
session = self.plan.session |
|
390 |
args = self.plan.args |
|
391 |
inputmap = self.inputmap |
|
392 |
union = self.union |
|
393 |
# do we have to use a inputmap from a previous step ? If so disable |
|
394 |
# cachekey |
|
395 |
if inputmap or self.plan.cache_key is None: |
|
396 |
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
|
397 |
# 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
|
398 |
# use plan.cache_key, rebuild a cache key |
0 | 399 |
elif isinstance(self.plan.cache_key, tuple): |
400 |
cachekey = list(self.plan.cache_key) |
|
401 |
cachekey[0] = union.as_string() |
|
402 |
cachekey = tuple(cachekey) |
|
403 |
else: |
|
404 |
cachekey = union.as_string() |
|
405 |
result = [] |
|
406 |
# limit / offset processing |
|
407 |
limit = self.limit |
|
408 |
offset = self.offset |
|
409 |
if offset is not None: |
|
410 |
if len(self.sources) > 1: |
|
411 |
# we'll have to deal with limit/offset by ourself |
|
412 |
if union.children[-1].limit: |
|
413 |
union.children[-1].limit = limit + offset |
|
414 |
union.children[-1].offset = None |
|
415 |
else: |
|
416 |
offset, limit = None, None |
|
417 |
for source in self.sources: |
|
418 |
if offset is None and limit is not None: |
|
419 |
# modifying the sample rqlst is enough since sql generation |
|
420 |
# will pick it here as well |
|
421 |
union.children[-1].limit = limit - len(result) |
|
422 |
result_ = source.syntax_tree_search(session, union, args, cachekey, |
|
423 |
inputmap) |
|
424 |
if offset is not None: |
|
425 |
offset, result_ = offset_result(offset, result_) |
|
426 |
result += result_ |
|
427 |
if limit is not None: |
|
428 |
if len(result) >= limit: |
|
429 |
return result[:limit] |
|
430 |
#print 'ONEFETCH RESULT %s' % (result) |
|
431 |
return result |
|
432 |
||
433 |
def mytest_repr(self): |
|
434 |
"""return a representation of this step suitable for test""" |
|
435 |
try: |
|
436 |
inputmap = varmap_test_repr(self.inputmap, self.plan.tablesinorder) |
|
437 |
except AttributeError: |
|
438 |
inputmap = self.inputmap |
|
439 |
return (self.__class__.__name__, |
|
440 |
sorted((r.as_string(kwargs=self.plan.args), r.solutions) |
|
441 |
for r in self.union.children), |
|
442 |
self.limit, self.offset, |
|
443 |
sorted(self.sources), inputmap) |
|
444 |
||
445 |
||
446 |
# UPDATE/INSERT/DELETE steps ################################################## |
|
447 |
||
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
|
448 |
class InsertRelationsStep(Step): |
0 | 449 |
"""step consisting in adding attributes/relations to entity defs from a |
450 |
previous FetchStep |
|
451 |
||
452 |
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
|
453 |
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
|
454 |
|
0 | 455 |
for one entity definition, we'll construct N entity, where N is the |
456 |
number of the latest result |
|
457 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
458 |
|
0 | 459 |
FINAL = 0 |
460 |
RELATION = 1 |
|
461 |
REVERSE_RELATION = 2 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
462 |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
463 |
def __init__(self, plan, edef, rdefs): |
0 | 464 |
Step.__init__(self, plan) |
465 |
# 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
|
466 |
self.edef = edef |
0 | 467 |
# 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
|
468 |
self.rdefs = rdefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
469 |
|
0 | 470 |
def execute(self): |
471 |
"""execute this step""" |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
472 |
base_edef = self.edef |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
473 |
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
|
474 |
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
|
475 |
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
|
476 |
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
|
477 |
result = [[]] |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
478 |
for row in result: |
0 | 479 |
# 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
|
480 |
edef = base_edef.clone() |
0 | 481 |
# 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
|
482 |
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
|
483 |
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
|
484 |
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
|
485 |
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
|
486 |
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
|
487 |
if rorder == InsertRelationsStep.FINAL: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
edef.querier_pending_relations[(rtype, 'subject')] = value |
0 | 492 |
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
|
493 |
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
|
494 |
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
|
495 |
edefs.append(edef) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
496 |
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
|
497 |
return result |
0 | 498 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
499 |
|
0 | 500 |
class InsertStep(Step): |
501 |
"""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
|
502 |
|
0 | 503 |
def execute(self): |
504 |
"""execute this step""" |
|
505 |
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
|
506 |
assert isinstance(step, InsertRelationsStep) |
0 | 507 |
step.plan = self.plan |
508 |
step.execute() |
|
509 |
# insert entities first |
|
510 |
result = self.plan.insert_entity_defs() |
|
511 |
# then relation |
|
512 |
self.plan.insert_relation_defs() |
|
513 |
# return eids of inserted entities |
|
514 |
return result |
|
515 |
||
516 |
||
517 |
class DeleteEntitiesStep(Step): |
|
518 |
"""step consisting in deleting entities""" |
|
519 |
||
520 |
def execute(self): |
|
521 |
"""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
|
522 |
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
|
523 |
if results: |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
524 |
todelete = frozenset(int(eid) for eid, in results) |
6893
2e10337c9c2c
avoid creating a new instance of CleanupDeletedEidsCacheOp if nothing was deleted
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6889
diff
changeset
|
525 |
session = self.plan.session |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
526 |
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
|
527 |
return results |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
528 |
|
0 | 529 |
class DeleteRelationsStep(Step): |
530 |
"""step consisting in deleting relations""" |
|
531 |
||
532 |
def __init__(self, plan, rtype): |
|
533 |
Step.__init__(self, plan) |
|
534 |
self.rtype = rtype |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
535 |
|
0 | 536 |
def execute(self): |
537 |
"""execute this step""" |
|
538 |
session = self.plan.session |
|
539 |
delete = session.repo.glob_delete_relation |
|
540 |
for subj, obj in self.execute_child(): |
|
541 |
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
|
542 |
|
0 | 543 |
|
544 |
class UpdateStep(Step): |
|
545 |
"""step consisting in updating entities / adding relations from relations |
|
546 |
definitions and from results fetched in previous step |
|
547 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
548 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
549 |
def __init__(self, plan, updatedefs): |
0 | 550 |
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
|
551 |
self.updatedefs = updatedefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
552 |
|
0 | 553 |
def execute(self): |
554 |
"""execute this step""" |
|
555 |
session = self.plan.session |
|
556 |
repo = session.repo |
|
557 |
edefs = {} |
|
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
558 |
relations = {} |
0 | 559 |
# 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
|
560 |
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
|
561 |
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
|
562 |
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
|
563 |
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
|
564 |
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
|
565 |
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 |
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
|
567 |
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
|
568 |
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
|
569 |
if rschema.final or rschema.inlined: |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
570 |
eid = int(lhsval) |
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 |
try: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
572 |
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
|
573 |
except KeyError: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
574 |
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
|
575 |
edefs[eid] = edited = EditedEntity(edef) |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
576 |
edited.edited_attribute(str(rschema), rhsval) |
0 | 577 |
else: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
578 |
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
|
579 |
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
|
580 |
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
|
581 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
582 |
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
|
583 |
result[i] = newrow |
0 | 584 |
# 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
|
585 |
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
|
586 |
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
|
587 |
repo.glob_update_entity(session, edited) |
0 | 588 |
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
|
589 |
|
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 |
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
|
591 |
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
|
592 |
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
|
593 |
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
|
594 |
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
|
595 |
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
|
596 |
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
|
597 |
return val |