Esempio n. 1
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")
Esempio n. 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")
Esempio n. 3
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
Esempio n. 4
0
    def __prepare_delete_many_query(self, 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(["?s = <%s>" % subject for subject in subjects])
        filter = Filter("(%s)" % filter)

        if inverse:
            filter2 = " OR ".join(["?o = <%s>" % 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        
Esempio n. 5
0
 def test_delete_from(self):
     """ Try to produce DELETE DATA FROM ... { ... } query. """
     
     expected = canonical(u"DELETE DATA FROM <g> { <a>  <b> <c> }")
     statement = URIRef("a"), URIRef("b"), URIRef("c")
     query = delete(data = True).from_(URIRef("g")).template(statement)
     result = canonical(SparulTranslator(query).translate())
     self.assertEqual(expected, result)
Esempio n. 6
0
    def test_delete_from(self):
        """ Try to produce DELETE DATA FROM ... { ... } query. """

        expected = canonical(u"DELETE DATA FROM <g> { <a>  <b> <c> }")
        statement = URIRef("a"), URIRef("b"), URIRef("c")
        query = delete(data=True).from_(URIRef("g")).template(statement)
        result = canonical(SparulTranslator(query).translate())
        self.assertEqual(expected, result)
Esempio n. 7
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
Esempio n. 8
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        
Esempio n. 9
0
 def test_delete_where(self):
     """ DELETE ... WHERE ... """
     
     expected = canonical(u"""
         DELETE { ?book ?p ?v }
         WHERE
           { ?book ?p ?v .
             FILTER ( ?date < "2000-01-01T00:00:00"^^xsd:dateTime )
           }
     """)        
     
     query = delete().template(("?book", "?p", "?v"))
     query.where(("?book", "?p", "?v"))
     query.filter('( ?date < "2000-01-01T00:00:00"^^xsd:dateTime )')
     
     result = canonical(SparulTranslator(query).translate())
     self.assertEqual(expected, result)
Esempio n. 10
0
    def test_delete_where(self):
        """ DELETE ... WHERE ... """

        expected = canonical(u"""
            DELETE { ?book ?p ?v }
            WHERE
              { ?book ?p ?v .
                FILTER ( ?date < "2000-01-01T00:00:00"^^xsd:dateTime )
              }
        """)

        query = delete().template(("?book", "?p", "?v"))
        query.where(("?book", "?p", "?v"))
        query.filter('( ?date < "2000-01-01T00:00:00"^^xsd:dateTime )')

        result = canonical(SparulTranslator(query).translate())
        self.assertEqual(expected, result)