author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Thu, 14 Feb 2013 15:38:25 +0100 | |
changeset 8695 | 358d8bed9626 |
parent 8694 | d901c36bcfce |
child 8696 | 0bb18407c053 |
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 |
__docformat__ = "restructuredtext en" |
22 |
||
23 |
from itertools import repeat |
|
24 |
||
25 |
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
|
26 |
from rql import RQLSyntaxError, CoercionError |
0 | 27 |
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
|
28 |
from rql.nodes import ETYPE_PYOBJ_MAP, etype_from_pyobj |
5890 | 29 |
from rql.nodes import (Relation, VariableRef, Constant, SubQuery, Function, |
30 |
Exists, Not) |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
31 |
from yams import BASE_TYPES |
0 | 32 |
|
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
|
33 |
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
|
34 |
from cubicweb import Binary, server, typed_eid |
0 | 35 |
from cubicweb.rset import ResultSet |
36 |
||
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.utils import QueryCache, RepeatList |
0 | 38 |
from cubicweb.server.utils import cleanup_solutions |
39 |
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
|
40 |
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
|
41 |
from cubicweb.server.edition import EditedEntity |
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
42 |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
43 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
44 |
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
|
45 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
46 |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
47 |
def empty_rset(rql, args, rqlst=None): |
0 | 48 |
"""build an empty result set object""" |
49 |
return ResultSet([], rql, args, rqlst=rqlst) |
|
50 |
||
51 |
def update_varmap(varmap, selected, table): |
|
52 |
"""return a sql schema to store RQL query result""" |
|
53 |
for i, term in enumerate(selected): |
|
54 |
key = term.as_string() |
|
55 |
value = '%s.C%s' % (table, i) |
|
56 |
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
|
57 |
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
|
58 |
% (key, value, varmap)) |
0 | 59 |
varmap[key] = value |
60 |
||
61 |
# permission utilities ######################################################## |
|
62 |
||
63 |
def check_no_password_selected(rqlst): |
|
64 |
"""check that Password entities are not selected""" |
|
65 |
for solution in rqlst.solutions: |
|
66 |
if 'Password' in solution.itervalues(): |
|
67 |
raise Unauthorized('Password selection is not allowed') |
|
68 |
||
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
|
69 |
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
|
70 |
"""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
|
71 |
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
|
72 |
""" |
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 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
|
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 |
def check_read_access(session, rqlst, solution, args): |
0 | 79 |
"""check that the given user has credentials to access data read the |
80 |
query |
|
81 |
||
82 |
return a dict defining necessary local checks (due to use of rql expression |
|
83 |
in the schema), keys are variable names and values associated rql expression |
|
84 |
for the associated variable with the given solution |
|
85 |
""" |
|
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
|
86 |
# 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
|
87 |
# 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
|
88 |
# 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
|
89 |
schema = session.repo.schema |
0 | 90 |
if rqlst.where is not None: |
91 |
for rel in rqlst.where.iget_nodes(Relation): |
|
92 |
# 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
|
93 |
if rel.r_type in READ_ONLY_RTYPES: |
0 | 94 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
95 |
rschema = schema.rschema(rel.r_type) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
96 |
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
|
97 |
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
|
98 |
solution, args)) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
99 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
if not session.user.matching_groups(rdef.get_groups('read')): |
4955 | 106 |
# XXX rqlexpr not allowed |
0 | 107 |
raise Unauthorized('read', rel.r_type) |
108 |
localchecks = {} |
|
109 |
# iterate on defined_vars and not on solutions to ignore column aliases |
|
110 |
for varname in rqlst.defined_vars: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
111 |
eschema = schema.eschema(solution[varname]) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
112 |
if eschema.final: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
113 |
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
|
114 |
if not session.user.matching_groups(eschema.get_groups('read')): |
0 | 115 |
erqlexprs = eschema.get_rqlexprs('read') |
116 |
if not erqlexprs: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
117 |
ex = Unauthorized('read', solution[varname]) |
0 | 118 |
ex.var = varname |
119 |
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
|
120 |
# 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
|
121 |
# '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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
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
|
128 |
!= 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
|
129 |
localchecks[varname] = erqlexprs |
0 | 130 |
return localchecks |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
131 |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
132 |
def add_noinvariant(noinvariant, restricted, select, nbtrees): |
0 | 133 |
# a variable can actually be invariant if it has not been restricted for |
134 |
# security reason or if security assertion hasn't modified the possible |
|
135 |
# solutions for the query |
|
136 |
if nbtrees != 1: |
|
137 |
for vname in restricted: |
|
138 |
try: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
139 |
noinvariant.add(select.defined_vars[vname]) |
0 | 140 |
except KeyError: |
141 |
# this is an alias |
|
142 |
continue |
|
143 |
else: |
|
144 |
for vname in restricted: |
|
145 |
try: |
|
146 |
var = select.defined_vars[vname] |
|
147 |
except KeyError: |
|
148 |
# this is an alias |
|
149 |
continue |
|
150 |
if len(var.stinfo['possibletypes']) != 1: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
151 |
noinvariant.add(var) |
0 | 152 |
|
153 |
def _expand_selection(terms, selected, aliases, select, newselect): |
|
154 |
for term in terms: |
|
155 |
for vref in term.iget_nodes(VariableRef): |
|
156 |
if not vref.name in selected: |
|
157 |
select.append_selected(vref) |
|
158 |
colalias = newselect.get_variable(vref.name, len(aliases)) |
|
159 |
aliases.append(VariableRef(colalias)) |
|
160 |
selected.add(vref.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
161 |
|
0 | 162 |
# Plans ####################################################################### |
163 |
||
164 |
class ExecutionPlan(object): |
|
165 |
"""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
|
166 |
|
0 | 167 |
def __init__(self, querier, rqlst, args, session): |
168 |
# original rql syntax tree |
|
169 |
self.rqlst = rqlst |
|
170 |
self.args = args or {} |
|
171 |
# session executing the query |
|
172 |
self.session = session |
|
173 |
# 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
|
174 |
self.syssource = session.cnxset.source('system') |
0 | 175 |
# execution steps |
176 |
self.steps = [] |
|
177 |
# index of temporary tables created during execution |
|
178 |
self.temp_tables = {} |
|
179 |
# various resource accesors |
|
180 |
self.querier = querier |
|
181 |
self.schema = querier.schema |
|
182 |
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
|
183 |
self.rqlhelper = session.vreg.rqlhelper |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
184 |
|
0 | 185 |
def annotate_rqlst(self): |
186 |
if not self.rqlst.annotated: |
|
187 |
self.rqlhelper.annotate(self.rqlst) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
188 |
|
0 | 189 |
def add_step(self, step): |
190 |
"""add a step to the plan""" |
|
191 |
self.steps.append(step) |
|
192 |
||
193 |
def clean(self): |
|
194 |
"""remove temporary tables""" |
|
195 |
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
|
196 |
|
0 | 197 |
def sqlexec(self, sql, args=None): |
198 |
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
|
199 |
|
0 | 200 |
def execute(self): |
201 |
"""execute a plan and return resulting rows""" |
|
202 |
try: |
|
203 |
for step in self.steps: |
|
204 |
result = step.execute() |
|
205 |
# the latest executed step contains the full query result |
|
206 |
return result |
|
207 |
finally: |
|
208 |
self.clean() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
209 |
|
5168
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
210 |
def make_temp_table_name(self, table): |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
211 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
212 |
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
|
213 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
214 |
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
|
215 |
|
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
216 |
|
0 | 217 |
def init_temp_table(self, table, selected, sol): |
218 |
"""initialize sql schema and variable map for a temporary table which |
|
219 |
will be used to store result for the given rqlst |
|
220 |
""" |
|
221 |
try: |
|
222 |
outputmap, sqlschema, _ = self.temp_tables[table] |
|
223 |
update_varmap(outputmap, selected, table) |
|
224 |
except KeyError: |
|
225 |
sqlschema, outputmap = self.syssource.temp_table_def(selected, sol, |
|
226 |
table) |
|
227 |
self.temp_tables[table] = [outputmap, sqlschema, False] |
|
228 |
return outputmap |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
229 |
|
0 | 230 |
def create_temp_table(self, table): |
231 |
"""create a temporary table to store result for the given rqlst""" |
|
232 |
if not self.temp_tables[table][-1]: |
|
233 |
sqlschema = self.temp_tables[table][1] |
|
234 |
self.syssource.create_temp_table(self.session, table, sqlschema) |
|
235 |
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
|
236 |
|
0 | 237 |
def preprocess(self, union, security=True): |
238 |
"""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
|
239 |
|
0 | 240 |
return rqlst to actually execute |
241 |
""" |
|
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
|
242 |
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
|
243 |
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
|
244 |
# 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
|
245 |
# 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
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
noinvariant = set() |
8562
0d2fb4604265
[session] fix arguments default value and promote usage of security_enabled as session method. Closes #2481820
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8542
diff
changeset
|
261 |
with self.session.security_enabled(read=False): |
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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
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 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
|
268 |
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
|
269 |
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
|
270 |
set_qdata(self.schema.rschema, union, noinvariant) |
0 | 271 |
if union.has_text_query: |
272 |
self.cache_key = None |
|
273 |
||
274 |
def _insert_security(self, union, noinvariant): |
|
275 |
for select in union.children[:]: |
|
276 |
for subquery in select.with_: |
|
277 |
self._insert_security(subquery.query, noinvariant) |
|
278 |
localchecks, restricted = self._check_permissions(select) |
|
279 |
if any(localchecks): |
|
280 |
rewrite = self.session.rql_rewriter.rewrite |
|
281 |
nbtrees = len(localchecks) |
|
282 |
myunion = union |
|
283 |
# transform in subquery when len(localchecks)>1 and groups |
|
284 |
if nbtrees > 1 and (select.orderby or select.groupby or |
|
285 |
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
|
286 |
select.distinct or |
0 | 287 |
select.limit or select.offset): |
288 |
newselect = Select() |
|
289 |
# only select variables in subqueries |
|
290 |
origselection = select.selection |
|
291 |
select.select_only_variables() |
|
292 |
select.has_aggregat = False |
|
293 |
# create subquery first so correct node are used on copy |
|
294 |
# (eg ColumnAlias instead of Variable) |
|
295 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
296 |
for i, vref in enumerate(select.selection)] |
|
297 |
selected = set(vref.name for vref in aliases) |
|
298 |
# now copy original selection and groups |
|
299 |
for term in origselection: |
|
300 |
newselect.append_selected(term.copy(newselect)) |
|
301 |
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
|
302 |
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
|
303 |
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
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
# 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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
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
|
312 |
# 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
|
313 |
# 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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
# 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
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
newselect.set_orderby(sortterms) |
0 | 322 |
_expand_selection(select.orderby, selected, aliases, select, newselect) |
323 |
select.orderby = () # XXX dereference? |
|
324 |
if select.groupby: |
|
325 |
newselect.set_groupby([g.copy(newselect) for g in select.groupby]) |
|
326 |
_expand_selection(select.groupby, selected, aliases, select, newselect) |
|
327 |
select.groupby = () # XXX dereference? |
|
328 |
if select.having: |
|
329 |
newselect.set_having([g.copy(newselect) for g in select.having]) |
|
330 |
_expand_selection(select.having, selected, aliases, select, newselect) |
|
331 |
select.having = () # XXX dereference? |
|
332 |
if select.limit: |
|
333 |
newselect.limit = select.limit |
|
334 |
select.limit = None |
|
335 |
if select.offset: |
|
336 |
newselect.offset = select.offset |
|
337 |
select.offset = 0 |
|
338 |
myunion = Union() |
|
339 |
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
|
340 |
newselect.distinct = select.distinct |
0 | 341 |
solutions = [sol.copy() for sol in select.solutions] |
342 |
cleanup_solutions(newselect, solutions) |
|
343 |
newselect.set_possible_types(solutions) |
|
344 |
# if some solutions doesn't need rewriting, insert original |
|
345 |
# select as first union subquery |
|
346 |
if () in localchecks: |
|
347 |
myunion.append(select) |
|
348 |
# we're done, replace original select by the new select with |
|
349 |
# subqueries (more added in the loop below) |
|
350 |
union.replace(select, newselect) |
|
351 |
elif not () in localchecks: |
|
352 |
union.remove(select) |
|
353 |
for lcheckdef, lchecksolutions in localchecks.iteritems(): |
|
354 |
if not lcheckdef: |
|
355 |
continue |
|
356 |
myrqlst = select.copy(solutions=lchecksolutions) |
|
357 |
myunion.append(myrqlst) |
|
358 |
# 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
|
359 |
lcheckdef = [({var: 'X'}, rqlexprs) for var, rqlexprs in lcheckdef] |
0 | 360 |
rewrite(myrqlst, lcheckdef, lchecksolutions, self.args) |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
361 |
add_noinvariant(noinvariant, restricted, myrqlst, nbtrees) |
0 | 362 |
if () in localchecks: |
363 |
select.set_possible_types(localchecks[()]) |
|
364 |
add_types_restriction(self.schema, select) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
365 |
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
|
366 |
self.rqlhelper.annotate(union) |
0 | 367 |
|
368 |
def _check_permissions(self, rqlst): |
|
369 |
"""return a dict defining "local checks", e.g. RQLExpression defined in |
|
370 |
the schema that should be inserted in the original query |
|
371 |
||
372 |
solutions where a variable has a type which the user can't definitly read |
|
373 |
are removed, else if the user may read it (eg if an rql expression is |
|
374 |
defined for the "read" permission of the related type), the local checks |
|
375 |
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
|
376 |
|
0 | 377 |
return a dict with entries for each different local check necessary, |
378 |
with associated solutions as value. A local check is defined by a list |
|
379 |
of 2-uple, with variable name as first item and the necessary rql |
|
380 |
expression as second item for each variable which has to be checked. |
|
381 |
So solutions which don't require local checks will be associated to |
|
382 |
the empty tuple key. |
|
383 |
||
384 |
note: rqlst should not have been simplified at this point |
|
385 |
""" |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
386 |
session = self.session |
0 | 387 |
msgs = [] |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
388 |
neweids = session.transaction_data.get('neweids', ()) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
389 |
varkwargs = {} |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
390 |
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
|
391 |
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
|
392 |
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
|
393 |
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
|
394 |
varkwargs[var.name] = typed_eid(eid) |
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8127
diff
changeset
|
395 |
# dictionary of variables restricted for security reason |
0 | 396 |
localchecks = {} |
397 |
restricted_vars = set() |
|
398 |
newsolutions = [] |
|
399 |
for solution in rqlst.solutions: |
|
400 |
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
|
401 |
localcheck = check_read_access(session, rqlst, solution, self.args) |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8694
diff
changeset
|
402 |
except Unauthorized as ex: |
0 | 403 |
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
|
404 |
msg %= (solution, session.user.login, ex.args[0], ex.args[1]) |
0 | 405 |
msgs.append(msg) |
406 |
LOGGER.info(msg) |
|
407 |
else: |
|
408 |
newsolutions.append(solution) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
409 |
# 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
|
410 |
# are specified by eid in query'args |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
411 |
for varname, eid in varkwargs.iteritems(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
412 |
try: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
413 |
rqlexprs = localcheck.pop(varname) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
414 |
except KeyError: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
415 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
416 |
if eid in neweids: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
417 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
418 |
for rqlexpr in rqlexprs: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
419 |
if rqlexpr.check(session, eid): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
420 |
break |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
421 |
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
|
422 |
raise Unauthorized('No read acces on %r with eid %i.' % (var, eid)) |
0 | 423 |
restricted_vars.update(localcheck) |
424 |
localchecks.setdefault(tuple(localcheck.iteritems()), []).append(solution) |
|
425 |
# raise Unautorized exception if the user can't access to any solution |
|
426 |
if not newsolutions: |
|
427 |
raise Unauthorized('\n'.join(msgs)) |
|
6128 | 428 |
if msgs: |
429 |
# (else solutions have not been modified) |
|
430 |
rqlst.set_possible_types(newsolutions) |
|
0 | 431 |
return localchecks, restricted_vars |
432 |
||
433 |
def finalize(self, select, solutions, insertedvars): |
|
434 |
rqlst = Union() |
|
435 |
rqlst.append(select) |
|
436 |
for mainvarname, rschema, newvarname in insertedvars: |
|
437 |
nvartype = str(rschema.objects(solutions[0][mainvarname])[0]) |
|
438 |
for sol in solutions: |
|
439 |
sol[newvarname] = nvartype |
|
440 |
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
|
441 |
add_types_restriction(self.schema, select) |
0 | 442 |
self.rqlhelper.annotate(rqlst) |
443 |
self.preprocess(rqlst, security=False) |
|
444 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
445 |
|
4759 | 446 |
|
0 | 447 |
class InsertPlan(ExecutionPlan): |
448 |
"""an execution model specific to the INSERT rql query |
|
449 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
450 |
|
0 | 451 |
def __init__(self, querier, rqlst, args, session): |
452 |
ExecutionPlan.__init__(self, querier, rqlst, args, session) |
|
453 |
# save originaly selected variable, we may modify this |
|
454 |
# dictionary for substitution (query parameters) |
|
455 |
self.selected = rqlst.selection |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
456 |
# list of rows of entities definition (ssplanner.EditedEntity) |
0 | 457 |
self.e_defs = [[]] |
458 |
# 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
|
459 |
self.r_defs = set() |
0 | 460 |
# indexes to track entity definitions bound to relation definitions |
461 |
self._r_subj_index = {} |
|
462 |
self._r_obj_index = {} |
|
463 |
self._expanded_r_defs = {} |
|
464 |
||
465 |
def add_entity_def(self, edef): |
|
466 |
"""add an entity definition to build""" |
|
467 |
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
|
468 |
|
0 | 469 |
def add_relation_def(self, rdef): |
470 |
"""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
|
471 |
self.r_defs.add(rdef) |
0 | 472 |
if not isinstance(rdef[0], int): |
473 |
self._r_subj_index.setdefault(rdef[0], []).append(rdef) |
|
474 |
if not isinstance(rdef[2], int): |
|
475 |
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
|
476 |
|
0 | 477 |
def substitute_entity_def(self, edef, edefs): |
478 |
"""substitute an incomplete entity definition by a list of complete |
|
479 |
equivalents |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
480 |
|
0 | 481 |
e.g. on queries such as :: |
482 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
483 |
WHERE U login 'admin', U login N |
|
484 |
||
485 |
X will be inserted as many times as U exists, and so the X travaille Y |
|
486 |
relations as to be added as many time as X is inserted |
|
487 |
""" |
|
488 |
if not edefs or not self.e_defs: |
|
489 |
# no result, no entity will be created |
|
490 |
self.e_defs = () |
|
491 |
return |
|
492 |
# first remove the incomplete entity definition |
|
493 |
colidx = self.e_defs[0].index(edef) |
|
494 |
for i, row in enumerate(self.e_defs[:]): |
|
495 |
self.e_defs[i][colidx] = edefs[0] |
|
496 |
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
|
497 |
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
|
498 |
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
|
499 |
if i != colidx] |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
500 |
row.insert(colidx, edef_) |
0 | 501 |
self.e_defs.append(row) |
502 |
# now, see if this entity def is referenced as subject in some relation |
|
503 |
# definition |
|
504 |
if self._r_subj_index.has_key(edef): |
|
505 |
for rdef in self._r_subj_index[edef]: |
|
506 |
expanded = self._expanded(rdef) |
|
507 |
result = [] |
|
508 |
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
|
509 |
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
|
510 |
result.append( (edef_, exp_rdef[1], exp_rdef[2]) ) |
0 | 511 |
self._expanded_r_defs[rdef] = result |
512 |
# and finally, see if this entity def is referenced as object in some |
|
513 |
# relation definition |
|
514 |
if self._r_obj_index.has_key(edef): |
|
515 |
for rdef in self._r_obj_index[edef]: |
|
516 |
expanded = self._expanded(rdef) |
|
517 |
result = [] |
|
518 |
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
|
519 |
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
|
520 |
result.append( (exp_rdef[0], exp_rdef[1], edef_) ) |
0 | 521 |
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
|
522 |
|
0 | 523 |
def _expanded(self, rdef): |
524 |
"""return expanded value for the given relation definition""" |
|
525 |
try: |
|
526 |
return self._expanded_r_defs[rdef] |
|
527 |
except KeyError: |
|
528 |
self.r_defs.remove(rdef) |
|
529 |
return [rdef] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
530 |
|
0 | 531 |
def relation_defs(self): |
532 |
"""return the list for relation definitions to insert""" |
|
533 |
for rdefs in self._expanded_r_defs.values(): |
|
534 |
for rdef in rdefs: |
|
535 |
yield rdef |
|
536 |
for rdef in self.r_defs: |
|
537 |
yield rdef |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
538 |
|
0 | 539 |
def insert_entity_defs(self): |
540 |
"""return eids of inserted entities in a suitable form for the resulting |
|
541 |
result set, e.g.: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
542 |
|
0 | 543 |
e.g. on queries such as :: |
544 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
545 |
WHERE U login 'admin', U login N |
|
546 |
||
547 |
if there is two entities matching U, the result set will look like |
|
548 |
[(eidX1, eidY1), (eidX2, eidY2)] |
|
549 |
""" |
|
550 |
session = self.session |
|
551 |
repo = session.repo |
|
552 |
results = [] |
|
553 |
for row in self.e_defs: |
|
554 |
results.append([repo.glob_add_entity(session, edef) |
|
555 |
for edef in row]) |
|
556 |
return results |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
557 |
|
0 | 558 |
def insert_relation_defs(self): |
559 |
session = self.session |
|
560 |
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
|
561 |
edited_entities = {} |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
562 |
relations = {} |
0 | 563 |
for subj, rtype, obj in self.relation_defs(): |
564 |
# if a string is given into args instead of an int, we get it here |
|
565 |
if isinstance(subj, basestring): |
|
566 |
subj = typed_eid(subj) |
|
567 |
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
|
568 |
subj = subj.entity.eid |
0 | 569 |
if isinstance(obj, basestring): |
570 |
obj = typed_eid(obj) |
|
571 |
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
|
572 |
obj = obj.entity.eid |
0 | 573 |
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
|
574 |
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
|
575 |
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
|
576 |
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
|
577 |
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
|
578 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
579 |
edited = edited_entities[subj] |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
580 |
edited.edited_attribute(rtype, obj) |
0 | 581 |
else: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
582 |
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
|
583 |
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
|
584 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
585 |
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
|
586 |
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
|
587 |
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
|
588 |
repo.glob_update_entity(session, edited) |
0 | 589 |
|
590 |
||
591 |
class QuerierHelper(object): |
|
592 |
"""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
|
593 |
|
0 | 594 |
def __init__(self, repo, schema): |
595 |
# system info helper |
|
596 |
self._repo = repo |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
597 |
# instance schema |
0 | 598 |
self.set_schema(schema) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
599 |
|
0 | 600 |
def set_schema(self, schema): |
601 |
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
|
602 |
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
|
603 |
# 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
|
604 |
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
|
605 |
# 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
|
606 |
# 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
|
607 |
# 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
|
608 |
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
|
609 |
# 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
|
610 |
self.cache_hit, self.cache_miss = 0, 0 |
0 | 611 |
# 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
|
612 |
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
|
613 |
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
|
614 |
# 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
|
615 |
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
|
616 |
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
|
617 |
self._annotate = rqlhelper.annotate |
0 | 618 |
# rql planner |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
619 |
if len(repo.sources) < 2: |
0 | 620 |
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
|
621 |
self._planner = SSPlanner(schema, rqlhelper) |
0 | 622 |
else: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
623 |
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
|
624 |
self._planner = MSPlanner(schema, rqlhelper) |
0 | 625 |
# sql generation annotator |
626 |
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
|
627 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
628 |
def set_planner(self): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
629 |
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
|
630 |
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
|
631 |
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
|
632 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
633 |
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
|
634 |
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
|
635 |
|
0 | 636 |
def parse(self, rql, annotate=False): |
637 |
"""return a rql syntax tree for the given rql""" |
|
638 |
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
|
639 |
return self._parse(unicode(rql), annotate=annotate) |
0 | 640 |
except UnicodeError: |
641 |
raise RQLSyntaxError(rql) |
|
642 |
||
643 |
def plan_factory(self, rqlst, args, session): |
|
644 |
"""create an execution plan for an INSERT RQL query""" |
|
645 |
if rqlst.TYPE == 'insert': |
|
646 |
return InsertPlan(self, rqlst, args, session) |
|
647 |
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
|
648 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
649 |
def execute(self, session, rql, args=None, build_descr=True): |
0 | 650 |
"""execute a rql query, return resulting rows and their description in |
651 |
a `ResultSet` object |
|
652 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
653 |
* `rql` should be an Unicode string or a plain ASCII string |
0 | 654 |
* `args` the optional parameters dictionary associated to the query |
655 |
* `build_descr` is a boolean flag indicating if the description should |
|
656 |
be built on select queries (if false, the description will be en empty |
|
657 |
list) |
|
658 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
659 |
on INSERT queries, there will be one row with the eid of each inserted |
0 | 660 |
entity |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
661 |
|
0 | 662 |
result for DELETE and SET queries is undefined yet |
663 |
||
664 |
to maximize the rql parsing/analyzing cache performance, you should |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
665 |
always use substitute arguments in queries (i.e. avoid query such as |
0 | 666 |
'Any X WHERE X eid 123'!) |
667 |
""" |
|
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
668 |
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
|
669 |
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
|
670 |
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
|
671 |
print 'querier input', repr(rql), repr(args) |
0 | 672 |
# 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
|
673 |
cachekey = (rql,) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
674 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
675 |
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
|
676 |
# 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
|
677 |
# 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
|
678 |
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
|
679 |
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
|
680 |
# 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
|
681 |
# 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
|
682 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
683 |
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
|
684 |
args, eidkeys) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
685 |
except UnknownEid: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
686 |
# 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
|
687 |
# 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
|
688 |
return empty_rset(rql, args) |
0 | 689 |
rqlst = self._rql_cache[cachekey] |
690 |
self.cache_hit += 1 |
|
691 |
except KeyError: |
|
692 |
self.cache_miss += 1 |
|
693 |
rqlst = self.parse(rql) |
|
694 |
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
|
695 |
# 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
|
696 |
# 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
|
697 |
# 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
|
698 |
# tree) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
699 |
eidkeys = self.solutions(session, rqlst, args) |
0 | 700 |
except UnknownEid: |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
701 |
# 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
|
702 |
# empty result instead of raising UnknownEid |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
703 |
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
|
704 |
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
|
705 |
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
|
706 |
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
|
707 |
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
|
708 |
eidkeys) |
0 | 709 |
self._rql_cache[cachekey] = rqlst |
710 |
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
|
711 |
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
|
712 |
if session.read_security: |
0 | 713 |
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
|
714 |
# 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
|
715 |
# be released until commit/rollback |
0 | 716 |
session.mode = 'write' |
717 |
cachekey = None |
|
718 |
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
|
719 |
if session.read_security: |
0 | 720 |
for select in rqlst.children: |
721 |
check_no_password_selected(select) |
|
722 |
# on select query, always copy the cached rqlst so we don't have to |
|
723 |
# bother modifying it. This is not necessary on write queries since |
|
724 |
# a new syntax tree is built from them. |
|
725 |
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
|
726 |
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
|
727 |
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
|
728 |
# 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
|
729 |
# 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
|
730 |
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
|
731 |
if v is None])) |
0 | 732 |
# make an execution plan |
733 |
plan = self.plan_factory(rqlst, args, session) |
|
734 |
plan.cache_key = cachekey |
|
735 |
self._planner.build_plan(plan) |
|
736 |
# execute the plan |
|
737 |
try: |
|
738 |
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
|
739 |
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
|
740 |
# 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
|
741 |
# transaction must been rollbacked |
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
742 |
# |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
743 |
# 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
|
744 |
# * 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
|
745 |
# 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
|
746 |
# * 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
|
747 |
# by the session |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
748 |
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
|
749 |
session.commit_state = 'uncommitable' |
0 | 750 |
raise |
751 |
# build a description for the results if necessary |
|
752 |
descr = () |
|
753 |
if build_descr: |
|
754 |
if rqlst.TYPE == 'select': |
|
755 |
# sample selection |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
756 |
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
|
757 |
# 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
|
758 |
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
|
759 |
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
|
760 |
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
|
761 |
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
|
762 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
763 |
# 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
|
764 |
descr = manual_build_descr(session, rqlst, args, results) |
0 | 765 |
elif rqlst.TYPE == 'insert': |
766 |
# on insert plan, some entities may have been auto-casted, |
|
767 |
# so compute description manually even if there is only |
|
768 |
# one solution |
|
769 |
basedescr = [None] * len(plan.selected) |
|
770 |
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
|
771 |
descr = _build_descr(session, results, basedescr, todetermine) |
0 | 772 |
# FIXME: get number of affected entities / relations on non |
773 |
# selection queries ? |
|
774 |
# 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
|
775 |
return ResultSet(results, rql, args, descr, orig_rqlst) |
0 | 776 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
777 |
# 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
|
778 |
# 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
|
779 |
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
|
780 |
|
0 | 781 |
from logging import getLogger |
782 |
from cubicweb import set_log_methods |
|
783 |
LOGGER = getLogger('cubicweb.querier') |
|
784 |
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
|
785 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
786 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
787 |
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
|
788 |
"""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
|
789 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
790 |
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
|
791 |
""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
792 |
# 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
|
793 |
# to another |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
794 |
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
|
795 |
basedescr = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
796 |
todetermine = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
797 |
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
|
798 |
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
|
799 |
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
|
800 |
ttype = None |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
801 |
isfinal = True |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
802 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
803 |
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
|
804 |
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
|
805 |
basedescr.append(None) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
806 |
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
|
807 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
808 |
basedescr.append(ttype) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
809 |
if not todetermine: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
810 |
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
|
811 |
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
|
812 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
813 |
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
|
814 |
description = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
815 |
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
|
816 |
todel = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
817 |
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
|
818 |
row_descr = basedescription[:] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
819 |
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
|
820 |
value = row[index] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
821 |
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
|
822 |
# 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
|
823 |
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
|
824 |
continue |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
825 |
if isfinal: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
826 |
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
|
827 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
828 |
try: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
829 |
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
|
830 |
except UnknownEid: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
831 |
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
|
832 |
'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
|
833 |
todel.append(i) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
834 |
break |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
835 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
836 |
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
|
837 |
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
|
838 |
del result[i] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
839 |
return description |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
840 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
841 |
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
|
842 |
"""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
|
843 |
description = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
844 |
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
|
845 |
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
|
846 |
return description |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
847 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
848 |
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
|
849 |
"""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
|
850 |
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
|
851 |
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
|
852 |
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
|
853 |
try: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
854 |
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
|
855 |
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
|
856 |
return ttype |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
857 |
except CoercionError: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
858 |
return None |