use of org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl in project hibernate-orm by hibernate.
the class MultiTableSqmMutationConverter method visitWhereClause.
public Predicate visitWhereClause(SqmWhereClause sqmWhereClause, Consumer<ColumnReference> restrictionColumnReferenceConsumer, SqmParameterResolutionConsumer parameterResolutionConsumer) {
this.parameterResolutionConsumer = parameterResolutionConsumer;
if (sqmWhereClause == null || sqmWhereClause.getPredicate() == null) {
return null;
}
final SqlAstProcessingState rootProcessingState = getCurrentProcessingState();
final SqlAstProcessingStateImpl restrictionProcessingState = new SqlAstProcessingStateImpl(rootProcessingState, this, getCurrentClauseStack()::getCurrent) {
@Override
public SqlExpressionResolver getSqlExpressionResolver() {
return this;
}
@Override
public Expression resolveSqlExpression(String key, Function<SqlAstProcessingState, Expression> creator) {
final Expression expression = rootProcessingState.getSqlExpressionResolver().resolveSqlExpression(key, creator);
if (expression instanceof ColumnReference) {
restrictionColumnReferenceConsumer.accept((ColumnReference) expression);
}
return expression;
}
};
pushProcessingState(restrictionProcessingState, getFromClauseIndex());
try {
return SqlAstHelper.combinePredicates((Predicate) sqmWhereClause.getPredicate().accept(this), discriminatorPredicate);
} finally {
popProcessingStateStack();
this.parameterResolutionConsumer = null;
}
}
use of org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitDeleteStatement.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Delete statement
@Override
public DeleteStatement visitDeleteStatement(SqmDeleteStatement<?> statement) {
final CteContainer cteContainer = this.visitCteContainer(statement);
final String entityName = statement.getTarget().getEntityName();
final EntityPersister entityDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
assert entityDescriptor != null;
pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent));
try {
final NavigablePath rootPath = statement.getTarget().getNavigablePath();
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, rootPath, statement.getRoot().getAlias(), () -> predicate -> additionalRestrictions = SqlAstTreeHelper.combinePredicates(additionalRestrictions, predicate), this, getCreationContext());
getFromClauseAccess().registerTableGroup(rootPath, rootTableGroup);
if (!rootTableGroup.getTableReferenceJoins().isEmpty()) {
throw new HibernateException("Not expecting multiple table references for an SQM DELETE");
}
FilterHelper.applyBaseRestrictions((filterPredicate) -> additionalRestrictions = filterPredicate, entityDescriptor, rootTableGroup, AbstractSqlAstTranslator.rendersTableReferenceAlias(Clause.DELETE), getLoadQueryInfluencers(), this);
Predicate suppliedPredicate = null;
final SqmWhereClause whereClause = statement.getWhereClause();
if (whereClause != null) {
suppliedPredicate = visitWhereClause(whereClause.getPredicate());
}
return new DeleteStatement(cteContainer, (NamedTableReference) rootTableGroup.getPrimaryTableReference(), SqlAstTreeHelper.combinePredicates(suppliedPredicate, additionalRestrictions), Collections.emptyList());
} finally {
popProcessingStateStack();
}
}
use of org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitSetClause.
@Override
public List<Assignment> visitSetClause(SqmSetClause setClause) {
final List<Assignment> assignments = new ArrayList<>(setClause.getAssignments().size());
for (SqmAssignment sqmAssignment : setClause.getAssignments()) {
final List<ColumnReference> targetColumnReferences = new ArrayList<>();
pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent) {
@Override
public Expression resolveSqlExpression(String key, Function<SqlAstProcessingState, Expression> creator) {
final Expression expression = getParentState().getSqlExpressionResolver().resolveSqlExpression(key, creator);
assert expression instanceof ColumnReference;
targetColumnReferences.add((ColumnReference) expression);
return expression;
}
}, getFromClauseIndex());
final SqmPathInterpretation<?> assignedPathInterpretation;
try {
assignedPathInterpretation = (SqmPathInterpretation<?>) sqmAssignment.getTargetPath().accept(this);
} finally {
popProcessingStateStack();
}
inferrableTypeAccessStack.push(assignedPathInterpretation::getExpressionType);
// final List<ColumnReference> valueColumnReferences = new ArrayList<>();
pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent) {
@Override
public Expression resolveSqlExpression(String key, Function<SqlAstProcessingState, Expression> creator) {
final Expression expression = getParentState().getSqlExpressionResolver().resolveSqlExpression(key, creator);
assert expression instanceof ColumnReference;
// valueColumnReferences.add( (ColumnReference) expression );
return expression;
}
}, getFromClauseIndex());
try {
final SqmExpression<?> assignmentValue = sqmAssignment.getValue();
final SqmParameter<?> assignmentValueParameter = getSqmParameter(assignmentValue);
if (assignmentValueParameter != null) {
consumeSqmParameter(assignmentValueParameter, assignedPathInterpretation.getExpressionType(), (index, jdbcParameter) -> assignments.add(new Assignment(targetColumnReferences.get(index), jdbcParameter)));
} else {
final Expression valueExpression = (Expression) assignmentValue.accept(this);
final int valueExprJdbcCount = getKeyExpressible(valueExpression.getExpressionType()).getJdbcTypeCount();
final int assignedPathJdbcCount = getKeyExpressible(assignedPathInterpretation.getExpressionType()).getJdbcTypeCount();
if (valueExprJdbcCount != assignedPathJdbcCount) {
SqlTreeCreationLogger.LOGGER.debugf("JDBC type count does not match in UPDATE assignment between the assigned-path and the assigned-value; " + "this will likely lead to problems executing the query");
}
assert assignedPathJdbcCount == valueExprJdbcCount;
for (ColumnReference columnReference : targetColumnReferences) {
assignments.add(new Assignment(columnReference, valueExpression));
}
}
} finally {
popProcessingStateStack();
inferrableTypeAccessStack.pop();
}
}
return assignments;
}
use of org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitIsEmptyPredicate.
@Override
public Object visitIsEmptyPredicate(SqmEmptinessPredicate predicate) {
prepareReusablePath(predicate.getPluralPath(), () -> null);
final QuerySpec subQuerySpec = new QuerySpec(false, 1);
final FromClauseAccess parentFromClauseAccess = getFromClauseAccess();
final SqlAstProcessingStateImpl subQueryState = new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, currentClauseStack::getCurrent);
pushProcessingState(subQueryState);
try {
final SqmPluralValuedSimplePath<?> sqmPluralPath = predicate.getPluralPath();
final NavigablePath pluralPathNavPath = sqmPluralPath.getNavigablePath();
final NavigablePath parentNavPath = pluralPathNavPath.getParent();
assert parentNavPath != null;
final TableGroup parentTableGroup = parentFromClauseAccess.getTableGroup(parentNavPath);
final SqlAliasBase sqlAliasBase = sqlAliasBaseManager.createSqlAliasBase(parentTableGroup.getGroupAlias());
final TableGroup tableGroup = new CorrelatedTableGroup(parentTableGroup, sqlAliasBase, subQuerySpec, subQuerySpec::applyPredicate, creationContext.getSessionFactory());
subQueryState.getSqlAstCreationState().getFromClauseAccess().registerTableGroup(parentNavPath, tableGroup);
registerPluralTableGroupParts(tableGroup);
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) visitPluralValuedPath(sqmPluralPath).getExpressionType();
// The creation of the table group join against the correlated table group
// has the side effect that the from and where clause of the sub-query are set
tableGroup.addTableGroupJoin(pluralAttributeMapping.createTableGroupJoin(pluralPathNavPath, tableGroup, sqmPluralPath.getExplicitAlias(), SqlAstJoinType.INNER, false, false, sqlAliasBaseManager, subQueryState, this, creationContext));
final ForeignKeyDescriptor collectionKeyDescriptor = pluralAttributeMapping.getKeyDescriptor();
final int jdbcTypeCount = collectionKeyDescriptor.getJdbcTypeCount();
assert jdbcTypeCount > 0;
final JdbcLiteral<Integer> jdbcLiteral = new JdbcLiteral<>(1, basicType(Integer.class));
subQuerySpec.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, jdbcLiteral));
return new ExistsPredicate(subQuerySpec, !predicate.isNegated(), getBooleanType());
} finally {
popProcessingStateStack();
}
}
use of org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitInsertValuesStatement.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Insert-values statement
@Override
public InsertStatement visitInsertValuesStatement(SqmInsertValuesStatement<?> 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;
pushProcessingState(new SqlAstProcessingStateImpl(null, this, getCurrentClauseStack()::getCurrent));
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());
if (!rootTableGroup.getTableReferenceJoins().isEmpty() || !rootTableGroup.getTableGroupJoins().isEmpty()) {
throw new HibernateException("Not expecting multiple table references for an SQM INSERT-SELECT");
}
getFromClauseAccess().registerTableGroup(rootPath, rootTableGroup);
final InsertStatement insertStatement = new InsertStatement(cteContainer, (NamedTableReference) rootTableGroup.getPrimaryTableReference(), Collections.emptyList());
final AdditionalInsertValues 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");
}
for (SqmValues sqmValues : sqmStatement.getValuesList()) {
final Values values = visitValues(sqmValues);
additionalInsertValues.applyValues(values);
insertStatement.getValuesList().add(values);
}
return insertStatement;
} finally {
popProcessingStateStack();
}
}
Aggregations