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