use of org.hibernate.sql.ast.tree.select.QueryPart in project hibernate-orm by hibernate.
the class ExpressionReplacementWalker method visitInSubQueryPredicate.
@Override
public void visitInSubQueryPredicate(InSubQueryPredicate inSubQueryPredicate) {
final Expression testExpression = replaceExpression(inSubQueryPredicate.getTestExpression());
final QueryPart subQuery = replaceExpression(inSubQueryPredicate.getSubQuery());
if (testExpression != inSubQueryPredicate.getTestExpression() || subQuery != inSubQueryPredicate.getSubQuery()) {
returnedNode = new InSubQueryPredicate(testExpression, subQuery, inSubQueryPredicate.isNegated(), inSubQueryPredicate.getExpressionType());
} else {
returnedNode = inSubQueryPredicate;
}
}
use of org.hibernate.sql.ast.tree.select.QueryPart in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitSelection.
@Override
public Void visitSelection(SqmSelection<?> sqmSelection) {
final List<Map.Entry<String, DomainResultProducer<?>>> resultProducers;
final SqmSelectableNode<?> selectionNode = sqmSelection.getSelectableNode();
if (selectionNode instanceof SqmJpaCompoundSelection<?>) {
final SqmJpaCompoundSelection<?> selectableNode = (SqmJpaCompoundSelection<?>) selectionNode;
resultProducers = new ArrayList<>(selectableNode.getSelectionItems().size());
for (SqmSelectableNode<?> selectionItem : selectableNode.getSelectionItems()) {
if (selectionItem instanceof SqmPath<?>) {
prepareForSelection((SqmPath<?>) selectionItem);
}
resultProducers.add(new AbstractMap.SimpleEntry<>(selectionItem.getAlias(), (DomainResultProducer<?>) selectionItem.accept(this)));
}
} else {
if (selectionNode instanceof SqmPath<?>) {
prepareForSelection((SqmPath<?>) selectionNode);
}
resultProducers = Collections.singletonList(new AbstractMap.SimpleEntry<>(sqmSelection.getAlias(), (DomainResultProducer<?>) selectionNode.accept(this)));
}
final Stack<SqlAstProcessingState> processingStateStack = getProcessingStateStack();
final boolean needsDomainResults = domainResults != null && currentClauseContributesToTopLevelSelectClause();
final boolean collectDomainResults;
if (processingStateStack.depth() == 1) {
collectDomainResults = needsDomainResults;
} else {
final SqlAstProcessingState current = processingStateStack.getCurrent();
// Since we only want to create domain results for the first/left-most query spec within query groups,
// we have to check if the current query spec is the left-most.
// This is the case when all upper level in-flight query groups are still empty
collectDomainResults = needsDomainResults && processingStateStack.findCurrentFirst(processingState -> {
if (!(processingState instanceof SqlAstQueryPartProcessingState)) {
return Boolean.FALSE;
}
if (processingState == current) {
return null;
}
final QueryPart part = ((SqlAstQueryPartProcessingState) processingState).getInflightQueryPart();
if (part instanceof QueryGroup) {
if (((QueryGroup) part).getQueryParts().isEmpty()) {
return null;
}
}
return Boolean.FALSE;
}) == null;
}
// arguments
if (collectDomainResults) {
resultProducers.forEach(entry -> {
if (!(entry.getValue() instanceof DynamicInstantiation<?>)) {
currentSqlSelectionCollector().next();
}
domainResults.add(entry.getValue().createDomainResult(entry.getKey(), this));
});
} else if (needsDomainResults) {
// We just create domain results for the purpose of creating selections
// This is necessary for top-level query specs within query groups to avoid cycles
resultProducers.forEach(entry -> {
if (!(entry.getValue() instanceof DynamicInstantiation<?>)) {
currentSqlSelectionCollector().next();
}
entry.getValue().createDomainResult(entry.getKey(), this);
});
} else {
resultProducers.forEach(entry -> {
if (!(entry.getValue() instanceof DynamicInstantiation<?>)) {
currentSqlSelectionCollector().next();
}
entry.getValue().applySqlSelections(this);
});
}
return null;
}
use of org.hibernate.sql.ast.tree.select.QueryPart in project hibernate-orm by hibernate.
the class SqlTreePrinter method logTableGroupDetails.
private void logTableGroupDetails(TableGroup tableGroup) {
if (tableGroup instanceof LazyTableGroup) {
TableGroup underlyingTableGroup = ((LazyTableGroup) tableGroup).getUnderlyingTableGroup();
if (underlyingTableGroup != null) {
logTableGroupDetails(underlyingTableGroup);
}
return;
}
if (tableGroup.getPrimaryTableReference() instanceof NamedTableReference) {
logWithIndentation("primaryTableReference : %s as %s", tableGroup.getPrimaryTableReference().getTableId(), tableGroup.getPrimaryTableReference().getIdentificationVariable());
} else {
if (tableGroup.getPrimaryTableReference() instanceof ValuesTableReference) {
logWithIndentation("primaryTableReference : values (..) as %s", tableGroup.getPrimaryTableReference().getIdentificationVariable());
} else if (tableGroup.getPrimaryTableReference() instanceof FunctionTableReference) {
logWithIndentation("primaryTableReference : %s(...) as %s", ((FunctionTableReference) tableGroup.getPrimaryTableReference()).getFunctionExpression().getFunctionName(), tableGroup.getPrimaryTableReference().getIdentificationVariable());
} else {
logNode("PrimaryTableReference as " + tableGroup.getPrimaryTableReference().getIdentificationVariable(), () -> {
QueryPart queryPart = ((QueryPartTableReference) tableGroup.getPrimaryTableReference()).getQueryPart();
visitQueryPart(queryPart);
});
}
}
final List<TableReferenceJoin> tableReferenceJoins = tableGroup.getTableReferenceJoins();
if (!tableReferenceJoins.isEmpty()) {
logNode("TableReferenceJoins", () -> {
for (TableReferenceJoin join : tableReferenceJoins) {
logWithIndentation("%s join %s as %s", join.getJoinType().getText(), join.getJoinedTableReference().getTableExpression(), join.getJoinedTableReference().getIdentificationVariable());
}
});
}
final List<TableGroupJoin> nestedTableGroupJoins = tableGroup.getNestedTableGroupJoins();
if (!nestedTableGroupJoins.isEmpty()) {
logNode("NestedTableGroupJoins", () -> tableGroup.visitNestedTableGroupJoins(this::visitTableGroupJoin));
}
final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
if (!tableGroupJoins.isEmpty()) {
logNode("TableGroupJoins", () -> tableGroup.visitTableGroupJoins(this::visitTableGroupJoin));
}
}
use of org.hibernate.sql.ast.tree.select.QueryPart in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method renderQueryGroup.
protected void renderQueryGroup(QueryGroup queryGroup, boolean renderOrderByAndOffsetFetchClause) {
final QueryPart queryPartForRowNumbering = this.queryPartForRowNumbering;
final int queryPartForRowNumberingClauseDepth = this.queryPartForRowNumberingClauseDepth;
final boolean needsSelectAliases = this.needsSelectAliases;
try {
String queryGroupAlias = null;
// See the field documentation of queryPartForRowNumbering etc. for an explanation about this
final QueryPart currentQueryPart = queryPartStack.getCurrent();
if (currentQueryPart != null && queryPartForRowNumberingClauseDepth != clauseStack.depth()) {
this.queryPartForRowNumbering = null;
this.queryPartForRowNumberingClauseDepth = -1;
this.needsSelectAliases = false;
}
final boolean needsParenthesis = !queryGroup.isRoot();
if (needsParenthesis) {
appendSql(OPEN_PARENTHESIS);
}
// order by and offset fetch clause, so we must do row counting on the query group level
if (queryPartForRowNumbering == queryGroup || additionalWherePredicate != null && !additionalWherePredicate.isEmpty()) {
this.needsSelectAliases = true;
queryGroupAlias = "grp_" + queryGroupAliasCounter + '_';
queryGroupAliasCounter++;
appendSql("select ");
appendSql(queryGroupAlias);
appendSql(".* ");
final SelectClause firstSelectClause = queryGroup.getFirstQuerySpec().getSelectClause();
final List<SqlSelection> sqlSelections = firstSelectClause.getSqlSelections();
final int sqlSelectionsSize = sqlSelections.size();
// We need this synthetic select clause to properly render the ORDER BY within the OVER clause
// of the row numbering functions
final SelectClause syntheticSelectClause = new SelectClause(sqlSelectionsSize);
for (int i = 0; i < sqlSelectionsSize; i++) {
syntheticSelectClause.addSqlSelection(new SqlSelectionImpl(i + 1, i, new ColumnReference(queryGroupAlias, "c" + i, false, null, null, getIntegerType(), null)));
}
renderRowNumberingSelectItems(syntheticSelectClause, queryPartForRowNumbering);
appendSql(" from (");
}
queryPartStack.push(queryGroup);
final List<QueryPart> queryParts = queryGroup.getQueryParts();
final String setOperatorString = ' ' + queryGroup.getSetOperator().sqlString() + ' ';
String separator = "";
for (int i = 0; i < queryParts.size(); i++) {
appendSql(separator);
queryParts.get(i).accept(this);
separator = setOperatorString;
}
if (renderOrderByAndOffsetFetchClause) {
visitOrderBy(queryGroup.getSortSpecifications());
visitOffsetFetchClause(queryGroup);
}
if (queryGroupAlias != null) {
appendSql(") ");
appendSql(queryGroupAlias);
if (additionalWherePredicate != null && !additionalWherePredicate.isEmpty()) {
visitWhereClause(additionalWherePredicate);
}
}
if (needsParenthesis) {
appendSql(CLOSE_PARENTHESIS);
}
} finally {
queryPartStack.pop();
this.queryPartForRowNumbering = queryPartForRowNumbering;
this.queryPartForRowNumberingClauseDepth = queryPartForRowNumberingClauseDepth;
this.needsSelectAliases = needsSelectAliases;
}
}
Aggregations