use of org.hibernate.query.sqm.tree.SqmStatement in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitInsertSelectStatement.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Insert-select statement
@Override
public InsertStatement visitInsertSelectStatement(SqmInsertSelectStatement<?> sqmStatement) {
final CteContainer cteContainer = this.visitCteContainer(sqmStatement);
final String entityName = sqmStatement.getTarget().getEntityName();
final EntityPersister entityDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
assert entityDescriptor != null;
SqmQueryPart<?> selectQueryPart = sqmStatement.getSelectQueryPart();
pushProcessingState(new SqlAstProcessingStateImpl(null, this, r -> new SqmAliasedNodePositionTracker(r, selectQueryPart.getFirstQuerySpec().getSelectClause().getSelections()), getCurrentClauseStack()::getCurrent));
currentClauseStack.push(Clause.INSERT);
final InsertStatement insertStatement;
final AdditionalInsertValues additionalInsertValues;
try {
final NavigablePath rootPath = sqmStatement.getTarget().getNavigablePath();
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, rootPath, sqmStatement.getTarget().getExplicitAlias(), () -> predicate -> additionalRestrictions = SqlAstTreeHelper.combinePredicates(additionalRestrictions, predicate), this, getCreationContext());
getFromClauseAccess().registerTableGroup(rootPath, rootTableGroup);
insertStatement = new InsertStatement(cteContainer, (NamedTableReference) rootTableGroup.getPrimaryTableReference(), Collections.emptyList());
additionalInsertValues = visitInsertionTargetPaths((assigable, references) -> insertStatement.addTargetColumnReferences(references), sqmStatement, entityDescriptor, rootTableGroup);
if (!rootTableGroup.getTableReferenceJoins().isEmpty() || !rootTableGroup.getTableGroupJoins().isEmpty()) {
throw new SemanticException("Not expecting multiple table references for an SQM INSERT-SELECT");
}
} finally {
popProcessingStateStack();
currentClauseStack.pop();
}
insertStatement.setSourceSelectStatement(visitQueryPart(selectQueryPart));
insertStatement.getSourceSelectStatement().visitQuerySpecs(querySpec -> {
final boolean appliedRowNumber = additionalInsertValues.applySelections(querySpec, creationContext.getSessionFactory());
// If this requires the special row number handling, it should use the mutation strategy
assert !appliedRowNumber;
});
return insertStatement;
}
Aggregations