author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 27 Jul 2010 12:36:03 +0200 | |
branch | oldstable |
changeset 6018 | f4d1d5d9ccbb |
parent 5888 | 3ee80d487f11 |
child 5890 | 141b935a38fc |
permissions | -rw-r--r-- |
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
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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. |
|
5886 | 20 |
""" |
0 | 21 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
22 |
from __future__ import with_statement |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4764
diff
changeset
|
23 |
|
0 | 24 |
__docformat__ = "restructuredtext en" |
25 |
||
26 |
from itertools import repeat |
|
27 |
||
28 |
from logilab.common.cache import Cache |
|
29 |
from logilab.common.compat import any |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4654
diff
changeset
|
30 |
from rql import RQLSyntaxError |
0 | 31 |
from rql.stmts import Union, Select |
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:
5886
diff
changeset
|
32 |
from rql.nodes import Relation, VariableRef, Constant, SubQuery, Exists, Not |
0 | 33 |
|
34 |
from cubicweb import Unauthorized, QueryError, UnknownEid, typed_eid |
|
35 |
from cubicweb import server |
|
36 |
from cubicweb.rset import ResultSet |
|
37 |
||
38 |
from cubicweb.server.utils import cleanup_solutions |
|
39 |
from cubicweb.server.rqlannotation import SQLGenAnnotator, set_qdata |
|
4764
ec9c20c6b9f7
[repo] improve planning of insert/update queries: do not select affected constants so the don't go and back to/from the source.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4759
diff
changeset
|
40 |
from cubicweb.server.ssplanner import READ_ONLY_RTYPES, add_types_restriction |
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
|
41 |
from cubicweb.server.session import security_enabled |
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
42 |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
43 |
def empty_rset(rql, args, rqlst=None): |
0 | 44 |
"""build an empty result set object""" |
45 |
return ResultSet([], rql, args, rqlst=rqlst) |
|
46 |
||
47 |
def update_varmap(varmap, selected, table): |
|
48 |
"""return a sql schema to store RQL query result""" |
|
49 |
for i, term in enumerate(selected): |
|
50 |
key = term.as_string() |
|
51 |
value = '%s.C%s' % (table, i) |
|
52 |
if varmap.get(key, value) != value: |
|
53 |
raise Exception('variable name conflict on %s' % key) |
|
54 |
varmap[key] = value |
|
55 |
||
56 |
# permission utilities ######################################################## |
|
57 |
||
58 |
def check_no_password_selected(rqlst): |
|
59 |
"""check that Password entities are not selected""" |
|
60 |
for solution in rqlst.solutions: |
|
61 |
if 'Password' in solution.itervalues(): |
|
62 |
raise Unauthorized('Password selection is not allowed') |
|
63 |
||
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
|
64 |
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
|
65 |
"""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
|
66 |
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
|
67 |
""" |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
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 |
def check_read_access(session, rqlst, solution, args): |
0 | 74 |
"""check that the given user has credentials to access data read the |
75 |
query |
|
76 |
||
77 |
return a dict defining necessary local checks (due to use of rql expression |
|
78 |
in the schema), keys are variable names and values associated rql expression |
|
79 |
for the associated variable with the given solution |
|
80 |
""" |
|
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
|
81 |
# 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
|
82 |
# 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
|
83 |
# 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
|
84 |
schema = session.repo.schema |
0 | 85 |
if rqlst.where is not None: |
86 |
for rel in rqlst.where.iget_nodes(Relation): |
|
87 |
# 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
|
88 |
if rel.r_type in READ_ONLY_RTYPES: |
0 | 89 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
90 |
rschema = schema.rschema(rel.r_type) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
solution, args)) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
94 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
if not session.user.matching_groups(rdef.get_groups('read')): |
4955 | 101 |
# XXX rqlexpr not allowed |
0 | 102 |
raise Unauthorized('read', rel.r_type) |
103 |
localchecks = {} |
|
104 |
# iterate on defined_vars and not on solutions to ignore column aliases |
|
105 |
for varname in rqlst.defined_vars: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
106 |
eschema = schema.eschema(solution[varname]) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
107 |
if eschema.final: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
108 |
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
|
109 |
if not session.user.matching_groups(eschema.get_groups('read')): |
0 | 110 |
erqlexprs = eschema.get_rqlexprs('read') |
111 |
if not erqlexprs: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
112 |
ex = Unauthorized('read', solution[varname]) |
0 | 113 |
ex.var = varname |
114 |
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:
5886
diff
changeset
|
115 |
# 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:
5886
diff
changeset
|
116 |
# '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:
5886
diff
changeset
|
117 |
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:
5886
diff
changeset
|
118 |
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:
5886
diff
changeset
|
119 |
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:
5886
diff
changeset
|
120 |
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:
5886
diff
changeset
|
121 |
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:
5886
diff
changeset
|
122 |
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:
5886
diff
changeset
|
123 |
!= 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:
5886
diff
changeset
|
124 |
localchecks[varname] = erqlexprs |
0 | 125 |
return localchecks |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
126 |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
127 |
def add_noinvariant(noinvariant, restricted, select, nbtrees): |
0 | 128 |
# a variable can actually be invariant if it has not been restricted for |
129 |
# security reason or if security assertion hasn't modified the possible |
|
130 |
# solutions for the query |
|
131 |
if nbtrees != 1: |
|
132 |
for vname in restricted: |
|
133 |
try: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
134 |
noinvariant.add(select.defined_vars[vname]) |
0 | 135 |
except KeyError: |
136 |
# this is an alias |
|
137 |
continue |
|
138 |
else: |
|
139 |
for vname in restricted: |
|
140 |
try: |
|
141 |
var = select.defined_vars[vname] |
|
142 |
except KeyError: |
|
143 |
# this is an alias |
|
144 |
continue |
|
145 |
if len(var.stinfo['possibletypes']) != 1: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
146 |
noinvariant.add(var) |
0 | 147 |
|
148 |
def _expand_selection(terms, selected, aliases, select, newselect): |
|
149 |
for term in terms: |
|
150 |
for vref in term.iget_nodes(VariableRef): |
|
151 |
if not vref.name in selected: |
|
152 |
select.append_selected(vref) |
|
153 |
colalias = newselect.get_variable(vref.name, len(aliases)) |
|
154 |
aliases.append(VariableRef(colalias)) |
|
155 |
selected.add(vref.name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
156 |
|
0 | 157 |
# Plans ####################################################################### |
158 |
||
159 |
class ExecutionPlan(object): |
|
160 |
"""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
|
161 |
|
0 | 162 |
def __init__(self, querier, rqlst, args, session): |
163 |
# original rql syntax tree |
|
164 |
self.rqlst = rqlst |
|
165 |
self.args = args or {} |
|
166 |
# session executing the query |
|
167 |
self.session = session |
|
168 |
# quick reference to the system source |
|
169 |
self.syssource = session.pool.source('system') |
|
170 |
# execution steps |
|
171 |
self.steps = [] |
|
172 |
# index of temporary tables created during execution |
|
173 |
self.temp_tables = {} |
|
174 |
# various resource accesors |
|
175 |
self.querier = querier |
|
176 |
self.schema = querier.schema |
|
177 |
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
|
178 |
self.rqlhelper = session.vreg.rqlhelper |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
179 |
|
0 | 180 |
def annotate_rqlst(self): |
181 |
if not self.rqlst.annotated: |
|
182 |
self.rqlhelper.annotate(self.rqlst) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
183 |
|
0 | 184 |
def add_step(self, step): |
185 |
"""add a step to the plan""" |
|
186 |
self.steps.append(step) |
|
187 |
||
188 |
def clean(self): |
|
189 |
"""remove temporary tables""" |
|
190 |
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
|
191 |
|
0 | 192 |
def sqlexec(self, sql, args=None): |
193 |
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
|
194 |
|
0 | 195 |
def execute(self): |
196 |
"""execute a plan and return resulting rows""" |
|
197 |
try: |
|
198 |
for step in self.steps: |
|
199 |
result = step.execute() |
|
200 |
# the latest executed step contains the full query result |
|
201 |
return result |
|
202 |
finally: |
|
203 |
self.clean() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
204 |
|
5168
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
205 |
def make_temp_table_name(self, table): |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
206 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
207 |
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
|
208 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
209 |
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
|
210 |
|
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
211 |
|
0 | 212 |
def init_temp_table(self, table, selected, sol): |
213 |
"""initialize sql schema and variable map for a temporary table which |
|
214 |
will be used to store result for the given rqlst |
|
215 |
""" |
|
216 |
try: |
|
217 |
outputmap, sqlschema, _ = self.temp_tables[table] |
|
218 |
update_varmap(outputmap, selected, table) |
|
219 |
except KeyError: |
|
220 |
sqlschema, outputmap = self.syssource.temp_table_def(selected, sol, |
|
221 |
table) |
|
222 |
self.temp_tables[table] = [outputmap, sqlschema, False] |
|
223 |
return outputmap |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
224 |
|
0 | 225 |
def create_temp_table(self, table): |
226 |
"""create a temporary table to store result for the given rqlst""" |
|
227 |
if not self.temp_tables[table][-1]: |
|
228 |
sqlschema = self.temp_tables[table][1] |
|
229 |
self.syssource.create_temp_table(self.session, table, sqlschema) |
|
230 |
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
|
231 |
|
0 | 232 |
def preprocess(self, union, security=True): |
233 |
"""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
|
234 |
|
0 | 235 |
return rqlst to actually execute |
236 |
""" |
|
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
|
237 |
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
|
238 |
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
|
239 |
# 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
|
240 |
# 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
|
241 |
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
|
242 |
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
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
noinvariant = set() |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
256 |
with security_enabled(self.session, read=False): |
96f67c5be0e6
[security] experimental rqlst cache for read security: to activate using a 'security-rqlst-cache' flag in transaction data when you'll issue a lot of identic queries with only kwargs varying
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4953
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
set_qdata(self.schema.rschema, union, noinvariant) |
0 | 266 |
if union.has_text_query: |
267 |
self.cache_key = None |
|
268 |
||
269 |
def _insert_security(self, union, noinvariant): |
|
270 |
for select in union.children[:]: |
|
271 |
for subquery in select.with_: |
|
272 |
self._insert_security(subquery.query, noinvariant) |
|
273 |
localchecks, restricted = self._check_permissions(select) |
|
274 |
if any(localchecks): |
|
275 |
rewrite = self.session.rql_rewriter.rewrite |
|
276 |
nbtrees = len(localchecks) |
|
277 |
myunion = union |
|
278 |
# transform in subquery when len(localchecks)>1 and groups |
|
279 |
if nbtrees > 1 and (select.orderby or select.groupby or |
|
280 |
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
|
281 |
select.distinct or |
0 | 282 |
select.limit or select.offset): |
283 |
newselect = Select() |
|
284 |
# only select variables in subqueries |
|
285 |
origselection = select.selection |
|
286 |
select.select_only_variables() |
|
287 |
select.has_aggregat = False |
|
288 |
# create subquery first so correct node are used on copy |
|
289 |
# (eg ColumnAlias instead of Variable) |
|
290 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
291 |
for i, vref in enumerate(select.selection)] |
|
292 |
selected = set(vref.name for vref in aliases) |
|
293 |
# now copy original selection and groups |
|
294 |
for term in origselection: |
|
295 |
newselect.append_selected(term.copy(newselect)) |
|
296 |
if select.orderby: |
|
297 |
newselect.set_orderby([s.copy(newselect) for s in select.orderby]) |
|
298 |
_expand_selection(select.orderby, selected, aliases, select, newselect) |
|
299 |
select.orderby = () # XXX dereference? |
|
300 |
if select.groupby: |
|
301 |
newselect.set_groupby([g.copy(newselect) for g in select.groupby]) |
|
302 |
_expand_selection(select.groupby, selected, aliases, select, newselect) |
|
303 |
select.groupby = () # XXX dereference? |
|
304 |
if select.having: |
|
305 |
newselect.set_having([g.copy(newselect) for g in select.having]) |
|
306 |
_expand_selection(select.having, selected, aliases, select, newselect) |
|
307 |
select.having = () # XXX dereference? |
|
308 |
if select.limit: |
|
309 |
newselect.limit = select.limit |
|
310 |
select.limit = None |
|
311 |
if select.offset: |
|
312 |
newselect.offset = select.offset |
|
313 |
select.offset = 0 |
|
314 |
myunion = Union() |
|
315 |
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
|
316 |
newselect.distinct = select.distinct |
0 | 317 |
solutions = [sol.copy() for sol in select.solutions] |
318 |
cleanup_solutions(newselect, solutions) |
|
319 |
newselect.set_possible_types(solutions) |
|
320 |
# if some solutions doesn't need rewriting, insert original |
|
321 |
# select as first union subquery |
|
322 |
if () in localchecks: |
|
323 |
myunion.append(select) |
|
324 |
# we're done, replace original select by the new select with |
|
325 |
# subqueries (more added in the loop below) |
|
326 |
union.replace(select, newselect) |
|
327 |
elif not () in localchecks: |
|
328 |
union.remove(select) |
|
329 |
for lcheckdef, lchecksolutions in localchecks.iteritems(): |
|
330 |
if not lcheckdef: |
|
331 |
continue |
|
332 |
myrqlst = select.copy(solutions=lchecksolutions) |
|
333 |
myunion.append(myrqlst) |
|
334 |
# in-place rewrite + annotation / simplification |
|
4955 | 335 |
lcheckdef = [((var, 'X'), rqlexprs) for var, rqlexprs in lcheckdef] |
0 | 336 |
rewrite(myrqlst, lcheckdef, lchecksolutions, self.args) |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
337 |
add_noinvariant(noinvariant, restricted, myrqlst, nbtrees) |
0 | 338 |
if () in localchecks: |
339 |
select.set_possible_types(localchecks[()]) |
|
340 |
add_types_restriction(self.schema, select) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
341 |
add_noinvariant(noinvariant, restricted, select, nbtrees) |
0 | 342 |
|
343 |
def _check_permissions(self, rqlst): |
|
344 |
"""return a dict defining "local checks", e.g. RQLExpression defined in |
|
345 |
the schema that should be inserted in the original query |
|
346 |
||
347 |
solutions where a variable has a type which the user can't definitly read |
|
348 |
are removed, else if the user may read it (eg if an rql expression is |
|
349 |
defined for the "read" permission of the related type), the local checks |
|
350 |
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
|
351 |
|
0 | 352 |
return a dict with entries for each different local check necessary, |
353 |
with associated solutions as value. A local check is defined by a list |
|
354 |
of 2-uple, with variable name as first item and the necessary rql |
|
355 |
expression as second item for each variable which has to be checked. |
|
356 |
So solutions which don't require local checks will be associated to |
|
357 |
the empty tuple key. |
|
358 |
||
359 |
note: rqlst should not have been simplified at this point |
|
360 |
""" |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
361 |
session = self.session |
0 | 362 |
msgs = [] |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
363 |
neweids = session.transaction_data.get('neweids', ()) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
364 |
varkwargs = {} |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
varkwargs[var.name] = typed_eid(eid) |
0 | 370 |
# dictionnary of variables restricted for security reason |
371 |
localchecks = {} |
|
372 |
restricted_vars = set() |
|
373 |
newsolutions = [] |
|
374 |
for solution in rqlst.solutions: |
|
375 |
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
|
376 |
localcheck = check_read_access(session, rqlst, solution, self.args) |
0 | 377 |
except Unauthorized, ex: |
378 |
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
|
379 |
msg %= (solution, session.user.login, ex.args[0], ex.args[1]) |
0 | 380 |
msgs.append(msg) |
381 |
LOGGER.info(msg) |
|
382 |
else: |
|
383 |
newsolutions.append(solution) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
384 |
# 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
|
385 |
# are specified by eid in query'args |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
386 |
for varname, eid in varkwargs.iteritems(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
387 |
try: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
388 |
rqlexprs = localcheck.pop(varname) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
389 |
except KeyError: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
390 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
391 |
if eid in neweids: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
392 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
393 |
for rqlexpr in rqlexprs: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
394 |
if rqlexpr.check(session, eid): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
395 |
break |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
396 |
else: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
397 |
raise Unauthorized() |
0 | 398 |
restricted_vars.update(localcheck) |
399 |
localchecks.setdefault(tuple(localcheck.iteritems()), []).append(solution) |
|
400 |
# raise Unautorized exception if the user can't access to any solution |
|
401 |
if not newsolutions: |
|
402 |
raise Unauthorized('\n'.join(msgs)) |
|
403 |
rqlst.set_possible_types(newsolutions) |
|
404 |
return localchecks, restricted_vars |
|
405 |
||
406 |
def finalize(self, select, solutions, insertedvars): |
|
407 |
rqlst = Union() |
|
408 |
rqlst.append(select) |
|
409 |
for mainvarname, rschema, newvarname in insertedvars: |
|
410 |
nvartype = str(rschema.objects(solutions[0][mainvarname])[0]) |
|
411 |
for sol in solutions: |
|
412 |
sol[newvarname] = nvartype |
|
413 |
select.clean_solutions(solutions) |
|
414 |
self.rqlhelper.annotate(rqlst) |
|
415 |
self.preprocess(rqlst, security=False) |
|
416 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
417 |
|
4759 | 418 |
|
0 | 419 |
class InsertPlan(ExecutionPlan): |
420 |
"""an execution model specific to the INSERT rql query |
|
421 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
422 |
|
0 | 423 |
def __init__(self, querier, rqlst, args, session): |
424 |
ExecutionPlan.__init__(self, querier, rqlst, args, session) |
|
425 |
# save originaly selected variable, we may modify this |
|
426 |
# dictionary for substitution (query parameters) |
|
427 |
self.selected = rqlst.selection |
|
428 |
# list of new or updated entities definition (utils.Entity) |
|
429 |
self.e_defs = [[]] |
|
430 |
# 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
|
431 |
self.r_defs = set() |
0 | 432 |
# indexes to track entity definitions bound to relation definitions |
433 |
self._r_subj_index = {} |
|
434 |
self._r_obj_index = {} |
|
435 |
self._expanded_r_defs = {} |
|
436 |
||
437 |
def add_entity_def(self, edef): |
|
438 |
"""add an entity definition to build""" |
|
439 |
edef.querier_pending_relations = {} |
|
440 |
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
|
441 |
|
0 | 442 |
def add_relation_def(self, rdef): |
443 |
"""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
|
444 |
self.r_defs.add(rdef) |
0 | 445 |
if not isinstance(rdef[0], int): |
446 |
self._r_subj_index.setdefault(rdef[0], []).append(rdef) |
|
447 |
if not isinstance(rdef[2], int): |
|
448 |
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
|
449 |
|
0 | 450 |
def substitute_entity_def(self, edef, edefs): |
451 |
"""substitute an incomplete entity definition by a list of complete |
|
452 |
equivalents |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
453 |
|
0 | 454 |
e.g. on queries such as :: |
455 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
456 |
WHERE U login 'admin', U login N |
|
457 |
||
458 |
X will be inserted as many times as U exists, and so the X travaille Y |
|
459 |
relations as to be added as many time as X is inserted |
|
460 |
""" |
|
461 |
if not edefs or not self.e_defs: |
|
462 |
# no result, no entity will be created |
|
463 |
self.e_defs = () |
|
464 |
return |
|
465 |
# first remove the incomplete entity definition |
|
466 |
colidx = self.e_defs[0].index(edef) |
|
467 |
for i, row in enumerate(self.e_defs[:]): |
|
468 |
self.e_defs[i][colidx] = edefs[0] |
|
469 |
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
|
470 |
for edef_ in edefs[1:]: |
0 | 471 |
row = samplerow[:] |
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
|
472 |
row[colidx] = edef_ |
0 | 473 |
self.e_defs.append(row) |
474 |
# now, see if this entity def is referenced as subject in some relation |
|
475 |
# definition |
|
476 |
if self._r_subj_index.has_key(edef): |
|
477 |
for rdef in self._r_subj_index[edef]: |
|
478 |
expanded = self._expanded(rdef) |
|
479 |
result = [] |
|
480 |
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
|
481 |
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
|
482 |
result.append( (edef_, exp_rdef[1], exp_rdef[2]) ) |
0 | 483 |
self._expanded_r_defs[rdef] = result |
484 |
# and finally, see if this entity def is referenced as object in some |
|
485 |
# relation definition |
|
486 |
if self._r_obj_index.has_key(edef): |
|
487 |
for rdef in self._r_obj_index[edef]: |
|
488 |
expanded = self._expanded(rdef) |
|
489 |
result = [] |
|
490 |
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
|
491 |
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
|
492 |
result.append( (exp_rdef[0], exp_rdef[1], edef_) ) |
0 | 493 |
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
|
494 |
|
0 | 495 |
def _expanded(self, rdef): |
496 |
"""return expanded value for the given relation definition""" |
|
497 |
try: |
|
498 |
return self._expanded_r_defs[rdef] |
|
499 |
except KeyError: |
|
500 |
self.r_defs.remove(rdef) |
|
501 |
return [rdef] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
502 |
|
0 | 503 |
def relation_defs(self): |
504 |
"""return the list for relation definitions to insert""" |
|
505 |
for rdefs in self._expanded_r_defs.values(): |
|
506 |
for rdef in rdefs: |
|
507 |
yield rdef |
|
508 |
for rdef in self.r_defs: |
|
509 |
yield rdef |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
510 |
|
0 | 511 |
def insert_entity_defs(self): |
512 |
"""return eids of inserted entities in a suitable form for the resulting |
|
513 |
result set, e.g.: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
514 |
|
0 | 515 |
e.g. on queries such as :: |
516 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
517 |
WHERE U login 'admin', U login N |
|
518 |
||
519 |
if there is two entities matching U, the result set will look like |
|
520 |
[(eidX1, eidY1), (eidX2, eidY2)] |
|
521 |
""" |
|
522 |
session = self.session |
|
523 |
repo = session.repo |
|
524 |
results = [] |
|
525 |
for row in self.e_defs: |
|
526 |
results.append([repo.glob_add_entity(session, edef) |
|
527 |
for edef in row]) |
|
528 |
return results |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
529 |
|
0 | 530 |
def insert_relation_defs(self): |
531 |
session = self.session |
|
532 |
repo = session.repo |
|
533 |
for subj, rtype, obj in self.relation_defs(): |
|
534 |
# if a string is given into args instead of an int, we get it here |
|
535 |
if isinstance(subj, basestring): |
|
536 |
subj = typed_eid(subj) |
|
537 |
elif not isinstance(subj, (int, long)): |
|
538 |
subj = subj.eid |
|
539 |
if isinstance(obj, basestring): |
|
540 |
obj = typed_eid(obj) |
|
541 |
elif not isinstance(obj, (int, long)): |
|
542 |
obj = obj.eid |
|
543 |
if repo.schema.rschema(rtype).inlined: |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2646
diff
changeset
|
544 |
entity = session.entity_from_eid(subj) |
0 | 545 |
entity[rtype] = obj |
3171
b10f0c1f3fe4
[repo] oops, glob_update_entity takes a list of edited attributes has third argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3109
diff
changeset
|
546 |
repo.glob_update_entity(session, entity, set((rtype,))) |
0 | 547 |
else: |
548 |
repo.glob_add_relation(session, subj, rtype, obj) |
|
549 |
||
550 |
||
551 |
class QuerierHelper(object): |
|
552 |
"""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
|
553 |
|
0 | 554 |
def __init__(self, repo, schema): |
555 |
# system info helper |
|
556 |
self._repo = repo |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
557 |
# instance schema |
0 | 558 |
self.set_schema(schema) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
559 |
|
0 | 560 |
def set_schema(self, schema): |
561 |
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
|
562 |
repo = self._repo |
5176
ddd5219d7eef
[querier] don't need a Cache instance for rql cacke key cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5174
diff
changeset
|
563 |
# rql st and solution cache. Don't bother using a Cache instance: we |
ddd5219d7eef
[querier] don't need a Cache instance for rql cacke key cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5174
diff
changeset
|
564 |
# should have a limited number of queries in there, since there are no |
ddd5219d7eef
[querier] don't need a Cache instance for rql cacke key cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5174
diff
changeset
|
565 |
# entries in this cache for user queries (which have no args) |
ddd5219d7eef
[querier] don't need a Cache instance for rql cacke key cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5174
diff
changeset
|
566 |
self._rql_cache = {} |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
567 |
# rql cache key cache |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
568 |
self._rql_ck_cache = Cache(repo.config['rql-cache-size']) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
569 |
# 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
|
570 |
self.cache_hit, self.cache_miss = 0, 0 |
0 | 571 |
# 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
|
572 |
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
|
573 |
rqlhelper = repo.vreg.rqlhelper |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
574 |
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
|
575 |
self._annotate = rqlhelper.annotate |
0 | 576 |
# rql planner |
577 |
# note: don't use repo.sources, may not be built yet, and also "admin" |
|
578 |
# isn't an actual source |
|
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
|
579 |
if len([uri for uri in repo.config.sources() if uri != 'admin']) < 2: |
0 | 580 |
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
|
581 |
self._planner = SSPlanner(schema, rqlhelper) |
0 | 582 |
else: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
583 |
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
|
584 |
self._planner = MSPlanner(schema, rqlhelper) |
0 | 585 |
# sql generation annotator |
586 |
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
|
587 |
|
0 | 588 |
def parse(self, rql, annotate=False): |
589 |
"""return a rql syntax tree for the given rql""" |
|
590 |
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
|
591 |
return self._parse(unicode(rql), annotate=annotate) |
0 | 592 |
except UnicodeError: |
593 |
raise RQLSyntaxError(rql) |
|
594 |
||
595 |
def plan_factory(self, rqlst, args, session): |
|
596 |
"""create an execution plan for an INSERT RQL query""" |
|
597 |
if rqlst.TYPE == 'insert': |
|
598 |
return InsertPlan(self, rqlst, args, session) |
|
599 |
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
|
600 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
601 |
def execute(self, session, rql, args=None, build_descr=True): |
0 | 602 |
"""execute a rql query, return resulting rows and their description in |
603 |
a `ResultSet` object |
|
604 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
605 |
* `rql` should be an Unicode string or a plain ASCII string |
0 | 606 |
* `args` the optional parameters dictionary associated to the query |
607 |
* `build_descr` is a boolean flag indicating if the description should |
|
608 |
be built on select queries (if false, the description will be en empty |
|
609 |
list) |
|
610 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
611 |
on INSERT queries, there will be one row with the eid of each inserted |
0 | 612 |
entity |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
613 |
|
0 | 614 |
result for DELETE and SET queries is undefined yet |
615 |
||
616 |
to maximize the rql parsing/analyzing cache performance, you should |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
617 |
always use substitute arguments in queries (i.e. avoid query such as |
0 | 618 |
'Any X WHERE X eid 123'!) |
619 |
""" |
|
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
620 |
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
|
621 |
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
|
622 |
print '*'*80 |
2646
d2874ddd4347
#F [querier] debugging tweak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2629
diff
changeset
|
623 |
print 'querier input', rql, args |
0 | 624 |
# parse the query and binds variables |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
625 |
try: |
0 | 626 |
cachekey = rql |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
627 |
if args: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
628 |
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
|
629 |
if eidkeys: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
630 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
631 |
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
|
632 |
args, eidkeys) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
633 |
except UnknownEid: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
634 |
# 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
|
635 |
# 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
|
636 |
return empty_rset(rql, args) |
0 | 637 |
rqlst = self._rql_cache[cachekey] |
638 |
self.cache_hit += 1 |
|
639 |
except KeyError: |
|
640 |
self.cache_miss += 1 |
|
641 |
rqlst = self.parse(rql) |
|
642 |
try: |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
643 |
eidkeys = self.solutions(session, rqlst, args) |
0 | 644 |
except UnknownEid: |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
645 |
# 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
|
646 |
# empty result instead of raising UnknownEid |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
647 |
return empty_rset(rql, args, rqlst) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
648 |
self._rql_ck_cache[rql] = eidkeys |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
649 |
if eidkeys: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
650 |
cachekey = self._repo.querier_cache_key(session, rql, args, |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
651 |
eidkeys) |
0 | 652 |
self._rql_cache[cachekey] = rqlst |
653 |
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
|
654 |
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
|
655 |
if session.read_security: |
0 | 656 |
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
|
657 |
# 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
|
658 |
# be released until commit/rollback |
0 | 659 |
session.mode = 'write' |
660 |
cachekey = None |
|
661 |
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
|
662 |
if session.read_security: |
0 | 663 |
for select in rqlst.children: |
664 |
check_no_password_selected(select) |
|
665 |
# on select query, always copy the cached rqlst so we don't have to |
|
666 |
# bother modifying it. This is not necessary on write queries since |
|
667 |
# a new syntax tree is built from them. |
|
668 |
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
|
669 |
self._annotate(rqlst) |
0 | 670 |
# make an execution plan |
671 |
plan = self.plan_factory(rqlst, args, session) |
|
672 |
plan.cache_key = cachekey |
|
673 |
self._planner.build_plan(plan) |
|
674 |
# execute the plan |
|
675 |
try: |
|
676 |
results = plan.execute() |
|
677 |
except Unauthorized: |
|
678 |
# XXX this could be done in security's after_add_relation hooks |
|
679 |
# since it's actually realy only needed there (other relations |
|
680 |
# security is done *before* actual changes, and add/update entity |
|
681 |
# security is done after changes but in an operation, and exception |
|
4533 | 682 |
# generated in operation's events properly generate a rollback on |
0 | 683 |
# the session). Even though, this is done here for a better |
684 |
# consistency: getting an Unauthorized exception means the |
|
685 |
# transaction has been rollbacked |
|
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
686 |
# |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
687 |
# notes: |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
688 |
# * we should not reset the pool here, since we don't want the |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
689 |
# session to loose its pool during processing |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
690 |
# * 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
|
691 |
# by the session |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
692 |
if session.commit_state is None: |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
693 |
session.rollback(reset_pool=False) |
0 | 694 |
raise |
695 |
# build a description for the results if necessary |
|
696 |
descr = () |
|
697 |
if build_descr: |
|
698 |
if rqlst.TYPE == 'select': |
|
699 |
# sample selection |
|
700 |
descr = session.build_description(orig_rqlst, args, results) |
|
701 |
elif rqlst.TYPE == 'insert': |
|
702 |
# on insert plan, some entities may have been auto-casted, |
|
703 |
# so compute description manually even if there is only |
|
704 |
# one solution |
|
705 |
basedescr = [None] * len(plan.selected) |
|
706 |
todetermine = zip(xrange(len(plan.selected)), repeat(False)) |
|
707 |
descr = session._build_descr(results, basedescr, todetermine) |
|
708 |
# FIXME: get number of affected entities / relations on non |
|
709 |
# selection queries ? |
|
710 |
# 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
|
711 |
return ResultSet(results, rql, args, descr, orig_rqlst) |
0 | 712 |
|
713 |
from logging import getLogger |
|
714 |
from cubicweb import set_log_methods |
|
715 |
LOGGER = getLogger('cubicweb.querier') |
|
716 |
set_log_methods(QuerierHelper, LOGGER) |