Search in sources :

Example 1 with Junction

use of org.hisp.dhis.query.Junction 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.getRootJunctionType());
    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;
}
Also used : Restriction(org.hisp.dhis.query.Restriction) Query(org.hisp.dhis.query.Query) Criterion(org.hisp.dhis.query.Criterion) Criterion(org.hisp.dhis.query.Criterion) Junction(org.hisp.dhis.query.Junction)

Example 2 with Junction

use of org.hisp.dhis.query.Junction 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;
}
Also used : Restriction(org.hisp.dhis.query.Restriction) Disjunction(org.hisp.dhis.query.Disjunction) Criterion(org.hisp.dhis.query.Criterion) Conjunction(org.hisp.dhis.query.Conjunction) Criterion(org.hisp.dhis.query.Criterion) Junction(org.hisp.dhis.query.Junction)

Aggregations

Criterion (org.hisp.dhis.query.Criterion)2 Junction (org.hisp.dhis.query.Junction)2 Restriction (org.hisp.dhis.query.Restriction)2 Conjunction (org.hisp.dhis.query.Conjunction)1 Disjunction (org.hisp.dhis.query.Disjunction)1 Query (org.hisp.dhis.query.Query)1