author | Jérôme Roy <jerome.roy@logilab.fr> |
Fri, 14 Mar 2014 15:19:31 +0100 | |
branch | stable |
changeset 9591 | 5b8bc486d0dc |
parent 9286 | 02a491f6fa92 |
child 9448 | 3e7cad3967c5 |
child 9701 | 46c8d8701240 |
permissions | -rw-r--r-- |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
1 |
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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 |
__docformat__ = "restructuredtext en" |
22 |
||
23 |
from itertools import repeat |
|
24 |
||
25 |
from logilab.common.compat import any |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
26 |
from rql import RQLSyntaxError, CoercionError |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
27 |
from rql.stmts import Union |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
28 |
from rql.nodes import ETYPE_PYOBJ_MAP, etype_from_pyobj, Relation, Exists, Not |
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
29 |
from yams import BASE_TYPES |
0 | 30 |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
31 |
from cubicweb import ValidationError, Unauthorized, UnknownEid |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
32 |
from cubicweb import Binary, server |
0 | 33 |
from cubicweb.rset import ResultSet |
34 |
||
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
35 |
from cubicweb.utils import QueryCache, RepeatList |
0 | 36 |
from cubicweb.server.rqlannotation import SQLGenAnnotator, set_qdata |
7118
e094b3d4eb95
[server] move EditedEntity class to its own module, to avoid cyclic dependency when needed from e.g. session.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
37 |
from cubicweb.server.ssplanner import READ_ONLY_RTYPES, add_types_restriction |
e094b3d4eb95
[server] move EditedEntity class to its own module, to avoid cyclic dependency when needed from e.g. session.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
38 |
from cubicweb.server.edition import EditedEntity |
2601
5196d213f4e3
[R querier] new READ_ONLY_RTYPES constant
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
39 |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
40 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
41 |
ETYPE_PYOBJ_MAP[Binary] = 'Bytes' |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
42 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
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: |
|
8863
98c107bd6cd0
[querier] improve 'Password selection not allowed' error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8697
diff
changeset
|
63 |
for var, etype in solution.iteritems(): |
98c107bd6cd0
[querier] improve 'Password selection not allowed' error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8697
diff
changeset
|
64 |
if etype == 'Password': |
98c107bd6cd0
[querier] improve 'Password selection not allowed' error message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8697
diff
changeset
|
65 |
raise Unauthorized('Password selection is not allowed (%s)' % var) |
0 | 66 |
|
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
|
67 |
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
|
68 |
"""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
|
69 |
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
|
70 |
""" |
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 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
|
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
|
76 |
def check_read_access(session, rqlst, solution, args): |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
77 |
"""Check that the given user has credentials to access data read by the |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
78 |
query and return a dict defining necessary "local checks" (i.e. rql |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
79 |
expression in read permission defined in the schema) where no group grants |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
80 |
him the permission. |
0 | 81 |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
82 |
Returned dictionary's keys are variable names and values the rql expressions |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
83 |
for this variable (with the given solution). |
0 | 84 |
""" |
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
|
85 |
# 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
|
86 |
# 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
|
87 |
# XXX what about local read security w/ those rewritten constants... |
9286
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
88 |
DBG = (server.DEBUG & server.DBG_SEC) and 'read' in server._SECURITY_CAPS |
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
|
89 |
schema = session.repo.schema |
0 | 90 |
if rqlst.where is not None: |
91 |
for rel in rqlst.where.iget_nodes(Relation): |
|
92 |
# 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
|
93 |
if rel.r_type in READ_ONLY_RTYPES: |
0 | 94 |
continue |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
95 |
rschema = schema.rschema(rel.r_type) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
96 |
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
|
97 |
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
|
98 |
solution, args)) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
99 |
rdef = eschema.rdef(rschema) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
if not session.user.matching_groups(rdef.get_groups('read')): |
9286
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
106 |
if DBG: |
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
107 |
print ('check_read_access: %s %s does not match %s' % |
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
108 |
(rdef, session.user.groups, rdef.get_groups('read'))) |
4955 | 109 |
# XXX rqlexpr not allowed |
0 | 110 |
raise Unauthorized('read', rel.r_type) |
9286
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
111 |
if DBG: |
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
112 |
print ('check_read_access: %s %s matches %s' % |
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
113 |
(rdef, session.user.groups, rdef.get_groups('read'))) |
0 | 114 |
localchecks = {} |
115 |
# iterate on defined_vars and not on solutions to ignore column aliases |
|
116 |
for varname in rqlst.defined_vars: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
117 |
eschema = schema.eschema(solution[varname]) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
118 |
if eschema.final: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
119 |
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
|
120 |
if not session.user.matching_groups(eschema.get_groups('read')): |
0 | 121 |
erqlexprs = eschema.get_rqlexprs('read') |
122 |
if not erqlexprs: |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
123 |
ex = Unauthorized('read', solution[varname]) |
0 | 124 |
ex.var = varname |
9286
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
125 |
if DBG: |
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
126 |
print ('check_read_access: %s %s %s %s' % |
02a491f6fa92
[querier/security] instrument a bit the querier read security checks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
127 |
(varname, eschema, session.user.groups, eschema.get_groups('read'))) |
0 | 128 |
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
|
129 |
# 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
|
130 |
# '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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
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
|
137 |
!= 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
|
138 |
localchecks[varname] = erqlexprs |
0 | 139 |
return localchecks |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
140 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
141 |
|
0 | 142 |
# Plans ####################################################################### |
143 |
||
144 |
class ExecutionPlan(object): |
|
145 |
"""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
|
146 |
|
0 | 147 |
def __init__(self, querier, rqlst, args, session): |
148 |
# original rql syntax tree |
|
149 |
self.rqlst = rqlst |
|
150 |
self.args = args or {} |
|
151 |
# session executing the query |
|
152 |
self.session = session |
|
153 |
# quick reference to the system source |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
154 |
self.syssource = session.cnxset.source('system') |
0 | 155 |
# execution steps |
156 |
self.steps = [] |
|
157 |
# index of temporary tables created during execution |
|
158 |
self.temp_tables = {} |
|
159 |
# various resource accesors |
|
160 |
self.querier = querier |
|
161 |
self.schema = querier.schema |
|
162 |
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
|
163 |
self.rqlhelper = session.vreg.rqlhelper |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
164 |
|
0 | 165 |
def annotate_rqlst(self): |
166 |
if not self.rqlst.annotated: |
|
167 |
self.rqlhelper.annotate(self.rqlst) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
168 |
|
0 | 169 |
def add_step(self, step): |
170 |
"""add a step to the plan""" |
|
171 |
self.steps.append(step) |
|
172 |
||
173 |
def clean(self): |
|
174 |
"""remove temporary tables""" |
|
175 |
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
|
176 |
|
0 | 177 |
def sqlexec(self, sql, args=None): |
178 |
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
|
179 |
|
0 | 180 |
def execute(self): |
181 |
"""execute a plan and return resulting rows""" |
|
182 |
try: |
|
183 |
for step in self.steps: |
|
184 |
result = step.execute() |
|
185 |
# the latest executed step contains the full query result |
|
186 |
return result |
|
187 |
finally: |
|
188 |
self.clean() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
189 |
|
5168
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
190 |
def make_temp_table_name(self, table): |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
191 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
192 |
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
|
193 |
""" |
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
194 |
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
|
195 |
|
1ab032df5ca3
SQL Server port: temporary table handling
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5072
diff
changeset
|
196 |
|
0 | 197 |
def init_temp_table(self, table, selected, sol): |
198 |
"""initialize sql schema and variable map for a temporary table which |
|
199 |
will be used to store result for the given rqlst |
|
200 |
""" |
|
201 |
try: |
|
202 |
outputmap, sqlschema, _ = self.temp_tables[table] |
|
203 |
update_varmap(outputmap, selected, table) |
|
204 |
except KeyError: |
|
205 |
sqlschema, outputmap = self.syssource.temp_table_def(selected, sol, |
|
206 |
table) |
|
207 |
self.temp_tables[table] = [outputmap, sqlschema, False] |
|
208 |
return outputmap |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
209 |
|
0 | 210 |
def create_temp_table(self, table): |
211 |
"""create a temporary table to store result for the given rqlst""" |
|
212 |
if not self.temp_tables[table][-1]: |
|
213 |
sqlschema = self.temp_tables[table][1] |
|
214 |
self.syssource.create_temp_table(self.session, table, sqlschema) |
|
215 |
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
|
216 |
|
0 | 217 |
def preprocess(self, union, security=True): |
218 |
"""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
|
219 |
|
0 | 220 |
return rqlst to actually execute |
221 |
""" |
|
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
|
222 |
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
|
223 |
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
|
224 |
# 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
|
225 |
# 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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
else: |
8562
0d2fb4604265
[session] fix arguments default value and promote usage of security_enabled as session method. Closes #2481820
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8542
diff
changeset
|
240 |
with self.session.security_enabled(read=False): |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
241 |
noinvariant = self._insert_security(union) |
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
|
242 |
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
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
set_qdata(self.schema.rschema, union, noinvariant) |
0 | 250 |
if union.has_text_query: |
251 |
self.cache_key = None |
|
252 |
||
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
253 |
def _insert_security(self, union): |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
254 |
noinvariant = set() |
0 | 255 |
for select in union.children[:]: |
256 |
for subquery in select.with_: |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
257 |
self._insert_security(subquery.query) |
0 | 258 |
localchecks, restricted = self._check_permissions(select) |
259 |
if any(localchecks): |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
260 |
self.session.rql_rewriter.insert_local_checks( |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
261 |
select, self.args, localchecks, restricted, noinvariant) |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
262 |
return noinvariant |
0 | 263 |
|
264 |
def _check_permissions(self, rqlst): |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
265 |
"""Return a dict defining "local checks", i.e. RQLExpression defined in |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
266 |
the schema that should be inserted in the original query, together with |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
267 |
a set of variable names which requires some security to be inserted. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
268 |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
269 |
Solutions where a variable has a type which the user can't definitly |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
270 |
read are removed, else if the user *may* read it (i.e. if an rql |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
271 |
expression is defined for the "read" permission of the related type), |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
272 |
the local checks dict is updated. |
0 | 273 |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
274 |
The local checks dict has entries for each different local check |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
275 |
necessary, with associated solutions as value, a local check being |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
276 |
defined by a list of 2-uple (variable name, rql expressions) for each |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
277 |
variable which has to be checked. Solutions which don't require local |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
278 |
checks will be associated to the empty tuple key. |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
279 |
|
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
280 |
Note rqlst should not have been simplified at this point. |
0 | 281 |
""" |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
282 |
session = self.session |
0 | 283 |
msgs = [] |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
284 |
# dict(varname: eid), allowing to check rql expression for variables |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
285 |
# which have a known eid |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
286 |
varkwargs = {} |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
eid = var.stinfo['constnode'].eval(self.args) |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
291 |
varkwargs[var.name] = int(eid) |
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8127
diff
changeset
|
292 |
# dictionary of variables restricted for security reason |
0 | 293 |
localchecks = {} |
294 |
restricted_vars = set() |
|
295 |
newsolutions = [] |
|
296 |
for solution in rqlst.solutions: |
|
297 |
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
|
298 |
localcheck = check_read_access(session, rqlst, solution, self.args) |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8694
diff
changeset
|
299 |
except Unauthorized as ex: |
0 | 300 |
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
|
301 |
msg %= (solution, session.user.login, ex.args[0], ex.args[1]) |
0 | 302 |
msgs.append(msg) |
303 |
LOGGER.info(msg) |
|
304 |
else: |
|
305 |
newsolutions.append(solution) |
|
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
306 |
# 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
|
307 |
# are specified by eid in query'args |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
308 |
for varname, eid in varkwargs.iteritems(): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
309 |
try: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
310 |
rqlexprs = localcheck.pop(varname) |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
311 |
except KeyError: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
312 |
continue |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
313 |
# if entity has been added in the current transaction, the |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
314 |
# user can read it whatever rql expressions are associated |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
315 |
# to its type |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
316 |
if session.added_in_transaction(eid): |
4953
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
317 |
continue |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
318 |
for rqlexpr in rqlexprs: |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
319 |
if rqlexpr.check(session, eid): |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
320 |
break |
c8c0e10dbd97
[read security] minor optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4843
diff
changeset
|
321 |
else: |
8333
baa3ea5eac2b
querier: Improve error message when ERQLExpression fails on some entity (closes #2267081)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8238
diff
changeset
|
322 |
raise Unauthorized('No read acces on %r with eid %i.' % (var, eid)) |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
323 |
# mark variables protected by an rql expression |
0 | 324 |
restricted_vars.update(localcheck) |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
325 |
# turn local check into a dict key |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
326 |
localcheck = tuple(sorted(localcheck.iteritems())) |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
327 |
localchecks.setdefault(localcheck, []).append(solution) |
0 | 328 |
# raise Unautorized exception if the user can't access to any solution |
329 |
if not newsolutions: |
|
330 |
raise Unauthorized('\n'.join(msgs)) |
|
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
331 |
# if there is some message, solutions have been modified and must be |
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
332 |
# reconsidered by the syntax treee |
6128 | 333 |
if msgs: |
334 |
rqlst.set_possible_types(newsolutions) |
|
0 | 335 |
return localchecks, restricted_vars |
336 |
||
337 |
def finalize(self, select, solutions, insertedvars): |
|
338 |
rqlst = Union() |
|
339 |
rqlst.append(select) |
|
340 |
for mainvarname, rschema, newvarname in insertedvars: |
|
341 |
nvartype = str(rschema.objects(solutions[0][mainvarname])[0]) |
|
342 |
for sol in solutions: |
|
343 |
sol[newvarname] = nvartype |
|
344 |
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
|
345 |
add_types_restriction(self.schema, select) |
0 | 346 |
self.rqlhelper.annotate(rqlst) |
347 |
self.preprocess(rqlst, security=False) |
|
348 |
return rqlst |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
349 |
|
4759 | 350 |
|
0 | 351 |
class InsertPlan(ExecutionPlan): |
352 |
"""an execution model specific to the INSERT rql query |
|
353 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
354 |
|
0 | 355 |
def __init__(self, querier, rqlst, args, session): |
356 |
ExecutionPlan.__init__(self, querier, rqlst, args, session) |
|
357 |
# save originaly selected variable, we may modify this |
|
358 |
# dictionary for substitution (query parameters) |
|
359 |
self.selected = rqlst.selection |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
360 |
# list of rows of entities definition (ssplanner.EditedEntity) |
0 | 361 |
self.e_defs = [[]] |
362 |
# 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
|
363 |
self.r_defs = set() |
0 | 364 |
# indexes to track entity definitions bound to relation definitions |
365 |
self._r_subj_index = {} |
|
366 |
self._r_obj_index = {} |
|
367 |
self._expanded_r_defs = {} |
|
368 |
||
369 |
def add_entity_def(self, edef): |
|
370 |
"""add an entity definition to build""" |
|
371 |
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
|
372 |
|
0 | 373 |
def add_relation_def(self, rdef): |
374 |
"""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
|
375 |
self.r_defs.add(rdef) |
0 | 376 |
if not isinstance(rdef[0], int): |
377 |
self._r_subj_index.setdefault(rdef[0], []).append(rdef) |
|
378 |
if not isinstance(rdef[2], int): |
|
379 |
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
|
380 |
|
0 | 381 |
def substitute_entity_def(self, edef, edefs): |
382 |
"""substitute an incomplete entity definition by a list of complete |
|
383 |
equivalents |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
384 |
|
0 | 385 |
e.g. on queries such as :: |
386 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
387 |
WHERE U login 'admin', U login N |
|
388 |
||
389 |
X will be inserted as many times as U exists, and so the X travaille Y |
|
390 |
relations as to be added as many time as X is inserted |
|
391 |
""" |
|
392 |
if not edefs or not self.e_defs: |
|
393 |
# no result, no entity will be created |
|
394 |
self.e_defs = () |
|
395 |
return |
|
396 |
# first remove the incomplete entity definition |
|
397 |
colidx = self.e_defs[0].index(edef) |
|
398 |
for i, row in enumerate(self.e_defs[:]): |
|
399 |
self.e_defs[i][colidx] = edefs[0] |
|
400 |
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
|
401 |
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
|
402 |
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
|
403 |
if i != colidx] |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
404 |
row.insert(colidx, edef_) |
0 | 405 |
self.e_defs.append(row) |
406 |
# now, see if this entity def is referenced as subject in some relation |
|
407 |
# definition |
|
8697
574bb05e40a4
[toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8696
diff
changeset
|
408 |
if edef in self._r_subj_index: |
0 | 409 |
for rdef in self._r_subj_index[edef]: |
410 |
expanded = self._expanded(rdef) |
|
411 |
result = [] |
|
412 |
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
|
413 |
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
|
414 |
result.append( (edef_, exp_rdef[1], exp_rdef[2]) ) |
0 | 415 |
self._expanded_r_defs[rdef] = result |
416 |
# and finally, see if this entity def is referenced as object in some |
|
417 |
# relation definition |
|
8697
574bb05e40a4
[toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8696
diff
changeset
|
418 |
if edef in self._r_obj_index: |
0 | 419 |
for rdef in self._r_obj_index[edef]: |
420 |
expanded = self._expanded(rdef) |
|
421 |
result = [] |
|
422 |
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
|
423 |
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
|
424 |
result.append( (exp_rdef[0], exp_rdef[1], edef_) ) |
0 | 425 |
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
|
426 |
|
0 | 427 |
def _expanded(self, rdef): |
428 |
"""return expanded value for the given relation definition""" |
|
429 |
try: |
|
430 |
return self._expanded_r_defs[rdef] |
|
431 |
except KeyError: |
|
432 |
self.r_defs.remove(rdef) |
|
433 |
return [rdef] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
434 |
|
0 | 435 |
def relation_defs(self): |
436 |
"""return the list for relation definitions to insert""" |
|
8696
0bb18407c053
[toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
437 |
for rdefs in self._expanded_r_defs.itervalues(): |
0 | 438 |
for rdef in rdefs: |
439 |
yield rdef |
|
440 |
for rdef in self.r_defs: |
|
441 |
yield rdef |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
442 |
|
0 | 443 |
def insert_entity_defs(self): |
444 |
"""return eids of inserted entities in a suitable form for the resulting |
|
445 |
result set, e.g.: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
446 |
|
0 | 447 |
e.g. on queries such as :: |
448 |
INSERT Personne X, Societe Y: X nom N, Y nom 'toto', X travaille Y |
|
449 |
WHERE U login 'admin', U login N |
|
450 |
||
451 |
if there is two entities matching U, the result set will look like |
|
452 |
[(eidX1, eidY1), (eidX2, eidY2)] |
|
453 |
""" |
|
454 |
session = self.session |
|
455 |
repo = session.repo |
|
456 |
results = [] |
|
457 |
for row in self.e_defs: |
|
458 |
results.append([repo.glob_add_entity(session, edef) |
|
459 |
for edef in row]) |
|
460 |
return results |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
461 |
|
0 | 462 |
def insert_relation_defs(self): |
463 |
session = self.session |
|
464 |
repo = session.repo |
|
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
465 |
edited_entities = {} |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
466 |
relations = {} |
0 | 467 |
for subj, rtype, obj in self.relation_defs(): |
468 |
# if a string is given into args instead of an int, we get it here |
|
469 |
if isinstance(subj, basestring): |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
470 |
subj = int(subj) |
0 | 471 |
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
|
472 |
subj = subj.entity.eid |
0 | 473 |
if isinstance(obj, basestring): |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8697
diff
changeset
|
474 |
obj = int(obj) |
0 | 475 |
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
|
476 |
obj = obj.entity.eid |
0 | 477 |
if repo.schema.rschema(rtype).inlined: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
478 |
if subj not in edited_entities: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
479 |
entity = session.entity_from_eid(subj) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
480 |
edited = EditedEntity(entity) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
481 |
edited_entities[subj] = edited |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
482 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
483 |
edited = edited_entities[subj] |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
484 |
edited.edited_attribute(rtype, obj) |
0 | 485 |
else: |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
486 |
if rtype in relations: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
487 |
relations[rtype].append((subj, obj)) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
488 |
else: |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
489 |
relations[rtype] = [(subj, obj)] |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
490 |
repo.glob_add_relations(session, relations) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
491 |
for edited in edited_entities.itervalues(): |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7139
diff
changeset
|
492 |
repo.glob_update_entity(session, edited) |
0 | 493 |
|
494 |
||
495 |
class QuerierHelper(object): |
|
496 |
"""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
|
497 |
|
0 | 498 |
def __init__(self, repo, schema): |
499 |
# system info helper |
|
500 |
self._repo = repo |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
501 |
# instance schema |
0 | 502 |
self.set_schema(schema) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
503 |
|
0 | 504 |
def set_schema(self, schema): |
505 |
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
|
506 |
repo = self._repo |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
507 |
# rql st and solution cache. |
7954
a3d3220669d6
[cache] replace lgc.cache with something more appropriate (closes #1921713)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7779
diff
changeset
|
508 |
self._rql_cache = QueryCache(repo.config['rql-cache-size']) |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
509 |
# rql cache key cache. Don't bother using a Cache instance: we should |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
510 |
# have a limited number of queries in there, since there are no entries |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
511 |
# in this cache for user queries (which have no args) |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
512 |
self._rql_ck_cache = {} |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
513 |
# 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
|
514 |
self.cache_hit, self.cache_miss = 0, 0 |
0 | 515 |
# 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
|
516 |
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
|
517 |
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
|
518 |
# 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
|
519 |
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
|
520 |
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
|
521 |
self._annotate = rqlhelper.annotate |
0 | 522 |
# rql planner |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
523 |
if len(repo.sources) < 2: |
0 | 524 |
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
|
525 |
self._planner = SSPlanner(schema, rqlhelper) |
0 | 526 |
else: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
527 |
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
|
528 |
self._planner = MSPlanner(schema, rqlhelper) |
0 | 529 |
# sql generation annotator |
530 |
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
|
531 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
532 |
def set_planner(self): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
533 |
if len(self._repo.sources) < 2: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
534 |
from cubicweb.server.ssplanner import SSPlanner |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
535 |
self._planner = SSPlanner(self.schema, self._repo.vreg.rqlhelper) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
536 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
537 |
from cubicweb.server.msplanner import MSPlanner |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
538 |
self._planner = MSPlanner(self.schema, self._repo.vreg.rqlhelper) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6401
diff
changeset
|
539 |
|
0 | 540 |
def parse(self, rql, annotate=False): |
541 |
"""return a rql syntax tree for the given rql""" |
|
542 |
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
|
543 |
return self._parse(unicode(rql), annotate=annotate) |
0 | 544 |
except UnicodeError: |
545 |
raise RQLSyntaxError(rql) |
|
546 |
||
547 |
def plan_factory(self, rqlst, args, session): |
|
548 |
"""create an execution plan for an INSERT RQL query""" |
|
549 |
if rqlst.TYPE == 'insert': |
|
550 |
return InsertPlan(self, rqlst, args, session) |
|
551 |
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
|
552 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
553 |
def execute(self, session, rql, args=None, build_descr=True): |
0 | 554 |
"""execute a rql query, return resulting rows and their description in |
555 |
a `ResultSet` object |
|
556 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
557 |
* `rql` should be an Unicode string or a plain ASCII string |
0 | 558 |
* `args` the optional parameters dictionary associated to the query |
559 |
* `build_descr` is a boolean flag indicating if the description should |
|
560 |
be built on select queries (if false, the description will be en empty |
|
561 |
list) |
|
562 |
||
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
563 |
on INSERT queries, there will be one row with the eid of each inserted |
0 | 564 |
entity |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
565 |
|
0 | 566 |
result for DELETE and SET queries is undefined yet |
567 |
||
568 |
to maximize the rql parsing/analyzing cache performance, you should |
|
4654
717310b3d576
docstring improvement
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4533
diff
changeset
|
569 |
always use substitute arguments in queries (i.e. avoid query such as |
0 | 570 |
'Any X WHERE X eid 123'!) |
571 |
""" |
|
2621
1b9d08840a0e
R [querier] debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2601
diff
changeset
|
572 |
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
|
573 |
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
|
574 |
print '*'*80 |
7779
3826d8480a68
[debug] use repr() in some place where you'll have a chance to get an unicode error if output encoding isn't properly detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
575 |
print 'querier input', repr(rql), repr(args) |
0 | 576 |
# parse the query and binds variables |
8127
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
577 |
cachekey = (rql,) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
578 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
579 |
if args: |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
580 |
# search for named args in query which are eids (hence |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
581 |
# influencing query's solutions) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
582 |
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
|
583 |
if eidkeys: |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
584 |
# if there are some, we need a better cache key, eg (rql + |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
585 |
# entity type of each eid) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
586 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
587 |
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
|
588 |
args, eidkeys) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
589 |
except UnknownEid: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
590 |
# 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
|
591 |
# 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
|
592 |
return empty_rset(rql, args) |
0 | 593 |
rqlst = self._rql_cache[cachekey] |
594 |
self.cache_hit += 1 |
|
595 |
except KeyError: |
|
596 |
self.cache_miss += 1 |
|
597 |
rqlst = self.parse(rql) |
|
598 |
try: |
|
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
599 |
# compute solutions for rqlst and return named args in query |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
600 |
# which are eids. Notice that if you may not need `eidkeys`, we |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
601 |
# have to compute solutions anyway (kept as annotation on the |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
602 |
# tree) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5082
diff
changeset
|
603 |
eidkeys = self.solutions(session, rqlst, args) |
0 | 604 |
except UnknownEid: |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
605 |
# 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
|
606 |
# empty result instead of raising UnknownEid |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
607 |
return empty_rset(rql, args, rqlst) |
8127
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
608 |
if args and rql not in self._rql_ck_cache: |
7027
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
609 |
self._rql_ck_cache[rql] = eidkeys |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
610 |
if eidkeys: |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
611 |
cachekey = self._repo.querier_cache_key(session, rql, args, |
d3d7ff6ab54c
[repo querier] fix rqlst cache no limit issue, comment the code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
612 |
eidkeys) |
0 | 613 |
self._rql_cache[cachekey] = rqlst |
614 |
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
|
615 |
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
|
616 |
if session.read_security: |
0 | 617 |
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
|
618 |
# 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
|
619 |
# be released until commit/rollback |
0 | 620 |
session.mode = 'write' |
621 |
cachekey = None |
|
622 |
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
|
623 |
if session.read_security: |
0 | 624 |
for select in rqlst.children: |
625 |
check_no_password_selected(select) |
|
626 |
# on select query, always copy the cached rqlst so we don't have to |
|
627 |
# bother modifying it. This is not necessary on write queries since |
|
628 |
# a new syntax tree is built from them. |
|
629 |
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
|
630 |
self._annotate(rqlst) |
8127
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
631 |
if args: |
8342
7a5271182ef0
[rql annotation] fix has_text_query detection (if coming from sub-query and if has_text on a column aliases. Closes #2275322
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8333
diff
changeset
|
632 |
# different SQL generated when some argument is None or not (IS |
8127
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
633 |
# NULL). This should be considered when computing sql cache key |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8867
diff
changeset
|
634 |
cachekey += tuple(sorted([k for k, v in args.iteritems() |
8127
96d343a5e01b
[rql2sql] None for attributes in kwargs generate IS NULL, so should be considered in sql cache key. Closes #2116693
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7954
diff
changeset
|
635 |
if v is None])) |
0 | 636 |
# make an execution plan |
637 |
plan = self.plan_factory(rqlst, args, session) |
|
638 |
plan.cache_key = cachekey |
|
639 |
self._planner.build_plan(plan) |
|
640 |
# execute the plan |
|
641 |
try: |
|
642 |
results = plan.execute() |
|
6361
843684a50e48
[transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
643 |
except (Unauthorized, ValidationError): |
843684a50e48
[transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6129
diff
changeset
|
644 |
# getting an Unauthorized/ValidationError exception means the |
9267
24d9b86dfa54
spelling: rollbacked -> rolled back
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9167
diff
changeset
|
645 |
# transaction must be rolled back |
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
646 |
# |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
647 |
# notes: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
648 |
# * we should not reset the connections set here, since we don't want the |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7237
diff
changeset
|
649 |
# session to loose it during processing |
4532
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
650 |
# * 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
|
651 |
# by the session |
85116e75f561
fix auto-rollback on unauthorized error:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
652 |
if session.commit_state is None: |
6385
9f91d09ee5fa
[repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6361
diff
changeset
|
653 |
session.commit_state = 'uncommitable' |
0 | 654 |
raise |
655 |
# build a description for the results if necessary |
|
656 |
descr = () |
|
657 |
if build_descr: |
|
658 |
if rqlst.TYPE == 'select': |
|
659 |
# sample selection |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
660 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
661 |
# easy, all lines are identical |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
662 |
selected = rqlst.children[0].selection |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
663 |
solution = rqlst.children[0].solutions[0] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
664 |
description = _make_description(selected, args, solution) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
665 |
descr = RepeatList(len(results), tuple(description)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
666 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
667 |
# hard, delegate the work :o) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
668 |
descr = manual_build_descr(session, rqlst, args, results) |
0 | 669 |
elif rqlst.TYPE == 'insert': |
670 |
# on insert plan, some entities may have been auto-casted, |
|
671 |
# so compute description manually even if there is only |
|
672 |
# one solution |
|
673 |
basedescr = [None] * len(plan.selected) |
|
674 |
todetermine = zip(xrange(len(plan.selected)), repeat(False)) |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
675 |
descr = _build_descr(session, results, basedescr, todetermine) |
0 | 676 |
# FIXME: get number of affected entities / relations on non |
677 |
# selection queries ? |
|
678 |
# 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
|
679 |
return ResultSet(results, rql, args, descr, orig_rqlst) |
0 | 680 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
681 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
682 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
683 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7027
diff
changeset
|
684 |
|
0 | 685 |
from logging import getLogger |
686 |
from cubicweb import set_log_methods |
|
687 |
LOGGER = getLogger('cubicweb.querier') |
|
688 |
set_log_methods(QuerierHelper, LOGGER) |
|
8542
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
689 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
690 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
691 |
def manual_build_descr(tx, rqlst, args, result): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
692 |
"""build a description for a given result by analysing each row |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
693 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
694 |
XXX could probably be done more efficiently during execution of query |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
695 |
""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
696 |
# not so easy, looks for variable which changes from one solution |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
697 |
# to another |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
698 |
unstables = rqlst.get_variable_indices() |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
699 |
basedescr = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
700 |
todetermine = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
701 |
for i in xrange(len(rqlst.children[0].selection)): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
702 |
ttype = _selection_idx_type(i, rqlst, args) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
703 |
if ttype is None or ttype == 'Any': |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
704 |
ttype = None |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
705 |
isfinal = True |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
706 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
707 |
isfinal = ttype in BASE_TYPES |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
708 |
if ttype is None or i in unstables: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
709 |
basedescr.append(None) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
710 |
todetermine.append( (i, isfinal) ) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
711 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
712 |
basedescr.append(ttype) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
713 |
if not todetermine: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
714 |
return RepeatList(len(result), tuple(basedescr)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
715 |
return _build_descr(tx, result, basedescr, todetermine) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
716 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
717 |
def _build_descr(tx, result, basedescription, todetermine): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
718 |
description = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
719 |
etype_from_eid = tx.describe |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
720 |
todel = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
721 |
for i, row in enumerate(result): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
722 |
row_descr = basedescription[:] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
723 |
for index, isfinal in todetermine: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
724 |
value = row[index] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
725 |
if value is None: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
726 |
# None value inserted by an outer join, no type |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
727 |
row_descr[index] = None |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
728 |
continue |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
729 |
if isfinal: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
730 |
row_descr[index] = etype_from_pyobj(value) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
731 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
732 |
try: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
733 |
row_descr[index] = etype_from_eid(value)[0] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
734 |
except UnknownEid: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
735 |
tx.error('wrong eid %s in repository, you should ' |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
736 |
'db-check the database' % value) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
737 |
todel.append(i) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
738 |
break |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
739 |
else: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
740 |
description.append(tuple(row_descr)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
741 |
for i in reversed(todel): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
742 |
del result[i] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
743 |
return description |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
744 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
745 |
def _make_description(selected, args, solution): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
746 |
"""return a description for a result set""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
747 |
description = [] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
748 |
for term in selected: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
749 |
description.append(term.get_type(solution, args)) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
750 |
return description |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
751 |
|
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
752 |
def _selection_idx_type(i, rqlst, args): |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
753 |
"""try to return type of term at index `i` of the rqlst's selection""" |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
754 |
for select in rqlst.children: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
755 |
term = select.selection[i] |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
756 |
for solution in select.solutions: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
757 |
try: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
758 |
ttype = term.get_type(solution, args) |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
759 |
if ttype is not None: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
760 |
return ttype |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
761 |
except CoercionError: |
7e264ce34cd4
[session / querier] reorganize code to building result set descriptions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8342
diff
changeset
|
762 |
return None |