use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class WhereClauseTest method testBindExprVar.
@ContractTest
public void testBindExprVar() {
Var v = Var.alloc("foo");
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addBind(new E_Random(), v);
assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
builder.setVar(v, NodeFactory.createURI("three"));
Query q = builder.build();
ElementGroup eg = (ElementGroup) q.getQueryPattern();
List<Element> lst = eg.getElements();
assertEquals("Should only be one element", 1, lst.size());
assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
assertTrue("ElementGroup should be empty", etb.isEmpty());
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class SelectHandlerTest method testAddAllVarsDistinct.
@Test
public void testAddAllVarsDistinct() {
AggregationHandler aggHandler = new AggregationHandler(new Query());
SelectHandler sh = new SelectHandler(aggHandler);
sh.addVar(Var.alloc("foo"));
sh.setDistinct(true);
handler.addAll(sh);
// make sure warning does not fire.
query.setQueryPattern(new ElementGroup());
assertTrue(query.isDistinct());
assertFalse(query.isQueryResultStar());
assertEquals(1, query.getResultVars().size());
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class QueryExecutionBase method execDescribe.
@Override
public Model execDescribe(Model model) {
checkNotClosed();
if (!query.isDescribeType())
throw new QueryExecException("Attempt to get a DESCRIBE result from a " + labelForQuery(query) + " query");
//Was: query.setQueryResultStar(true) ; but why?
query.setResultVars();
// If there was no WhereClause, use an empty pattern (one solution, no columns).
if (query.getQueryPattern() == null)
query.setQueryPattern(new ElementGroup());
Set<RDFNode> set = new HashSet<>();
//May return null (no query pattern)
ResultSet qRes = execResultSet();
// Prefixes for result (after initialization)
insertPrefixesInto(model);
if (qRes != null) {
for (; qRes.hasNext(); ) {
QuerySolution rb = qRes.nextSolution();
for (String varName : query.getResultVars()) {
RDFNode n = rb.get(varName);
set.add(n);
}
}
}
if (query.getResultURIs() != null) {
// Any URIs in the DESCRIBE
for (Node n : query.getResultURIs()) {
// Need to make dataset available to describe handlers.
RDFNode rNode = ModelUtils.convertGraphNodeToRDFNode(n, dataset.getDefaultModel());
set.add(rNode);
}
}
// Create new handlers for this process.
List<DescribeHandler> dhList = DescribeHandlerRegistry.get().newHandlerList();
getContext().put(ARQConstants.sysCurrentDataset, getDataset());
// Notify start of describe phase
for (DescribeHandler dh : dhList) dh.start(model, getContext());
// Do describe for each resource found.
for (RDFNode n : set) {
if (n instanceof Resource) {
for (DescribeHandler dh : dhList) {
dh.describe((Resource) n);
}
} else {
// Can't describe literals
continue;
}
}
for (DescribeHandler dh : dhList) dh.finish();
this.close();
return model;
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class WhereClauseTest method testBindStringVar.
@ContractTest
public void testBindStringVar() throws ParseException {
Var v = Var.alloc("foo");
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addBind("rand()", v);
assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
builder.setVar(v, NodeFactory.createURI("three"));
Query q = builder.build();
ElementGroup eg = (ElementGroup) q.getQueryPattern();
List<Element> lst = eg.getElements();
assertEquals("Should only be one element", 1, lst.size());
assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
assertTrue("ElementGroup should be empty", etb.isEmpty());
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class TestParameterizedSparqlString method test_param_string_positional_injection_05.
@Test
public void test_param_string_positional_injection_05() {
// This injection attempt results in a valid query but a failed
// injection
String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ? . }";
ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
pss.setLiteral(0, "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());
}
}
}
Aggregations