author | Rabah Meradi <rabah.meradi@logilab.fr> |
Wed, 25 Nov 2015 18:31:48 +0100 | |
changeset 10932 | cb217b2b3463 |
parent 10682 | 7e111b606005 |
permissions | -rw-r--r-- |
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9206
diff
changeset
|
1 |
# copyright 2003-2013 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 |
||
10682
7e111b606005
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
22 |
from six import text_type |
7e111b606005
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
23 |
|
0 | 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 |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
27 |
from cubicweb import QueryError |
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 |
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
|
30 |
from cubicweb.server.edition import EditedEntity |
0 | 31 |
|
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
|
32 |
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
|
33 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select 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 |
_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
|
35 |
_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
|
36 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select 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 |
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
|
38 |
"""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
|
39 |
""" |
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select 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 |
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
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
# 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
|
50 |
# 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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
# 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
|
56 |
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
|
57 |
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
|
58 |
attrtype = eschema.subjrels[rtype].objects(eschema)[0] |
10682
7e111b606005
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
59 |
if attrtype == 'Password' and isinstance(value, text_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
|
60 |
value = value.encode('UTF8') |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
# 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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select 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 |
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
|
70 |
"""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
|
71 |
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
|
72 |
""" |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
73 |
cnx = plan.cnx |
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
|
74 |
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
|
75 |
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
|
76 |
eidconsts = {} |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
77 |
neweids = cnx.transaction_data.get('neweids', ()) |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
78 |
checkread = cnx.read_security |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
79 |
eschema = cnx.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
|
80 |
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
|
81 |
# 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
|
82 |
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
|
83 |
# ... 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
|
84 |
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
|
85 |
# ... 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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
# 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
|
91 |
# 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
|
92 |
# 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
|
93 |
if checkread and eid not in neweids: |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
94 |
with cnx.security_enabled(read=False): |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
95 |
eschema(cnx.entity_metas(eid)['type']).check_perm( |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
96 |
cnx, '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
|
97 |
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
|
98 |
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
|
99 |
|
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
|
100 |
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
|
101 |
"""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
|
102 |
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
|
103 |
|
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 |
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
|
105 |
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
|
106 |
""" |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
107 |
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
|
108 |
# no selection, append one randomly by searching for a relation which is |
9206
bf642b50135b
[rql / querier] fix bad interpretation of some RQL SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
109 |
# not neged neither a type restriction (is/is_instance_of) |
8018
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): |
9206
bf642b50135b
[rql / querier] fix bad interpretation of some RQL SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
111 |
if not (rel.neged(traverse_scope=True) or rel.is_types_restriction()): |
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
112 |
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
|
113 |
break |
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
114 |
else: |
9206
bf642b50135b
[rql / querier] fix bad interpretation of some RQL SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
115 |
return None |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
return None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
123 |
|
0 | 124 |
class SSPlanner(object): |
125 |
"""SingleSourcePlanner: build execution plan for rql queries |
|
126 |
||
127 |
optimized for single source repositories |
|
128 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
129 |
|
0 | 130 |
def __init__(self, schema, rqlhelper): |
131 |
self.schema = schema |
|
132 |
self.rqlhelper = rqlhelper |
|
133 |
||
134 |
def build_plan(self, plan): |
|
135 |
"""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
|
136 |
|
0 | 137 |
do nothing here, dispatch according to the statement type |
138 |
""" |
|
139 |
build_plan = getattr(self, 'build_%s_plan' % plan.rqlst.TYPE) |
|
140 |
for step in build_plan(plan, plan.rqlst): |
|
141 |
plan.add_step(step) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
142 |
|
0 | 143 |
def build_select_plan(self, plan, rqlst): |
144 |
"""build execution plan for a SELECT RQL query. Suppose only one source |
|
145 |
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
|
146 |
|
0 | 147 |
the rqlst should not be tagged at this point. |
148 |
""" |
|
149 |
plan.preprocess(rqlst) |
|
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9206
diff
changeset
|
150 |
return (OneFetchStep(plan, rqlst),) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
151 |
|
0 | 152 |
def build_insert_plan(self, plan, rqlst): |
153 |
"""get an execution plan from an INSERT RQL query""" |
|
154 |
# each variable in main variables is a new entity to insert |
|
155 |
to_build = {} |
|
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
156 |
cnx = plan.cnx |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
157 |
etype_class = cnx.vreg['etypes'].etype_class |
0 | 158 |
for etype, var in rqlst.main_variables: |
159 |
# need to do this since entity class is shared w. web client code ! |
|
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
160 |
to_build[var.name] = EditedEntity(etype_class(etype)(cnx)) |
0 | 161 |
plan.add_entity_def(to_build[var.name]) |
162 |
# 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
|
163 |
to_select = _extract_const_attributes(plan, rqlst, to_build) |
0 | 164 |
# add necessary steps to add relations and update attributes |
165 |
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
|
166 |
step.children += self._compute_relation_steps(plan, rqlst, to_select) |
0 | 167 |
return (step,) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
168 |
|
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 |
def _compute_relation_steps(self, plan, rqlst, to_select): |
0 | 170 |
"""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
|
171 |
eidconsts = _extract_eid_consts(plan, rqlst) |
0 | 172 |
for edef, rdefs in to_select.items(): |
173 |
# create a select rql st to fetch needed data |
|
174 |
select = Select() |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
value = _FROM_SUBSTEP |
0 | 182 |
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
|
183 |
rdefs[i] = (rtype, InsertRelationsStep.REVERSE_RELATION, value) |
0 | 184 |
else: |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
185 |
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
|
186 |
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
|
187 |
rdefs[i] = (rtype, InsertRelationsStep.FINAL, value) |
0 | 188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
0 | 194 |
yield step |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
195 |
|
0 | 196 |
def build_delete_plan(self, plan, rqlst): |
197 |
"""get an execution plan from a DELETE RQL query""" |
|
198 |
# build a select query to fetch entities to delete |
|
199 |
steps = [] |
|
200 |
for etype, var in rqlst.main_variables: |
|
201 |
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
|
202 |
step.children += self._sel_variable_step(plan, rqlst, etype, var) |
0 | 203 |
steps.append(step) |
204 |
for relation in rqlst.main_relations: |
|
205 |
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
|
206 |
step.children += self._sel_relation_steps(plan, rqlst, relation) |
0 | 207 |
steps.append(step) |
208 |
return steps |
|
209 |
||
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
210 |
def _sel_variable_step(self, plan, rqlst, etype, varref): |
0 | 211 |
"""handle the selection of variables for a delete query""" |
212 |
select = Select() |
|
213 |
varref = varref.copy(select) |
|
214 |
select.defined_vars = {varref.name: varref.variable} |
|
215 |
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
|
216 |
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
|
217 |
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
|
218 |
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
|
219 |
select.set_having([x.copy(select) for x in rqlst.having]) |
0 | 220 |
if etype != 'Any': |
221 |
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
|
222 |
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
|
223 |
|
8018
f01c80513274
[rql] closes #2054468: support for HAVING in SET/DELETE queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
224 |
def _sel_relation_steps(self, plan, rqlst, relation): |
0 | 225 |
"""handle the selection of relations for a delete query""" |
226 |
select = Select() |
|
227 |
lhs, rhs = relation.get_variable_parts() |
|
228 |
select.append_selected(lhs.copy(select)) |
|
229 |
select.append_selected(rhs.copy(select)) |
|
230 |
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
|
231 |
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
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
|
0 | 237 |
def build_set_plan(self, plan, rqlst): |
238 |
"""get an execution plan from an SET RQL query""" |
|
239 |
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
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
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
|
244 |
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
|
245 |
# 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
|
246 |
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
|
247 |
# 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
|
248 |
# 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
|
249 |
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
|
250 |
if relation.r_type in VIRTUAL_RTYPES: |
0 | 251 |
raise QueryError('can not assign to %r relation' |
252 |
% relation.r_type) |
|
253 |
lhs, rhs = relation.get_variable_parts() |
|
10314
8f3783dc6358
Don't pass an encoding to rqlst.as_string()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9508
diff
changeset
|
254 |
lhskey = lhs.as_string() |
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
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
lhsinfo = selectedidx[lhskey][:-1] + (None,) |
10314
8f3783dc6358
Don't pass an encoding to rqlst.as_string()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9508
diff
changeset
|
267 |
rhskey = rhs.as_string() |
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
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
rhsinfo = selectedidx[rhskey][:-1] + (None,) |
0 | 282 |
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
|
283 |
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
|
284 |
# the update step |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
285 |
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
|
286 |
# 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
|
287 |
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
|
288 |
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
|
289 |
# 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
|
290 |
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
|
291 |
step.children += self._select_plan(plan, select, rqlst.solutions) |
0 | 292 |
return (step,) |
293 |
||
294 |
# internal methods ######################################################## |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
295 |
|
0 | 296 |
def _select_plan(self, plan, select, solutions): |
297 |
union = Union() |
|
298 |
union.append(select) |
|
299 |
select.clean_solutions(solutions) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
300 |
add_types_restriction(self.schema, select) |
0 | 301 |
self.rqlhelper.annotate(union) |
302 |
return self.build_select_plan(plan, union) |
|
303 |
||
304 |
||
305 |
# execution steps and helper functions ######################################## |
|
306 |
||
307 |
def varmap_test_repr(varmap, tablesinorder): |
|
308 |
if varmap is None: |
|
309 |
return varmap |
|
310 |
maprepr = {} |
|
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10314
diff
changeset
|
311 |
for var, sql in varmap.items(): |
0 | 312 |
table, col = sql.split('.') |
313 |
maprepr[var] = '%s.%s' % (tablesinorder[table], col) |
|
314 |
return maprepr |
|
315 |
||
316 |
class Step(object): |
|
317 |
"""base abstract class for execution step""" |
|
318 |
def __init__(self, plan): |
|
319 |
self.plan = plan |
|
320 |
self.children = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
321 |
|
0 | 322 |
def execute_child(self): |
323 |
assert len(self.children) == 1 |
|
324 |
return self.children[0].execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
325 |
|
0 | 326 |
def execute_children(self): |
327 |
for step in self.children: |
|
328 |
step.execute() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
329 |
|
0 | 330 |
def execute(self): |
331 |
"""execute this step and store partial (eg this step) results""" |
|
332 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
333 |
|
0 | 334 |
def mytest_repr(self): |
335 |
"""return a representation of this step suitable for test""" |
|
336 |
return (self.__class__.__name__,) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
337 |
|
0 | 338 |
def test_repr(self): |
339 |
"""return a representation of this step suitable for test""" |
|
340 |
return self.mytest_repr() + ( |
|
341 |
[step.test_repr() for step in self.children],) |
|
342 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
343 |
|
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9206
diff
changeset
|
344 |
class OneFetchStep(Step): |
0 | 345 |
"""step consisting in fetching data from sources and directly returning |
346 |
results |
|
347 |
""" |
|
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9206
diff
changeset
|
348 |
def __init__(self, plan, union, inputmap=None): |
0 | 349 |
Step.__init__(self, plan) |
350 |
self.union = union |
|
351 |
self.inputmap = inputmap |
|
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 |
"""call .syntax_tree_search with the given syntax tree on each |
|
355 |
source for each solution |
|
356 |
""" |
|
357 |
self.execute_children() |
|
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
358 |
cnx = self.plan.cnx |
0 | 359 |
args = self.plan.args |
360 |
inputmap = self.inputmap |
|
361 |
union = self.union |
|
362 |
# do we have to use a inputmap from a previous step ? If so disable |
|
363 |
# cachekey |
|
364 |
if inputmap or self.plan.cache_key is None: |
|
365 |
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
|
366 |
# 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
|
367 |
# use plan.cache_key, rebuild a cache key |
0 | 368 |
elif isinstance(self.plan.cache_key, tuple): |
369 |
cachekey = list(self.plan.cache_key) |
|
370 |
cachekey[0] = union.as_string() |
|
371 |
cachekey = tuple(cachekey) |
|
372 |
else: |
|
373 |
cachekey = union.as_string() |
|
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9206
diff
changeset
|
374 |
# get results for query |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
375 |
source = cnx.repo.system_source |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
376 |
result = source.syntax_tree_search(cnx, union, args, cachekey, inputmap) |
0 | 377 |
#print 'ONEFETCH RESULT %s' % (result) |
378 |
return result |
|
379 |
||
380 |
def mytest_repr(self): |
|
381 |
"""return a representation of this step suitable for test""" |
|
382 |
try: |
|
383 |
inputmap = varmap_test_repr(self.inputmap, self.plan.tablesinorder) |
|
384 |
except AttributeError: |
|
385 |
inputmap = self.inputmap |
|
386 |
return (self.__class__.__name__, |
|
387 |
sorted((r.as_string(kwargs=self.plan.args), r.solutions) |
|
388 |
for r in self.union.children), |
|
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9206
diff
changeset
|
389 |
inputmap) |
0 | 390 |
|
391 |
||
392 |
# UPDATE/INSERT/DELETE steps ################################################## |
|
393 |
||
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
|
394 |
class InsertRelationsStep(Step): |
0 | 395 |
"""step consisting in adding attributes/relations to entity defs from a |
396 |
previous FetchStep |
|
397 |
||
398 |
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
|
399 |
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
|
400 |
|
0 | 401 |
for one entity definition, we'll construct N entity, where N is the |
402 |
number of the latest result |
|
403 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
404 |
|
0 | 405 |
FINAL = 0 |
406 |
RELATION = 1 |
|
407 |
REVERSE_RELATION = 2 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
408 |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
409 |
def __init__(self, plan, edef, rdefs): |
0 | 410 |
Step.__init__(self, plan) |
411 |
# 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
|
412 |
self.edef = edef |
0 | 413 |
# 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
|
414 |
self.rdefs = rdefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
415 |
|
0 | 416 |
def execute(self): |
417 |
"""execute this step""" |
|
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
418 |
base_edef = self.edef |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
419 |
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
|
420 |
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
|
421 |
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
|
422 |
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
|
423 |
result = [[]] |
2921
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
424 |
for row in result: |
0 | 425 |
# 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
|
426 |
edef = base_edef.clone() |
0 | 427 |
# 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
|
428 |
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
|
429 |
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
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
if rorder == InsertRelationsStep.FINAL: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
434 |
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
|
435 |
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
|
436 |
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
|
437 |
edef.querier_pending_relations[(rtype, 'subject')] = value |
0 | 438 |
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
|
439 |
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
|
440 |
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
|
441 |
edefs.append(edef) |
8e2544e78a5e
test and fix rset returned by SET query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
442 |
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
|
443 |
return result |
0 | 444 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
445 |
|
0 | 446 |
class InsertStep(Step): |
447 |
"""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
|
448 |
|
0 | 449 |
def execute(self): |
450 |
"""execute this step""" |
|
451 |
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
|
452 |
assert isinstance(step, InsertRelationsStep) |
0 | 453 |
step.plan = self.plan |
454 |
step.execute() |
|
455 |
# insert entities first |
|
456 |
result = self.plan.insert_entity_defs() |
|
457 |
# then relation |
|
458 |
self.plan.insert_relation_defs() |
|
459 |
# return eids of inserted entities |
|
460 |
return result |
|
461 |
||
462 |
||
463 |
class DeleteEntitiesStep(Step): |
|
464 |
"""step consisting in deleting entities""" |
|
465 |
||
466 |
def execute(self): |
|
467 |
"""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
|
468 |
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
|
469 |
if results: |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
470 |
todelete = frozenset(int(eid) for eid, in results) |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
471 |
cnx = self.plan.cnx |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
472 |
cnx.repo.glob_delete_entities(cnx, todelete) |
3648
665c37544060
on entity deletion query, return eids of deleted entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
473 |
return results |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
474 |
|
0 | 475 |
class DeleteRelationsStep(Step): |
476 |
"""step consisting in deleting relations""" |
|
477 |
||
478 |
def __init__(self, plan, rtype): |
|
479 |
Step.__init__(self, plan) |
|
480 |
self.rtype = rtype |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
481 |
|
0 | 482 |
def execute(self): |
483 |
"""execute this step""" |
|
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
484 |
cnx = self.plan.cnx |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
485 |
delete = cnx.repo.glob_delete_relation |
0 | 486 |
for subj, obj in self.execute_child(): |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
487 |
delete(cnx, subj, self.rtype, obj) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
488 |
|
0 | 489 |
|
490 |
class UpdateStep(Step): |
|
491 |
"""step consisting in updating entities / adding relations from relations |
|
492 |
definitions and from results fetched in previous step |
|
493 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
494 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
495 |
def __init__(self, plan, updatedefs): |
0 | 496 |
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
|
497 |
self.updatedefs = updatedefs |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
498 |
|
0 | 499 |
def execute(self): |
500 |
"""execute this step""" |
|
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
501 |
cnx = self.plan.cnx |
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
502 |
repo = cnx.repo |
0 | 503 |
edefs = {} |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
504 |
relations = {} |
0 | 505 |
# 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
|
506 |
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
|
507 |
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
|
508 |
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
|
509 |
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
|
510 |
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
|
511 |
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
|
512 |
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
|
513 |
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
|
514 |
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
|
515 |
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
|
516 |
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
|
517 |
try: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
518 |
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
|
519 |
except KeyError: |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
520 |
edef = cnx.entity_from_eid(eid) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
521 |
edefs[eid] = edited = EditedEntity(edef) |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5821
diff
changeset
|
522 |
edited.edited_attribute(str(rschema), rhsval) |
0 | 523 |
else: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
524 |
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
|
525 |
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
|
526 |
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
|
527 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7118
diff
changeset
|
528 |
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
|
529 |
result[i] = newrow |
0 | 530 |
# update entities |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
531 |
repo.glob_add_relations(cnx, relations) |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10314
diff
changeset
|
532 |
for eid, edited in edefs.items(): |
9508
1263f1258796
[server] rename session to cnx in querier and plan
Julien Cristau <julien.cristau@logilab.fr>
parents:
9469
diff
changeset
|
533 |
repo.glob_update_entity(cnx, edited) |
0 | 534 |
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
|
535 |
|
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
536 |
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
|
537 |
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
|
538 |
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
|
539 |
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
|
540 |
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
|
541 |
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
|
542 |
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
|
543 |
return val |