I am trying to intercept an incoming SPARQL-UPDATE query string and insert some additional statements. More precisely, I want to add some triples in the WHERE clause.
E.g. if I have this update query coming from a user request:
DELETE {
<urn:res:dummy> a <urn:type:A> , <urn:type:b> ;
<urn:p:a> ?x .
} INSERT {
<urn:res:dummy> <urn:p:a> "hello" .
} WHERE {
?s <urn:p:a> ?x .
}
I want it to become:
DELETE {
<urn:res:dummy> a <urn:type:A> , <urn:type:b> ;
<urn:p:a> ?x .
} INSERT {
<urn:res:dummy> <urn:p:a> "hello" .
} WHERE {
<urn:res:dummy> ?p ?o .
?s <urn:p:a> ?x .
}
(I cannot control anything that is in the query so I can't just bind ?s.)
Parsing the above original query:
parseUpdate(qs)
Returns:
Update_{'prologue': [([], {})], 'request': [Modify_{'insert': InsertClause_{'quads': Quads_{'triples': [([rdflib.term.URIRef('urn:res:dummy'), rdflib.term.URIRef('urn:p:a'), literal_{'string': rdflib.term.Literal('hello')}], {})]}}, 'delete': DeleteClause_{'quads': Quads_{'triples': [([rdflib.term.URIRef('urn:res:dummy'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:type:A'), rdflib.term.URIRef('urn:res:dummy'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('urn:type:b'), rdflib.term.URIRef('urn:res:dummy'), rdflib.term.URIRef('urn:p:a'), rdflib.term.Variable('x')], {})]}}, 'where': GroupGraphPatternSub_{'part': [TriplesBlock_{'triples': [([rdflib.term.URIRef('urn:res:dummy'), PathAlternative_{'part': [PathSequence_{'part': [PathElt_{'part': rdflib.term.URIRef('urn:p:a')}]}]}, rdflib.term.Variable('x')], {})]}]}}]}
Precisely, where I want to add my additional triple is the very beginning of the "request[0].where" part.
Is there a way to do this in a manner that works predictably with any possibly complex query? I.e. without using string-based hacks, but possibly the structure offered above, which alas, loks unfathomable to me.
Thanks,
Stefano