Search in sources :

Example 1 with ColumnReference

use of org.hibernate.sql.ast.tree.expression.ColumnReference 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 ColumnReference

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

the class EmbeddedForeignKeyDescriptor method isSimpleJoinPredicate.

@Override
public boolean isSimpleJoinPredicate(Predicate predicate) {
    if (!(predicate instanceof Junction)) {
        return false;
    }
    final Junction junction = (Junction) predicate;
    if (junction.getNature() != Junction.Nature.CONJUNCTION) {
        return false;
    }
    final List<Predicate> predicates = junction.getPredicates();
    if (predicates.size() != keySelectableMappings.getJdbcTypeCount()) {
        return false;
    }
    Boolean lhsIsKey = null;
    for (int i = 0; i < predicates.size(); i++) {
        final Predicate p = predicates.get(i);
        if (!(p instanceof ComparisonPredicate)) {
            return false;
        }
        final ComparisonPredicate comparisonPredicate = (ComparisonPredicate) p;
        if (comparisonPredicate.getOperator() != ComparisonOperator.EQUAL) {
            return false;
        }
        final Expression lhsExpr = comparisonPredicate.getLeftHandExpression();
        final Expression rhsExpr = comparisonPredicate.getRightHandExpression();
        if (!(lhsExpr instanceof ColumnReference) || !(rhsExpr instanceof ColumnReference)) {
            return false;
        }
        final ColumnReference lhs = (ColumnReference) lhsExpr;
        final ColumnReference rhs = (ColumnReference) rhsExpr;
        if (lhsIsKey == null) {
            final String keyExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
            final String targetExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
            if (keyExpression.equals(targetExpression)) {
                if (!lhs.getColumnExpression().equals(keyExpression) || !rhs.getColumnExpression().equals(keyExpression)) {
                    return false;
                }
            } else {
                if (keyExpression.equals(lhs.getColumnExpression())) {
                    if (!targetExpression.equals(rhs.getColumnExpression())) {
                        return false;
                    }
                    lhsIsKey = true;
                } else if (keyExpression.equals(rhs.getColumnExpression())) {
                    if (!targetExpression.equals(lhs.getColumnExpression())) {
                        return false;
                    }
                    lhsIsKey = false;
                } else {
                    return false;
                }
            }
        } else {
            final String lhsSelectionExpression;
            final String rhsSelectionExpression;
            if (lhsIsKey) {
                lhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
                rhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
            } else {
                lhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
                rhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
            }
            if (!lhs.getColumnExpression().equals(lhsSelectionExpression) || !rhs.getColumnExpression().equals(rhsSelectionExpression)) {
                return false;
            }
        }
    }
    return true;
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) Junction(org.hibernate.sql.ast.tree.predicate.Junction) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 3 with ColumnReference

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

the class EntityVersionMappingImpl method resolveSqlSelection.

private SqlSelection resolveSqlSelection(TableGroup tableGroup, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final TableReference columnTableReference = tableGroup.resolveTableReference(tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName()), columnTableExpression);
    return sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(columnTableReference, columnExpression), sqlAstProcessingState -> new ColumnReference(columnTableReference, columnExpression, false, null, null, versionBasicType, sqlAstCreationState.getCreationContext().getSessionFactory())), versionBasicType.getJdbcMapping().getJavaTypeDescriptor(), sqlAstCreationState.getCreationContext().getSessionFactory().getTypeConfiguration());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicType(org.hibernate.type.BasicType) UnsavedValueFactory(org.hibernate.engine.internal.UnsavedValueFactory) RootClass(org.hibernate.mapping.RootClass) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) Clause(org.hibernate.sql.ast.Clause) Supplier(java.util.function.Supplier) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) TableReference(org.hibernate.sql.ast.tree.from.TableReference) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) BiConsumer(java.util.function.BiConsumer) EntityVersionMapping(org.hibernate.metamodel.mapping.EntityVersionMapping) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) VersionJavaType(org.hibernate.type.descriptor.java.VersionJavaType) Fetch(org.hibernate.sql.results.graph.Fetch) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) FetchStyle(org.hibernate.engine.FetchStyle) VersionValue(org.hibernate.engine.spi.VersionValue) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) KeyValue(org.hibernate.mapping.KeyValue) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) FetchParent(org.hibernate.sql.results.graph.FetchParent) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 4 with ColumnReference

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

the class EntityVersionMappingImpl method generateFetch.

@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().findTableGroup(fetchParent.getNavigablePath());
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final TableReference columnTableReference = tableGroup.resolveTableReference(fetchablePath, columnTableExpression);
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(columnTableReference, columnExpression), sqlAstProcessingState -> new ColumnReference(columnTableReference, columnExpression, false, null, null, versionBasicType, sqlAstCreationState.getCreationContext().getSessionFactory())), versionBasicType.getJdbcMapping().getJavaTypeDescriptor(), sqlAstCreationState.getCreationContext().getSessionFactory().getTypeConfiguration());
    return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, null, fetchTiming, creationState);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicType(org.hibernate.type.BasicType) UnsavedValueFactory(org.hibernate.engine.internal.UnsavedValueFactory) RootClass(org.hibernate.mapping.RootClass) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) Clause(org.hibernate.sql.ast.Clause) Supplier(java.util.function.Supplier) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) MappingType(org.hibernate.metamodel.mapping.MappingType) TableReference(org.hibernate.sql.ast.tree.from.TableReference) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) BiConsumer(java.util.function.BiConsumer) EntityVersionMapping(org.hibernate.metamodel.mapping.EntityVersionMapping) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) VersionJavaType(org.hibernate.type.descriptor.java.VersionJavaType) Fetch(org.hibernate.sql.results.graph.Fetch) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) FetchStyle(org.hibernate.engine.FetchStyle) VersionValue(org.hibernate.engine.spi.VersionValue) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) KeyValue(org.hibernate.mapping.KeyValue) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) FetchParent(org.hibernate.sql.results.graph.FetchParent) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableReference(org.hibernate.sql.ast.tree.from.TableReference) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 5 with ColumnReference

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

the class ExplicitColumnDiscriminatorMappingImpl method resolveSqlExpression.

@Override
public Expression resolveSqlExpression(NavigablePath navigablePath, JdbcMapping jdbcMappingToUse, TableGroup tableGroup, SqlAstCreationState creationState) {
    final SqlExpressionResolver expressionResolver = creationState.getSqlExpressionResolver();
    final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, tableExpression);
    final String selectionExpression = getSelectionExpression();
    return expressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selectionExpression), sqlAstProcessingState -> new ColumnReference(tableReference, selectionExpression, columnFormula != null, null, null, jdbcMappingToUse, getSessionFactory()));
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)64 Expression (org.hibernate.sql.ast.tree.expression.Expression)40 TableReference (org.hibernate.sql.ast.tree.from.TableReference)37 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)35 ArrayList (java.util.ArrayList)27 NavigablePath (org.hibernate.query.spi.NavigablePath)27 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)23 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)23 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)21 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)20 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)20 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)19 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)19 List (java.util.List)18 BiConsumer (java.util.function.BiConsumer)17 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)16 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)16 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)16 Collections (java.util.Collections)15 FetchTiming (org.hibernate.engine.FetchTiming)15