use of com.evolveum.midpoint.repo.sql.query.hqm.CountProjectionElement in project midpoint by Evolveum.
the class QueryInterpreter method interpret.
public RootHibernateQuery interpret(ObjectQuery query, @NotNull Class<? extends Containerable> type, Collection<SelectorOptions<GetOperationOptions>> options, @NotNull PrismContext prismContext, @NotNull RelationRegistry relationRegistry, boolean countingObjects, @NotNull Session session) throws QueryException {
boolean distinctRequested = GetOperationOptions.isDistinct(SelectorOptions.findRootOptions(options));
LOGGER.trace("Interpreting query for type '{}' (counting={}, distinctRequested={}), query:\n{}", type, countingObjects, distinctRequested, query);
InterpretationContext context = new InterpretationContext(this, type, prismContext, relationRegistry, extItemDictionary, session);
interpretQueryFilter(context, query);
String rootAlias = context.getHibernateQuery().getPrimaryEntityAlias();
ResultStyle resultStyle = getResultStyle(context);
if (countingObjects) {
interpretPagingAndSorting(context, query, true);
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
boolean distinct = distinctRequested && !hibernateQuery.isDistinctNotNecessary();
hibernateQuery.addProjectionElement(new CountProjectionElement(resultStyle.getCountString(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();
boolean distinct = distinctRequested && !hibernateQuery.isDistinctNotNecessary();
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, relationRegistry, extItemDictionary, session);
try {
interpretPagingAndSorting(wrapperContext, query, false);
} catch (QueryException e) {
// This fixes cases like MID-6561 and brings in needed joins and wheres
// to the outer query, but we don't want to burden all queries with it.
LOGGER.debug("Potentially recoverable '{}'.\n" + "Trying once more after wrapper context interprets the query.", e.toString());
interpretQueryFilter(wrapperContext, query);
interpretPagingAndSorting(wrapperContext, query, false);
}
RootHibernateQuery wrapperQuery = wrapperContext.getHibernateQuery();
if (repoConfiguration.isUsingSQLServer() && resultStyle.getIdentifiers("").size() > 1) {
// using 'where exists' clause
// FIXME refactor this ugly code
// to distinguish from the same alias in inner query
String wrappedRootAlias = "_" + wrapperQuery.getPrimaryEntityAlias();
wrapperQuery.setPrimaryEntityAlias(wrappedRootAlias);
wrapperQuery.setResultTransformer(resultStyle.getResultTransformer());
wrapperQuery.addProjectionElementsFor(resultStyle.getIdentifiers(wrappedRootAlias));
wrapperQuery.addProjectionElementsFor(resultStyle.getContentAttributes(wrappedRootAlias));
StringBuilder linkingCondition = new StringBuilder();
for (String id : resultStyle.getIdentifiers(wrappedRootAlias)) {
linkingCondition.append(" and ").append(id).append(" = ").append(id.substring(1));
}
wrapperQuery.getConditions().add(wrapperQuery.createExists(subqueryText, linkingCondition.toString()));
} else {
// using 'in' clause (multi-column only for Oracle)
String wrappedRootAlias = wrapperQuery.getPrimaryEntityAlias();
wrapperQuery.setResultTransformer(resultStyle.getResultTransformer());
wrapperQuery.addProjectionElementsFor(resultStyle.getIdentifiers(wrappedRootAlias));
wrapperQuery.addProjectionElementsFor(resultStyle.getContentAttributes(wrappedRootAlias));
List<String> inVariablesList = resultStyle.getIdentifiers(wrapperQuery.getPrimaryEntityAlias());
String inVariablesString = inVariablesList.size() != 1 ? "(" + StringUtils.join(inVariablesList, ", ") + ")" : inVariablesList.get(0);
wrapperQuery.getConditions().add(wrapperQuery.createIn(inVariablesString, 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