Search in sources :

Example 1 with SqlTuple

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

the class EmbeddedCollectionPart method toSqlExpression.

@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final List<Expression> expressions = new ArrayList<>();
    getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
        assert containingTableExpression.equals(selection.getContainingTableExpression());
        final TableReference tableReference = tableGroup.resolveTableReference(tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName()), selection.getContainingTableExpression());
        expressions.add(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, sqlAstCreationState.getCreationContext().getSessionFactory())));
    });
    return new SqlTuple(expressions, this);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SqlAliasBaseGenerator(org.hibernate.sql.ast.spi.SqlAliasBaseGenerator) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) Clause(org.hibernate.sql.ast.Clause) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ArrayList(java.util.ArrayList) MappingType(org.hibernate.metamodel.mapping.MappingType) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) BiConsumer(java.util.function.BiConsumer) EmbeddableResultImpl(org.hibernate.sql.results.graph.embeddable.internal.EmbeddableResultImpl) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) EmbeddableValuedFetchable(org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable) Fetch(org.hibernate.sql.results.graph.Fetch) EmbeddableFetchImpl(org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Consumer(java.util.function.Consumer) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) FetchStyle(org.hibernate.engine.FetchStyle) List(java.util.List) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) PropertyAccessStrategyBasicImpl(org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) FetchParent(org.hibernate.sql.results.graph.FetchParent) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) ArrayList(java.util.ArrayList) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 2 with SqlTuple

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

the class H2SqlAstTranslator method visitInSubQueryPredicate.

@Override
public void visitInSubQueryPredicate(InSubQueryPredicate inSubQueryPredicate) {
    final SqlTuple lhsTuple;
    // As of 1.4.200 this is supported
    if (getDialect().getVersion().isBefore(1, 4, 200) && (lhsTuple = SqlTupleContainer.getSqlTuple(inSubQueryPredicate.getTestExpression())) != null && lhsTuple.getExpressions().size() != 1) {
        inSubQueryPredicate.getTestExpression().accept(this);
        if (inSubQueryPredicate.isNegated()) {
            appendSql(" not");
        }
        appendSql(" in");
        final boolean renderAsArray = this.renderAsArray;
        this.renderAsArray = true;
        inSubQueryPredicate.getSubQuery().accept(this);
        this.renderAsArray = renderAsArray;
    } else {
        super.visitInSubQueryPredicate(inSubQueryPredicate);
    }
}
Also used : SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

Example 3 with SqlTuple

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

the class AbstractCompositeIdentifierMapping method toSqlExpression.

@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
    final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
    final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
    final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
    final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
    getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
        final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
        final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
        columnReferences.add((ColumnReference) columnReference);
    });
    return new SqlTuple(columnReferences, this);
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) NavigablePath(org.hibernate.query.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 4 with SqlTuple

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

the class AbstractSqlAstTranslator method visitInSubQueryPredicate.

@Override
public void visitInSubQueryPredicate(InSubQueryPredicate inSubQueryPredicate) {
    final SqlTuple lhsTuple;
    if ((lhsTuple = SqlTupleContainer.getSqlTuple(inSubQueryPredicate.getTestExpression())) != null) {
        if (lhsTuple.getExpressions().size() == 1) {
            // Special case for tuples with arity 1 as any DBMS supports scalar IN predicates
            lhsTuple.getExpressions().get(0).accept(this);
            if (inSubQueryPredicate.isNegated()) {
                appendSql(" not");
            }
            appendSql(" in");
            inSubQueryPredicate.getSubQuery().accept(this);
        } else if (!supportsRowValueConstructorSyntaxInInSubQuery()) {
            emulateSubQueryRelationalRestrictionPredicate(inSubQueryPredicate, inSubQueryPredicate.isNegated(), inSubQueryPredicate.getSubQuery(), lhsTuple, this::renderSelectTupleComparison, ComparisonOperator.EQUAL);
        } else {
            inSubQueryPredicate.getTestExpression().accept(this);
            if (inSubQueryPredicate.isNegated()) {
                appendSql(" not");
            }
            appendSql(" in");
            inSubQueryPredicate.getSubQuery().accept(this);
        }
    } else {
        inSubQueryPredicate.getTestExpression().accept(this);
        if (inSubQueryPredicate.isNegated()) {
            appendSql(" not");
        }
        appendSql(" in");
        inSubQueryPredicate.getSubQuery().accept(this);
    }
}
Also used : SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

Example 5 with SqlTuple

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

the class AbstractSqlAstTranslator method visitNullnessPredicate.

@Override
public void visitNullnessPredicate(NullnessPredicate nullnessPredicate) {
    final Expression expression = nullnessPredicate.getExpression();
    final String predicateValue;
    if (nullnessPredicate.isNegated()) {
        predicateValue = " is not null";
    } else {
        predicateValue = " is null";
    }
    final SqlTuple tuple;
    if ((tuple = SqlTupleContainer.getSqlTuple(expression)) != null) {
        String separator = NO_SEPARATOR;
        // as the embeddable is not considered as null, if at least one sub-part is not null
        if (nullnessPredicate.isNegated() && expression.getExpressionType() instanceof AttributeMapping) {
            appendSql('(');
            for (Expression exp : tuple.getExpressions()) {
                appendSql(separator);
                exp.accept(this);
                appendSql(predicateValue);
                separator = " or ";
            }
            appendSql(')');
        } else // For the is null check, and also for tuples in SQL in general,
        // the semantics is that all sub-parts must match the predicate
        {
            for (Expression exp : tuple.getExpressions()) {
                appendSql(separator);
                exp.accept(this);
                appendSql(predicateValue);
                separator = " and ";
            }
        }
    } else {
        expression.accept(this);
        appendSql(predicateValue);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

Aggregations

SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)34 Expression (org.hibernate.sql.ast.tree.expression.Expression)30 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)20 ArrayList (java.util.ArrayList)17 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)15 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)14 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)14 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)14 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)14 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)14 NavigablePath (org.hibernate.query.spi.NavigablePath)12 TableReference (org.hibernate.sql.ast.tree.from.TableReference)11 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)10 ModelPart (org.hibernate.metamodel.mapping.ModelPart)10 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)10 List (java.util.List)9 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)9 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)9 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)9 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)9