Search in sources :

Example 36 with Predicate

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);
}
Also used : MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) ArrayList(java.util.ArrayList) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SqmCaseSearched(org.hibernate.query.sqm.tree.expression.SqmCaseSearched) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmBooleanExpressionPredicate(org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) SqmNegatedPredicate(org.hibernate.query.sqm.tree.predicate.SqmNegatedPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) SqmAndPredicate(org.hibernate.query.sqm.tree.predicate.SqmAndPredicate) SqmMemberOfPredicate(org.hibernate.query.sqm.tree.predicate.SqmMemberOfPredicate) SqmLikePredicate(org.hibernate.query.sqm.tree.predicate.SqmLikePredicate) SqmExistsPredicate(org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate) SqmOrPredicate(org.hibernate.query.sqm.tree.predicate.SqmOrPredicate) SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmGroupedPredicate(org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) SqmBetweenPredicate(org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) SqmEmptinessPredicate(org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 37 with Predicate

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();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SingleTableEntityPersister(org.hibernate.persister.entity.SingleTableEntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SqmCteContainer(org.hibernate.query.sqm.tree.cte.SqmCteContainer) CteContainer(org.hibernate.sql.ast.tree.cte.CteContainer) UpdateStatement(org.hibernate.sql.ast.tree.update.UpdateStatement) SqmUpdateStatement(org.hibernate.query.sqm.tree.update.SqmUpdateStatement) NavigablePath(org.hibernate.query.spi.NavigablePath) VirtualTableGroup(org.hibernate.sql.ast.tree.from.VirtualTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) HibernateException(org.hibernate.HibernateException) SqlAstProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmBooleanExpressionPredicate(org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) SqmNegatedPredicate(org.hibernate.query.sqm.tree.predicate.SqmNegatedPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) SqmAndPredicate(org.hibernate.query.sqm.tree.predicate.SqmAndPredicate) SqmMemberOfPredicate(org.hibernate.query.sqm.tree.predicate.SqmMemberOfPredicate) SqmLikePredicate(org.hibernate.query.sqm.tree.predicate.SqmLikePredicate) SqmExistsPredicate(org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate) SqmOrPredicate(org.hibernate.query.sqm.tree.predicate.SqmOrPredicate) SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmGroupedPredicate(org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) SqmBetweenPredicate(org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) SqmEmptinessPredicate(org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Assignment(org.hibernate.sql.ast.tree.update.Assignment) SqmAssignment(org.hibernate.query.sqm.tree.update.SqmAssignment) SqmWhereClause(org.hibernate.query.sqm.tree.predicate.SqmWhereClause)

Example 38 with Predicate

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;
}
Also used : TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 39 with Predicate

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));
}
Also used : BasicType(org.hibernate.type.BasicType) ArrayList(java.util.ArrayList) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Expression(org.hibernate.sql.ast.tree.expression.Expression) Junction(org.hibernate.sql.ast.tree.predicate.Junction) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 40 with Predicate

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);
    }
}
Also used : ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Aggregations

Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)40 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)25 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)24 NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)24 BooleanExpressionPredicate (org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate)23 InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)23 NegatedPredicate (org.hibernate.sql.ast.tree.predicate.NegatedPredicate)23 BetweenPredicate (org.hibernate.sql.ast.tree.predicate.BetweenPredicate)21 ExistsPredicate (org.hibernate.sql.ast.tree.predicate.ExistsPredicate)21 GroupedPredicate (org.hibernate.sql.ast.tree.predicate.GroupedPredicate)21 LikePredicate (org.hibernate.sql.ast.tree.predicate.LikePredicate)21 SelfRenderingPredicate (org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate)21 ArrayList (java.util.ArrayList)14 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)14 Expression (org.hibernate.sql.ast.tree.expression.Expression)14 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)12 SqmAndPredicate (org.hibernate.query.sqm.tree.predicate.SqmAndPredicate)12 SqmBetweenPredicate (org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate)12 SqmBooleanExpressionPredicate (org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate)12 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)12