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