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