author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 20 Sep 2011 09:20:41 +0200 | |
changeset 7812 | 6806a7db9732 |
parent 7555 | c3bf459268d7 |
child 7843 | 3b51806da60b |
permissions | -rw-r--r-- |
7138
9aba650eea6b
[rql rewriter] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7137
diff
changeset
|
1 |
# copyright 2003-2011 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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
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:
4908
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
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:
1977
diff
changeset
|
18 |
"""RQL rewriting utilities : insert rql expression snippets into rql syntax |
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:
1977
diff
changeset
|
19 |
tree. |
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:
1977
diff
changeset
|
20 |
|
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:
1977
diff
changeset
|
21 |
This is used for instance for read security checking in the repository. |
5992 | 22 |
""" |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
23 |
from __future__ import with_statement |
0 | 24 |
|
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:
1977
diff
changeset
|
25 |
__docformat__ = "restructuredtext en" |
0 | 26 |
|
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:
1977
diff
changeset
|
27 |
from rql import nodes as n, stmts, TypeResolverException |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
28 |
from rql.utils import common_parent |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
29 |
|
4908
b3ad329cbe17
[rql rewrite] until a better solution is found raise BadSchemaDefinition when two inlined relations with security on an optional variable is used, explaining how to bypass it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4907
diff
changeset
|
30 |
from yams import BadSchemaDefinition |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
31 |
|
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
32 |
from logilab.common import tempattr |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
33 |
from logilab.common.graph import has_path |
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:
1977
diff
changeset
|
34 |
|
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
35 |
from cubicweb import Unauthorized, typed_eid |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
36 |
|
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
37 |
|
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
38 |
def add_types_restriction(schema, rqlst, newroot=None, solutions=None): |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
39 |
if newroot is None: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
40 |
assert solutions is None |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
41 |
if hasattr(rqlst, '_types_restr_added'): |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
42 |
return |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
43 |
solutions = rqlst.solutions |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
44 |
newroot = rqlst |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
45 |
rqlst._types_restr_added = True |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
46 |
else: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
47 |
assert solutions is not None |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
48 |
rqlst = rqlst.stmt |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
49 |
eschema = schema.eschema |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
50 |
allpossibletypes = {} |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
51 |
for solution in solutions: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
52 |
for varname, etype in solution.iteritems(): |
6123
0d0a87e88281
[querier] add_types_restriction_cleanups: remove useless try/except (we already filtered out variables not in newroot.defined_vars) and dict accss. Also systematically set computed solutions instead of modifying existing ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5992
diff
changeset
|
53 |
# XXX not considering aliases by design, right ? |
0d0a87e88281
[querier] add_types_restriction_cleanups: remove useless try/except (we already filtered out variables not in newroot.defined_vars) and dict accss. Also systematically set computed solutions instead of modifying existing ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5992
diff
changeset
|
54 |
if varname not in newroot.defined_vars or eschema(etype).final: |
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
55 |
continue |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
56 |
allpossibletypes.setdefault(varname, set()).add(etype) |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
57 |
for varname in sorted(allpossibletypes): |
6123
0d0a87e88281
[querier] add_types_restriction_cleanups: remove useless try/except (we already filtered out variables not in newroot.defined_vars) and dict accss. Also systematically set computed solutions instead of modifying existing ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5992
diff
changeset
|
58 |
var = newroot.defined_vars[varname] |
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
59 |
stinfo = var.stinfo |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4908
diff
changeset
|
60 |
if stinfo.get('uidrel') is not None: |
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
61 |
continue # eid specified, no need for additional type specification |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
62 |
try: |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4908
diff
changeset
|
63 |
typerel = rqlst.defined_vars[varname].stinfo.get('typerel') |
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
64 |
except KeyError: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
65 |
assert varname in rqlst.aliases |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
66 |
continue |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4908
diff
changeset
|
67 |
if newroot is rqlst and typerel is not None: |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4908
diff
changeset
|
68 |
mytyperel = typerel |
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
69 |
else: |
6123
0d0a87e88281
[querier] add_types_restriction_cleanups: remove useless try/except (we already filtered out variables not in newroot.defined_vars) and dict accss. Also systematically set computed solutions instead of modifying existing ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5992
diff
changeset
|
70 |
for vref in var.references(): |
3437
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
71 |
rel = vref.relation() |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
72 |
if rel and rel.is_types_restriction(): |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
73 |
mytyperel = rel |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
74 |
break |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
75 |
else: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
76 |
mytyperel = None |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
77 |
possibletypes = allpossibletypes[varname] |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
78 |
if mytyperel is not None: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
79 |
# variable as already some types restriction. new possible types |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
80 |
# can only be a subset of existing ones, so only remove no more |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
81 |
# possible types |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
82 |
for cst in mytyperel.get_nodes(n.Constant): |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
83 |
if not cst.value in possibletypes: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
84 |
cst.parent.remove(cst) |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
85 |
else: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
86 |
# we have to add types restriction |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
87 |
if stinfo.get('scope') is not None: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
88 |
rel = var.scope.add_type_restriction(var, possibletypes) |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
89 |
else: |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
90 |
# tree is not annotated yet, no scope set so add the restriction |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
91 |
# to the root |
a30b5b5138a4
cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3254
diff
changeset
|
92 |
rel = newroot.add_type_restriction(var, possibletypes) |
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4908
diff
changeset
|
93 |
stinfo['typerel'] = rel |
6123
0d0a87e88281
[querier] add_types_restriction_cleanups: remove useless try/except (we already filtered out variables not in newroot.defined_vars) and dict accss. Also systematically set computed solutions instead of modifying existing ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5992
diff
changeset
|
94 |
stinfo['possibletypes'] = possibletypes |
0 | 95 |
|
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:
1977
diff
changeset
|
96 |
|
0 | 97 |
def remove_solutions(origsolutions, solutions, defined): |
98 |
"""when a rqlst has been generated from another by introducing security |
|
99 |
assertions, this method returns solutions which are contained in orig |
|
100 |
solutions |
|
101 |
""" |
|
102 |
newsolutions = [] |
|
103 |
for origsol in origsolutions: |
|
104 |
for newsol in solutions[:]: |
|
105 |
for var, etype in origsol.items(): |
|
106 |
try: |
|
107 |
if newsol[var] != etype: |
|
108 |
try: |
|
109 |
defined[var].stinfo['possibletypes'].remove(newsol[var]) |
|
110 |
except KeyError: |
|
111 |
pass |
|
112 |
break |
|
1132 | 113 |
except KeyError: |
0 | 114 |
# variable has been rewritten |
115 |
continue |
|
116 |
else: |
|
117 |
newsolutions.append(newsol) |
|
118 |
solutions.remove(newsol) |
|
119 |
return newsolutions |
|
120 |
||
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:
1977
diff
changeset
|
121 |
|
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
122 |
class Unsupported(Exception): |
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
123 |
"""raised when an rql expression can't be inserted in some rql query |
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
124 |
because it create an unresolvable query (eg no solutions found) |
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
125 |
""" |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
126 |
|
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:
1977
diff
changeset
|
127 |
|
0 | 128 |
class RQLRewriter(object): |
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:
1977
diff
changeset
|
129 |
"""insert some rql snippets into another rql syntax tree |
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:
1977
diff
changeset
|
130 |
|
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:
1977
diff
changeset
|
131 |
this class *isn't thread safe* |
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:
1977
diff
changeset
|
132 |
""" |
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:
1977
diff
changeset
|
133 |
|
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:
1977
diff
changeset
|
134 |
def __init__(self, session): |
0 | 135 |
self.session = session |
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:
1977
diff
changeset
|
136 |
vreg = session.vreg |
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:
1977
diff
changeset
|
137 |
self.schema = vreg.schema |
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:
1977
diff
changeset
|
138 |
self.annotate = vreg.rqlhelper.annotate |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
139 |
self._compute_solutions = vreg.solutions |
0 | 140 |
|
141 |
def compute_solutions(self): |
|
142 |
self.annotate(self.select) |
|
143 |
try: |
|
144 |
self._compute_solutions(self.session, self.select, self.kwargs) |
|
145 |
except TypeResolverException: |
|
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:
1977
diff
changeset
|
146 |
raise Unsupported(str(self.select)) |
0 | 147 |
if len(self.select.solutions) < len(self.solutions): |
148 |
raise Unsupported() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
149 |
|
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
150 |
def rewrite(self, select, snippets, solutions, kwargs, existingvars=None): |
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:
1977
diff
changeset
|
151 |
""" |
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:
1977
diff
changeset
|
152 |
snippets: (varmap, list of rql expression) |
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:
1977
diff
changeset
|
153 |
with varmap a *tuple* (select var, snippet var) |
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:
1977
diff
changeset
|
154 |
""" |
5582
3e133b29a1a4
[rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
155 |
self.select = select |
0 | 156 |
self.solutions = solutions |
157 |
self.kwargs = kwargs |
|
158 |
self.u_varname = None |
|
159 |
self.removing_ambiguity = False |
|
160 |
self.exists_snippet = {} |
|
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:
1977
diff
changeset
|
161 |
self.pending_keys = [] |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
162 |
self.existingvars = existingvars |
0 | 163 |
# we have to annotate the rqlst before inserting snippets, even though |
164 |
# we'll have to redo it latter |
|
165 |
self.annotate(select) |
|
166 |
self.insert_snippets(snippets) |
|
167 |
if not self.exists_snippet and self.u_varname: |
|
168 |
# U has been inserted than cancelled, cleanup |
|
169 |
select.undefine_variable(select.defined_vars[self.u_varname]) |
|
170 |
# clean solutions according to initial solutions |
|
171 |
newsolutions = remove_solutions(solutions, select.solutions, |
|
172 |
select.defined_vars) |
|
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:
1977
diff
changeset
|
173 |
assert len(newsolutions) >= len(solutions), ( |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
174 |
'rewritten rql %s has lost some solutions, there is probably ' |
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:
1977
diff
changeset
|
175 |
'something wrong in your schema permission (for instance using a ' |
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:
1977
diff
changeset
|
176 |
'RQLExpression which insert a relation which doesn\'t exists in ' |
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:
1977
diff
changeset
|
177 |
'the schema)\nOrig solutions: %s\nnew solutions: %s' % ( |
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:
1977
diff
changeset
|
178 |
select, solutions, newsolutions)) |
0 | 179 |
if len(newsolutions) > len(solutions): |
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:
1977
diff
changeset
|
180 |
newsolutions = self.remove_ambiguities(snippets, newsolutions) |
0 | 181 |
select.solutions = newsolutions |
182 |
add_types_restriction(self.schema, select) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
183 |
|
0 | 184 |
def insert_snippets(self, snippets, varexistsmap=None): |
185 |
self.rewritten = {} |
|
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:
1977
diff
changeset
|
186 |
for varmap, rqlexprs in snippets: |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
187 |
if isinstance(varmap, dict): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
188 |
varmap = tuple(sorted(varmap.items())) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
189 |
else: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
190 |
assert isinstance(varmap, tuple), varmap |
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:
1977
diff
changeset
|
191 |
if varexistsmap is not None and not varmap in varexistsmap: |
0 | 192 |
continue |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
193 |
self.insert_varmap_snippets(varmap, rqlexprs, varexistsmap) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
194 |
|
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
195 |
def insert_varmap_snippets(self, varmap, rqlexprs, varexistsmap): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
196 |
self.varmap = varmap |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
197 |
self.revvarmap = {} |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
198 |
self.varinfos = [] |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
199 |
self._insert_scope = None |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
200 |
for i, (selectvar, snippetvar) in enumerate(varmap): |
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:
1977
diff
changeset
|
201 |
assert snippetvar in 'SOX' |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
202 |
self.revvarmap[snippetvar] = (selectvar, i) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
203 |
vi = {} |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
204 |
self.varinfos.append(vi) |
0 | 205 |
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:
1977
diff
changeset
|
206 |
vi['const'] = typed_eid(selectvar) # XXX gae |
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:
1977
diff
changeset
|
207 |
vi['rhs_rels'] = vi['lhs_rels'] = {} |
0 | 208 |
except ValueError: |
4906
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
209 |
try: |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
210 |
vi['stinfo'] = sti = self.select.defined_vars[selectvar].stinfo |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
211 |
except KeyError: |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
212 |
# variable may have been moved to a newly inserted subquery |
4906
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
213 |
# we should insert snippet in that subquery |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
214 |
subquery = self.select.aliases[selectvar].query |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
215 |
assert len(subquery.children) == 1 |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
216 |
subselect = subquery.children[0] |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
217 |
RQLRewriter(self.session).rewrite(subselect, [(varmap, rqlexprs)], |
9a50539f01d1
[rql rewriting] handle case where we want to insert snippet on a variable that has previously been moved to a subquery
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
218 |
subselect.solutions, self.kwargs) |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
219 |
return |
0 | 220 |
if varexistsmap is None: |
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:
1977
diff
changeset
|
221 |
vi['rhs_rels'] = dict( (r.r_type, r) for r in sti['rhsrelations']) |
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:
1977
diff
changeset
|
222 |
vi['lhs_rels'] = dict( (r.r_type, r) for r in sti['relations'] |
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:
1977
diff
changeset
|
223 |
if not r in sti['rhsrelations']) |
0 | 224 |
else: |
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:
1977
diff
changeset
|
225 |
vi['rhs_rels'] = vi['lhs_rels'] = {} |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
226 |
parent = None |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
227 |
inserted = False |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
228 |
for rqlexpr in rqlexprs: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
229 |
self.current_expr = rqlexpr |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
230 |
if varexistsmap is None: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
231 |
try: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
232 |
new = self.insert_snippet(varmap, rqlexpr.snippet_rqlst, parent) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
233 |
except Unsupported: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
234 |
continue |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
235 |
inserted = True |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
236 |
if new is not None and self._insert_scope is None: |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
237 |
self.exists_snippet[rqlexpr] = new |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
238 |
parent = parent or new |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
239 |
else: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
240 |
# called to reintroduce snippet due to ambiguity creation, |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
241 |
# so skip snippets which are not introducing this ambiguity |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
242 |
exists = varexistsmap[varmap] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
243 |
if self.exists_snippet[rqlexpr] is exists: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
244 |
self.insert_snippet(varmap, rqlexpr.snippet_rqlst, exists) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
245 |
if varexistsmap is None and not inserted: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
246 |
# no rql expression found matching rql solutions. User has no access right |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
247 |
raise Unauthorized() # XXX bad constraint when inserting constraints |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
248 |
|
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:
1977
diff
changeset
|
249 |
def insert_snippet(self, varmap, snippetrqlst, parent=None): |
0 | 250 |
new = snippetrqlst.where.accept(self) |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
251 |
existing = self.existingvars |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
252 |
self.existingvars = None |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
253 |
try: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
254 |
return self._insert_snippet(varmap, parent, new) |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
255 |
finally: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
256 |
self.existingvars = existing |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
257 |
|
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
258 |
def _insert_snippet(self, varmap, parent, new): |
0 | 259 |
if new is not None: |
5582
3e133b29a1a4
[rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
260 |
if self._insert_scope is None: |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
261 |
insert_scope = None |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
262 |
for vi in self.varinfos: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
263 |
scope = vi.get('stinfo', {}).get('scope', self.select) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
264 |
if insert_scope is None: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
265 |
insert_scope = scope |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
266 |
else: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
267 |
insert_scope = common_parent(scope, insert_scope) |
5582
3e133b29a1a4
[rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
268 |
else: |
3e133b29a1a4
[rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
269 |
insert_scope = self._insert_scope |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
270 |
if self._insert_scope is None and any(vi.get('stinfo', {}).get('optrelations') |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
271 |
for vi in self.varinfos): |
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:
1977
diff
changeset
|
272 |
assert parent is None |
5582
3e133b29a1a4
[rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
273 |
self._insert_scope = self.snippet_subquery(varmap, new) |
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:
1977
diff
changeset
|
274 |
self.insert_pending() |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
275 |
#self._insert_scope = None |
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:
1977
diff
changeset
|
276 |
return |
7137
01b23ec9c066
[rql rewriter] don't add unnecessary (potentially buggy) exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6123
diff
changeset
|
277 |
if not isinstance(new, (n.Exists, n.Not)): |
01b23ec9c066
[rql rewriter] don't add unnecessary (potentially buggy) exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6123
diff
changeset
|
278 |
new = n.Exists(new) |
0 | 279 |
if parent is None: |
5582
3e133b29a1a4
[rql2sql] follow rql 0.26.1 changes: NOT nodes normalization, allowing simplification of sql generation, and fix #XXX
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
280 |
insert_scope.add_restriction(new) |
0 | 281 |
else: |
282 |
grandpa = parent.parent |
|
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:
1977
diff
changeset
|
283 |
or_ = n.Or(parent, new) |
0 | 284 |
grandpa.replace(parent, or_) |
285 |
if not self.removing_ambiguity: |
|
286 |
try: |
|
287 |
self.compute_solutions() |
|
288 |
except Unsupported: |
|
289 |
# some solutions have been lost, can't apply this rql expr |
|
290 |
if parent is None: |
|
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
291 |
self.current_statement().remove_node(new, undefine=True) |
0 | 292 |
else: |
293 |
parent.parent.replace(or_, or_.children[0]) |
|
294 |
self._cleanup_inserted(new) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
295 |
raise |
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:
1977
diff
changeset
|
296 |
else: |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
297 |
with tempattr(self, '_insert_scope', new): |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
298 |
self.insert_pending() |
0 | 299 |
return new |
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:
1977
diff
changeset
|
300 |
self.insert_pending() |
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:
1977
diff
changeset
|
301 |
|
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:
1977
diff
changeset
|
302 |
def insert_pending(self): |
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:
1977
diff
changeset
|
303 |
"""pending_keys hold variable referenced by U has_<action>_permission X |
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:
1977
diff
changeset
|
304 |
relation. |
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:
1977
diff
changeset
|
305 |
|
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:
1977
diff
changeset
|
306 |
Once the snippet introducing this has been inserted and solutions |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
307 |
recomputed, we have to insert snippet defined for <action> of entity |
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:
1977
diff
changeset
|
308 |
types taken by X |
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:
1977
diff
changeset
|
309 |
""" |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
310 |
stmt = self.current_statement() |
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:
1977
diff
changeset
|
311 |
while self.pending_keys: |
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:
1977
diff
changeset
|
312 |
key, action = self.pending_keys.pop() |
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:
1977
diff
changeset
|
313 |
try: |
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:
1977
diff
changeset
|
314 |
varname = self.rewritten[key] |
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:
1977
diff
changeset
|
315 |
except KeyError: |
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:
1977
diff
changeset
|
316 |
try: |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
317 |
varname = self.revvarmap[key[-1]][0] |
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:
1977
diff
changeset
|
318 |
except KeyError: |
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:
1977
diff
changeset
|
319 |
# variable isn't used anywhere else, we can't insert security |
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:
1977
diff
changeset
|
320 |
raise Unauthorized() |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
321 |
ptypes = stmt.defined_vars[varname].stinfo['possibletypes'] |
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:
1977
diff
changeset
|
322 |
if len(ptypes) > 1: |
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:
1977
diff
changeset
|
323 |
# XXX dunno how to handle this |
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:
1977
diff
changeset
|
324 |
self.session.error( |
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:
1977
diff
changeset
|
325 |
'cant check security of %s, ambigous type for %s in %s', |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
326 |
stmt, varname, key[0]) # key[0] == the rql expression |
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:
1977
diff
changeset
|
327 |
raise Unauthorized() |
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:
1977
diff
changeset
|
328 |
etype = iter(ptypes).next() |
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:
1977
diff
changeset
|
329 |
eschema = self.schema.eschema(etype) |
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:
1977
diff
changeset
|
330 |
if not eschema.has_perm(self.session, action): |
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:
1977
diff
changeset
|
331 |
rqlexprs = eschema.get_rqlexprs(action) |
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:
1977
diff
changeset
|
332 |
if not rqlexprs: |
3934
d9a29a1fbe43
bugfix typo in exception name
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3826
diff
changeset
|
333 |
raise Unauthorized() |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
334 |
self.insert_snippets([({varname: 'X'}, rqlexprs)]) |
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:
1977
diff
changeset
|
335 |
|
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:
1977
diff
changeset
|
336 |
def snippet_subquery(self, varmap, transformedsnippet): |
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:
1977
diff
changeset
|
337 |
"""introduce the given snippet in a subquery""" |
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:
1977
diff
changeset
|
338 |
subselect = stmts.Select() |
4907
e623afd49356
[rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4906
diff
changeset
|
339 |
snippetrqlst = n.Exists(transformedsnippet.copy(subselect)) |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
340 |
aliases = [] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
341 |
rels_done = set() |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
342 |
for i, (selectvar, snippetvar) in enumerate(varmap): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
343 |
subselectvar = subselect.get_variable(selectvar) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
344 |
subselect.append_selected(n.VariableRef(subselectvar)) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
345 |
aliases.append(selectvar) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
346 |
vi = self.varinfos[i] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
347 |
need_null_test = False |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
348 |
stinfo = vi['stinfo'] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
349 |
for rel in stinfo['relations']: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
350 |
if rel in rels_done: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
351 |
continue |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
352 |
rels_done.add(rel) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
353 |
rschema = self.schema.rschema(rel.r_type) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
354 |
if rschema.final or (rschema.inlined and |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
355 |
not rel in stinfo['rhsrelations']): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
356 |
rel.children[0].name = selectvar # XXX explain why |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
357 |
subselect.add_restriction(rel.copy(subselect)) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
358 |
for vref in rel.children[1].iget_nodes(n.VariableRef): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
359 |
if isinstance(vref.variable, n.ColumnAlias): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
360 |
# XXX could probably be handled by generating the |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
361 |
# subquery into the detected subquery |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
362 |
raise BadSchemaDefinition( |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
363 |
"cant insert security because of usage two inlined " |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
364 |
"relations in this query. You should probably at " |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
365 |
"least uninline %s" % rel.r_type) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
366 |
subselect.append_selected(vref.copy(subselect)) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
367 |
aliases.append(vref.name) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
368 |
self.select.remove_node(rel) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
369 |
# when some inlined relation has to be copied in the |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
370 |
# subquery, we need to test that either value is NULL or |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
371 |
# that the snippet condition is satisfied |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
372 |
if rschema.inlined and rel.optional: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
373 |
need_null_test = True |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
374 |
if need_null_test: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
375 |
snippetrqlst = n.Or( |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
376 |
n.make_relation(subselectvar, 'is', (None, None), n.Constant, |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
377 |
operator='='), |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
378 |
snippetrqlst) |
4907
e623afd49356
[rql rewriting] handle case where we've and optional inlined relation in the original query. Also, we should append EXISTS even in subquery to avoid inserting duplicates in results
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4906
diff
changeset
|
379 |
subselect.add_restriction(snippetrqlst) |
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:
1977
diff
changeset
|
380 |
if self.u_varname: |
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:
1977
diff
changeset
|
381 |
# generate an identifier for the substitution |
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:
1977
diff
changeset
|
382 |
argname = subselect.allocate_varname() |
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:
1977
diff
changeset
|
383 |
while argname in self.kwargs: |
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:
1977
diff
changeset
|
384 |
argname = subselect.allocate_varname() |
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:
1977
diff
changeset
|
385 |
subselect.add_constant_restriction(subselect.get_variable(self.u_varname), |
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:
1977
diff
changeset
|
386 |
'eid', unicode(argname), 'Substitute') |
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:
1977
diff
changeset
|
387 |
self.kwargs[argname] = self.session.user.eid |
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:
1977
diff
changeset
|
388 |
add_types_restriction(self.schema, subselect, subselect, |
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:
1977
diff
changeset
|
389 |
solutions=self.solutions) |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
390 |
myunion = stmts.Union() |
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:
1977
diff
changeset
|
391 |
myunion.append(subselect) |
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:
1977
diff
changeset
|
392 |
aliases = [n.VariableRef(self.select.get_variable(name, i)) |
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:
1977
diff
changeset
|
393 |
for i, name in enumerate(aliases)] |
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:
1977
diff
changeset
|
394 |
self.select.add_subquery(n.SubQuery(aliases, myunion), check=False) |
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:
1977
diff
changeset
|
395 |
self._cleanup_inserted(transformedsnippet) |
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:
1977
diff
changeset
|
396 |
try: |
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:
1977
diff
changeset
|
397 |
self.compute_solutions() |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
398 |
except Unsupported: |
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:
1977
diff
changeset
|
399 |
# some solutions have been lost, can't apply this rql expr |
4318
8a6356914bd0
fix #615848: undefined 'new' variable was refering to the newly introduced subquery / remove_subquery doesn't take an undefine argument (probable copy/paste from the remove_node call)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
400 |
self.select.remove_subquery(self.select.with_[-1]) |
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:
1977
diff
changeset
|
401 |
raise |
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:
1977
diff
changeset
|
402 |
return subselect |
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:
1977
diff
changeset
|
403 |
|
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:
1977
diff
changeset
|
404 |
def remove_ambiguities(self, snippets, newsolutions): |
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:
1977
diff
changeset
|
405 |
# the snippet has introduced some ambiguities, we have to resolve them |
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:
1977
diff
changeset
|
406 |
# "manually" |
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:
1977
diff
changeset
|
407 |
variantes = self.build_variantes(newsolutions) |
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:
1977
diff
changeset
|
408 |
# insert "is" where necessary |
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:
1977
diff
changeset
|
409 |
varexistsmap = {} |
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:
1977
diff
changeset
|
410 |
self.removing_ambiguity = True |
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:
1977
diff
changeset
|
411 |
for (erqlexpr, varmap, oldvarname), etype in variantes[0].iteritems(): |
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:
1977
diff
changeset
|
412 |
varname = self.rewritten[(erqlexpr, varmap, oldvarname)] |
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:
1977
diff
changeset
|
413 |
var = self.select.defined_vars[varname] |
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:
1977
diff
changeset
|
414 |
exists = var.references()[0].scope |
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:
1977
diff
changeset
|
415 |
exists.add_constant_restriction(var, 'is', etype, 'etype') |
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:
1977
diff
changeset
|
416 |
varexistsmap[varmap] = exists |
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:
1977
diff
changeset
|
417 |
# insert ORED exists where necessary |
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:
1977
diff
changeset
|
418 |
for variante in variantes[1:]: |
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:
1977
diff
changeset
|
419 |
self.insert_snippets(snippets, varexistsmap) |
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:
1977
diff
changeset
|
420 |
for key, etype in variante.iteritems(): |
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:
1977
diff
changeset
|
421 |
varname = self.rewritten[key] |
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:
1977
diff
changeset
|
422 |
try: |
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:
1977
diff
changeset
|
423 |
var = self.select.defined_vars[varname] |
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:
1977
diff
changeset
|
424 |
except KeyError: |
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:
1977
diff
changeset
|
425 |
# not a newly inserted variable |
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:
1977
diff
changeset
|
426 |
continue |
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:
1977
diff
changeset
|
427 |
exists = var.references()[0].scope |
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:
1977
diff
changeset
|
428 |
exists.add_constant_restriction(var, 'is', etype, 'etype') |
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:
1977
diff
changeset
|
429 |
# recompute solutions |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
430 |
#select.annotated = False # avoid assertion error |
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:
1977
diff
changeset
|
431 |
self.compute_solutions() |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
432 |
# clean solutions according to initial solutions |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
433 |
return remove_solutions(self.solutions, self.select.solutions, |
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
434 |
self.select.defined_vars) |
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:
1977
diff
changeset
|
435 |
|
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:
1977
diff
changeset
|
436 |
def build_variantes(self, newsolutions): |
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:
1977
diff
changeset
|
437 |
variantes = set() |
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:
1977
diff
changeset
|
438 |
for sol in newsolutions: |
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:
1977
diff
changeset
|
439 |
variante = [] |
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:
1977
diff
changeset
|
440 |
for key, newvar in self.rewritten.iteritems(): |
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:
1977
diff
changeset
|
441 |
variante.append( (key, sol[newvar]) ) |
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:
1977
diff
changeset
|
442 |
variantes.add(tuple(variante)) |
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:
1977
diff
changeset
|
443 |
# rebuild variantes as dict |
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:
1977
diff
changeset
|
444 |
variantes = [dict(variante) for variante in variantes] |
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:
1977
diff
changeset
|
445 |
# remove variable which have always the same type |
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:
1977
diff
changeset
|
446 |
for key in self.rewritten: |
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:
1977
diff
changeset
|
447 |
it = iter(variantes) |
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:
1977
diff
changeset
|
448 |
etype = it.next()[key] |
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:
1977
diff
changeset
|
449 |
for variante in it: |
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:
1977
diff
changeset
|
450 |
if variante[key] != etype: |
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:
1977
diff
changeset
|
451 |
break |
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:
1977
diff
changeset
|
452 |
else: |
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:
1977
diff
changeset
|
453 |
for variante in variantes: |
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:
1977
diff
changeset
|
454 |
del variante[key] |
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:
1977
diff
changeset
|
455 |
return variantes |
0 | 456 |
|
457 |
def _cleanup_inserted(self, node): |
|
458 |
# cleanup inserted variable references |
|
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:
1977
diff
changeset
|
459 |
for vref in node.iget_nodes(n.VariableRef): |
0 | 460 |
vref.unregister_reference() |
461 |
if not vref.variable.stinfo['references']: |
|
462 |
# no more references, undefine the variable |
|
463 |
del self.select.defined_vars[vref.name] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
464 |
|
7138
9aba650eea6b
[rql rewriter] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7137
diff
changeset
|
465 |
def _may_be_shared_with(self, sniprel, target): |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
466 |
"""if the snippet relation can be skipped to use a relation from the |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
467 |
original query, return that relation node |
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:
1977
diff
changeset
|
468 |
""" |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
469 |
rschema = self.schema.rschema(sniprel.r_type) |
7555
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
470 |
stmt = self.current_statement() |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
471 |
for vi in self.varinfos: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
472 |
try: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
473 |
if target == 'object': |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
474 |
orel = vi['lhs_rels'][sniprel.r_type] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
475 |
cardindex = 0 |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
476 |
ttypes_func = rschema.objects |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
477 |
rdef = rschema.rdef |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
478 |
else: # target == 'subject': |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
479 |
orel = vi['rhs_rels'][sniprel.r_type] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
480 |
cardindex = 1 |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
481 |
ttypes_func = rschema.subjects |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
482 |
rdef = lambda x, y: rschema.rdef(y, x) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
483 |
except KeyError: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
484 |
# may be raised by vi['xhs_rels'][sniprel.r_type] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
485 |
return None |
7555
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
486 |
# don't share if relation's statement is not the current statement |
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
487 |
if orel.stmt is not stmt: |
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
488 |
return None |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
489 |
# can't share neged relation or relations with different outer join |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
490 |
if (orel.neged(strict=True) or sniprel.neged(strict=True) |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
491 |
or (orel.optional and orel.optional != sniprel.optional)): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
492 |
return None |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
493 |
# if cardinality is in '?1', we can ignore the snippet relation and use |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
494 |
# variable from the original query |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
495 |
for etype in vi['stinfo']['possibletypes']: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
496 |
for ttype in ttypes_func(etype): |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
497 |
if rdef(etype, ttype).cardinality[cardindex] in '+*': |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
498 |
return None |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
499 |
break |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
500 |
return orel |
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:
1977
diff
changeset
|
501 |
|
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
502 |
def _use_orig_term(self, snippet_varname, term): |
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:
1977
diff
changeset
|
503 |
key = (self.current_expr, self.varmap, snippet_varname) |
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:
1977
diff
changeset
|
504 |
if key in self.rewritten: |
7555
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
505 |
stmt = self.current_statement() |
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
506 |
insertedvar = stmt.defined_vars.pop(self.rewritten[key]) |
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:
1977
diff
changeset
|
507 |
for inserted_vref in insertedvar.references(): |
7555
c3bf459268d7
[rqlrewrite] closes #1772135: 1. don't try to reuse a relation from another statement (eg because a subquery has been introduced) 2. _use_orig_term should consider the current statement
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7535
diff
changeset
|
508 |
inserted_vref.parent.replace(inserted_vref, term.copy(stmt)) |
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:
1977
diff
changeset
|
509 |
self.rewritten[key] = term.name |
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:
1977
diff
changeset
|
510 |
|
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:
1977
diff
changeset
|
511 |
def _get_varname_or_term(self, vname): |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
512 |
stmt = self.current_statement() |
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:
1977
diff
changeset
|
513 |
if vname == 'U': |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
514 |
stmt = self.select |
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:
1977
diff
changeset
|
515 |
if self.u_varname is None: |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
516 |
self.u_varname = stmt.allocate_varname() |
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:
1977
diff
changeset
|
517 |
# generate an identifier for the substitution |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
518 |
argname = stmt.allocate_varname() |
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:
1977
diff
changeset
|
519 |
while argname in self.kwargs: |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
520 |
argname = stmt.allocate_varname() |
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:
1977
diff
changeset
|
521 |
# insert "U eid %(u)s" |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
522 |
stmt.add_constant_restriction( |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
523 |
stmt.get_variable(self.u_varname), |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
524 |
'eid', unicode(argname), 'Substitute') |
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:
1977
diff
changeset
|
525 |
self.kwargs[argname] = self.session.user.eid |
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:
1977
diff
changeset
|
526 |
return self.u_varname |
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:
1977
diff
changeset
|
527 |
key = (self.current_expr, self.varmap, vname) |
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:
1977
diff
changeset
|
528 |
try: |
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:
1977
diff
changeset
|
529 |
return self.rewritten[key] |
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:
1977
diff
changeset
|
530 |
except KeyError: |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
531 |
self.rewritten[key] = newvname = stmt.allocate_varname() |
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:
1977
diff
changeset
|
532 |
return newvname |
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:
1977
diff
changeset
|
533 |
|
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:
1977
diff
changeset
|
534 |
# visitor methods ########################################################## |
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:
1977
diff
changeset
|
535 |
|
0 | 536 |
def _visit_binary(self, node, cls): |
537 |
newnode = cls() |
|
538 |
for c in node.children: |
|
539 |
new = c.accept(self) |
|
540 |
if new is None: |
|
541 |
continue |
|
542 |
newnode.append(new) |
|
543 |
if len(newnode.children) == 0: |
|
544 |
return None |
|
545 |
if len(newnode.children) == 1: |
|
546 |
return newnode.children[0] |
|
547 |
return newnode |
|
548 |
||
549 |
def _visit_unary(self, node, cls): |
|
550 |
newc = node.children[0].accept(self) |
|
551 |
if newc is None: |
|
552 |
return None |
|
553 |
newnode = cls() |
|
554 |
newnode.append(newc) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
555 |
return newnode |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
556 |
|
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:
1977
diff
changeset
|
557 |
def visit_and(self, node): |
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:
1977
diff
changeset
|
558 |
return self._visit_binary(node, n.And) |
0 | 559 |
|
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:
1977
diff
changeset
|
560 |
def visit_or(self, node): |
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:
1977
diff
changeset
|
561 |
return self._visit_binary(node, n.Or) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
562 |
|
0 | 563 |
def visit_not(self, node): |
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:
1977
diff
changeset
|
564 |
return self._visit_unary(node, n.Not) |
0 | 565 |
|
566 |
def visit_exists(self, node): |
|
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:
1977
diff
changeset
|
567 |
return self._visit_unary(node, n.Exists) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
568 |
|
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
569 |
def keep_var(self, varname): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
570 |
if varname in 'SO': |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
571 |
return varname in self.existingvars |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
572 |
if varname == 'U': |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
573 |
return True |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
574 |
vargraph = self.current_expr.vargraph |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
575 |
for existingvar in self.existingvars: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
576 |
#path = has_path(vargraph, varname, existingvar) |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
577 |
if has_path(vargraph, varname, existingvar): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
578 |
return True |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
579 |
# no path from this variable to an existing variable |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
580 |
return False |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
581 |
|
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:
1977
diff
changeset
|
582 |
def visit_relation(self, node): |
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:
1977
diff
changeset
|
583 |
lhs, rhs = node.get_variable_parts() |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
584 |
# remove relations where an unexistant variable and or a variable linked |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
585 |
# to an unexistant variable is used. |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
586 |
if self.existingvars: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
587 |
if not self.keep_var(lhs.name): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
588 |
return |
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:
1977
diff
changeset
|
589 |
if node.r_type in ('has_add_permission', 'has_update_permission', |
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:
1977
diff
changeset
|
590 |
'has_delete_permission', 'has_read_permission'): |
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:
1977
diff
changeset
|
591 |
assert lhs.name == 'U' |
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:
1977
diff
changeset
|
592 |
action = node.r_type.split('_')[1] |
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:
1977
diff
changeset
|
593 |
key = (self.current_expr, self.varmap, rhs.name) |
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:
1977
diff
changeset
|
594 |
self.pending_keys.append( (key, action) ) |
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:
1977
diff
changeset
|
595 |
return |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
596 |
if isinstance(rhs, n.VariableRef): |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
597 |
if self.existingvars and not self.keep_var(rhs.name): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
598 |
return |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
599 |
if lhs.name in self.revvarmap and rhs.name != 'U': |
7138
9aba650eea6b
[rql rewriter] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7137
diff
changeset
|
600 |
orel = self._may_be_shared_with(node, 'object') |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
601 |
if orel is not None: |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
602 |
self._use_orig_term(rhs.name, orel.children[1].children[0]) |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
603 |
return |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
604 |
elif rhs.name in self.revvarmap and lhs.name != 'U': |
7138
9aba650eea6b
[rql rewriter] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7137
diff
changeset
|
605 |
orel = self._may_be_shared_with(node, 'subject') |
3443
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
606 |
if orel is not None: |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
607 |
self._use_orig_term(lhs.name, orel.children[0]) |
34e451da9b5d
[security] test and fix/refactor optimization of optional varialbe when rewriting rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3437
diff
changeset
|
608 |
return |
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:
1977
diff
changeset
|
609 |
rel = n.Relation(node.r_type, node.optional) |
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:
1977
diff
changeset
|
610 |
for c in node.children: |
0 | 611 |
rel.append(c.accept(self)) |
612 |
return rel |
|
613 |
||
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:
1977
diff
changeset
|
614 |
def visit_comparison(self, node): |
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:
1977
diff
changeset
|
615 |
cmp_ = n.Comparison(node.operator) |
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:
1977
diff
changeset
|
616 |
for c in node.children: |
0 | 617 |
cmp_.append(c.accept(self)) |
618 |
return cmp_ |
|
619 |
||
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:
1977
diff
changeset
|
620 |
def visit_mathexpression(self, node): |
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:
1977
diff
changeset
|
621 |
cmp_ = n.MathExpression(node.operator) |
0 | 622 |
for c in cmp.children: |
623 |
cmp_.append(c.accept(self)) |
|
624 |
return cmp_ |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
625 |
|
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:
1977
diff
changeset
|
626 |
def visit_function(self, node): |
0 | 627 |
"""generate filter name for a function""" |
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:
1977
diff
changeset
|
628 |
function_ = n.Function(node.name) |
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:
1977
diff
changeset
|
629 |
for c in node.children: |
0 | 630 |
function_.append(c.accept(self)) |
631 |
return function_ |
|
632 |
||
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:
1977
diff
changeset
|
633 |
def visit_constant(self, node): |
0 | 634 |
"""generate filter name for a constant""" |
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:
1977
diff
changeset
|
635 |
return n.Constant(node.value, node.type) |
0 | 636 |
|
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:
1977
diff
changeset
|
637 |
def visit_variableref(self, node): |
0 | 638 |
"""get the sql name for a variable reference""" |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
639 |
stmt = self.current_statement() |
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:
1977
diff
changeset
|
640 |
if node.name in self.revvarmap: |
7139
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
641 |
selectvar, index = self.revvarmap[node.name] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
642 |
vi = self.varinfos[index] |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
643 |
if vi.get('const') is not None: |
20807d3d7cf6
[rql rewriter] to properly handle 'relation' rql expressions, rql rewriter must support multiple variables (eg S and O) at once to be given as varmap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7138
diff
changeset
|
644 |
return n.Constant(vi['const'], 'Int') # XXX gae |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
645 |
return n.VariableRef(stmt.get_variable(selectvar)) |
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:
1977
diff
changeset
|
646 |
vname_or_term = self._get_varname_or_term(node.name) |
0 | 647 |
if isinstance(vname_or_term, basestring): |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
648 |
return n.VariableRef(stmt.get_variable(vname_or_term)) |
0 | 649 |
# shared term |
7535
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
650 |
return vname_or_term.copy(stmt) |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
651 |
|
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
652 |
def current_statement(self): |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
653 |
if self._insert_scope is None: |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
654 |
return self.select |
d5725a89dac9
[rqlrewrite] test and fix rql snippets insertion when several snippets match an optional variable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7251
diff
changeset
|
655 |
return self._insert_scope.stmt |