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;
}
}
Aggregations