Search in sources :

Example 6 with InListPredicate

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

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

the class BaseSqmToSqlAstConverter method createTreatTypeRestriction.

private Predicate createTreatTypeRestriction(SqmPath<?> lhs, Set<String> subclassEntityNames) {
    final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    // Do what visitSelfInterpretingSqmPath does, except for calling preparingReusablePath
    // as that would register a type usage for the table group that we don't want here
    final DiscriminatorSqmPath discriminatorSqmPath = (DiscriminatorSqmPath) lhs.type();
    registerTypeUsage(discriminatorSqmPath);
    final Expression typeExpression = discriminatorSqmPath.interpret(this, this, jpaQueryComplianceEnabled);
    if (subclassEntityNames.size() == 1) {
        return new ComparisonPredicate(typeExpression, ComparisonOperator.EQUAL, new EntityTypeLiteral(domainModel.findEntityDescriptor(subclassEntityNames.iterator().next())));
    } else {
        final List<Expression> typeLiterals = new ArrayList<>(subclassEntityNames.size());
        for (String subclassEntityName : subclassEntityNames) {
            typeLiterals.add(new EntityTypeLiteral(domainModel.findEntityDescriptor(subclassEntityName)));
        }
        return new InListPredicate(typeExpression, typeLiterals);
    }
}
Also used : MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) EntityTypeLiteral(org.hibernate.sql.ast.tree.expression.EntityTypeLiteral) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmFkExpression(org.hibernate.query.sqm.tree.domain.SqmFkExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) ArrayList(java.util.ArrayList) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)

Example 8 with InListPredicate

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

the class BaseSqmToSqlAstConverter method processInSingleParameter.

@SuppressWarnings("rawtypes")
private Predicate processInSingleParameter(SqmInListPredicate<?> sqmPredicate, SqmParameter<?> sqmParameter, QueryParameterImplementor<?> domainParam, QueryParameterBinding<?> domainParamBinding) {
    final Iterator<?> iterator = domainParamBinding.getBindValues().iterator();
    final InListPredicate inListPredicate = new InListPredicate((Expression) sqmPredicate.getTestExpression().accept(this), sqmPredicate.isNegated(), getBooleanType());
    final FromClauseIndex fromClauseIndex = fromClauseIndexStack.getCurrent();
    if (!iterator.hasNext()) {
        domainParamBinding.setType((MappingModelExpressible) determineValueMapping(sqmPredicate.getTestExpression(), fromClauseIndex));
        return inListPredicate;
    }
    inferrableTypeAccessStack.push(() -> determineValueMapping(sqmPredicate.getTestExpression(), fromClauseIndex));
    try {
        inListPredicate.addExpression(consumeSingleSqmParameter(sqmParameter));
        iterator.next();
        while (iterator.hasNext()) {
            iterator.next();
            // for each bind value create an "expansion"
            final SqmParameter<?> sqmParamToConsume = sqmParameter.copy();
            domainParameterXref.addExpansion(domainParam, sqmParameter, sqmParamToConsume);
            inListPredicate.addExpression(consumeSingleSqmParameter(sqmParamToConsume));
        }
        return inListPredicate;
    } finally {
        inferrableTypeAccessStack.pop();
    }
}
Also used : SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate)

Aggregations

InListPredicate (org.hibernate.sql.ast.tree.predicate.InListPredicate)8 Expression (org.hibernate.sql.ast.tree.expression.Expression)7 ArrayList (java.util.ArrayList)5 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)5 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)4 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)3 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)3 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)3 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)3 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)3 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)3 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)3 NullnessPredicate (org.hibernate.sql.ast.tree.predicate.NullnessPredicate)3 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)3 List (java.util.List)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)2 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)2 SqmInListPredicate (org.hibernate.query.sqm.tree.predicate.SqmInListPredicate)2 NavigablePath (org.hibernate.spi.NavigablePath)2