Search in sources :

Example 1 with JdbcLiteral

use of org.hibernate.sql.ast.tree.expression.JdbcLiteral in project hibernate-orm by hibernate.

the class InPredicateRestrictionProducer method produceRestriction.

@Override
public InListPredicate produceRestriction(List<?> matchingIdValues, EntityMappingType entityDescriptor, int valueIndex, ModelPart valueModelPart, TableReference mutatingTableReference, Supplier<Consumer<SelectableConsumer>> columnsToMatchVisitationSupplier, ExecutionContext executionContext) {
    assert matchingIdValues != null;
    assert !matchingIdValues.isEmpty();
    final SessionFactoryImplementor sessionFactory = executionContext.getSession().getFactory();
    final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
    final int idColumnCount = identifierMapping.getJdbcTypeCount();
    assert idColumnCount > 0;
    final InListPredicate predicate;
    if (idColumnCount == 1) {
        final BasicValuedModelPart basicIdMapping = (BasicValuedModelPart) identifierMapping;
        final String idColumn = basicIdMapping.getSelectionExpression();
        final Expression inFixture = new ColumnReference(mutatingTableReference, idColumn, // id columns cannot be formulas and cannot have custom read and write expressions
        false, null, null, basicIdMapping.getJdbcMapping(), sessionFactory);
        predicate = new InListPredicate(inFixture);
        matchingIdValues.forEach(matchingId -> predicate.addExpression(new JdbcLiteral<>(matchingId, basicIdMapping.getJdbcMapping())));
    } else {
        final List<ColumnReference> columnReferences = new ArrayList<>(idColumnCount);
        final List<JdbcMapping> jdbcMappings = new ArrayList<>(idColumnCount);
        identifierMapping.forEachSelectable((columnIndex, selection) -> {
            columnReferences.add(new ColumnReference(mutatingTableReference, selection, sessionFactory));
            jdbcMappings.add(selection.getJdbcMapping());
        });
        final Expression inFixture = new SqlTuple(columnReferences, identifierMapping);
        predicate = new InListPredicate(inFixture);
        matchingIdValues.forEach(matchingId -> {
            assert matchingId instanceof Object[];
            final Object[] matchingIdParts = (Object[]) matchingId;
            final List<JdbcLiteral<?>> tupleParts = new ArrayList<>(idColumnCount);
            for (int p = 0; p < matchingIdParts.length; p++) {
                tupleParts.add(new JdbcLiteral<>(matchingIdParts[p], jdbcMappings.get(p)));
            }
            predicate.addExpression(new SqlTuple(tupleParts, identifierMapping));
        });
    }
    return predicate;
}
Also used : BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ArrayList(java.util.ArrayList) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Expression(org.hibernate.sql.ast.tree.expression.Expression) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JdbcLiteral(org.hibernate.sql.ast.tree.expression.JdbcLiteral)

Example 2 with JdbcLiteral

use of org.hibernate.sql.ast.tree.expression.JdbcLiteral 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();
    }
}
Also used : 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) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) SqmExistsPredicate(org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SqlAstProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) BigInteger(java.math.BigInteger) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SqlAliasBase(org.hibernate.sql.ast.spi.SqlAliasBase) JdbcLiteral(org.hibernate.sql.ast.tree.expression.JdbcLiteral)

Aggregations

JdbcLiteral (org.hibernate.sql.ast.tree.expression.JdbcLiteral)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)1 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)1 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 SqlAstProcessingStateImpl (org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl)1 SqmExistsPredicate (org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate)1 SqmQuerySpec (org.hibernate.query.sqm.tree.select.SqmQuerySpec)1 FromClauseAccess (org.hibernate.sql.ast.spi.FromClauseAccess)1 SqlAliasBase (org.hibernate.sql.ast.spi.SqlAliasBase)1 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)1 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)1 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)1