rqlrewrite.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 11 Jun 2014 14:47:50 +0200
changeset 10361 46d72f33c428
parent 10249 e38b8d37c5d8
child 10588 fdaa0e4b7eaf
permissions -rw-r--r--
[session/transaction] removed unused transaction class Related to #3933480.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9593
48a84fb4f301 [rewrite] Fix crash when the main variable doesn't appear in the snippet's vargraph
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9358
diff changeset
     1
# copyright 2003-2014 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
5f9a9086c171 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5704
diff changeset
    22
"""
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
    23
__docformat__ = "restructuredtext en"
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
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
    26
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
    27
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
    28
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
    29
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
    30
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
    31
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
    32
8748
f5027f8d2478 drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
    33
from cubicweb import Unauthorized
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
    34
from cubicweb.schema import RRQLExpression
3437
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    35
9167
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    36
def cleanup_solutions(rqlst, solutions):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    37
    for sol in solutions:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    38
        for vname in list(sol):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    39
            if not (vname in rqlst.defined_vars or vname in rqlst.aliases):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    40
                del sol[vname]
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    41
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
    42
3437
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    43
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
    44
    if newroot is None:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    45
        assert solutions is None
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    46
        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
    47
            return
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    48
        solutions = rqlst.solutions
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    49
        newroot = rqlst
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    50
        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
    51
    else:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    52
        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
    53
        rqlst = rqlst.stmt
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    54
    eschema = schema.eschema
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    55
    allpossibletypes = {}
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    56
    for solution in solutions:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    57
        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
    58
            # 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
    59
            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
    60
                continue
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    61
            allpossibletypes.setdefault(varname, set()).add(etype)
8074
a7f76e322659 add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8056
diff changeset
    62
    # XXX could be factorized with add_etypes_restriction from rql 0.31
3437
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    63
    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
    64
        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
    65
        stinfo = var.stinfo
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    66
        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
    67
            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
    68
        try:
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    69
            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
    70
        except KeyError:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    71
            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
    72
            continue
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4908
diff changeset
    73
        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
    74
            mytyperel = typerel
3437
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    75
        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
    76
            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
    77
                rel = vref.relation()
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    78
                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
    79
                    mytyperel = rel
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    80
                    break
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    81
            else:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    82
                mytyperel = None
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    83
        possibletypes = allpossibletypes[varname]
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
    84
        if mytyperel is not None:
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    85
            if mytyperel.r_type == 'is_instance_of':
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    86
                # turn is_instance_of relation into a is relation since we've
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    87
                # all possible solutions and don't want to bother with
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    88
                # potential is_instance_of incompatibility
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    89
                mytyperel.r_type = 'is'
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    90
                if len(possibletypes) > 1:
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    91
                    node = n.Function('IN')
10249
e38b8d37c5d8 [rqlrewrite] sort possible types when turning is_instance_of into is
Julien Cristau <julien.cristau@logilab.fr>
parents: 9953
diff changeset
    92
                    for etype in sorted(possibletypes):
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    93
                        node.append(n.Constant(etype, 'etype'))
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    94
                else:
9358
1e0235478403 [rewriter] fix latent bug: arbitrary etype may be substituted when using is_instance_of type restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9189
diff changeset
    95
                    etype = iter(possibletypes).next()
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    96
                    node = n.Constant(etype, 'etype')
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    97
                comp = mytyperel.children[1]
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    98
                comp.replace(comp.children[0], node)
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
    99
            else:
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
   100
                # variable has already some strict types restriction. new
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
   101
                # possible types can only be a subset of existing ones, so only
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
   102
                # remove no more possible types
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
   103
                for cst in mytyperel.get_nodes(n.Constant):
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
   104
                    if not cst.value in possibletypes:
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8296
diff changeset
   105
                        cst.parent.remove(cst)
3437
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
   106
        else:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
   107
            # 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
   108
            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
   109
                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
   110
            else:
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
   111
                # 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
   112
                # to the root
a30b5b5138a4 cw.rqlrewrite shouldn't depend on cw.server
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3254
diff changeset
   113
                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
   114
            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
   115
        stinfo['possibletypes'] = possibletypes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
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
   117
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
def remove_solutions(origsolutions, solutions, defined):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
    """when a rqlst has been generated from another by introducing security
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
    assertions, this method returns solutions which are contained in orig
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    solutions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
    newsolutions = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    for origsol in origsolutions:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        for newsol in solutions[:]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            for var, etype in origsol.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
                    if newsol[var] != etype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
                        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
                            defined[var].stinfo['possibletypes'].remove(newsol[var])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
                        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
                            pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
                        break
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   134
                except KeyError:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
                    # variable has been rewritten
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
                newsolutions.append(newsol)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
                solutions.remove(newsol)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
    return newsolutions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
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
   142
9167
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   143
def _add_noinvariant(noinvariant, restricted, select, nbtrees):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   144
    # a variable can actually be invariant if it has not been restricted for
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   145
    # security reason or if security assertion hasn't modified the possible
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   146
    # solutions for the query
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   147
    for vname in restricted:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   148
        try:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   149
            var = select.defined_vars[vname]
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   150
        except KeyError:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   151
            # this is an alias
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   152
            continue
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   153
        if nbtrees != 1 or len(var.stinfo['possibletypes']) != 1:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   154
            noinvariant.add(var)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   155
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   156
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   157
def _expand_selection(terms, selected, aliases, select, newselect):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   158
    for term in terms:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   159
        for vref in term.iget_nodes(n.VariableRef):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   160
            if not vref.name in selected:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   161
                select.append_selected(vref)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   162
                colalias = newselect.get_variable(vref.name, len(aliases))
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   163
                aliases.append(n.VariableRef(colalias))
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   164
                selected.add(vref.name)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   165
9169
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   166
def _has_multiple_cardinality(etypes, rdef, ttypes_func, cardindex):
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   167
    """return True if relation definitions from entity types (`etypes`) to
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   168
    target types returned by the `ttypes_func` function all have single (1 or ?)
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   169
    cardinality.
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   170
    """
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   171
    for etype in etypes:
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   172
        for ttype in ttypes_func(etype):
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   173
            if rdef(etype, ttype).cardinality[cardindex] in '+*':
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   174
                return True
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   175
    return False
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   176
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   177
def _compatible_relation(relations, stmt, sniprel):
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   178
    """Search among given rql relation nodes if there is one 'compatible' with the
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   179
    snippet relation, and return it if any, else None.
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   180
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   181
    A relation is compatible if it:
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   182
    * belongs to the currently processed statement,
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   183
    * isn't negged (i.e. direct parent is a NOT node)
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   184
    * isn't optional (outer join) or similarly as the snippet relation
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   185
    """
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   186
    for rel in relations:
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   187
        # don't share if relation's scope is not the current statement
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   188
        if rel.scope is not stmt:
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   189
            continue
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   190
        # don't share neged relation
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   191
        if rel.neged(strict=True):
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   192
            continue
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   193
        # don't share optional relation, unless the snippet relation is
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   194
        # similarly optional
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   195
        if rel.optional and rel.optional != sniprel.optional:
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   196
            continue
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   197
        return rel
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   198
    return None
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   199
9167
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   200
7850
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
   201
def iter_relations(stinfo):
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
   202
    # this is a function so that test may return relation in a predictable order
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
   203
    return stinfo['relations'] - stinfo['rhsrelations']
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
   204
9167
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   205
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   206
class Unsupported(Exception):
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   207
    """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
   208
    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
   209
    """
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   210
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   211
class VariableFromSubQuery(Exception):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   212
    """flow control exception to indicate that a variable is coming from a
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   213
    subquery, and let parent act accordingly
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   214
    """
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   215
    def __init__(self, variable):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   216
        self.variable = variable
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   217
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
   218
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
class RQLRewriter(object):
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   220
    """Insert some rql snippets into another rql syntax tree, for security /
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   221
    relation vocabulary. This implies that it should only restrict results of
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   222
    the original query, not generate new ones. Hence, inserted snippets are
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   223
    inserted under an EXISTS 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
   224
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   225
    This class *isn't thread safe*.
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
   226
    """
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
   227
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
   228
    def __init__(self, session):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
        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
   230
        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
   231
        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
   232
        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
   233
        self._compute_solutions = vreg.solutions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
    def compute_solutions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        self.annotate(self.select)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
            self._compute_solutions(self.session, self.select, self.kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
        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
   240
            raise Unsupported(str(self.select))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
        if len(self.select.solutions) < len(self.solutions):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
            raise Unsupported()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   243
9167
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   244
    def insert_local_checks(self, select, kwargs,
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   245
                            localchecks, restricted, noinvariant):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   246
        """
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   247
        select: the rql syntax tree Select node
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   248
        kwargs: query arguments
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   249
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   250
        localchecks: {(('Var name', (rqlexpr1, rqlexpr2)),
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   251
                       ('Var name1', (rqlexpr1, rqlexpr23))): [solution]}
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   252
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   253
              (see querier._check_permissions docstring for more information)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   254
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   255
        restricted: set of variable names to which an rql expression has to be
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   256
              applied
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   257
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   258
        noinvariant: set of variable names that can't be considered has
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   259
              invariant due to security reason (will be filed by this method)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   260
        """
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   261
        nbtrees = len(localchecks)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   262
        myunion = union = select.parent
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   263
        # transform in subquery when len(localchecks)>1 and groups
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   264
        if nbtrees > 1 and (select.orderby or select.groupby or
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   265
                            select.having or select.has_aggregat or
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   266
                            select.distinct or
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   267
                            select.limit or select.offset):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   268
            newselect = stmts.Select()
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   269
            # only select variables in subqueries
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   270
            origselection = select.selection
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   271
            select.select_only_variables()
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   272
            select.has_aggregat = False
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   273
            # create subquery first so correct node are used on copy
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   274
            # (eg ColumnAlias instead of Variable)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   275
            aliases = [n.VariableRef(newselect.get_variable(vref.name, i))
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   276
                       for i, vref in enumerate(select.selection)]
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   277
            selected = set(vref.name for vref in aliases)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   278
            # now copy original selection and groups
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   279
            for term in origselection:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   280
                newselect.append_selected(term.copy(newselect))
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   281
            if select.orderby:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   282
                sortterms = []
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   283
                for sortterm in select.orderby:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   284
                    sortterms.append(sortterm.copy(newselect))
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   285
                    for fnode in sortterm.get_nodes(n.Function):
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   286
                        if fnode.name == 'FTIRANK':
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   287
                            # we've to fetch the has_text relation as well
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   288
                            var = fnode.children[0].variable
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   289
                            rel = iter(var.stinfo['ftirels']).next()
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   290
                            assert not rel.ored(), 'unsupported'
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   291
                            newselect.add_restriction(rel.copy(newselect))
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   292
                            # remove relation from the orig select and
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   293
                            # cleanup variable stinfo
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   294
                            rel.parent.remove(rel)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   295
                            var.stinfo['ftirels'].remove(rel)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   296
                            var.stinfo['relations'].remove(rel)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   297
                            # XXX not properly re-annotated after security insertion?
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   298
                            newvar = newselect.get_variable(var.name)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   299
                            newvar.stinfo.setdefault('ftirels', set()).add(rel)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   300
                            newvar.stinfo.setdefault('relations', set()).add(rel)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   301
                newselect.set_orderby(sortterms)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   302
                _expand_selection(select.orderby, selected, aliases, select, newselect)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   303
                select.orderby = () # XXX dereference?
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   304
            if select.groupby:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   305
                newselect.set_groupby([g.copy(newselect) for g in select.groupby])
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   306
                _expand_selection(select.groupby, selected, aliases, select, newselect)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   307
                select.groupby = () # XXX dereference?
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   308
            if select.having:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   309
                newselect.set_having([g.copy(newselect) for g in select.having])
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   310
                _expand_selection(select.having, selected, aliases, select, newselect)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   311
                select.having = () # XXX dereference?
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   312
            if select.limit:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   313
                newselect.limit = select.limit
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   314
                select.limit = None
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   315
            if select.offset:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   316
                newselect.offset = select.offset
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   317
                select.offset = 0
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   318
            myunion = stmts.Union()
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   319
            newselect.set_with([n.SubQuery(aliases, myunion)], check=False)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   320
            newselect.distinct = select.distinct
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   321
            solutions = [sol.copy() for sol in select.solutions]
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   322
            cleanup_solutions(newselect, solutions)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   323
            newselect.set_possible_types(solutions)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   324
            # if some solutions doesn't need rewriting, insert original
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   325
            # select as first union subquery
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   326
            if () in localchecks:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   327
                myunion.append(select)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   328
            # we're done, replace original select by the new select with
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   329
            # subqueries (more added in the loop below)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   330
            union.replace(select, newselect)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   331
        elif not () in localchecks:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   332
            union.remove(select)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   333
        for lcheckdef, lchecksolutions in localchecks.iteritems():
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   334
            if not lcheckdef:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   335
                continue
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   336
            myrqlst = select.copy(solutions=lchecksolutions)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   337
            myunion.append(myrqlst)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   338
            # in-place rewrite + annotation / simplification
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   339
            lcheckdef = [({var: 'X'}, rqlexprs) for var, rqlexprs in lcheckdef]
9188
0677e03077fb [rqlrewrite] rewrite doesn't need a solutions argument, always use the Select'ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9187
diff changeset
   340
            self.rewrite(myrqlst, lcheckdef, kwargs)
9167
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   341
            _add_noinvariant(noinvariant, restricted, myrqlst, nbtrees)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   342
        if () in localchecks:
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   343
            select.set_possible_types(localchecks[()])
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   344
            add_types_restriction(self.schema, select)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   345
            _add_noinvariant(noinvariant, restricted, select, nbtrees)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   346
        self.annotate(union)
c05652b108ce [rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8748
diff changeset
   347
9188
0677e03077fb [rqlrewrite] rewrite doesn't need a solutions argument, always use the Select'ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9187
diff changeset
   348
    def rewrite(self, select, snippets, 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
   349
        """
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
   350
        snippets: (varmap, list of rql expression)
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   351
                  with varmap a *dict* {select var: snippet var}
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
   352
        """
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
   353
        self.select = select
9188
0677e03077fb [rqlrewrite] rewrite doesn't need a solutions argument, always use the Select'ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9187
diff changeset
   354
        # remove_solutions used below require a copy
0677e03077fb [rqlrewrite] rewrite doesn't need a solutions argument, always use the Select'ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9187
diff changeset
   355
        self.solutions = solutions = select.solutions[:]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
        self.kwargs = kwargs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
        self.u_varname = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
        self.removing_ambiguity = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
        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
   360
        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
   361
        self.existingvars = existingvars
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
        # we have to annotate the rqlst before inserting snippets, even though
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   363
        # we'll have to redo it later
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
        self.annotate(select)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
        self.insert_snippets(snippets)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
        if not self.exists_snippet and self.u_varname:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
            # U has been inserted than cancelled, cleanup
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
            select.undefine_variable(select.defined_vars[self.u_varname])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
        # clean solutions according to initial solutions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
        newsolutions = remove_solutions(solutions, select.solutions,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
                                        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
   372
        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
   373
            '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
   374
            'something wrong in your schema permission (for instance using a '
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   375
            'RQLExpression which inserts a relation which doesn\'t exist in '
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
   376
            '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
   377
            select, solutions, newsolutions))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        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
   379
            newsolutions = self.remove_ambiguities(snippets, newsolutions)
9188
0677e03077fb [rqlrewrite] rewrite doesn't need a solutions argument, always use the Select'ones
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9187
diff changeset
   380
            assert newsolutions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
        select.solutions = newsolutions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
        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
   383
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
    def insert_snippets(self, snippets, varexistsmap=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
        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
   386
        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
   387
            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
   388
                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
   389
            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
   390
                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
   391
            if varexistsmap is not None and not varmap in varexistsmap:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
                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
   393
            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
   394
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   395
    def init_from_varmap(self, varmap, varexistsmap=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
   396
        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
   397
        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
   398
        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
   399
        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
   400
            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
   401
            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
   402
            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
   403
            self.varinfos.append(vi)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
            try:
8748
f5027f8d2478 drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   405
                vi['const'] = int(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
   406
                vi['rhs_rels'] = vi['lhs_rels'] = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
            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
   408
                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
   409
                    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
   410
                except KeyError:
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   411
                    vi['stinfo'] = sti = self._subquery_variable(selectvar)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
                if varexistsmap is None:
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   413
                    # build an index for quick access to relations
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   414
                    vi['rhs_rels'] = {}
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   415
                    for rel in sti.get('rhsrelations', []):
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   416
                        vi['rhs_rels'].setdefault(rel.r_type, []).append(rel)
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   417
                    vi['lhs_rels'] = {}
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   418
                    for rel in sti.get('relations', []):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   419
                        if not rel in sti.get('rhsrelations', []):
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   420
                            vi['lhs_rels'].setdefault(rel.r_type, []).append(rel)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
                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
   422
                    vi['rhs_rels'] = vi['lhs_rels'] = {}
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   423
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   424
    def _subquery_variable(self, selectvar):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   425
        raise VariableFromSubQuery(selectvar)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   426
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   427
    def insert_varmap_snippets(self, varmap, rqlexprs, varexistsmap):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   428
        try:
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   429
            self.init_from_varmap(varmap, varexistsmap)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   430
        except VariableFromSubQuery, ex:
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   431
            # variable may have been moved to a newly inserted subquery
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   432
            # we should insert snippet in that subquery
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   433
            subquery = self.select.aliases[ex.variable].query
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   434
            assert len(subquery.children) == 1, subquery
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   435
            subselect = subquery.children[0]
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   436
            RQLRewriter(self.session).rewrite(subselect, [(varmap, rqlexprs)],
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   437
                                              self.kwargs)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   438
            return
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   439
        self._insert_scope = None
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   440
        previous = 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
   441
        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
   442
        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
   443
            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
   444
            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
   445
                try:
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   446
                    new = self.insert_snippet(varmap, rqlexpr.snippet_rqlst, previous)
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
   447
                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
   448
                    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
   449
                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
   450
                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
   451
                    self.exists_snippet[rqlexpr] = new
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   452
                previous = previous or new
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
   453
            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
   454
                # 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
   455
                # 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
   456
                exists = varexistsmap[varmap]
8296
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   457
                if self.exists_snippet.get(rqlexpr) is exists:
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
   458
                    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
   459
        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
   460
            # no rql expression found matching rql solutions. User has no access right
8112
d3f1e28d5bdb [rqlrewrite] fix comment
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8074
diff changeset
   461
            raise Unauthorized() # XXX may also be because of bad constraints in schema definition
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   462
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   463
    def insert_snippet(self, varmap, snippetrqlst, previous=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
        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
   465
        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
   466
        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
   467
        try:
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   468
            return self._insert_snippet(varmap, previous, new)
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
   469
        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
   470
            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
   471
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   472
    def _inserted_root(self, new):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   473
        if not isinstance(new, (n.Exists, n.Not)):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   474
            new = n.Exists(new)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   475
        return new
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   476
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   477
    def _insert_snippet(self, varmap, previous, new):
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   478
        """insert `new` snippet into the syntax tree, which have been rewritten
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   479
        using `varmap`. In cases where an action is protected by several rql
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   480
        expresssion, `previous` will be the first rql expression which has been
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   481
        inserted, and so should be ORed with the following expressions.
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   482
        """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
        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
   484
            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
   485
                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
   486
                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
   487
                    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
   488
                    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
   489
                        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
   490
                    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
   491
                        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
   492
            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
   493
                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
   494
            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
   495
                                                  for vi in self.varinfos):
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   496
                assert previous is None
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   497
                self._insert_scope, new = 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
   498
                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
   499
                #self._insert_scope = None
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   500
                return new
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   501
            new = self._inserted_root(new)
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   502
            if previous 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
   503
                insert_scope.add_restriction(new)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
            else:
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   505
                grandpa = previous.parent
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   506
                or_ = n.Or(previous, new)
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   507
                grandpa.replace(previous, or_)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
            if not self.removing_ambiguity:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
                    self.compute_solutions()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
                except Unsupported:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
                    # some solutions have been lost, can't apply this rql expr
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   513
                    if previous 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
   514
                        self.current_statement().remove_node(new, undefine=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
                    else:
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   516
                        grandpa.replace(or_, previous)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
                        self._cleanup_inserted(new)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   518
                    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
   519
                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
   520
                    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
   521
                        self.insert_pending()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   522
            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
   523
        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
   524
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
    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
   526
        """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
   527
        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
   528
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
        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
   530
        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
   531
        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
   532
        """
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
   533
        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
   534
        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
   535
            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
   536
            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
   537
                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
   538
            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
   539
                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
   540
                    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
   541
                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
   542
                    # 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
   543
                    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
   544
            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
   545
            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
   546
                # 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
   547
                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
   548
                    '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
   549
                    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
   550
                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
   551
            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
   552
            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
   553
            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
   554
                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
   555
                if not rqlexprs:
3934
d9a29a1fbe43 bugfix typo in exception name
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3826
diff changeset
   556
                    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
   557
                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
   558
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
   559
    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
   560
        """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
   561
        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
   562
        snippetrqlst = n.Exists(transformedsnippet.copy(subselect))
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   563
        get_rschema = self.schema.rschema
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
   564
        aliases = []
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   565
        done = set()
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   566
        for i, (selectvar, _) in enumerate(varmap):
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   567
            need_null_test = False
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
   568
            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
   569
            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
   570
            aliases.append(selectvar)
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   571
            todo = [(selectvar, self.varinfos[i]['stinfo'])]
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   572
            while todo:
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   573
                varname, stinfo = todo.pop()
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   574
                done.add(varname)
7850
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
   575
                for rel in iter_relations(stinfo):
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   576
                    if rel in done:
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   577
                        continue
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   578
                    done.add(rel)
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   579
                    rschema = get_rschema(rel.r_type)
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   580
                    if rschema.final or rschema.inlined:
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   581
                        rel.children[0].name = varname # XXX explain why
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   582
                        subselect.add_restriction(rel.copy(subselect))
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   583
                        for vref in rel.children[1].iget_nodes(n.VariableRef):
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   584
                            if isinstance(vref.variable, n.ColumnAlias):
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   585
                                # XXX could probably be handled by generating the
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   586
                                # subquery into the detected subquery
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   587
                                raise BadSchemaDefinition(
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   588
                                    "cant insert security because of usage two inlined "
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   589
                                    "relations in this query. You should probably at "
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   590
                                    "least uninline %s" % rel.r_type)
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   591
                            subselect.append_selected(vref.copy(subselect))
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   592
                            aliases.append(vref.name)
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   593
                        self.select.remove_node(rel)
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   594
                        # when some inlined relation has to be copied in the
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   595
                        # subquery and that relation is optional, we need to
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   596
                        # test that either value is NULL or that the snippet
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   597
                        # condition is satisfied
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   598
                        if varname == selectvar and rel.optional and rschema.inlined:
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   599
                            need_null_test = True
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   600
                        # also, if some attributes or inlined relation of the
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   601
                        # object variable are accessed, we need to get all those
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   602
                        # from the subquery as well
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   603
                        if vref.name not in done and rschema.inlined:
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   604
                            # we can use vref here define in above for loop
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   605
                            ostinfo = vref.variable.stinfo
7850
d14b77c42b06 [test] sort to avoid random failure due to dict order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7843
diff changeset
   606
                            for orel in iter_relations(ostinfo):
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   607
                                orschema = get_rschema(orel.r_type)
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   608
                                if orschema.final or orschema.inlined:
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   609
                                    todo.append( (vref.name, ostinfo) )
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   610
                                    break
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
   611
            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
   612
                snippetrqlst = n.Or(
7843
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   613
                    n.make_relation(subselect.get_variable(selectvar), 'is',
3b51806da60b [rqlrewrite] if inlined relation has to be moved to a subquery, take care of the object of the relation (closes #1945725)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7555
diff changeset
   614
                                    (None, None), n.Constant,
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
   615
                                    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
   616
                    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
   617
        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
   618
        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
   619
            # 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
   620
            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
   621
            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
   622
                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
   623
            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
   624
                                               '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
   625
            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
   626
        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
   627
                              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
   628
        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
   629
        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
   630
        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
   631
                   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
   632
        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
   633
        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
   634
        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
   635
            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
   636
        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
   637
            # 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
   638
            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
   639
            raise
8264
a4b009ba92ce [rql rewrite] when a subquery has to be introduced, properly return the snippet expression so that further expressions are properly ored. Closes #2207180
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8147
diff changeset
   640
        return subselect, 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
   641
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
   642
    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
   643
        # 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
   644
        # "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
   645
        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
   646
        # 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
   647
        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
   648
        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
   649
        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
   650
            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
   651
            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
   652
            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
   653
            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
   654
            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
   655
        # 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
   656
        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
   657
            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
   658
            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
   659
                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
   660
                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
   661
                    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
   662
                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
   663
                    # 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
   664
                    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
   665
                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
   666
                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
   667
        # 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
   668
        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
   669
        # 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
   670
        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
   671
                                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
   672
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
   673
    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
   674
        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
   675
        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
   676
            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
   677
            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
   678
                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
   679
            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
   680
        # 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
   681
        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
   682
        # 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
   683
        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
   684
            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
   685
            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
   686
            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
   687
                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
   688
                    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
   689
            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
   690
                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
   691
                    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
   692
        return variantes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   693
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   694
    def _cleanup_inserted(self, node):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   695
        # cleanup inserted variable references
8296
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   696
        removed = set()
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
   697
        for vref in node.iget_nodes(n.VariableRef):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   698
            vref.unregister_reference()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   699
            if not vref.variable.stinfo['references']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   700
                # no more references, undefine the variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   701
                del self.select.defined_vars[vref.name]
8296
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   702
                removed.add(vref.name)
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   703
        for key, newvar in self.rewritten.items(): # I mean items we alter it
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   704
            if newvar in removed:
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   705
                del self.rewritten[key]
f23782a2cdee rqlrewrite: remove element in rewritten when we remove them from the select (closes #2236985)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8264
diff changeset
   706
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   707
7138
9aba650eea6b [rql rewriter] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7137
diff changeset
   708
    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
   709
        """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
   710
        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
   711
        """
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   712
        if sniprel.neged(strict=True):
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   713
            return None # no way
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
   714
        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
   715
        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
   716
        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
   717
            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
   718
                if target == 'object':
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   719
                    orels = vi['lhs_rels'][sniprel.r_type]
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
   720
                    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
   721
                    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
   722
                    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
   723
                else: # target == 'subject':
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   724
                    orels = vi['rhs_rels'][sniprel.r_type]
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
   725
                    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
   726
                    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
   727
                    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
   728
            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
   729
                # may be raised by vi['xhs_rels'][sniprel.r_type]
9169
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   730
                continue
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   731
            # if cardinality isn't in '?1', we can't ignore the snippet relation
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   732
            # and use variable from the original query
9169
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   733
            if _has_multiple_cardinality(vi['stinfo']['possibletypes'], rdef,
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   734
                                         ttypes_func, cardindex):
544b22a3485b [rql rewrite] fix may_be_shared_with method so that it actually considers all variable infos and not only the first one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9167
diff changeset
   735
                continue
9189
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   736
            orel = _compatible_relation(orels, stmt, sniprel)
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   737
            if orel is not None:
9448215c73c4 [rqlrewrite] fix rqlrewrite unpredictability vs relation sharing. Closes #3036144
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9188
diff changeset
   738
                return orel
9187
8406bef5d5f2 [rql rewrite] equivalent but much simpler flow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9170
diff changeset
   739
        return 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
   740
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
   741
    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
   742
        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
   743
        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
   744
            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
   745
            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
   746
            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
   747
                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
   748
        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
   749
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
   750
    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
   751
        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
   752
        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
   753
            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
   754
            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
   755
                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
   756
                # 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
   757
                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
   758
                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
   759
                    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
   760
                # 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
   761
                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
   762
                    stmt.get_variable(self.u_varname),
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   763
                    '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
   764
                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
   765
            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
   766
        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
   767
        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
   768
            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
   769
        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
   770
            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
   771
            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
   772
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
   773
    # 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
   774
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   775
    def _visit_binary(self, node, cls):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   776
        newnode = cls()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   777
        for c in node.children:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   778
            new = c.accept(self)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   779
            if new is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   780
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   781
            newnode.append(new)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
        if len(newnode.children) == 0:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   783
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   784
        if len(newnode.children) == 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   785
            return newnode.children[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   786
        return newnode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   787
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   788
    def _visit_unary(self, node, cls):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   789
        newc = node.children[0].accept(self)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   790
        if newc is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   791
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   792
        newnode = cls()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   793
        newnode.append(newc)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   794
        return newnode
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   795
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
   796
    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
   797
        return self._visit_binary(node, n.And)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   798
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
   799
    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
   800
        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
   801
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   802
    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
   803
        return self._visit_unary(node, n.Not)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   804
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   805
    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
   806
        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
   807
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
   808
    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
   809
        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
   810
            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
   811
        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
   812
            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
   813
        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
   814
        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
   815
            #path = has_path(vargraph, varname, existingvar)
9593
48a84fb4f301 [rewrite] Fix crash when the main variable doesn't appear in the snippet's vargraph
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9358
diff changeset
   816
            if not varname in vargraph or has_path(vargraph, varname, existingvar):
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
   817
                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
   818
        # 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
   819
        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
   820
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
   821
    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
   822
        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
   823
        # 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
   824
        # 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
   825
        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
   826
            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
   827
                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
   828
        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
   829
                           '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
   830
            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
   831
            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
   832
            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
   833
            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
   834
            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
   835
        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
   836
            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
   837
                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
   838
            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
   839
                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
   840
                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
   841
                    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
   842
                    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
   843
            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
   844
                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
   845
                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
   846
                    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
   847
                    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
   848
        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
   849
        for c in node.children:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   850
            rel.append(c.accept(self))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   851
        return rel
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   852
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
   853
    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
   854
        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
   855
        for c in node.children:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   856
            cmp_.append(c.accept(self))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   857
        return cmp_
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   858
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
   859
    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
   860
        cmp_ = n.MathExpression(node.operator)
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7850
diff changeset
   861
        for c in node.children:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   862
            cmp_.append(c.accept(self))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   863
        return cmp_
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   864
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
   865
    def visit_function(self, node):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   866
        """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
   867
        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
   868
        for c in node.children:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   869
            function_.append(c.accept(self))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   870
        return function_
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   871
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
   872
    def visit_constant(self, node):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   873
        """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
   874
        return n.Constant(node.value, node.type)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   875
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
   876
    def visit_variableref(self, node):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   877
        """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
   878
        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
   879
        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
   880
            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
   881
            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
   882
            if vi.get('const') is not None:
8056
8909800a8c51 [cleanup] drop some appengine support junk
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   883
                return n.Constant(vi['const'], 'Int')
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
   884
            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
   885
        vname_or_term = self._get_varname_or_term(node.name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   886
        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
   887
            return n.VariableRef(stmt.get_variable(vname_or_term))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   888
        # 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
   889
        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
   890
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
   891
    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
   892
        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
   893
            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
   894
        return self._insert_scope.stmt
9953
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   895
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   896
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   897
class RQLRelationRewriter(RQLRewriter):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   898
    """Insert some rql snippets into another rql syntax tree, replacing computed
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   899
    relations by their associated rule.
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   900
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   901
    This class *isn't thread safe*.
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   902
    """
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   903
    def __init__(self, session):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   904
        super(RQLRelationRewriter, self).__init__(session)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   905
        self.rules = {}
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   906
        for rschema in self.schema.iter_computed_relations():
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   907
            self.rules[rschema.type] = RRQLExpression(rschema.rule)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   908
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   909
    def rewrite(self, union, kwargs=None):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   910
        self.kwargs = kwargs
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   911
        self.removing_ambiguity = False
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   912
        self.existingvars = None
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   913
        self.pending_keys = None
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   914
        for relation in union.iget_nodes(n.Relation):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   915
            if relation.r_type in self.rules:
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   916
                self.select = relation.stmt
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   917
                self.solutions = solutions = self.select.solutions[:]
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   918
                self.current_expr = self.rules[relation.r_type]
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   919
                self._insert_scope = relation.scope
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   920
                self.rewritten = {}
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   921
                lhs, rhs = relation.get_variable_parts()
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   922
                varmap = {lhs.name: 'S', rhs.name: 'O'}
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   923
                self.init_from_varmap(tuple(sorted(varmap.items())))
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   924
                self.insert_snippet(varmap, self.current_expr.snippet_rqlst)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   925
                self.select.remove_node(relation)
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   926
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   927
    def _subquery_variable(self, selectvar):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   928
        return self.select.aliases[selectvar].stinfo
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   929
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   930
    def _inserted_root(self, new):
643b19d79e4a [CWEP002] introduce RQLRelationRewriter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9593
diff changeset
   931
        return new