Search in sources :

Example 1 with NullnessPredicate

use of org.hibernate.sql.ast.tree.predicate.NullnessPredicate in project hibernate-orm by hibernate.

the class SimpleNaturalIdLoader method applyNaturalIdRestriction.

@Override
protected void applyNaturalIdRestriction(Object bindValue, TableGroup rootTableGroup, Consumer<Predicate> predicateConsumer, BiConsumer<JdbcParameter, JdbcParameterBinding> jdbcParameterConsumer, LoaderSqlAstCreationState sqlAstCreationState, SharedSessionContractImplementor session) {
    if (bindValue == null) {
        naturalIdMapping().getAttribute().forEachSelectable((index, selectable) -> {
            final Expression columnReference = resolveColumnReference(rootTableGroup, selectable, sqlAstCreationState.getSqlExpressionResolver(), session.getFactory());
            predicateConsumer.accept(new NullnessPredicate(columnReference));
        });
    } else {
        naturalIdMapping().getAttribute().breakDownJdbcValues(bindValue, (jdbcValue, jdbcValueMapping) -> {
            final Expression columnReference = resolveColumnReference(rootTableGroup, jdbcValueMapping, sqlAstCreationState.getSqlExpressionResolver(), session.getFactory());
            if (jdbcValue == null) {
                predicateConsumer.accept(new NullnessPredicate(columnReference));
            } else {
                final JdbcParameter jdbcParameter = new JdbcParameterImpl(jdbcValueMapping.getJdbcMapping());
                final ComparisonPredicate predicate = new ComparisonPredicate(columnReference, ComparisonOperator.EQUAL, jdbcParameter);
                predicateConsumer.accept(predicate);
                jdbcParameterConsumer.accept(jdbcParameter, new JdbcParameterBindingImpl(jdbcValueMapping.getJdbcMapping(), jdbcValue));
            }
        }, session);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) JdbcParameterBindingImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingImpl) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)

Example 2 with NullnessPredicate

use of org.hibernate.sql.ast.tree.predicate.NullnessPredicate in project hibernate-orm by hibernate.

the class CompoundNaturalIdLoader method applyNaturalIdRestriction.

@Override
protected void applyNaturalIdRestriction(Object bindValue, TableGroup rootTableGroup, Consumer<Predicate> predicateConsumer, BiConsumer<JdbcParameter, JdbcParameterBinding> jdbcParameterConsumer, LoaderSqlAstCreationState sqlAstCreationState, SharedSessionContractImplementor session) {
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final SessionFactoryImplementor factory = session.getFactory();
    if (bindValue == null) {
        final List<SingularAttributeMapping> naturalIdAttributes = naturalIdMapping().getNaturalIdAttributes();
        for (int i = 0; i < naturalIdAttributes.size(); i++) {
            naturalIdAttributes.get(i).forEachSelectable((index, selectable) -> {
                final Expression columnRef = resolveColumnReference(rootTableGroup, selectable, sqlExpressionResolver, factory);
                predicateConsumer.accept(new NullnessPredicate(columnRef));
            });
        }
        // EARLY EXIT!!
        return;
    }
    naturalIdMapping().breakDownJdbcValues(bindValue, (jdbcValue, jdbcValueMapping) -> {
        final Expression columnReference = resolveColumnReference(rootTableGroup, jdbcValueMapping, sqlExpressionResolver, factory);
        if (jdbcValue == null) {
            predicateConsumer.accept(new NullnessPredicate(columnReference));
        } else {
            final JdbcParameter jdbcParameter = new JdbcParameterImpl(jdbcValueMapping.getJdbcMapping());
            final ComparisonPredicate predicate = new ComparisonPredicate(columnReference, ComparisonOperator.EQUAL, jdbcParameter);
            predicateConsumer.accept(predicate);
            jdbcParameterConsumer.accept(jdbcParameter, new JdbcParameterBindingImpl(jdbcValueMapping.getJdbcMapping(), jdbcValue));
        }
    }, session);
}
Also used : SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) JdbcParameterBindingImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingImpl) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)

Example 3 with NullnessPredicate

use of org.hibernate.sql.ast.tree.predicate.NullnessPredicate in project hibernate-orm by hibernate.

the class CaseStatementDiscriminatorMappingImpl method createCaseSearchedExpression.

private Expression createCaseSearchedExpression(TableGroup entityTableGroup) {
    return new SelfRenderingExpression() {

        CaseSearchedExpression caseSearchedExpression;

        @Override
        public void renderToSql(SqlAppender sqlAppender, SqlAstTranslator<?> walker, SessionFactoryImplementor sessionFactory) {
            if (caseSearchedExpression == null) {
                // todo (6.0): possible optimization is to omit cases for table reference joins, that touch a super class, where a subclass is inner joined due to pruning
                caseSearchedExpression = new CaseSearchedExpression(CaseStatementDiscriminatorMappingImpl.this);
                tableDiscriminatorDetailsMap.forEach((tableName, tableDiscriminatorDetails) -> {
                    final TableReference tableReference = entityTableGroup.getTableReference(entityTableGroup.getNavigablePath(), tableName, false, false);
                    if (tableReference == null) {
                        // assume this is because it is a table that is not part of the processing entity's sub-hierarchy
                        return;
                    }
                    final Predicate predicate = new NullnessPredicate(new ColumnReference(tableReference, tableDiscriminatorDetails.getCheckColumnName(), false, null, null, getJdbcMapping(), getSessionFactory()), true);
                    caseSearchedExpression.when(predicate, new QueryLiteral<>(tableDiscriminatorDetails.getDiscriminatorValue(), getUnderlyingJdbcMappingType()));
                });
            }
            caseSearchedExpression.accept(walker);
        }

        @Override
        public JdbcMappingContainer getExpressionType() {
            return CaseStatementDiscriminatorMappingImpl.this;
        }
    };
}
Also used : SqlAppender(org.hibernate.sql.ast.spi.SqlAppender) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlAstTranslator(org.hibernate.sql.ast.SqlAstTranslator) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 4 with NullnessPredicate

use of org.hibernate.sql.ast.tree.predicate.NullnessPredicate 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)

Aggregations

NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)4 Expression (org.hibernate.sql.ast.tree.expression.Expression)3 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)2 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)2 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)2 JdbcParameterBindingImpl (org.hibernate.sql.exec.internal.JdbcParameterBindingImpl)2 JdbcParameterImpl (org.hibernate.sql.exec.internal.JdbcParameterImpl)2 ArrayList (java.util.ArrayList)1 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)1 SqlAstTranslator (org.hibernate.sql.ast.SqlAstTranslator)1 SqlAppender (org.hibernate.sql.ast.spi.SqlAppender)1 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 QueryLiteral (org.hibernate.sql.ast.tree.expression.QueryLiteral)1 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)1 TableReference (org.hibernate.sql.ast.tree.from.TableReference)1 InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)1 Junction (org.hibernate.sql.ast.tree.predicate.Junction)1