Search in sources :

Example 1 with QueryPlan

use of org.hisp.dhis.query.planner.QueryPlan in project dhis2-core by dhis2.

the class DefaultQueryService method queryObjects.

//---------------------------------------------------------------------------------------------
// Helper methods
//---------------------------------------------------------------------------------------------
private List<? extends IdentifiableObject> queryObjects(Query query) {
    List<? extends IdentifiableObject> objects = query.getObjects();
    if (objects != null) {
        return inMemoryQueryEngine.query(query.setObjects(objects));
    }
    QueryPlan queryPlan = queryPlanner.planQuery(query);
    Query pQuery = queryPlan.getPersistedQuery();
    Query npQuery = queryPlan.getNonPersistedQuery();
    objects = criteriaQueryEngine.query(pQuery);
    if (npQuery.isEmpty()) {
        return objects;
    }
    if (log.isDebugEnabled()) {
        log.debug("Doing in-memory for " + npQuery.getCriterions().size() + " criterions and " + npQuery.getOrders().size() + " orders.");
    }
    npQuery.setObjects(objects);
    return inMemoryQueryEngine.query(npQuery);
}
Also used : QueryPlan(org.hisp.dhis.query.planner.QueryPlan)

Example 2 with QueryPlan

use of org.hisp.dhis.query.planner.QueryPlan in project dhis2-core by dhis2.

the class CriteriaQueryEngine method query.

@Override
@SuppressWarnings("unchecked")
public List<T> query(Query query) {
    Schema schema = query.getSchema();
    InternalHibernateGenericStore<?> store = getStore((Class<? extends IdentifiableObject>) schema.getKlass());
    if (store == null) {
        return new ArrayList<>();
    }
    if (query.getUser() == null) {
        query.setUser(currentUserService.getCurrentUser());
    }
    if (!query.isPlannedQuery()) {
        QueryPlan queryPlan = queryPlanner.planQuery(query, true);
        query = queryPlan.getPersistedQuery();
    }
    DetachedCriteria detachedCriteria = buildCriteria(store.getSharingDetachedCriteria(query.getUser()), query);
    Criteria criteria = store.getCriteria();
    if (criteria == null) {
        return new ArrayList<>();
    }
    criteria.setFirstResult(query.getFirstResult());
    criteria.setMaxResults(query.getMaxResults());
    for (Order order : query.getOrders()) {
        criteria.addOrder(getHibernateOrder(order));
    }
    return criteria.add(Subqueries.propertyIn("id", detachedCriteria)).list();
}
Also used : Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) QueryPlan(org.hisp.dhis.query.planner.QueryPlan)

Example 3 with QueryPlan

use of org.hisp.dhis.query.planner.QueryPlan in project dhis2-core by dhis2.

the class CriteriaQueryEngine method count.

@Override
@SuppressWarnings("unchecked")
public int count(Query query) {
    Schema schema = query.getSchema();
    InternalHibernateGenericStore<?> store = getStore((Class<? extends IdentifiableObject>) schema.getKlass());
    if (store == null) {
        return 0;
    }
    if (query.getUser() == null) {
        query.setUser(currentUserService.getCurrentUser());
    }
    if (!query.isPlannedQuery()) {
        QueryPlan queryPlan = queryPlanner.planQuery(query, true);
        query = queryPlan.getPersistedQuery();
    }
    DetachedCriteria detachedCriteria = buildCriteria(store.getSharingDetachedCriteria(query.getUser()), query);
    Criteria criteria = store.getCriteria();
    if (criteria == null) {
        return 0;
    }
    return ((Number) criteria.add(Subqueries.propertyIn("id", detachedCriteria)).setProjection(Projections.countDistinct("id")).uniqueResult()).intValue();
}
Also used : Schema(org.hisp.dhis.schema.Schema) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) QueryPlan(org.hisp.dhis.query.planner.QueryPlan)

Aggregations

QueryPlan (org.hisp.dhis.query.planner.QueryPlan)3 Criteria (org.hibernate.Criteria)2 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)2 Schema (org.hisp.dhis.schema.Schema)2 ArrayList (java.util.ArrayList)1