use of org.hibernate.query.sqm.tree.select.SqmSelectClause in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitSelectClause.
@Override
public SqmSelectClause visitSelectClause(HqlParser.SelectClauseContext ctx) {
// todo (6.0) : primer a select-clause-specific SemanticPathPart into the stack
final int selectionListIndex;
if (ctx.getChild(1) instanceof HqlParser.SelectionListContext) {
selectionListIndex = 1;
} else {
selectionListIndex = 2;
}
final SqmSelectClause selectClause = new SqmSelectClause(selectionListIndex == 2, creationContext.getNodeBuilder());
final HqlParser.SelectionListContext selectionListContext = (HqlParser.SelectionListContext) ctx.getChild(selectionListIndex);
for (ParseTree subCtx : selectionListContext.children) {
if (subCtx instanceof HqlParser.SelectionContext) {
selectClause.addSelection(visitSelection((HqlParser.SelectionContext) subCtx));
}
}
return selectClause;
}
use of org.hibernate.query.sqm.tree.select.SqmSelectClause in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitQuerySpec.
@Override
public QuerySpec visitQuerySpec(SqmQuerySpec<?> sqmQuerySpec) {
final boolean topLevel = getProcessingStateStack().isEmpty();
final QuerySpec sqlQuerySpec = new QuerySpec(topLevel, sqmQuerySpec.getFromClause().getNumberOfRoots());
final SqmSelectClause selectClause = sqmQuerySpec.getSelectClause();
final Predicate originalAdditionalRestrictions = additionalRestrictions;
additionalRestrictions = null;
final boolean trackAliasedNodePositions;
if (trackSelectionsForGroup) {
trackAliasedNodePositions = true;
} else if (sqmQuerySpec.getOrderByClause() != null && sqmQuerySpec.getOrderByClause().hasPositionalSortItem()) {
trackAliasedNodePositions = true;
} else if (sqmQuerySpec.hasPositionalGroupItem()) {
trackAliasedNodePositions = true;
} else {
// Since JPA Criteria queries can use the same expression object in order or group by items,
// we need to track the positions to be able to replace the expression in the items with alias references
// Also see #resolveGroupOrOrderByExpression for more details
trackAliasedNodePositions = statement.getQuerySource() == SqmQuerySource.CRITERIA && (sqmQuerySpec.getOrderByClause() != null || !sqmQuerySpec.getGroupByClauseExpressions().isEmpty());
}
final SqlAstProcessingState processingState;
if (trackAliasedNodePositions) {
processingState = new SqlAstQueryPartProcessingStateImpl(sqlQuerySpec, getCurrentProcessingState(), this, r -> new SqmAliasedNodePositionTracker(r, selectClause.getSelections()), currentClauseStack::getCurrent, deduplicateSelectionItems);
} else {
processingState = new SqlAstQueryPartProcessingStateImpl(sqlQuerySpec, getCurrentProcessingState(), this, currentClauseStack::getCurrent, deduplicateSelectionItems);
}
final SqmQueryPart<?> sqmQueryPart = currentSqmQueryPart;
final boolean originalDeduplicateSelectionItems = deduplicateSelectionItems;
currentSqmQueryPart = sqmQuerySpec;
// In sub-queries, we can never deduplicate the selection items as that might change semantics
deduplicateSelectionItems = false;
pushProcessingState(processingState);
queryTransformers.push(new ArrayList<>());
try {
// we want to visit the from-clause first
visitFromClause(sqmQuerySpec.getFromClause());
visitSelectClause(selectClause);
final SqmWhereClause whereClause = sqmQuerySpec.getWhereClause();
if (whereClause != null) {
sqlQuerySpec.applyPredicate(visitWhereClause(whereClause.getPredicate()));
}
sqlQuerySpec.setGroupByClauseExpressions(visitGroupByClause(sqmQuerySpec.getGroupByClauseExpressions()));
if (sqmQuerySpec.getHavingClausePredicate() != null) {
sqlQuerySpec.setHavingClauseRestrictions(visitHavingClause(sqmQuerySpec.getHavingClausePredicate()));
}
visitOrderByOffsetAndFetch(sqmQuerySpec, sqlQuerySpec);
if (topLevel && statement instanceof SqmSelectStatement<?>) {
if (orderByFragments != null) {
orderByFragments.forEach(entry -> entry.getKey().apply(sqlQuerySpec, entry.getValue(), this));
orderByFragments = null;
}
applyCollectionFilterPredicates(sqlQuerySpec);
}
QuerySpec finalQuerySpec = sqlQuerySpec;
for (QueryTransformer transformer : queryTransformers.getCurrent()) {
finalQuerySpec = transformer.transform(cteContainer, finalQuerySpec, this);
}
return finalQuerySpec;
} finally {
if (additionalRestrictions != null) {
sqlQuerySpec.applyPredicate(additionalRestrictions);
}
additionalRestrictions = originalAdditionalRestrictions;
popProcessingStateStack();
queryTransformers.pop();
currentSqmQueryPart = sqmQueryPart;
deduplicateSelectionItems = originalDeduplicateSelectionItems;
}
}
use of org.hibernate.query.sqm.tree.select.SqmSelectClause in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitSelectClause.
@Override
public SelectClause visitSelectClause(SqmSelectClause selectClause) {
currentClauseStack.push(Clause.SELECT);
try {
final SelectClause sqlSelectClause = currentQuerySpec().getSelectClause();
if (selectClause == null) {
final SqmFrom<?, ?> implicitSelection = determineImplicitSelection((SqmQuerySpec<?>) currentSqmQueryPart);
visitSelection(new SqmSelection<>(implicitSelection, implicitSelection.nodeBuilder()));
} else {
super.visitSelectClause(selectClause);
sqlSelectClause.makeDistinct(selectClause.isDistinct());
}
return sqlSelectClause;
} finally {
currentClauseStack.pop();
}
}
use of org.hibernate.query.sqm.tree.select.SqmSelectClause in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method buildInferredSelectClause.
protected SqmSelectClause buildInferredSelectClause(SqmFromClause fromClause) {
// for now, this is slightly different than the legacy behavior where
// the root and each non-fetched-join was selected. For now, here, we simply
// select the root
final SqmSelectClause selectClause = new SqmSelectClause(false, fromClause.getNumberOfRoots(), creationContext.getNodeBuilder());
fromClause.visitRoots(sqmRoot -> selectClause.addSelection(new SqmSelection<>(sqmRoot, sqmRoot.getAlias(), creationContext.getNodeBuilder())));
return selectClause;
}
use of org.hibernate.query.sqm.tree.select.SqmSelectClause in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitQuery.
@Override
public SqmQuerySpec<Object> visitQuery(HqlParser.QueryContext ctx) {
// noinspection unchecked
final SqmQuerySpec<Object> sqmQuerySpec = (SqmQuerySpec<Object>) currentQuerySpec();
final int fromIndex;
if (ctx.getChild(0) instanceof HqlParser.FromClauseContext) {
fromIndex = 0;
} else {
fromIndex = 1;
}
// visit from-clause first!!!
visitFromClause((HqlParser.FromClauseContext) ctx.getChild(fromIndex));
final SqmSelectClause selectClause;
if (fromIndex == 1) {
selectClause = visitSelectClause((HqlParser.SelectClauseContext) ctx.getChild(0));
} else if (ctx.getChild(ctx.getChildCount() - 1) instanceof HqlParser.SelectClauseContext) {
selectClause = visitSelectClause((HqlParser.SelectClauseContext) ctx.getChild(ctx.getChildCount() - 1));
} else {
if (creationOptions.useStrictJpaCompliance()) {
throw new StrictJpaComplianceViolation("Encountered implicit select-clause, but strict JPQL compliance was requested", StrictJpaComplianceViolation.Type.IMPLICIT_SELECT);
}
log.debugf("Encountered implicit select clause : %s", ctx.getText());
selectClause = buildInferredSelectClause(sqmQuerySpec.getFromClause());
}
sqmQuerySpec.setSelectClause(selectClause);
int currentIndex = fromIndex + 1;
final SqmWhereClause whereClause = new SqmWhereClause(creationContext.getNodeBuilder());
if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.WhereClauseContext) {
whereClause.setPredicate((SqmPredicate) ctx.getChild(currentIndex++).accept(this));
}
sqmQuerySpec.setWhereClause(whereClause);
if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.GroupByClauseContext) {
sqmQuerySpec.setGroupByClauseExpressions(visitGroupByClause((HqlParser.GroupByClauseContext) ctx.getChild(currentIndex++)));
}
if (currentIndex < ctx.getChildCount() && ctx.getChild(currentIndex) instanceof HqlParser.HavingClauseContext) {
sqmQuerySpec.setHavingClausePredicate(visitHavingClause((HqlParser.HavingClauseContext) ctx.getChild(currentIndex)));
}
return sqmQuerySpec;
}
Aggregations