Search in sources :

Example 1 with CountProjectionElement

use of com.evolveum.midpoint.repo.sql.query2.hqm.CountProjectionElement in project midpoint by Evolveum.

the class QueryInterpreter2 method interpret.

public RootHibernateQuery interpret(ObjectQuery query, @NotNull Class<? extends Containerable> type, Collection<SelectorOptions<GetOperationOptions>> options, @NotNull PrismContext prismContext, boolean countingObjects, @NotNull Session session) throws QueryException {
    boolean distinct = GetOperationOptions.isDistinct(SelectorOptions.findRootOptions(options));
    LOGGER.trace("Interpreting query for type '{}' (counting={}, distinct={}), query:\n{}", type, countingObjects, distinct, query);
    InterpretationContext context = new InterpretationContext(this, type, prismContext, session);
    interpretQueryFilter(context, query);
    String rootAlias = context.getHibernateQuery().getPrimaryEntityAlias();
    ResultStyle resultStyle = getResultStyle(context);
    if (countingObjects) {
        interpretPagingAndSorting(context, query, true);
        RootHibernateQuery hibernateQuery = context.getHibernateQuery();
        hibernateQuery.addProjectionElement(new CountProjectionElement(resultStyle.getIdentifiers(rootAlias), distinct));
        return hibernateQuery;
    }
    /*
		   Some databases don't support DISTINCT on BLOBs. In these cases we have to create query like:
		   select
		     u.oid, u.fullObject, u.stringsCount, ..., u.booleansCount
		   from
		     RUser u
		   where
		     u.oid in (select distinct u.oid from RUser u where ...)
		 */
    boolean distinctBlobCapable = !repoConfiguration.isUsingOracle() && !repoConfiguration.isUsingSQLServer();
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    hibernateQuery.setDistinct(distinct);
    hibernateQuery.addProjectionElementsFor(resultStyle.getIdentifiers(rootAlias));
    if (distinct && !distinctBlobCapable) {
        String subqueryText = "\n" + hibernateQuery.getAsHqlText(2, true);
        InterpretationContext wrapperContext = new InterpretationContext(this, type, prismContext, session);
        interpretPagingAndSorting(wrapperContext, query, false);
        RootHibernateQuery wrapperQuery = wrapperContext.getHibernateQuery();
        String wrappedRootAlias = wrapperQuery.getPrimaryEntityAlias();
        wrapperQuery.setResultTransformer(resultStyle.getResultTransformer());
        wrapperQuery.addProjectionElementsFor(resultStyle.getIdentifiers(wrappedRootAlias));
        wrapperQuery.addProjectionElementsFor(resultStyle.getContentAttributes(wrappedRootAlias));
        wrapperQuery.getConditions().add(wrapperQuery.createIn(wrapperQuery.getPrimaryEntityAlias() + ".oid", subqueryText));
        wrapperQuery.addParametersFrom(hibernateQuery.getParameters());
        return wrapperQuery;
    } else {
        interpretPagingAndSorting(context, query, false);
        hibernateQuery.setResultTransformer(resultStyle.getResultTransformer());
        hibernateQuery.addProjectionElementsFor(resultStyle.getContentAttributes(rootAlias));
        if (distinct) {
            // SQL requires this
            hibernateQuery.addProjectionElementsFor(getOrderingAttributes(context));
        }
        return hibernateQuery;
    }
}
Also used : CountProjectionElement(com.evolveum.midpoint.repo.sql.query2.hqm.CountProjectionElement) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) ResultStyle(com.evolveum.midpoint.repo.sql.util.ResultStyle) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)

Aggregations

PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)1 CountProjectionElement (com.evolveum.midpoint.repo.sql.query2.hqm.CountProjectionElement)1 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)1 ResultStyle (com.evolveum.midpoint.repo.sql.util.ResultStyle)1