use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitSearchedCaseExpression.
@Override
public CaseSearchedExpression visitSearchedCaseExpression(SqmCaseSearched<?> expression) {
final List<CaseSearchedExpression.WhenFragment> whenFragments = new ArrayList<>(expression.getWhenFragments().size());
final Supplier<MappingModelExpressible<?>> inferenceSupplier = inferrableTypeAccessStack.getCurrent();
MappingModelExpressible<?> resolved = determineCurrentExpressible(expression);
Expression otherwise = null;
for (SqmCaseSearched.WhenFragment<?> whenFragment : expression.getWhenFragments()) {
inferrableTypeAccessStack.push(() -> null);
final Predicate whenPredicate = visitNestedTopLevelPredicate(whenFragment.getPredicate());
inferrableTypeAccessStack.pop();
final MappingModelExpressible<?> alreadyKnown = resolved;
inferrableTypeAccessStack.push(() -> alreadyKnown == null && inferenceSupplier != null ? inferenceSupplier.get() : alreadyKnown);
final Expression resultExpression = (Expression) whenFragment.getResult().accept(this);
inferrableTypeAccessStack.pop();
resolved = (MappingModelExpressible<?>) highestPrecedence(resolved, resultExpression.getExpressionType());
whenFragments.add(new CaseSearchedExpression.WhenFragment(whenPredicate, resultExpression));
}
if (expression.getOtherwise() != null) {
final MappingModelExpressible<?> alreadyKnown = resolved;
inferrableTypeAccessStack.push(() -> alreadyKnown == null && inferenceSupplier != null ? inferenceSupplier.get() : alreadyKnown);
otherwise = (Expression) expression.getOtherwise().accept(this);
inferrableTypeAccessStack.pop();
resolved = (MappingModelExpressible<?>) highestPrecedence(resolved, otherwise.getExpressionType());
}
return new CaseSearchedExpression(resolved, whenFragments, otherwise);
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitUpdateStatement.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Update statement
@Override
public UpdateStatement visitUpdateStatement(SqmUpdateStatement<?> sqmStatement) {
final CteContainer cteContainer = this.visitCteContainer(sqmStatement);
final SqmRoot<?> sqmTarget = sqmStatement.getTarget();
final String entityName = sqmTarget.getEntityName();
final EntityPersister entityDescriptor = getCreationContext().getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
assert entityDescriptor != null;
pushProcessingState(new SqlAstProcessingStateImpl(getCurrentProcessingState(), this, getCurrentClauseStack()::getCurrent));
try {
final NavigablePath rootPath = sqmTarget.getNavigablePath();
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, rootPath, sqmStatement.getRoot().getAlias(), () -> predicate -> additionalRestrictions = SqlAstTreeHelper.combinePredicates(additionalRestrictions, predicate), this, getCreationContext());
if (!rootTableGroup.getTableReferenceJoins().isEmpty()) {
throw new HibernateException("Not expecting multiple table references for an SQM UPDATE");
}
if (sqmTarget.hasJoins()) {
throw new HibernateException("SQM UPDATE does not support explicit joins");
}
getFromClauseAccess().registerTableGroup(rootPath, rootTableGroup);
final List<Assignment> assignments = visitSetClause(sqmStatement.getSetClause());
addVersionedAssignment(assignments::add, sqmStatement);
FilterHelper.applyBaseRestrictions((filterPredicate) -> additionalRestrictions = filterPredicate, entityDescriptor, rootTableGroup, AbstractSqlAstTranslator.rendersTableReferenceAlias(Clause.UPDATE), getLoadQueryInfluencers(), this);
Predicate suppliedPredicate = null;
final SqmWhereClause whereClause = sqmStatement.getWhereClause();
if (whereClause != null) {
suppliedPredicate = visitWhereClause(whereClause.getPredicate());
}
return new UpdateStatement(cteContainer, (NamedTableReference) rootTableGroup.getPrimaryTableReference(), assignments, SqlAstTreeHelper.combinePredicates(suppliedPredicate, additionalRestrictions), Collections.emptyList());
} finally {
popProcessingStateStack();
}
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class SingleTableEntityPersister method createRootTableGroup.
@Override
public TableGroup createRootTableGroup(boolean canUseInnerJoins, NavigablePath navigablePath, String explicitSourceAlias, Supplier<Consumer<Predicate>> additionalPredicateCollectorAccess, SqlAliasBase sqlAliasBase, SqlExpressionResolver expressionResolver, FromClauseAccess fromClauseAccess, SqlAstCreationContext creationContext) {
final TableGroup tableGroup = super.createRootTableGroup(canUseInnerJoins, navigablePath, explicitSourceAlias, additionalPredicateCollectorAccess, sqlAliasBase, expressionResolver, fromClauseAccess, creationContext);
if (additionalPredicateCollectorAccess != null && needsDiscriminator()) {
final Predicate discriminatorPredicate = createDiscriminatorPredicate(tableGroup.getPrimaryTableReference().getIdentificationVariable(), tableGroup, expressionResolver);
additionalPredicateCollectorAccess.get().accept(discriminatorPredicate);
}
return tableGroup;
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class SingleTableEntityPersister method createDiscriminatorPredicate.
private Predicate createDiscriminatorPredicate(String alias, TableGroup tableGroup, SqlExpressionResolver sqlExpressionResolver) {
final String columnReferenceKey;
final String discriminatorExpression;
if (isDiscriminatorFormula()) {
discriminatorExpression = getDiscriminatorFormulaTemplate();
columnReferenceKey = SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), getDiscriminatorFormulaTemplate());
} else {
discriminatorExpression = getDiscriminatorColumnName();
columnReferenceKey = SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), getDiscriminatorColumnName());
}
final BasicType<?> discriminatorType = (BasicType<?>) getDiscriminatorType();
final Expression sqlExpression = sqlExpressionResolver.resolveSqlExpression(columnReferenceKey, sqlAstProcessingState -> new ColumnReference(alias, discriminatorExpression, isDiscriminatorFormula(), null, null, discriminatorType.getJdbcMapping(), getFactory()));
if (hasSubclasses()) {
final List<Expression> values = new ArrayList<>(fullDiscriminatorValues.length);
boolean hasNull = false, hasNonNull = false;
for (Object discriminatorValue : fullDiscriminatorValues) {
if (discriminatorValue == DiscriminatorHelper.NULL_DISCRIMINATOR) {
hasNull = true;
} else if (discriminatorValue == DiscriminatorHelper.NOT_NULL_DISCRIMINATOR) {
hasNonNull = true;
} else {
values.add(new QueryLiteral<>(discriminatorValue, discriminatorType));
}
}
final Predicate p = new InListPredicate(sqlExpression, values);
if (hasNull || hasNonNull) {
final Junction junction = new Junction(Junction.Nature.DISJUNCTION);
// so we return an empty Junction
if (hasNull && hasNonNull) {
return junction;
}
junction.add(new NullnessPredicate(sqlExpression));
junction.add(p);
return junction;
}
return p;
}
final Object value = getDiscriminatorValue();
final boolean hasNotNullDiscriminator = value == DiscriminatorHelper.NOT_NULL_DISCRIMINATOR;
final boolean hasNullDiscriminator = value == DiscriminatorHelper.NULL_DISCRIMINATOR;
if (hasNotNullDiscriminator || hasNullDiscriminator) {
final NullnessPredicate nullnessPredicate = new NullnessPredicate(sqlExpression);
if (hasNotNullDiscriminator) {
return new NegatedPredicate(nullnessPredicate);
}
return nullnessPredicate;
}
return new ComparisonPredicate(sqlExpression, ComparisonOperator.EQUAL, new QueryLiteral<>(value, discriminatorType));
}
use of org.hibernate.sql.ast.tree.predicate.Predicate in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method renderTableGroupJoin.
protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGroupJoin> tableGroupJoinCollector) {
appendSql(WHITESPACE);
appendSql(tableGroupJoin.getJoinType().getText());
appendSql("join ");
final Predicate predicate;
if (tableGroupJoin.getPredicate() == null) {
if (tableGroupJoin.getJoinType() == SqlAstJoinType.CROSS) {
predicate = null;
} else {
predicate = new BooleanExpressionPredicate(new QueryLiteral<>(true, getBooleanType()));
}
} else {
predicate = tableGroupJoin.getPredicate();
}
if (predicate != null && !predicate.isEmpty()) {
renderTableGroup(tableGroupJoin.getJoinedGroup(), predicate, tableGroupJoinCollector);
} else {
renderTableGroup(tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector);
}
}
Aggregations