author | Katia Saurfelt <katia.saurfelt@logilab.fr> |
Wed, 05 May 2010 18:39:59 +0200 | |
changeset 5482 | 8c8c6d3f3b3a |
parent 5426 | 0d4853a6e5ee |
child 5596 | d66a5d98db5b |
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. |
|
20 |
||
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 |
|
272 |
select.limit or select.offset): |
|
273 |
newselect = Select() |
|
274 |
# only select variables in subqueries |
|
275 |
origselection = select.selection |
|
276 |
select.select_only_variables() |
|
277 |
select.has_aggregat = False |
|
278 |
# create subquery first so correct node are used on copy |
|
279 |
# (eg ColumnAlias instead of Variable) |
|
280 |
aliases = [VariableRef(newselect.get_variable(vref.name, i)) |
|
281 |
for i, vref in enumerate(select.selection)] |
|
282 |
selected = set(vref.name for vref in aliases) |
|
283 |
# now copy original selection and groups |
|
284 |
for term in origselection: |
|
285 |
newselect.append_selected(term.copy(newselect)) |
|
286 |
if select.orderby: |
|
287 |
newselect.set_orderby([s.copy(newselect) for s in select.orderby]) |
|
288 |
_expand_selection(select.orderby, selected, aliases, select, newselect) |
|
289 |
select.orderby = () # XXX dereference? |
|
290 |
if select.groupby: |
|
291 |
newselect.set_groupby([g.copy(newselect) for g in select.groupby]) |
|
292 |
_expand_selection(select.groupby, selected, aliases, select, newselect) |
|
293 |
select.groupby = () # XXX dereference? |
|
294 |
if select.having: |
|
295 |
newselect.set_having([g.copy(newselect) for g in select.having]) |
|
296 |
_expand_selection(select.having, selected, aliases, select, newselect) |
|
297 |
select.having = () # XXX dereference? |
|
298 |
if select.limit: |
|
299 |
newselect.limit = select.limit |
|
300 |
select.limit = None |
|
301 |
if select.offset: |
|
302 |
newselect.offset = select.offset |
|
303 |
select.offset = 0 |
|
304 |
myunion = Union() |
|
305 |
newselect.set_with([SubQuery(aliases, myunion)], check=False) |
|
306 |
solutions = [sol.copy() for sol in select.solutions] |
|
307 |
cleanup_solutions(newselect, solutions) |
|
308 |
newselect.set_possible_types(solutions) |
|
309 |
# if some solutions doesn't need rewriting, insert original |
|
310 |
# select as first union subquery |
|
311 |
if () in localchecks: |
|
312 |
myunion.append(select) |
|
313 |
# we're done, replace original select by the new select with |
|
314 |
# subqueries (more added in the loop below) |
|
315 |
union.replace(select, newselect) |
|
316 |
elif not () in localchecks: |
|
317 |
union.remove(select) |
|
318 |
for lcheckdef, lchecksolutions in localchecks.iteritems(): |
|
319 |
if not lcheckdef: |
|
320 |
continue |
|
321 |
myrqlst = select.copy(solutions=lchecksolutions) |
|
322 |
myunion.append(myrqlst) |
|
323 |
# in-place rewrite + annotation / simplification |
|
4955 | 324 |
lcheckdef = [((var, 'X'), rqlexprs) for var, rqlexprs in lcheckdef] |
0 | 325 |
rewrite(myrqlst, lcheckdef, lchecksolutions, self.args) |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
326 |
add_noinvariant(noinvariant, restricted, myrqlst, nbtrees) |
0 | 327 |
if () in localchecks: |
328 |
select.set_possible_types(localchecks[()]) |
|
329 |
add_types_restriction(self.schema, select) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
330 |
add_noinvariant(noinvariant, restricted, select, nbtrees) |
0 | 331 |
|
332 |
def _check_permissions(self, rqlst): |
|
333 |
"""return a dict defining "local checks", e.g. RQLExpression defined in |
|
334 |
the schema that should be inserted in the original query |
|
335 |
||
336 |
solutions where a variable has a type which the user can't definitly read |
|
337 |
are removed, else if the user may read it (eg if an rql expression is |
|
338 |
defined for the "read" permission of the related type), the local checks |
|
339 |
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
|
340 |
|
0 | 341 |
return a dict with entries for each different local check necessary, |
342 |
with associated solutions as value. A local check is defined by a list |
|
343 |
of 2-uple, with variable name as first item and the necessary rql |
|
344 |
expression as second item for each variable which has to be checked. |
|
345 |
So solutions which don't require local checks will be associated to |
|
346 |
the empty tuple key. |
|
347 |
||
348 |
note: rqlst should not have been simplified at this point |
|
349 |
""" |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
350 |
session = self.session |
0 | 351 |
msgs = [] |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
352 |
neweids = session.transaction_data.get('neweids', ()) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
353 |
varkwargs = {} |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
354 |
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
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
varkwargs[var.name] = typed_eid(eid) |
0 | 359 |
# dictionnary of variables restricted for security reason |
360 |
localchecks = {} |
|
361 |
restricted_vars = set() |
|
362 |
newsolutions = [] |
|
363 |
for solution in rqlst.solutions: |
|
364 |
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
|
365 |
localcheck = check_read_access(session, rqlst, solution, self.args) |
0 | 366 |
except Unauthorized, ex: |
367 |
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
|
368 |
msg %= (solution, session.user.login, ex.args[0], ex.args[1]) |
0 | 369 |
msgs.append(msg) |
370 |
LOGGER.info(msg) |
|
371 |
else: |
|
372 |
newsolutions.append(solution) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
373 |
# 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
|
374 |
# are specified by eid in query'args |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
375 |
for varname, eid in varkwargs.iteritems(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
376 |
try: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
377 |
rqlexprs = localcheck.pop(varname) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
378 |
except KeyError: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
379 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
380 |
if eid in neweids: |
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 |
for rqlexpr in rqlexprs: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
383 |
if rqlexpr.check(session, eid): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
384 |
break |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
385 |
else: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
386 |
raise Unauthorized() |
0 | 387 |
restricted_vars.update(localcheck) |
388 |
localchecks.setdefault(tuple(localcheck.iteritems()), []).append(solution) |
|
389 |
# raise Unautorized exception if the user can't access to any solution |
|
390 |
if not newsolutions: |
|
391 |
raise Unauthorized('\n'.join(msgs)) |
|
392 |
rqlst.set_possible_types(newsolutions) |
|
393 |
return localchecks, restricted_vars |
|
394 |
||
395 |
def finalize(self, select, solutions, insertedvars): |
|
396 |
rqlst = Union() |
|
397 |
rqlst.append(select) |
|
398 |
for mainvarname, rschema, newvarname in insertedvars: |
|
399 |
nvartype = str(rschema.objects(solutions[0][mainvarname])[0]) |
|
400 |
for sol in solutions: |
|
401 |
sol[newvarname] = nvartype |
|
402 |
select.clean_solutions(solutions) |
|
403 |
self.rqlhelper.annotate(rqlst) |
|
404 |
self.preprocess(rqlst, security=False) |
|
405 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
406 |
|
4759 | 407 |
|
0 | 408 |
class InsertPlan(ExecutionPlan): |
409 |
"""an execution model specific to the INSERT rql query |
|
410 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
411 |
|
0 | 412 |
def __init__(self, querier, rqlst, args, session): |
413 |
ExecutionPlan.__init__(self, querier, rqlst, args, session) |
|
414 |
# save originaly selected variable, we may modify this |
|
415 |
# dictionary for substitution (query parameters) |
|
416 |
self.selected = rqlst.selection |
|
417 |
# list of new or updated entities definition (utils.Entity) |
|
418 |
self.e_defs = [[]] |
|
419 |
# list of new relation definition (3-uple (from_eid, r_type, to_eid) |
|
420 |
self.r_defs = [] |
|
421 |
# indexes to track entity definitions bound to relation definitions |
|
422 |
self._r_subj_index = {} |
|
423 |
self._r_obj_index = {} |
|
424 |
self._expanded_r_defs = {} |
|
425 |
||
426 |
def add_entity_def(self, edef): |
|
427 |
"""add an entity definition to build""" |
|
428 |
edef.querier_pending_relations = {} |
|
429 |
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
|
430 |
|
0 | 431 |
def add_relation_def(self, rdef): |
432 |
"""add an relation definition to build""" |
|
433 |
self.r_defs.append(rdef) |
|
434 |
if not isinstance(rdef[0], int): |
|
435 |
self._r_subj_index.setdefault(rdef[0], []).append(rdef) |
|
436 |
if not isinstance(rdef[2], int): |
|
437 |
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
|
438 |
|
0 | 439 |
def substitute_entity_def(self, edef, edefs): |
440 |
"""substitute an incomplete entity definition by a list of complete |
|
441 |
equivalents |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
442 |
|
0 | 443 |
e.g. on queries such as :: |
444 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
445 |
WHERE U login 'admin', U login N |
|
446 |
||
447 |
X will be inserted as many times as U exists, and so the X travaille Y |
|
448 |
relations as to be added as many time as X is inserted |
|
449 |
""" |
|
450 |
if not edefs or not self.e_defs: |
|
451 |
# no result, no entity will be created |
|
452 |
self.e_defs = () |
|
453 |
return |
|
454 |
# first remove the incomplete entity definition |
|
455 |
colidx = self.e_defs[0].index(edef) |
|
456 |
for i, row in enumerate(self.e_defs[:]): |
|
457 |
self.e_defs[i][colidx] = edefs[0] |
|
458 |
samplerow = self.e_defs[i] |
|
459 |
for edef in edefs[1:]: |
|
460 |
row = samplerow[:] |
|
461 |
row[colidx] = edef |
|
462 |
self.e_defs.append(row) |
|
463 |
# now, see if this entity def is referenced as subject in some relation |
|
464 |
# definition |
|
465 |
if self._r_subj_index.has_key(edef): |
|
466 |
for rdef in self._r_subj_index[edef]: |
|
467 |
expanded = self._expanded(rdef) |
|
468 |
result = [] |
|
469 |
for exp_rdef in expanded: |
|
470 |
for edef in edefs: |
|
471 |
result.append( (edef, exp_rdef[1], exp_rdef[2]) ) |
|
472 |
self._expanded_r_defs[rdef] = result |
|
473 |
# and finally, see if this entity def is referenced as object in some |
|
474 |
# relation definition |
|
475 |
if self._r_obj_index.has_key(edef): |
|
476 |
for rdef in self._r_obj_index[edef]: |
|
477 |
expanded = self._expanded(rdef) |
|
478 |
result = [] |
|
479 |
for exp_rdef in expanded: |
|
480 |
for edef in edefs: |
|
481 |
result.append( (exp_rdef[0], exp_rdef[1], edef) ) |
|
482 |
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
|
483 |
|
0 | 484 |
def _expanded(self, rdef): |
485 |
"""return expanded value for the given relation definition""" |
|
486 |
try: |
|
487 |
return self._expanded_r_defs[rdef] |
|
488 |
except KeyError: |
|
489 |
self.r_defs.remove(rdef) |
|
490 |
return [rdef] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
491 |
|
0 | 492 |
def relation_defs(self): |
493 |
"""return the list for relation definitions to insert""" |
|
494 |
for rdefs in self._expanded_r_defs.values(): |
|
495 |
for rdef in rdefs: |
|
496 |
yield rdef |
|
497 |
for rdef in self.r_defs: |
|
498 |
yield rdef |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
499 |
|
0 | 500 |
def insert_entity_defs(self): |
501 |
"""return eids of inserted entities in a suitable form for the resulting |
|
502 |
result set, e.g.: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
503 |
|
0 | 504 |
e.g. on queries such as :: |
505 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
506 |
WHERE U login 'admin', U login N |
|
507 |
||
508 |
if there is two entities matching U, the result set will look like |
|
509 |
[(eidX1, eidY1), (eidX2, eidY2)] |
|
510 |
""" |
|
511 |
session = self.session |
|
512 |
repo = session.repo |
|
513 |
results = [] |
|
514 |
for row in self.e_defs: |
|
515 |
results.append([repo.glob_add_entity(session, edef) |
|
516 |
for edef in row]) |
|
517 |
return results |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
518 |
|
0 | 519 |
def insert_relation_defs(self): |
520 |
session = self.session |
|
521 |
repo = session.repo |
|
522 |
for subj, rtype, obj in self.relation_defs(): |
|
523 |
# if a string is given into args instead of an int, we get it here |
|
524 |
if isinstance(subj, basestring): |
|
525 |
subj = typed_eid(subj) |
|
526 |
elif not isinstance(subj, (int, long)): |
|
527 |
subj = subj.eid |
|
528 |
if isinstance(obj, basestring): |
|
529 |
obj = typed_eid(obj) |
|
530 |
elif not isinstance(obj, (int, long)): |
|
531 |
obj = obj.eid |
|
532 |
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
|
533 |
entity = session.entity_from_eid(subj) |
0 | 534 |
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
|
535 |
repo.glob_update_entity(session, entity, set((rtype,))) |
0 | 536 |
else: |
537 |
repo.glob_add_relation(session, subj, rtype, obj) |
|
538 |
||
539 |
||
540 |
class QuerierHelper(object): |
|
541 |
"""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
|
542 |
|
0 | 543 |
def __init__(self, repo, schema): |
544 |
# system info helper |
|
545 |
self._repo = repo |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
546 |
# instance schema |
0 | 547 |
self.set_schema(schema) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
548 |
|
0 | 549 |
def set_schema(self, schema): |
550 |
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
|
551 |
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
|
552 |
# 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
|
553 |
# 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
|
554 |
# 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
|
555 |
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
|
556 |
# 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
|
557 |
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
|
558 |
# 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
|
559 |
self.cache_hit, self.cache_miss = 0, 0 |
0 | 560 |
# 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
|
561 |
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
|
562 |
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
|
563 |
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
|
564 |
self._annotate = rqlhelper.annotate |
0 | 565 |
# rql planner |
566 |
# note: don't use repo.sources, may not be built yet, and also "admin" |
|
567 |
# 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
|
568 |
if len([uri for uri in repo.config.sources() if uri != 'admin']) < 2: |
0 | 569 |
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
|
570 |
self._planner = SSPlanner(schema, rqlhelper) |
0 | 571 |
else: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
572 |
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
|
573 |
self._planner = MSPlanner(schema, rqlhelper) |
0 | 574 |
# sql generation annotator |
575 |
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
|
576 |
|
0 | 577 |
def parse(self, rql, annotate=False): |
578 |
"""return a rql syntax tree for the given rql""" |
|
579 |
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
|
580 |
return self._parse(unicode(rql), annotate=annotate) |
0 | 581 |
except UnicodeError: |
582 |
raise RQLSyntaxError(rql) |
|
583 |
||
584 |
def plan_factory(self, rqlst, args, session): |
|
585 |
"""create an execution plan for an INSERT RQL query""" |
|
586 |
if rqlst.TYPE == 'insert': |
|
587 |
return InsertPlan(self, rqlst, args, session) |
|
588 |
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
|
589 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
590 |
def execute(self, session, rql, args=None, build_descr=True): |
0 | 591 |
"""execute a rql query, return resulting rows and their description in |
592 |
a `ResultSet` object |
|
593 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
594 |
* `rql` should be an Unicode string or a plain ASCII string |
0 | 595 |
* `args` the optional parameters dictionary associated to the query |
596 |
* `build_descr` is a boolean flag indicating if the description should |
|
597 |
be built on select queries (if false, the description will be en empty |
|
598 |
list) |
|
599 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
600 |
on INSERT queries, there will be one row with the eid of each inserted |
0 | 601 |
entity |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
602 |
|
0 | 603 |
result for DELETE and SET queries is undefined yet |
604 |
||
605 |
to maximize the rql parsing/analyzing cache performance, you should |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
606 |
always use substitute arguments in queries (i.e. avoid query such as |
0 | 607 |
'Any X WHERE X eid 123'!) |
608 |
""" |
|
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
609 |
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
|
610 |
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
|
611 |
print '*'*80 |
2646
d2874ddd4347
#F [querier] debugging tweak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2629
diff
changeset
|
612 |
print 'querier input', rql, args |
0 | 613 |
# 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
|
614 |
try: |
0 | 615 |
cachekey = rql |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
616 |
if args: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
617 |
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
|
618 |
if eidkeys: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
619 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
620 |
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
|
621 |
args, eidkeys) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
622 |
except UnknownEid: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
623 |
# 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
|
624 |
# 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
|
625 |
return empty_rset(rql, args) |
0 | 626 |
rqlst = self._rql_cache[cachekey] |
627 |
self.cache_hit += 1 |
|
628 |
except KeyError: |
|
629 |
self.cache_miss += 1 |
|
630 |
rqlst = self.parse(rql) |
|
631 |
try: |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
632 |
eidkeys = self.solutions(session, rqlst, args) |
0 | 633 |
except UnknownEid: |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
634 |
# 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
|
635 |
# empty result instead of raising UnknownEid |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
636 |
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
|
637 |
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
|
638 |
if eidkeys: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
639 |
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
|
640 |
eidkeys) |
0 | 641 |
self._rql_cache[cachekey] = rqlst |
642 |
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
|
643 |
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
|
644 |
if session.read_security: |
0 | 645 |
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
|
646 |
# 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
|
647 |
# be released until commit/rollback |
0 | 648 |
session.mode = 'write' |
649 |
cachekey = None |
|
650 |
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
|
651 |
if session.read_security: |
0 | 652 |
for select in rqlst.children: |
653 |
check_no_password_selected(select) |
|
654 |
# on select query, always copy the cached rqlst so we don't have to |
|
655 |
# bother modifying it. This is not necessary on write queries since |
|
656 |
# a new syntax tree is built from them. |
|
657 |
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
|
658 |
self._annotate(rqlst) |
0 | 659 |
# make an execution plan |
660 |
plan = self.plan_factory(rqlst, args, session) |
|
661 |
plan.cache_key = cachekey |
|
662 |
self._planner.build_plan(plan) |
|
663 |
# execute the plan |
|
664 |
try: |
|
665 |
results = plan.execute() |
|
666 |
except Unauthorized: |
|
667 |
# XXX this could be done in security's after_add_relation hooks |
|
668 |
# since it's actually realy only needed there (other relations |
|
669 |
# security is done *before* actual changes, and add/update entity |
|
670 |
# security is done after changes but in an operation, and exception |
|
4533 | 671 |
# generated in operation's events properly generate a rollback on |
0 | 672 |
# the session). Even though, this is done here for a better |
673 |
# consistency: getting an Unauthorized exception means the |
|
674 |
# transaction has been rollbacked |
|
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
675 |
# |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
676 |
# notes: |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
677 |
# * 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
|
678 |
# 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
|
679 |
# * 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
|
680 |
# by the session |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
681 |
if session.commit_state is None: |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
682 |
session.rollback(reset_pool=False) |
0 | 683 |
raise |
684 |
# build a description for the results if necessary |
|
685 |
descr = () |
|
686 |
if build_descr: |
|
687 |
if rqlst.TYPE == 'select': |
|
688 |
# sample selection |
|
689 |
descr = session.build_description(orig_rqlst, args, results) |
|
690 |
elif rqlst.TYPE == 'insert': |
|
691 |
# on insert plan, some entities may have been auto-casted, |
|
692 |
# so compute description manually even if there is only |
|
693 |
# one solution |
|
694 |
basedescr = [None] * len(plan.selected) |
|
695 |
todetermine = zip(xrange(len(plan.selected)), repeat(False)) |
|
696 |
descr = session._build_descr(results, basedescr, todetermine) |
|
697 |
# FIXME: get number of affected entities / relations on non |
|
698 |
# selection queries ? |
|
699 |
# 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
|
700 |
return ResultSet(results, rql, args, descr, orig_rqlst) |
0 | 701 |
|
702 |
from logging import getLogger |
|
703 |
from cubicweb import set_log_methods |
|
704 |
LOGGER = getLogger('cubicweb.querier') |
|
705 |
set_log_methods(QuerierHelper, LOGGER) |