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