use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class UpdateEngineWorker method visit.
@Override
public void visit(UpdateDeleteWhere update) {
List<Quad> quads = update.getQuads();
// Removed from SPARQL : Convert bNodes to named variables first.
//quads = convertBNodesToVariables(quads) ;
// Convert quads to a pattern.
Element el = elementFromQuads(quads);
// Decided to serialize the bindings, but could also have decided to
// serialize the quads after applying the template instead.
ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(datasetGraph.getContext());
DataBag<Binding> db = BagFactory.newDefaultBag(policy, SerializationFactoryFinder.bindingSerializationFactory());
try {
Iterator<Binding> bindings = evalBindings(el);
db.addAll(bindings);
Iter.close(bindings);
Iterator<Binding> it = db.iterator();
execDelete(datasetGraph, quads, null, it);
Iter.close(it);
} finally {
db.close();
}
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class UpdateEngineWorker method visit.
@Override
public void visit(UpdateModify update) {
Node withGraph = update.getWithIRI();
Element elt = update.getWherePattern();
// null or a dataset for USING clause.
// USING/USING NAMED
DatasetGraph dsg = processUsing(update);
// USING overrides WITH
if (dsg == null && withGraph != null) {
// Subtle difference : WITH <uri>... WHERE {}
// and an empty/unknown graph <uri>
// rewrite with GRAPH -> no match.
// redo as dataset with different default graph -> match
// SPARQL is unclear about what happens when the graph does not exist.
// but the rewrite with ElementNamedGraph is closer to SPARQL.
// Better, treat as
// WHERE { GRAPH <with> { ... } }
// This is the SPARQL wording (which is a bit loose).
elt = new ElementNamedGraph(withGraph, elt);
}
if (dsg == null)
dsg = datasetGraph;
Query query = elementToQuery(elt);
ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(datasetGraph.getContext());
DataBag<Binding> db = BagFactory.newDefaultBag(policy, SerializationFactoryFinder.bindingSerializationFactory());
try {
Iterator<Binding> bindings = evalBindings(query, dsg, inputBinding, context);
if (false) {
List<Binding> x = Iter.toList(bindings);
System.out.printf("====>> Bindings (%d)\n", x.size());
Iter.print(System.out, x.iterator());
System.out.println("====<<");
bindings = Iter.iter(x);
}
db.addAll(bindings);
Iter.close(bindings);
Iterator<Binding> it = db.iterator();
execDelete(datasetGraph, update.getDeleteQuads(), withGraph, it);
Iter.close(it);
Iterator<Binding> it2 = db.iterator();
execInsert(datasetGraph, update.getInsertQuads(), withGraph, it2);
Iter.close(it2);
} finally {
db.close();
}
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class TestParameterizedSparqlString method test_param_string_injection_05.
@Test
public void test_param_string_injection_05() {
// This injection attempt results in a valid query but a failed
// injection
String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ?var2 . }";
ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
pss.setLiteral("var2", "hello\" . ?s ?p ?o");
Query q = pss.asQuery();
Element el = q.getQueryPattern();
if (el instanceof ElementTriplesBlock) {
Assert.assertEquals(1, ((ElementTriplesBlock) q.getQueryPattern()).getPattern().size());
} else if (el instanceof ElementGroup) {
Assert.assertEquals(1, ((ElementGroup) el).getElements().size());
el = ((ElementGroup) el).getElements().get(0);
if (el instanceof ElementTriplesBlock) {
Assert.assertEquals(1, ((ElementTriplesBlock) el).getPattern().size());
}
}
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class UpdateBuilderTest method testInsertAndDeleteWithVar.
@Test
public void testInsertAndDeleteWithVar() {
UpdateBuilder builder = new UpdateBuilder();
Var v = Var.alloc("v");
builder.addInsert(new Quad(g, s, v, o));
builder.addDelete(new Triple(s, v, o));
builder.addWhere(null, v, "foo");
builder.setVar(v, p);
Update update = builder.build();
assertTrue(update instanceof UpdateModify);
UpdateModify um = (UpdateModify) update;
List<Quad> quads = um.getInsertQuads();
assertEquals(1, quads.size());
Quad q = quads.get(0);
assertEquals(g, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
quads = um.getDeleteQuads();
assertEquals(1, quads.size());
q = quads.get(0);
assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
Element e = um.getWherePattern();
assertTrue(e instanceof ElementGroup);
ElementGroup eg = (ElementGroup) e;
assertEquals(1, eg.getElements().size());
ElementPathBlock epb = (ElementPathBlock) eg.getElements().get(0);
Triple t = epb.getPattern().get(0).asTriple();
assertEquals(Node.ANY, t.getSubject());
assertEquals(p, t.getPredicate());
assertEquals(builder.makeNode("foo"), t.getObject());
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class UpdateBuilderTest method testInsertAndDelete.
@Test
public void testInsertAndDelete() {
UpdateBuilder builder = new UpdateBuilder();
builder.addInsert(new Quad(g, s, p, o));
builder.addDelete(new Triple(s, p, o));
builder.addWhere(null, p, "foo");
Update update = builder.build();
assertTrue(update instanceof UpdateModify);
UpdateModify um = (UpdateModify) update;
List<Quad> quads = um.getInsertQuads();
assertEquals(1, quads.size());
Quad q = quads.get(0);
assertEquals(g, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
quads = um.getDeleteQuads();
assertEquals(1, quads.size());
q = quads.get(0);
assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
Element e = um.getWherePattern();
assertTrue(e instanceof ElementGroup);
ElementGroup eg = (ElementGroup) e;
assertEquals(1, eg.getElements().size());
ElementPathBlock epb = (ElementPathBlock) eg.getElements().get(0);
Triple t = epb.getPattern().get(0).asTriple();
assertEquals(Node.ANY, t.getSubject());
assertEquals(p, t.getPredicate());
assertEquals(builder.makeNode("foo"), t.getObject());
}
Aggregations