author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 10 Sep 2012 17:36:22 +0200 | |
changeset 8542 | 7e264ce34cd4 |
parent 8342 | 7a5271182ef0 |
child 8562 | 0d2fb4604265 |
permissions | -rw-r--r-- |
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8333
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
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:
5419
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Helper classes to execute RQL queries on a set of sources, performing |
19 |
security checking and data aggregation. |
|
5726
c3b99606644d
[json] fix json serialization for recent simplejson implementation, and test encoding of entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5657
diff
changeset
|
20 |
""" |
0 | 21 |
|
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:
4764
diff
changeset
|
22 |
from __future__ import with_statement |
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:
4764
diff
changeset
|
23 |
|
0 | 24 |
__docformat__ = "restructuredtext en" |
25 |
||
26 |
from itertools import repeat |
|
27 |
||
28 |
from logilab.common.compat import any |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
29 |
from rql import RQLSyntaxError, CoercionError |
0 | 30 |
from rql.stmts import Union, Select |
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
31 |
from rql.nodes import ETYPE_PYOBJ_MAP, etype_from_pyobj |
5890 | 32 |
from rql.nodes import (Relation, VariableRef, Constant, SubQuery, Function, |
33 |
Exists, Not) |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
34 |
from yams import BASE_TYPES |
0 | 35 |
|
6361
843684a50e48
[transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
36 |
from cubicweb import ValidationError, Unauthorized, QueryError, UnknownEid |
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
37 |
from cubicweb import Binary, server, typed_eid |
0 | 38 |
from cubicweb.rset import ResultSet |
39 |
||
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
40 |
from cubicweb.utils import QueryCache, RepeatList |
0 | 41 |
from cubicweb.server.utils import cleanup_solutions |
42 |
from cubicweb.server.rqlannotation import SQLGenAnnotator, set_qdata |
|
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:
7083
diff
changeset
|
43 |
from cubicweb.server.ssplanner import READ_ONLY_RTYPES, add_types_restriction |
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:
7083
diff
changeset
|
44 |
from cubicweb.server.edition import EditedEntity |
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:
4764
diff
changeset
|
45 |
from cubicweb.server.session import security_enabled |
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
46 |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
47 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
48 |
ETYPE_PYOBJ_MAP[Binary] = 'Bytes' |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
49 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
50 |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
51 |
def empty_rset(rql, args, rqlst=None): |
0 | 52 |
"""build an empty result set object""" |
53 |
return ResultSet([], rql, args, rqlst=rqlst) |
|
54 |
||
55 |
def update_varmap(varmap, selected, table): |
|
56 |
"""return a sql schema to store RQL query result""" |
|
57 |
for i, term in enumerate(selected): |
|
58 |
key = term.as_string() |
|
59 |
value = '%s.C%s' % (table, i) |
|
60 |
if varmap.get(key, value) != value: |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
61 |
raise Exception('variable name conflict on %s: got %s / %s' |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
62 |
% (key, value, varmap)) |
0 | 63 |
varmap[key] = value |
64 |
||
65 |
# permission utilities ######################################################## |
|
66 |
||
67 |
def check_no_password_selected(rqlst): |
|
68 |
"""check that Password entities are not selected""" |
|
69 |
for solution in rqlst.solutions: |
|
70 |
if 'Password' in solution.itervalues(): |
|
71 |
raise Unauthorized('Password selection is not allowed') |
|
72 |
||
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
73 |
def term_etype(session, term, solution, args): |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
74 |
"""return the entity type for the given term (a VariableRef or a Constant |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
75 |
node) |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
76 |
""" |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
77 |
try: |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
78 |
return solution[term.name] |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
79 |
except AttributeError: |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
80 |
return session.describe(term.eval(args))[0] |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
81 |
|
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
82 |
def check_read_access(session, rqlst, solution, args): |
0 | 83 |
"""check that the given user has credentials to access data read the |
84 |
query |
|
85 |
||
86 |
return a dict defining necessary local checks (due to use of rql expression |
|
87 |
in the schema), keys are variable names and values associated rql expression |
|
88 |
for the associated variable with the given solution |
|
89 |
""" |
|
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
90 |
# use `term_etype` since we've to deal with rewritten constants here, |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
91 |
# when used as an external source by another repository. |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
92 |
# XXX what about local read security w/ those rewritten constants... |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
93 |
schema = session.repo.schema |
0 | 94 |
if rqlst.where is not None: |
95 |
for rel in rqlst.where.iget_nodes(Relation): |
|
96 |
# XXX has_text may have specific perm ? |
|
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
97 |
if rel.r_type in READ_ONLY_RTYPES: |
0 | 98 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
99 |
rschema = schema.rschema(rel.r_type) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
100 |
if rschema.final: |
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
101 |
eschema = schema.eschema(term_etype(session, rel.children[0], |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
102 |
solution, args)) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
103 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
104 |
else: |
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
105 |
rdef = rschema.rdef(term_etype(session, rel.children[0], |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
106 |
solution, args), |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
107 |
term_etype(session, rel.children[1].children[0], |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
108 |
solution, args)) |
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
109 |
if not session.user.matching_groups(rdef.get_groups('read')): |
4955 | 110 |
# XXX rqlexpr not allowed |
0 | 111 |
raise Unauthorized('read', rel.r_type) |
112 |
localchecks = {} |
|
113 |
# iterate on defined_vars and not on solutions to ignore column aliases |
|
114 |
for varname in rqlst.defined_vars: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
115 |
eschema = schema.eschema(solution[varname]) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
116 |
if eschema.final: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
117 |
continue |
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
118 |
if not session.user.matching_groups(eschema.get_groups('read')): |
0 | 119 |
erqlexprs = eschema.get_rqlexprs('read') |
120 |
if not erqlexprs: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
121 |
ex = Unauthorized('read', solution[varname]) |
0 | 122 |
ex.var = varname |
123 |
raise ex |
|
5888
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
124 |
# don't insert security on variable only referenced by 'NOT X relation Y' or |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
125 |
# 'NOT EXISTS(X relation Y)' |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
126 |
varinfo = rqlst.defined_vars[varname].stinfo |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
127 |
if varinfo['selected'] or ( |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
128 |
len([r for r in varinfo['relations'] |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
129 |
if (not schema.rschema(r.r_type).final |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
130 |
and ((isinstance(r.parent, Exists) and r.parent.neged(strict=True)) |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
131 |
or isinstance(r.parent, Not)))]) |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
132 |
!= len(varinfo['relations'])): |
3ee80d487f11
[security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
133 |
localchecks[varname] = erqlexprs |
0 | 134 |
return localchecks |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
135 |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
136 |
def add_noinvariant(noinvariant, restricted, select, nbtrees): |
0 | 137 |
# a variable can actually be invariant if it has not been restricted for |
138 |
# security reason or if security assertion hasn't modified the possible |
|
139 |
# solutions for the query |
|
140 |
if nbtrees != 1: |
|
141 |
for vname in restricted: |
|
142 |
try: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
143 |
noinvariant.add(select.defined_vars[vname]) |
0 | 144 |
except KeyError: |
145 |
# this is an alias |
|
146 |
continue |
|
147 |
else: |
|
148 |
for vname in restricted: |
|
149 |
try: |
|
150 |
var = select.defined_vars[vname] |
|
151 |
except KeyError: |
|
152 |
# this is an alias |
|
153 |
continue |
|
154 |
if len(var.stinfo['possibletypes']) != 1: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
155 |
noinvariant.add(var) |
0 | 156 |
|
157 |
def _expand_selection(terms, selected, aliases, select, newselect): |
|
158 |
for term in terms: |
|
159 |
for vref in term.iget_nodes(VariableRef): |
|
160 |
if not vref.name in selected: |
|
161 |
select.append_selected(vref) |
|
162 |
colalias = newselect.get_variable(vref.name, len(aliases)) |
|
163 |
aliases.append(VariableRef(colalias)) |
|
164 |
selected.add(vref.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
165 |
|
0 | 166 |
# Plans ####################################################################### |
167 |
||
168 |
class ExecutionPlan(object): |
|
169 |
"""the execution model of a rql query, composed of querier steps""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
170 |
|
0 | 171 |
def __init__(self, querier, rqlst, args, session): |
172 |
# original rql syntax tree |
|
173 |
self.rqlst = rqlst |
|
174 |
self.args = args or {} |
|
175 |
# session executing the query |
|
176 |
self.session = session |
|
177 |
# quick reference to the system source |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
178 |
self.syssource = session.cnxset.source('system') |
0 | 179 |
# execution steps |
180 |
self.steps = [] |
|
181 |
# index of temporary tables created during execution |
|
182 |
self.temp_tables = {} |
|
183 |
# various resource accesors |
|
184 |
self.querier = querier |
|
185 |
self.schema = querier.schema |
|
186 |
self.sqlannotate = querier.sqlgen_annotate |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
187 |
self.rqlhelper = session.vreg.rqlhelper |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
188 |
|
0 | 189 |
def annotate_rqlst(self): |
190 |
if not self.rqlst.annotated: |
|
191 |
self.rqlhelper.annotate(self.rqlst) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
192 |
|
0 | 193 |
def add_step(self, step): |
194 |
"""add a step to the plan""" |
|
195 |
self.steps.append(step) |
|
196 |
||
197 |
def clean(self): |
|
198 |
"""remove temporary tables""" |
|
199 |
self.syssource.clean_temp_data(self.session, self.temp_tables) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
200 |
|
0 | 201 |
def sqlexec(self, sql, args=None): |
202 |
return self.syssource.sqlexec(self.session, sql, args) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
203 |
|
0 | 204 |
def execute(self): |
205 |
"""execute a plan and return resulting rows""" |
|
206 |
try: |
|
207 |
for step in self.steps: |
|
208 |
result = step.execute() |
|
209 |
# the latest executed step contains the full query result |
|
210 |
return result |
|
211 |
finally: |
|
212 |
self.clean() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
213 |
|
5168
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
214 |
def make_temp_table_name(self, table): |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
215 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
216 |
return a temp table name according to db backend |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
217 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
218 |
return self.syssource.make_temp_table_name(table) |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
219 |
|
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
220 |
|
0 | 221 |
def init_temp_table(self, table, selected, sol): |
222 |
"""initialize sql schema and variable map for a temporary table which |
|
223 |
will be used to store result for the given rqlst |
|
224 |
""" |
|
225 |
try: |
|
226 |
outputmap, sqlschema, _ = self.temp_tables[table] |
|
227 |
update_varmap(outputmap, selected, table) |
|
228 |
except KeyError: |
|
229 |
sqlschema, outputmap = self.syssource.temp_table_def(selected, sol, |
|
230 |
table) |
|
231 |
self.temp_tables[table] = [outputmap, sqlschema, False] |
|
232 |
return outputmap |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
233 |
|
0 | 234 |
def create_temp_table(self, table): |
235 |
"""create a temporary table to store result for the given rqlst""" |
|
236 |
if not self.temp_tables[table][-1]: |
|
237 |
sqlschema = self.temp_tables[table][1] |
|
238 |
self.syssource.create_temp_table(self.session, table, sqlschema) |
|
239 |
self.temp_tables[table][-1] = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
240 |
|
0 | 241 |
def preprocess(self, union, security=True): |
242 |
"""insert security when necessary then annotate rql st for sql generation |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
243 |
|
0 | 244 |
return rqlst to actually execute |
245 |
""" |
|
4954
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
246 |
cached = None |
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:
4764
diff
changeset
|
247 |
if security and self.session.read_security: |
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:
4764
diff
changeset
|
248 |
# ensure security is turned of when security is inserted, |
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:
4764
diff
changeset
|
249 |
# else we may loop for ever... |
4954
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
250 |
if self.session.transaction_data.get('security-rqlst-cache'): |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
251 |
key = self.cache_key |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
252 |
else: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
253 |
key = None |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
254 |
if key is not None and key in self.session.transaction_data: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
255 |
cachedunion, args = self.session.transaction_data[key] |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
256 |
union.children[:] = [] |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
257 |
for select in cachedunion.children: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
258 |
union.append(select) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
259 |
union.has_text_query = cachedunion.has_text_query |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
260 |
args.update(self.args) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
261 |
self.args = args |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
262 |
cached = True |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
263 |
else: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
264 |
noinvariant = set() |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
265 |
with security_enabled(self.session, read=False): |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
266 |
self._insert_security(union, noinvariant) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
267 |
if key is not None: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
268 |
self.session.transaction_data[key] = (union, self.args) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
269 |
else: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
270 |
noinvariant = () |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
271 |
if cached is None: |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
272 |
self.rqlhelper.simplify(union) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
273 |
self.sqlannotate(union) |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
274 |
set_qdata(self.schema.rschema, union, noinvariant) |
0 | 275 |
if union.has_text_query: |
276 |
self.cache_key = None |
|
277 |
||
278 |
def _insert_security(self, union, noinvariant): |
|
279 |
for select in union.children[:]: |
|
280 |
for subquery in select.with_: |
|
281 |
self._insert_security(subquery.query, noinvariant) |
|
282 |
localchecks, restricted = self._check_permissions(select) |
|
283 |
if any(localchecks): |
|
284 |
rewrite = self.session.rql_rewriter.rewrite |
|
285 |
nbtrees = len(localchecks) |
|
286 |
myunion = union |
|
287 |
# transform in subquery when len(localchecks)>1 and groups |
|
288 |
if nbtrees > 1 and (select.orderby or select.groupby or |
|
289 |
select.having or select.has_aggregat or |
|
5596
d66a5d98db5b
[security] fix security insertion on DISTINCT queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
290 |
select.distinct or |
0 | 291 |
select.limit or select.offset): |
292 |
newselect = Select() |
|
293 |
# only select variables in subqueries |
|
294 |
origselection = select.selection |
|
295 |
select.select_only_variables() |
|
296 |
select.has_aggregat = False |
|
297 |
# create subquery first so correct node are used on copy |
|
298 |
# (eg ColumnAlias instead of Variable) |
|
299 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
300 |
for i, vref in enumerate(select.selection)] |
|
301 |
selected = set(vref.name for vref in aliases) |
|
302 |
# now copy original selection and groups |
|
303 |
for term in origselection: |
|
304 |
newselect.append_selected(term.copy(newselect)) |
|
305 |
if select.orderby: |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
306 |
sortterms = [] |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
307 |
for sortterm in select.orderby: |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
308 |
sortterms.append(sortterm.copy(newselect)) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
309 |
for fnode in sortterm.get_nodes(Function): |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
310 |
if fnode.name == 'FTIRANK': |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
311 |
# we've to fetch the has_text relation as well |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
312 |
var = fnode.children[0].variable |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
313 |
rel = iter(var.stinfo['ftirels']).next() |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
314 |
assert not rel.ored(), 'unsupported' |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
315 |
newselect.add_restriction(rel.copy(newselect)) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
316 |
# remove relation from the orig select and |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
317 |
# cleanup variable stinfo |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
318 |
rel.parent.remove(rel) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
319 |
var.stinfo['ftirels'].remove(rel) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
320 |
var.stinfo['relations'].remove(rel) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
321 |
# XXX not properly re-annotated after security insertion? |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
322 |
newvar = newselect.get_variable(var.name) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
323 |
newvar.stinfo.setdefault('ftirels', set()).add(rel) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
324 |
newvar.stinfo.setdefault('relations', set()).add(rel) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
325 |
newselect.set_orderby(sortterms) |
0 | 326 |
_expand_selection(select.orderby, selected, aliases, select, newselect) |
327 |
select.orderby = () # XXX dereference? |
|
328 |
if select.groupby: |
|
329 |
newselect.set_groupby([g.copy(newselect) for g in select.groupby]) |
|
330 |
_expand_selection(select.groupby, selected, aliases, select, newselect) |
|
331 |
select.groupby = () # XXX dereference? |
|
332 |
if select.having: |
|
333 |
newselect.set_having([g.copy(newselect) for g in select.having]) |
|
334 |
_expand_selection(select.having, selected, aliases, select, newselect) |
|
335 |
select.having = () # XXX dereference? |
|
336 |
if select.limit: |
|
337 |
newselect.limit = select.limit |
|
338 |
select.limit = None |
|
339 |
if select.offset: |
|
340 |
newselect.offset = select.offset |
|
341 |
select.offset = 0 |
|
342 |
myunion = Union() |
|
343 |
newselect.set_with([SubQuery(aliases, myunion)], check=False) |
|
5596
d66a5d98db5b
[security] fix security insertion on DISTINCT queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
344 |
newselect.distinct = select.distinct |
0 | 345 |
solutions = [sol.copy() for sol in select.solutions] |
346 |
cleanup_solutions(newselect, solutions) |
|
347 |
newselect.set_possible_types(solutions) |
|
348 |
# if some solutions doesn't need rewriting, insert original |
|
349 |
# select as first union subquery |
|
350 |
if () in localchecks: |
|
351 |
myunion.append(select) |
|
352 |
# we're done, replace original select by the new select with |
|
353 |
# subqueries (more added in the loop below) |
|
354 |
union.replace(select, newselect) |
|
355 |
elif not () in localchecks: |
|
356 |
union.remove(select) |
|
357 |
for lcheckdef, lchecksolutions in localchecks.iteritems(): |
|
358 |
if not lcheckdef: |
|
359 |
continue |
|
360 |
myrqlst = select.copy(solutions=lchecksolutions) |
|
361 |
myunion.append(myrqlst) |
|
362 |
# in-place rewrite + annotation / simplification |
|
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7118
diff
changeset
|
363 |
lcheckdef = [({var: 'X'}, rqlexprs) for var, rqlexprs in lcheckdef] |
0 | 364 |
rewrite(myrqlst, lcheckdef, lchecksolutions, self.args) |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
365 |
add_noinvariant(noinvariant, restricted, myrqlst, nbtrees) |
0 | 366 |
if () in localchecks: |
367 |
select.set_possible_types(localchecks[()]) |
|
368 |
add_types_restriction(self.schema, select) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
369 |
add_noinvariant(noinvariant, restricted, select, nbtrees) |
5989
099d46422f5a
[querier] actually fix bug attempted to be fixed in 5942:f1abba8ccc01. The pb was not in multi-sources but because of missing annotation on rql generated by security insertion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5890
diff
changeset
|
370 |
self.rqlhelper.annotate(union) |
0 | 371 |
|
372 |
def _check_permissions(self, rqlst): |
|
373 |
"""return a dict defining "local checks", e.g. RQLExpression defined in |
|
374 |
the schema that should be inserted in the original query |
|
375 |
||
376 |
solutions where a variable has a type which the user can't definitly read |
|
377 |
are removed, else if the user may read it (eg if an rql expression is |
|
378 |
defined for the "read" permission of the related type), the local checks |
|
379 |
dict for the solution is updated |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
380 |
|
0 | 381 |
return a dict with entries for each different local check necessary, |
382 |
with associated solutions as value. A local check is defined by a list |
|
383 |
of 2-uple, with variable name as first item and the necessary rql |
|
384 |
expression as second item for each variable which has to be checked. |
|
385 |
So solutions which don't require local checks will be associated to |
|
386 |
the empty tuple key. |
|
387 |
||
388 |
note: rqlst should not have been simplified at this point |
|
389 |
""" |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
390 |
session = self.session |
0 | 391 |
msgs = [] |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
392 |
neweids = session.transaction_data.get('neweids', ()) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
393 |
varkwargs = {} |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
394 |
if not session.transaction_data.get('security-rqlst-cache'): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
395 |
for var in rqlst.defined_vars.itervalues(): |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4957
diff
changeset
|
396 |
if var.stinfo['constnode'] is not None: |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4957
diff
changeset
|
397 |
eid = var.stinfo['constnode'].eval(self.args) |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4957
diff
changeset
|
398 |
varkwargs[var.name] = typed_eid(eid) |
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8127
diff
changeset
|
399 |
# dictionary of variables restricted for security reason |
0 | 400 |
localchecks = {} |
401 |
restricted_vars = set() |
|
402 |
newsolutions = [] |
|
403 |
for solution in rqlst.solutions: |
|
404 |
try: |
|
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
405 |
localcheck = check_read_access(session, rqlst, solution, self.args) |
0 | 406 |
except Unauthorized, ex: |
407 |
msg = 'remove %s from solutions since %s has no %s access to %s' |
|
5419
0b7805928a27
[repo security]Â deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5168
diff
changeset
|
408 |
msg %= (solution, session.user.login, ex.args[0], ex.args[1]) |
0 | 409 |
msgs.append(msg) |
410 |
LOGGER.info(msg) |
|
411 |
else: |
|
412 |
newsolutions.append(solution) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
413 |
# try to benefit of rqlexpr.check cache for entities which |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
414 |
# are specified by eid in query'args |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
415 |
for varname, eid in varkwargs.iteritems(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
416 |
try: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
417 |
rqlexprs = localcheck.pop(varname) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
418 |
except KeyError: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
419 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
420 |
if eid in neweids: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
421 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
422 |
for rqlexpr in rqlexprs: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
423 |
if rqlexpr.check(session, eid): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
424 |
break |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
425 |
else: |
8333
baa3ea5eac2b
querier: Improve error message when ERQLExpression fails on some entity (closes #2267081)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8238
diff
changeset
|
426 |
raise Unauthorized('No read acces on %r with eid %i.' % (var, eid)) |
0 | 427 |
restricted_vars.update(localcheck) |
428 |
localchecks.setdefault(tuple(localcheck.iteritems()), []).append(solution) |
|
429 |
# raise Unautorized exception if the user can't access to any solution |
|
430 |
if not newsolutions: |
|
431 |
raise Unauthorized('\n'.join(msgs)) |
|
6128 | 432 |
if msgs: |
433 |
# (else solutions have not been modified) |
|
434 |
rqlst.set_possible_types(newsolutions) |
|
0 | 435 |
return localchecks, restricted_vars |
436 |
||
437 |
def finalize(self, select, solutions, insertedvars): |
|
438 |
rqlst = Union() |
|
439 |
rqlst.append(select) |
|
440 |
for mainvarname, rschema, newvarname in insertedvars: |
|
441 |
nvartype = str(rschema.objects(solutions[0][mainvarname])[0]) |
|
442 |
for sol in solutions: |
|
443 |
sol[newvarname] = nvartype |
|
444 |
select.clean_solutions(solutions) |
|
6129
fea746b60093
[ms] fix two planner bugs: one occuring query such as X created_by U where X in a external source and U may come from an ldap source. The other being that when we've to merge input maps, we were modifying the same tree/solutions while a copy were needed. Also, ensure we add type restrictions, necessary for pyro source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6128
diff
changeset
|
445 |
add_types_restriction(self.schema, select) |
0 | 446 |
self.rqlhelper.annotate(rqlst) |
447 |
self.preprocess(rqlst, security=False) |
|
448 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
449 |
|
4759 | 450 |
|
0 | 451 |
class InsertPlan(ExecutionPlan): |
452 |
"""an execution model specific to the INSERT rql query |
|
453 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
454 |
|
0 | 455 |
def __init__(self, querier, rqlst, args, session): |
456 |
ExecutionPlan.__init__(self, querier, rqlst, args, session) |
|
457 |
# save originaly selected variable, we may modify this |
|
458 |
# dictionary for substitution (query parameters) |
|
459 |
self.selected = rqlst.selection |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
460 |
# list of rows of entities definition (ssplanner.EditedEntity) |
0 | 461 |
self.e_defs = [[]] |
462 |
# list of new relation definition (3-uple (from_eid, r_type, to_eid) |
|
5657
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
463 |
self.r_defs = set() |
0 | 464 |
# indexes to track entity definitions bound to relation definitions |
465 |
self._r_subj_index = {} |
|
466 |
self._r_obj_index = {} |
|
467 |
self._expanded_r_defs = {} |
|
468 |
||
469 |
def add_entity_def(self, edef): |
|
470 |
"""add an entity definition to build""" |
|
471 |
self.e_defs[-1].append(edef) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
472 |
|
0 | 473 |
def add_relation_def(self, rdef): |
474 |
"""add an relation definition to build""" |
|
5657
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
475 |
self.r_defs.add(rdef) |
0 | 476 |
if not isinstance(rdef[0], int): |
477 |
self._r_subj_index.setdefault(rdef[0], []).append(rdef) |
|
478 |
if not isinstance(rdef[2], int): |
|
479 |
self._r_obj_index.setdefault(rdef[2], []).append(rdef) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
480 |
|
0 | 481 |
def substitute_entity_def(self, edef, edefs): |
482 |
"""substitute an incomplete entity definition by a list of complete |
|
483 |
equivalents |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
484 |
|
0 | 485 |
e.g. on queries such as :: |
486 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
487 |
WHERE U login 'admin', U login N |
|
488 |
||
489 |
X will be inserted as many times as U exists, and so the X travaille Y |
|
490 |
relations as to be added as many time as X is inserted |
|
491 |
""" |
|
492 |
if not edefs or not self.e_defs: |
|
493 |
# no result, no entity will be created |
|
494 |
self.e_defs = () |
|
495 |
return |
|
496 |
# first remove the incomplete entity definition |
|
497 |
colidx = self.e_defs[0].index(edef) |
|
498 |
for i, row in enumerate(self.e_defs[:]): |
|
499 |
self.e_defs[i][colidx] = edefs[0] |
|
500 |
samplerow = self.e_defs[i] |
|
5657
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
501 |
for edef_ in edefs[1:]: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
502 |
row = [ed.clone() for i, ed in enumerate(samplerow) |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
503 |
if i != colidx] |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
504 |
row.insert(colidx, edef_) |
0 | 505 |
self.e_defs.append(row) |
506 |
# now, see if this entity def is referenced as subject in some relation |
|
507 |
# definition |
|
508 |
if self._r_subj_index.has_key(edef): |
|
509 |
for rdef in self._r_subj_index[edef]: |
|
510 |
expanded = self._expanded(rdef) |
|
511 |
result = [] |
|
512 |
for exp_rdef in expanded: |
|
5657
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
513 |
for edef_ in edefs: |
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
514 |
result.append( (edef_, exp_rdef[1], exp_rdef[2]) ) |
0 | 515 |
self._expanded_r_defs[rdef] = result |
516 |
# and finally, see if this entity def is referenced as object in some |
|
517 |
# relation definition |
|
518 |
if self._r_obj_index.has_key(edef): |
|
519 |
for rdef in self._r_obj_index[edef]: |
|
520 |
expanded = self._expanded(rdef) |
|
521 |
result = [] |
|
522 |
for exp_rdef in expanded: |
|
5657
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
523 |
for edef_ in edefs: |
8f56691f01de
[querier] test and fix potential bug in insertion query with multiple relations inside, caused by a name conflict. Also fix another bug non reproduced by using a set instead of a list (list may leads to compare entity instances which isn't allowed).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5596
diff
changeset
|
524 |
result.append( (exp_rdef[0], exp_rdef[1], edef_) ) |
0 | 525 |
self._expanded_r_defs[rdef] = result |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
526 |
|
0 | 527 |
def _expanded(self, rdef): |
528 |
"""return expanded value for the given relation definition""" |
|
529 |
try: |
|
530 |
return self._expanded_r_defs[rdef] |
|
531 |
except KeyError: |
|
532 |
self.r_defs.remove(rdef) |
|
533 |
return [rdef] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
534 |
|
0 | 535 |
def relation_defs(self): |
536 |
"""return the list for relation definitions to insert""" |
|
537 |
for rdefs in self._expanded_r_defs.values(): |
|
538 |
for rdef in rdefs: |
|
539 |
yield rdef |
|
540 |
for rdef in self.r_defs: |
|
541 |
yield rdef |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
542 |
|
0 | 543 |
def insert_entity_defs(self): |
544 |
"""return eids of inserted entities in a suitable form for the resulting |
|
545 |
result set, e.g.: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
546 |
|
0 | 547 |
e.g. on queries such as :: |
548 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
549 |
WHERE U login 'admin', U login N |
|
550 |
||
551 |
if there is two entities matching U, the result set will look like |
|
552 |
[(eidX1, eidY1), (eidX2, eidY2)] |
|
553 |
""" |
|
554 |
session = self.session |
|
555 |
repo = session.repo |
|
556 |
results = [] |
|
557 |
for row in self.e_defs: |
|
558 |
results.append([repo.glob_add_entity(session, edef) |
|
559 |
for edef in row]) |
|
560 |
return results |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
561 |
|
0 | 562 |
def insert_relation_defs(self): |
563 |
session = self.session |
|
564 |
repo = session.repo |
|
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
565 |
edited_entities = {} |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
566 |
relations = {} |
0 | 567 |
for subj, rtype, obj in self.relation_defs(): |
568 |
# if a string is given into args instead of an int, we get it here |
|
569 |
if isinstance(subj, basestring): |
|
570 |
subj = typed_eid(subj) |
|
571 |
elif not isinstance(subj, (int, long)): |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
572 |
subj = subj.entity.eid |
0 | 573 |
if isinstance(obj, basestring): |
574 |
obj = typed_eid(obj) |
|
575 |
elif not isinstance(obj, (int, long)): |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
576 |
obj = obj.entity.eid |
0 | 577 |
if repo.schema.rschema(rtype).inlined: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
578 |
if subj not in edited_entities: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
579 |
entity = session.entity_from_eid(subj) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
580 |
edited = EditedEntity(entity) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
581 |
edited_entities[subj] = edited |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
582 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
583 |
edited = edited_entities[subj] |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
584 |
edited.edited_attribute(rtype, obj) |
0 | 585 |
else: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
586 |
if rtype in relations: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
587 |
relations[rtype].append((subj, obj)) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
588 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
589 |
relations[rtype] = [(subj, obj)] |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
590 |
repo.glob_add_relations(session, relations) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
591 |
for edited in edited_entities.itervalues(): |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
592 |
repo.glob_update_entity(session, edited) |
0 | 593 |
|
594 |
||
595 |
class QuerierHelper(object): |
|
596 |
"""helper class to execute rql queries, putting all things together""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
597 |
|
0 | 598 |
def __init__(self, repo, schema): |
599 |
# system info helper |
|
600 |
self._repo = repo |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
601 |
# instance schema |
0 | 602 |
self.set_schema(schema) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
603 |
|
0 | 604 |
def set_schema(self, schema): |
605 |
self.schema = schema |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
606 |
repo = self._repo |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
607 |
# rql st and solution cache. |
7954
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7779
diff
changeset
|
608 |
self._rql_cache = QueryCache(repo.config['rql-cache-size']) |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
609 |
# rql cache key cache. Don't bother using a Cache instance: we should |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
610 |
# have a limited number of queries in there, since there are no entries |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
611 |
# in this cache for user queries (which have no args) |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
612 |
self._rql_ck_cache = {} |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
613 |
# some cache usage stats |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
614 |
self.cache_hit, self.cache_miss = 0, 0 |
0 | 615 |
# rql parsing / analysing helper |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
616 |
self.solutions = repo.vreg.solutions |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
617 |
rqlhelper = repo.vreg.rqlhelper |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
618 |
# set backend on the rql helper, will be used for function checking |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5726
diff
changeset
|
619 |
rqlhelper.backend = repo.config.sources()['system']['db-driver'] |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
620 |
self._parse = rqlhelper.parse |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
621 |
self._annotate = rqlhelper.annotate |
0 | 622 |
# rql planner |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
623 |
if len(repo.sources) < 2: |
0 | 624 |
from cubicweb.server.ssplanner import SSPlanner |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
625 |
self._planner = SSPlanner(schema, rqlhelper) |
0 | 626 |
else: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
627 |
from cubicweb.server.msplanner import MSPlanner |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
628 |
self._planner = MSPlanner(schema, rqlhelper) |
0 | 629 |
# sql generation annotator |
630 |
self.sqlgen_annotate = SQLGenAnnotator(schema).annotate |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
631 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
632 |
def set_planner(self): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
633 |
if len(self._repo.sources) < 2: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
634 |
from cubicweb.server.ssplanner import SSPlanner |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
635 |
self._planner = SSPlanner(self.schema, self._repo.vreg.rqlhelper) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
636 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
637 |
from cubicweb.server.msplanner import MSPlanner |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
638 |
self._planner = MSPlanner(self.schema, self._repo.vreg.rqlhelper) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
639 |
|
0 | 640 |
def parse(self, rql, annotate=False): |
641 |
"""return a rql syntax tree for the given rql""" |
|
642 |
try: |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
643 |
return self._parse(unicode(rql), annotate=annotate) |
0 | 644 |
except UnicodeError: |
645 |
raise RQLSyntaxError(rql) |
|
646 |
||
647 |
def plan_factory(self, rqlst, args, session): |
|
648 |
"""create an execution plan for an INSERT RQL query""" |
|
649 |
if rqlst.TYPE == 'insert': |
|
650 |
return InsertPlan(self, rqlst, args, session) |
|
651 |
return ExecutionPlan(self, rqlst, args, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
652 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
653 |
def execute(self, session, rql, args=None, build_descr=True): |
0 | 654 |
"""execute a rql query, return resulting rows and their description in |
655 |
a `ResultSet` object |
|
656 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
657 |
* `rql` should be an Unicode string or a plain ASCII string |
0 | 658 |
* `args` the optional parameters dictionary associated to the query |
659 |
* `build_descr` is a boolean flag indicating if the description should |
|
660 |
be built on select queries (if false, the description will be en empty |
|
661 |
list) |
|
662 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
663 |
on INSERT queries, there will be one row with the eid of each inserted |
0 | 664 |
entity |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
665 |
|
0 | 666 |
result for DELETE and SET queries is undefined yet |
667 |
||
668 |
to maximize the rql parsing/analyzing cache performance, you should |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
669 |
always use substitute arguments in queries (i.e. avoid query such as |
0 | 670 |
'Any X WHERE X eid 123'!) |
671 |
""" |
|
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
672 |
if server.DEBUG & (server.DBG_RQL | server.DBG_SQL): |
2629
0d445c2171e4
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2621
diff
changeset
|
673 |
if server.DEBUG & (server.DBG_MORE | server.DBG_SQL): |
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
674 |
print '*'*80 |
7779
3826d8480a68
[debug] use repr() in some place where you'll have a chance to get an unicode error if output encoding isn't properly detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
675 |
print 'querier input', repr(rql), repr(args) |
0 | 676 |
# parse the query and binds variables |
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:
7954
diff
changeset
|
677 |
cachekey = (rql,) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
678 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
679 |
if args: |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
680 |
# search for named args in query which are eids (hence |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
681 |
# influencing query's solutions) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
682 |
eidkeys = self._rql_ck_cache[rql] |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
683 |
if eidkeys: |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
684 |
# if there are some, we need a better cache key, eg (rql + |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
685 |
# entity type of each eid) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
686 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
687 |
cachekey = self._repo.querier_cache_key(session, rql, |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
688 |
args, eidkeys) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
689 |
except UnknownEid: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
690 |
# we want queries such as "Any X WHERE X eid 9999" |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
691 |
# return an empty result instead of raising UnknownEid |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
692 |
return empty_rset(rql, args) |
0 | 693 |
rqlst = self._rql_cache[cachekey] |
694 |
self.cache_hit += 1 |
|
695 |
except KeyError: |
|
696 |
self.cache_miss += 1 |
|
697 |
rqlst = self.parse(rql) |
|
698 |
try: |
|
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
699 |
# compute solutions for rqlst and return named args in query |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
700 |
# which are eids. Notice that if you may not need `eidkeys`, we |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
701 |
# have to compute solutions anyway (kept as annotation on the |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
702 |
# tree) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
703 |
eidkeys = self.solutions(session, rqlst, args) |
0 | 704 |
except UnknownEid: |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
705 |
# we want queries such as "Any X WHERE X eid 9999" return an |
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
706 |
# empty result instead of raising UnknownEid |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
707 |
return empty_rset(rql, args, rqlst) |
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:
7954
diff
changeset
|
708 |
if args and rql not in self._rql_ck_cache: |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
709 |
self._rql_ck_cache[rql] = eidkeys |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
710 |
if eidkeys: |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
711 |
cachekey = self._repo.querier_cache_key(session, rql, args, |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
712 |
eidkeys) |
0 | 713 |
self._rql_cache[cachekey] = rqlst |
714 |
orig_rqlst = rqlst |
|
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4957
diff
changeset
|
715 |
if rqlst.TYPE != 'select': |
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:
4764
diff
changeset
|
716 |
if session.read_security: |
0 | 717 |
check_no_password_selected(rqlst) |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
718 |
# write query, ensure session's mode is 'write' so connections won't |
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
719 |
# be released until commit/rollback |
0 | 720 |
session.mode = 'write' |
721 |
cachekey = None |
|
722 |
else: |
|
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:
4764
diff
changeset
|
723 |
if session.read_security: |
0 | 724 |
for select in rqlst.children: |
725 |
check_no_password_selected(select) |
|
726 |
# on select query, always copy the cached rqlst so we don't have to |
|
727 |
# bother modifying it. This is not necessary on write queries since |
|
728 |
# a new syntax tree is built from them. |
|
729 |
rqlst = rqlst.copy() |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3171
diff
changeset
|
730 |
self._annotate(rqlst) |
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:
7954
diff
changeset
|
731 |
if args: |
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8333
diff
changeset
|
732 |
# different SQL generated when some argument is None or not (IS |
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:
7954
diff
changeset
|
733 |
# NULL). This should be considered when computing sql cache key |
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:
7954
diff
changeset
|
734 |
cachekey += tuple(sorted([k for k,v in args.iteritems() |
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:
7954
diff
changeset
|
735 |
if v is None])) |
0 | 736 |
# make an execution plan |
737 |
plan = self.plan_factory(rqlst, args, session) |
|
738 |
plan.cache_key = cachekey |
|
739 |
self._planner.build_plan(plan) |
|
740 |
# execute the plan |
|
741 |
try: |
|
742 |
results = plan.execute() |
|
6361
843684a50e48
[transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
743 |
except (Unauthorized, ValidationError): |
843684a50e48
[transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
744 |
# getting an Unauthorized/ValidationError exception means the |
843684a50e48
[transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
745 |
# transaction must been rollbacked |
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
746 |
# |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
747 |
# notes: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
748 |
# * we should not reset the connections set here, since we don't want the |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
749 |
# session to loose it during processing |
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
750 |
# * don't rollback if we're in the commit process, will be handled |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
751 |
# by the session |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
752 |
if session.commit_state is None: |
6385
9f91d09ee5fa
[repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6361
diff
changeset
|
753 |
session.commit_state = 'uncommitable' |
0 | 754 |
raise |
755 |
# build a description for the results if necessary |
|
756 |
descr = () |
|
757 |
if build_descr: |
|
758 |
if rqlst.TYPE == 'select': |
|
759 |
# sample selection |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
760 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
761 |
# easy, all lines are identical |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
762 |
selected = rqlst.children[0].selection |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
763 |
solution = rqlst.children[0].solutions[0] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
764 |
description = _make_description(selected, args, solution) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
765 |
descr = RepeatList(len(results), tuple(description)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
766 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
767 |
# hard, delegate the work :o) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
768 |
descr = manual_build_descr(session, rqlst, args, results) |
0 | 769 |
elif rqlst.TYPE == 'insert': |
770 |
# on insert plan, some entities may have been auto-casted, |
|
771 |
# so compute description manually even if there is only |
|
772 |
# one solution |
|
773 |
basedescr = [None] * len(plan.selected) |
|
774 |
todetermine = zip(xrange(len(plan.selected)), repeat(False)) |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
775 |
descr = _build_descr(session, results, basedescr, todetermine) |
0 | 776 |
# FIXME: get number of affected entities / relations on non |
777 |
# selection queries ? |
|
778 |
# return a result set object |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
779 |
return ResultSet(results, rql, args, descr, orig_rqlst) |
0 | 780 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
781 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
782 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
783 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
784 |
|
0 | 785 |
from logging import getLogger |
786 |
from cubicweb import set_log_methods |
|
787 |
LOGGER = getLogger('cubicweb.querier') |
|
788 |
set_log_methods(QuerierHelper, LOGGER) |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
789 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
790 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
791 |
def manual_build_descr(tx, rqlst, args, result): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
792 |
"""build a description for a given result by analysing each row |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
793 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
794 |
XXX could probably be done more efficiently during execution of query |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
795 |
""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
796 |
# not so easy, looks for variable which changes from one solution |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
797 |
# to another |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
798 |
unstables = rqlst.get_variable_indices() |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
799 |
basedescr = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
800 |
todetermine = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
801 |
for i in xrange(len(rqlst.children[0].selection)): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
802 |
ttype = _selection_idx_type(i, rqlst, args) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
803 |
if ttype is None or ttype == 'Any': |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
804 |
ttype = None |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
805 |
isfinal = True |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
806 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
807 |
isfinal = ttype in BASE_TYPES |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
808 |
if ttype is None or i in unstables: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
809 |
basedescr.append(None) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
810 |
todetermine.append( (i, isfinal) ) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
811 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
812 |
basedescr.append(ttype) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
813 |
if not todetermine: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
814 |
return RepeatList(len(result), tuple(basedescr)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
815 |
return _build_descr(tx, result, basedescr, todetermine) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
816 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
817 |
def _build_descr(tx, result, basedescription, todetermine): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
818 |
description = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
819 |
etype_from_eid = tx.describe |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
820 |
todel = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
821 |
for i, row in enumerate(result): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
822 |
row_descr = basedescription[:] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
823 |
for index, isfinal in todetermine: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
824 |
value = row[index] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
825 |
if value is None: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
826 |
# None value inserted by an outer join, no type |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
827 |
row_descr[index] = None |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
828 |
continue |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
829 |
if isfinal: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
830 |
row_descr[index] = etype_from_pyobj(value) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
831 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
832 |
try: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
833 |
row_descr[index] = etype_from_eid(value)[0] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
834 |
except UnknownEid: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
835 |
tx.error('wrong eid %s in repository, you should ' |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
836 |
'db-check the database' % value) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
837 |
todel.append(i) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
838 |
break |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
839 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
840 |
description.append(tuple(row_descr)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
841 |
for i in reversed(todel): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
842 |
del result[i] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
843 |
return description |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
844 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
845 |
def _make_description(selected, args, solution): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
846 |
"""return a description for a result set""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
847 |
description = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
848 |
for term in selected: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
849 |
description.append(term.get_type(solution, args)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
850 |
return description |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
851 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
852 |
def _selection_idx_type(i, rqlst, args): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
853 |
"""try to return type of term at index `i` of the rqlst's selection""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
854 |
for select in rqlst.children: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
855 |
term = select.selection[i] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
856 |
for solution in select.solutions: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
857 |
try: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
858 |
ttype = term.get_type(solution, args) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
859 |
if ttype is not None: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
860 |
return ttype |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
861 |
except CoercionError: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
862 |
return None |