use of org.hibernate.query.sqm.internal.SqmCreationProcessingStateImpl in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitInsertStatement.
@Override
public SqmInsertStatement<R> visitInsertStatement(HqlParser.InsertStatementContext ctx) {
final int dmlTargetIndex;
if (ctx.getChild(1) instanceof HqlParser.TargetEntityContext) {
dmlTargetIndex = 1;
} else {
dmlTargetIndex = 2;
}
final HqlParser.TargetEntityContext dmlTargetContext = (HqlParser.TargetEntityContext) ctx.getChild(dmlTargetIndex);
final HqlParser.TargetFieldsContext targetFieldsSpecContext = (HqlParser.TargetFieldsContext) ctx.getChild(dmlTargetIndex + 1);
final SqmRoot<R> root = visitTargetEntity(dmlTargetContext);
if (root.getReferencedPathSource() instanceof SqmPolymorphicRootDescriptor<?>) {
throw new SemanticException(String.format("Target type '%s' in insert statement is not an entity", root.getReferencedPathSource().getHibernateEntityName()));
}
final HqlParser.QueryExpressionContext queryExpressionContext = ctx.queryExpression();
if (queryExpressionContext != null) {
final SqmInsertSelectStatement<R> insertStatement = new SqmInsertSelectStatement<>(root, creationContext.getNodeBuilder());
parameterCollector = insertStatement;
final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(insertStatement, this);
processingStateStack.push(processingState);
try {
queryExpressionContext.accept(this);
final SqmCreationProcessingState stateFieldsProcessingState = new SqmCreationProcessingStateImpl(insertStatement, this);
stateFieldsProcessingState.getPathRegistry().register(root);
processingStateStack.push(stateFieldsProcessingState);
try {
for (HqlParser.SimplePathContext stateFieldCtx : targetFieldsSpecContext.simplePath()) {
final SqmPath<?> stateField = (SqmPath<?>) visitSimplePath(stateFieldCtx);
// todo : validate each resolved stateField...
insertStatement.addInsertTargetStateField(stateField);
}
} finally {
processingStateStack.pop();
}
return insertStatement;
} finally {
processingStateStack.pop();
}
} else {
final SqmInsertValuesStatement<R> insertStatement = new SqmInsertValuesStatement<>(root, creationContext.getNodeBuilder());
parameterCollector = insertStatement;
final SqmDmlCreationProcessingState processingState = new SqmDmlCreationProcessingState(insertStatement, this);
processingStateStack.push(processingState);
processingState.getPathRegistry().register(root);
try {
final HqlParser.ValuesListContext valuesListContext = ctx.valuesList();
for (int i = 1; i < valuesListContext.getChildCount(); i += 2) {
final ParseTree values = valuesListContext.getChild(i);
final SqmValues sqmValues = new SqmValues();
for (int j = 1; j < values.getChildCount(); j += 2) {
sqmValues.getExpressions().add((SqmExpression<?>) values.getChild(j).accept(this));
}
insertStatement.getValuesList().add(sqmValues);
}
for (HqlParser.SimplePathContext stateFieldCtx : targetFieldsSpecContext.simplePath()) {
final SqmPath<?> stateField = (SqmPath<?>) visitSimplePath(stateFieldCtx);
// todo : validate each resolved stateField...
insertStatement.addInsertTargetStateField(stateField);
}
return insertStatement;
} finally {
processingStateStack.pop();
}
}
}
Aggregations