Example #1
0
def _prepare_delete_many_query(resources, context, inverse=False):
    query = delete()
    if context:
        query.from_(context)

    query.template(("?s", "?p", "?o"))

    if context:
        where_clause = NamedGroup(context)
    else:
        where_clause = Group()

    subjects = [resource.subject for resource in resources]
    filter = " OR ".join(
        [u"?s = <{0:s}>".format(subject) for subject in subjects])
    filter = Filter("(%s)" % filter)

    if inverse:
        filter2 = " OR ".join(
            [u"?o = <{0:s}>".format(subject) for subject in subjects])
        filter2 = Filter("(%s)" % filter2)

        where1 = Group([("?s", "?p", "?o"), filter])
        where2 = Group([("?s", "?p", "?o"), filter2])
        where_clause.append(Union([where1, where2]))
    else:
        where_clause.append(("?s", "?p", "?o"))
        where_clause.append(filter)

    query.where(where_clause)

    return query
Example #2
0
    def __remove(self, s = None, p = None, o = None, context = None):
        self.log.debug('REM : %s, %s, %s, %s' % (s, p, o, context))

        query = delete()
        try:
            #clear
            if s == None and p == None and o == None and context:
                query = clear().graph(context)
            else:
                if context:
                    query = delete().from_(context)

                query.template(("?s", "?p", "?o"))

                if context:
                    where_group = NamedGroup(context)
                else:
                    where_group = Group()

                where_group.append(("?s", "?p", "?o"))
                filter = Filter("(" + self.__build_filter(s, p, o) + ")")
                where_group.append(filter)

                query.where(where_group)

            query_str = unicode(query)
            self.log.debug(query_str)
            self.__sparql_wrapper.setQuery(query_str)
            self.__sparql_wrapper.query().convert()
            return True
        except EndPointNotFound, notfound:
            self.log.exception("SPARQL endpoint not found")
Example #3
0
    def _remove_from_endpoint(self, s=None, p=None, o=None, context=None):
        debug('REM : %s, %s, %s, %s' % (s, p, o, context))

        query = delete()
        try:
            if s is None and p is None and o is None and context:
                query = clear().graph(context)
            else:
                if context:
                    query = delete().from_(context)

                query.template(("?s", "?p", "?o"))

                if context:
                    where_group = NamedGroup(context)
                else:
                    where_group = Group()

                where_group.append(("?s", "?p", "?o"))
                filter = Filter("({0})".format(self.__build_filter(s, p, o)))
                where_group.append(filter)
                query.where(where_group)

            query_str = unicode(query)
            debug(query_str)
            self._sparql_wrapper.setQuery(query_str)
            self._sparql_wrapper.query().convert()
            return True
        except EndPointNotFound, _:
            error("SPARQL endpoint not found")
Example #4
0
    def __prepare_selective_delete_query(self, resources, context = None):
        query = delete()
        if context:
            query.from_(context)

        query.template(("?s", "?p", "?o"))
        
        clauses = []
        for resource in resources:
            for p in resource.rdf_direct:
                filter = Filter("(?s = <%s> AND ?p = <%s>)" % (resource.subject, p))
                clauses.append(Group([("?s", "?p", "?o"), filter]))
                 
        query.union(*clauses)
        return query        
Example #5
0
    def _remove_from_endpoint(self, s=None, p=None, o=None, context=None):
        debug('REM : %s, %s, %s, %s' % (s, p, o, context))

        #context = URIRef("#heeey")
        # print(NamedGroup(context))
        # print(Group())

        query = delete()
        try:
            if s is None and p is None and o is None and context:
                query = clear().graph(context)
            else:
                if context:
                    query = delete().from_(context)

                query.template(("?s", "?p", "?o"))

                if context:
                    where_group = NamedGroup(context)
                else:
                    where_group = Group()

                where_group.append(("?s", "?p", "?o"))
                filter_terms = self.__build_filter(s, p, o)

                if len(filter_terms) > 0:
                    filter = Filter("({0})".format())
                    where_group.append(filter)

                query.where(*where_group)

            query_str = str(query)
            debug(query_str)
            self._sparql_wrapper.setQuery(query_str)
            self._sparql_wrapper.query().convert()
            return True
        except EndPointNotFound as _:
            error("SPARQL endpoint not found")
        except QueryBadFormed as _:
            error("Bad-formed SPARQL query")
        except SPARQLWrapperException as _:
            error("SPARQLWrapper exception")

        return None
Example #6
0
    def __build_where_clause(cls, q_obj, edge_iterator):
        clauses = []
        for child in q_obj.children:
            if isinstance(child, Q):
                subclauses = cls.__build_where_clause(child, edge_iterator)
                connection = child.connection
            else:
                subclauses = cls.__build_attribute_clause(child, edge_iterator)
                connection = Q.AND

            if len(subclauses) > 1:
                if connection == Q.AND:
                    clause = Group(subclauses)
                elif connection == Q.OR:
                    clause = Union(subclauses)
            else:
                clause = subclauses[0]

            clauses.append(clause)

        return clauses