use of org.hisp.dhis.query.Restriction in project dhis2-core by dhis2.
the class DefaultQueryPlanner method getQuery.
/**
* @param query Query
* @return Query instance
*/
private Query getQuery(Query query, boolean persistedOnly) {
Query pQuery = Query.from(query.getSchema(), query.getRootJunction().getType());
Iterator<Criterion> iterator = query.getCriterions().iterator();
while (iterator.hasNext()) {
org.hisp.dhis.query.Criterion criterion = iterator.next();
if (Junction.class.isInstance(criterion)) {
Junction junction = handleJunction(pQuery, (Junction) criterion, persistedOnly);
if (!junction.getCriterions().isEmpty()) {
pQuery.getAliases().addAll(junction.getAliases());
pQuery.add(junction);
}
if (((Junction) criterion).getCriterions().isEmpty()) {
iterator.remove();
}
} else if (Restriction.class.isInstance(criterion)) {
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));
if (restriction.getQueryPath().isPersisted() && !restriction.getQueryPath().haveAlias()) {
pQuery.getAliases().addAll(Arrays.asList(((Restriction) criterion).getQueryPath().getAlias()));
pQuery.getCriterions().add(criterion);
iterator.remove();
}
}
}
if (query.ordersPersisted()) {
pQuery.addOrders(query.getOrders());
query.clearOrders();
}
return pQuery;
}
use of org.hisp.dhis.query.Restriction in project dhis2-core by dhis2.
the class DefaultQueryPlanner method handleJunction.
private Junction handleJunction(Query query, Junction queryJunction, boolean persistedOnly) {
Iterator<org.hisp.dhis.query.Criterion> iterator = queryJunction.getCriterions().iterator();
Junction criteriaJunction = Disjunction.class.isInstance(queryJunction) ? new Disjunction(query.getSchema()) : new Conjunction(query.getSchema());
while (iterator.hasNext()) {
org.hisp.dhis.query.Criterion criterion = iterator.next();
if (Junction.class.isInstance(criterion)) {
Junction junction = handleJunction(query, (Junction) criterion, persistedOnly);
if (!junction.getCriterions().isEmpty()) {
criteriaJunction.getAliases().addAll(junction.getAliases());
criteriaJunction.add(junction);
}
if (((Junction) criterion).getCriterions().isEmpty()) {
iterator.remove();
}
} else if (Restriction.class.isInstance(criterion)) {
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));
if (restriction.getQueryPath().isPersisted() && !restriction.getQueryPath().haveAlias(1)) {
criteriaJunction.getAliases().addAll(Arrays.asList(((Restriction) criterion).getQueryPath().getAlias()));
criteriaJunction.getCriterions().add(criterion);
iterator.remove();
} else if (persistedOnly) {
throw new RuntimeException("Path " + restriction.getQueryPath().getPath() + " is not fully persisted, unable to build persisted only query plan.");
}
}
}
return criteriaJunction;
}
Aggregations