use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.
the class CriteriaEntityGraphTest method buildSqlSelectAst.
private <T> SelectStatement buildSqlSelectAst(Class<T> entityType, RootGraphImplementor<T> entityGraph, GraphSemantic mode, SessionImplementor session) {
final LoadQueryInfluencers loadQueryInfluencers = new LoadQueryInfluencers(session.getSessionFactory());
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityType);
criteriaQuery.select(criteriaQuery.from(entityType));
final QueryImplementor<T> query = session.createQuery(criteriaQuery);
final SqmQueryImplementor<String> hqlQuery = (SqmQueryImplementor<String>) query;
hqlQuery.applyGraph(entityGraph, mode);
final SqmSelectStatement<String> sqmStatement = (SqmSelectStatement<String>) hqlQuery.getSqmStatement();
final StandardSqmTranslator<SelectStatement> sqmConverter = new StandardSqmTranslator<>(sqmStatement, hqlQuery.getQueryOptions(), ((QuerySqmImpl<?>) hqlQuery).getDomainParameterXref(), query.getParameterBindings(), loadQueryInfluencers, session.getSessionFactory(), true);
final SqmTranslation<SelectStatement> sqmInterpretation = sqmConverter.translate();
return sqmInterpretation.getSqlAst();
}
use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitSetQueryGroup.
@Override
public SqmQueryGroup<Object> visitSetQueryGroup(HqlParser.SetQueryGroupContext ctx) {
if (creationOptions.useStrictJpaCompliance()) {
throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.SET_OPERATIONS);
}
final List<ParseTree> children = ctx.children;
// noinspection unchecked
final SqmQueryPart<Object> firstQueryPart = (SqmQueryPart<Object>) children.get(0).accept(this);
SqmQueryGroup<Object> queryGroup;
if (firstQueryPart instanceof SqmQueryGroup<?>) {
queryGroup = (SqmQueryGroup<Object>) firstQueryPart;
} else {
queryGroup = new SqmQueryGroup<>(firstQueryPart);
}
setCurrentQueryPart(queryGroup);
final int size = children.size();
final SqmCreationProcessingState firstProcessingState = processingStateStack.pop();
for (int i = 1; i < size; i += 2) {
final SetOperator operator = visitSetOperator((HqlParser.SetOperatorContext) children.get(i));
final HqlParser.OrderedQueryContext simpleQueryCtx = (HqlParser.OrderedQueryContext) children.get(i + 1);
final List<SqmQueryPart<Object>> queryParts;
if (queryGroup.getSetOperator() == null || queryGroup.getSetOperator() == operator) {
queryGroup.setSetOperator(operator);
queryParts = queryGroup.queryParts();
} else {
queryParts = new ArrayList<>(size - (i >> 1));
queryParts.add(queryGroup);
queryGroup = new SqmQueryGroup<>(creationContext.getNodeBuilder(), operator, queryParts);
setCurrentQueryPart(queryGroup);
}
final SqmQueryPart<Object> queryPart;
try {
processingStateStack.push(new SqmQuerySpecCreationProcessingStateStandardImpl(processingStateStack.getCurrent(), (SqmSelectQuery<?>) firstProcessingState.getProcessingQuery(), this));
final List<ParseTree> subChildren = simpleQueryCtx.children;
if (subChildren.get(0) instanceof HqlParser.QueryContext) {
final SqmQuerySpec<Object> querySpec = new SqmQuerySpec<>(creationContext.getNodeBuilder());
queryParts.add(querySpec);
visitQuerySpecExpression((HqlParser.QuerySpecExpressionContext) simpleQueryCtx);
} else {
try {
final SqmSelectStatement<Object> selectStatement = new SqmSelectStatement<>(creationContext.getNodeBuilder());
processingStateStack.push(new SqmQuerySpecCreationProcessingStateStandardImpl(processingStateStack.getCurrent(), selectStatement, this));
queryPart = visitNestedQueryExpression((HqlParser.NestedQueryExpressionContext) simpleQueryCtx);
queryParts.add(queryPart);
} finally {
processingStateStack.pop();
}
}
} finally {
processingStateStack.pop();
}
}
processingStateStack.push(firstProcessingState);
return queryGroup;
}
use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitSelectStatement.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Top-level statements
@Override
public SqmSelectStatement<R> visitSelectStatement(HqlParser.SelectStatementContext ctx) {
final HqlParser.QueryExpressionContext queryExpressionContext = ctx.queryExpression();
final SqmSelectStatement<R> selectStatement = new SqmSelectStatement<>(creationContext.getNodeBuilder());
parameterCollector = selectStatement;
processingStateStack.push(new SqmQuerySpecCreationProcessingStateStandardImpl(processingStateStack.getCurrent(), selectStatement, this));
try {
queryExpressionContext.accept(this);
} finally {
processingStateStack.pop();
}
return selectStatement;
}
use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.
the class QuerySqmImpl method doList.
protected List<R> doList() {
verifySelect();
getSession().prepareForQueryExecution(requiresTxn(getQueryOptions().getLockOptions().findGreatestLockMode()));
final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches();
final boolean hasLimit = hasLimit(sqmStatement, getQueryOptions());
final boolean needsDistinct = containsCollectionFetches && (sqmStatement.usesDistinct() || hasAppliedGraph(getQueryOptions()) || hasLimit);
final DomainQueryExecutionContext executionContextToUse;
if (hasLimit && containsCollectionFetches) {
boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled();
if (fail) {
throw new HibernateException("firstResult/maxResults specified with collection fetch. " + "In memory pagination was about to be applied. " + "Failing because 'Fail on pagination over collection fetch' is enabled.");
} else {
QueryLogging.QUERY_MESSAGE_LOGGER.firstOrMaxResultsSpecifiedWithCollectionFetch();
}
final MutableQueryOptions originalQueryOptions = getQueryOptions();
final QueryOptions normalizedQueryOptions = omitSqlQueryOptions(originalQueryOptions, true, false);
if (originalQueryOptions == normalizedQueryOptions) {
executionContextToUse = this;
} else {
executionContextToUse = new DelegatingDomainQueryExecutionContext(this) {
@Override
public QueryOptions getQueryOptions() {
return normalizedQueryOptions;
}
};
}
} else {
executionContextToUse = this;
}
final List<R> list = resolveSelectQueryPlan().performList(executionContextToUse);
if (needsDistinct) {
int includedCount = -1;
// NOTE : firstRow is zero-based
final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null ? getIntegerLiteral(sqmStatement.getOffset(), 0) : getQueryOptions().getLimit().getFirstRow();
final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null ? getMaxRows(sqmStatement, list.size()) : getQueryOptions().getLimit().getMaxRows();
final List<R> tmp = new ArrayList<>(list.size());
final IdentitySet<Object> distinction = new IdentitySet<>(list.size());
for (final R result : list) {
if (!distinction.add(result)) {
continue;
}
includedCount++;
if (includedCount < first) {
continue;
}
tmp.add(result);
// NOTE : ( max - 1 ) because first is zero-based while max is not...
if (max >= 0 && (includedCount - first) >= (max - 1)) {
break;
}
}
return tmp;
}
return list;
}
use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.
the class SqmSelectionQueryImpl method doList.
protected List<R> doList() {
getSession().prepareForQueryExecution(requiresTxn(getQueryOptions().getLockOptions().findGreatestLockMode()));
final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches();
final boolean hasLimit = hasLimit(sqmStatement, getQueryOptions());
final boolean needsDistinct = containsCollectionFetches && (sqmStatement.usesDistinct() || hasAppliedGraph(getQueryOptions()) || hasLimit);
final DomainQueryExecutionContext executionContextToUse;
if (hasLimit && containsCollectionFetches) {
boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled();
if (fail) {
throw new HibernateException("firstResult/maxResults specified with collection fetch. " + "In memory pagination was about to be applied. " + "Failing because 'Fail on pagination over collection fetch' is enabled.");
} else {
QueryLogging.QUERY_MESSAGE_LOGGER.firstOrMaxResultsSpecifiedWithCollectionFetch();
}
final MutableQueryOptions originalQueryOptions = getQueryOptions();
final QueryOptions normalizedQueryOptions = omitSqlQueryOptions(originalQueryOptions, true, false);
if (originalQueryOptions == normalizedQueryOptions) {
executionContextToUse = this;
} else {
executionContextToUse = new DelegatingDomainQueryExecutionContext(this) {
@Override
public QueryOptions getQueryOptions() {
return normalizedQueryOptions;
}
};
}
} else {
executionContextToUse = this;
}
final List<R> list = resolveQueryPlan().performList(executionContextToUse);
if (needsDistinct) {
int includedCount = -1;
// NOTE : firstRow is zero-based
final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null ? getIntegerLiteral(sqmStatement.getOffset(), 0) : getQueryOptions().getLimit().getFirstRow();
final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null ? getMaxRows(sqmStatement, list.size()) : getQueryOptions().getLimit().getMaxRows();
final List<R> tmp = new ArrayList<>(list.size());
final IdentitySet<Object> distinction = new IdentitySet<>(list.size());
for (final R result : list) {
if (!distinction.add(result)) {
continue;
}
includedCount++;
if (includedCount < first) {
continue;
}
tmp.add(result);
// NOTE : ( max - 1 ) because first is zero-based while max is not...
if (max >= 0 && (includedCount - first) >= (max - 1)) {
break;
}
}
return tmp;
}
return list;
}
Aggregations