cubicweb/spa2rql.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 04 Apr 2017 16:28:50 +0200
branch3.25
changeset 12142 db2fc87348ab
parent 11057 0b59724cb3f2
permissions -rw-r--r--
[server] Make "sources_by_uri" and "sources_by_eid" properties of repository I.e. do not populate these dict as repo initialization (bootstrap step) but always use information from database. This is needed because when multiple instances of the same application run, if one instance adds a CWSource the other ones will not see it. In particular, when using a scheduler instance, new CWSource will be added by the web instance and not seen by the scheduler which is supposed to update them. We thus define properties for sources_by_eid and sources_by_uri instead attributes on repository instance. CWSource entities are thus retrieved from database every time these properties are accessed. We factor out initialization of the "source" instance (subclass of cubicweb.server.source.AbstractSource) in a _sources() method. Note that this method takes care of calling "init" method on the source as well as "set_schema" (previously done in repo.set_schema(), which now only touches system_source). Accordingly the "init_sources_from_database" method is dropped along with "add_source"/"remove_source" methods. In syncsources hook, we thus drop: * SourceAddedOp operation which called repo.add_source() so that the SourceAddedHook only cares about checking source configuration now; * SourceRemovedOp and SourceRenamedOp operations for the same reason; * SourceConfigUpdatedOp as updating the live config of source is meaningless once we rely on them being retrieved from the database; * SourceHostConfigUpdatedHook hook which is now useless without call to SourceConfigUpdatedOp; In 3.10 migration script, remove usage of sources_by_uri repo attribute which, unless I'm missing something, appears useless (at least now). In tests: * unittest_datafeed: remove test_update_url method since we dropped respective hook; * unittest_ldapsource: LDAPFeedUserDeletionTC.test_a_filter_inactivate() currently fails because it still relies on live config being updated, this will be fixed in the next changeset once all "live source" logic will be removed.

# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
"""SPARQL -> RQL translator"""

from logilab.common import make_domains
from rql import TypeResolverException
from fyzz.yappsparser import parse
from fyzz import ast

from cubicweb.xy import xy


class UnsupportedQuery(Exception): pass

def order_limit_offset(sparqlst):
    addons = ''
    if sparqlst.orderby:
        sortterms = ', '.join('%s %s' % (var.name.upper(), ascdesc.upper())
                              for var, ascdesc in sparqlst.orderby)
        addons += ' ORDERBY %s' % sortterms
    if sparqlst.limit:
        addons += ' LIMIT %s' % sparqlst.limit
    if sparqlst.offset:
        addons += ' OFFSET %s' % sparqlst.offset
    return addons


class QueryInfo(object):
    """wrapper class containing necessary information to generate a RQL query
    from a sparql syntax tree
    """
    def __init__(self, sparqlst):
        self.sparqlst = sparqlst
        if sparqlst.selected == ['*']:
            self.selection = [var.upper() for var in sparqlst.variables]
        else:
            self.selection = [var.name.upper() for var in sparqlst.selected]
        self.possible_types = {}
        self.infer_types_info = []
        self.union_params = []
        self.restrictions = []
        self.literals = {}
        self._litcount = 0

    def add_literal(self, value):
        key = chr(ord('a') + self._litcount)
        self._litcount += 1
        self.literals[key] = value
        return key

    def set_possible_types(self, var, varpossibletypes):
        """set/restrict possible types for the given variable.

        :return: True if something changed, else false.
        :raise: TypeResolverException if no more type allowed
        """
        varpossibletypes = set(varpossibletypes)
        try:
            ctypes = self.possible_types[var]
            nbctypes = len(ctypes)
            ctypes &= varpossibletypes
            if not ctypes:
                raise TypeResolverException('No possible type')
            return len(ctypes) != nbctypes
        except KeyError:
            self.possible_types[var] = varpossibletypes
            return True

    def infer_types(self):
        # XXX should use something similar to rql.analyze for proper type inference
        modified = True
        # loop to infer types until nothing changed
        while modified:
            modified = False
            for yams_predicates, subjvar, obj in self.infer_types_info:
                nbchoices = len(yams_predicates)
                # get possible types for the subject variable, according to the
                # current predicate
                svptypes = set(s for s, r, o in yams_predicates)
                if not '*' in svptypes:
                    if self.set_possible_types(subjvar, svptypes):
                        modified = True
                # restrict predicates according to allowed subject var types
                if subjvar in self.possible_types:
                    yams_predicates[:] = [(s, r, o) for s, r, o in yams_predicates
                                          if s == '*' or s in self.possible_types[subjvar]]
                if isinstance(obj, ast.SparqlVar):
                    # make a valid rql var name
                    objvar = obj.name.upper()
                    # get possible types for the object variable, according to
                    # the current predicate
                    ovptypes = set(o for s, r, o in yams_predicates)
                    if not '*' in ovptypes:
                        if self.set_possible_types(objvar, ovptypes):
                            modified = True
                    # restrict predicates according to allowed object var types
                    if objvar in self.possible_types:
                        yams_predicates[:] = [(s, r, o) for s, r, o in yams_predicates
                                              if o == '*' or o in self.possible_types[objvar]]
                # ensure this still make sense
                if not yams_predicates:
                    raise TypeResolverException('No yams predicate')
                if len(yams_predicates) != nbchoices:
                    modified = True

    def build_restrictions(self):
        # now, for each predicate
        for yams_predicates, subjvar, obj in self.infer_types_info:
            rel = yams_predicates[0]
            # if there are several yams relation type equivalences, we will have
            # to generate several unioned rql queries
            for s, r, o in yams_predicates[1:]:
                if r != rel[1]:
                    self.union_params.append((yams_predicates, subjvar, obj))
                    break
            # else we can simply add it to base rql restrictions
            else:
                restr = self.build_restriction(subjvar, rel[1], obj)
                self.restrictions.append(restr)

    def build_restriction(self, subjvar, rtype, obj):
        if isinstance(obj, ast.SparqlLiteral):
            key = self.add_literal(obj.value)
            objvar = '%%(%s)s' % key
        else:
            assert isinstance(obj, ast.SparqlVar)
            # make a valid rql var name
            objvar = obj.name.upper()
        # else we can simply add it to base rql restrictions
        return '%s %s %s' % (subjvar, rtype, objvar)

    def finalize(self):
        """return corresponding rql query (string) / args (dict)"""
        for varname, ptypes in self.possible_types.items():
            if len(ptypes) == 1:
                self.restrictions.append('%s is %s' % (varname, next(iter(ptypes))))
        unions = []
        for releq, subjvar, obj in self.union_params:
            thisunions = []
            for st, rt, ot in releq:
                thisunions.append([self.build_restriction(subjvar, rt, obj)])
                if st != '*':
                    thisunions[-1].append('%s is %s' % (subjvar, st))
                if isinstance(obj, ast.SparqlVar) and ot != '*':
                    objvar = obj.name.upper()
                    thisunions[-1].append('%s is %s' % (objvar, objvar))
            if not unions:
                unions = thisunions
            else:
                unions = zip(*make_domains([unions, thisunions]))
        selection = 'Any ' + ', '.join(self.selection)
        sparqlst = self.sparqlst
        if sparqlst.distinct:
            selection = 'DISTINCT ' + selection
        if unions:
            baserql = '%s WHERE %s' % (selection, ', '.join(self.restrictions))
            rqls = ['(%s, %s)' % (baserql, ', '.join(unionrestrs))
                    for unionrestrs in unions]
            rql = ' UNION '.join(rqls)
            if sparqlst.orderby or sparqlst.limit or sparqlst.offset:
                rql = '%s%s WITH %s BEING (%s)' % (
                    selection, order_limit_offset(sparqlst),
                    ', '.join(self.selection), rql)
        else:
            rql = '%s%s WHERE %s' % (selection, order_limit_offset(sparqlst),
                                      ', '.join(self.restrictions))
        return rql, self.literals


class Sparql2rqlTranslator(object):
    def __init__(self, yschema):
        self.yschema = yschema

    def translate(self, sparql):
        sparqlst = parse(sparql)
        if sparqlst.type != 'select':
            raise UnsupportedQuery()
        qi = QueryInfo(sparqlst)
        for subj, predicate, obj in sparqlst.where:
            if not isinstance(subj, ast.SparqlVar):
                raise UnsupportedQuery()
            # make a valid rql var name
            subjvar = subj.name.upper()
            if predicate in [('', 'a'),
                             ('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'type')]:
                # special 'is' relation
                if not isinstance(obj, tuple):
                    raise UnsupportedQuery()
                # restrict possible types for the subject variable
                qi.set_possible_types(
                    subjvar, xy.yeq(':'.join(obj), isentity=True))
            else:
                # 'regular' relation (eg not 'is')
                if not isinstance(predicate, tuple):
                    raise UnsupportedQuery()
                # list of 3-uple
                #   (yams etype (subject), yams rtype, yams etype (object))
                # where subject / object entity type may '*' if not specified
                yams_predicates = xy.yeq(':'.join(predicate))
                qi.infer_types_info.append((yams_predicates, subjvar, obj))
                if not isinstance(obj, (ast.SparqlLiteral, ast.SparqlVar)):
                    raise UnsupportedQuery()
        qi.infer_types()
        qi.build_restrictions()
        return qi