Search in sources :

Example 16 with SqlExpressionResolver

use of org.hibernate.sql.ast.spi.SqlExpressionResolver in project hibernate-orm by hibernate.

the class CaseSearchedExpression method applySqlSelections.

@Override
public void applySqlSelections(DomainResultCreationState creationState) {
    final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
    sqlExpressionResolver.resolveSqlSelection(this, type.getExpressibleJavaType(), null, creationState.getSqlAstCreationState().getCreationContext().getMappingMetamodel().getTypeConfiguration());
}
Also used : SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver)

Example 17 with SqlExpressionResolver

use of org.hibernate.sql.ast.spi.SqlExpressionResolver 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 18 with SqlExpressionResolver

use of org.hibernate.sql.ast.spi.SqlExpressionResolver in project hibernate-orm by hibernate.

the class LoaderSelectBuilder method applyRestriction.

private void applyRestriction(QuerySpec rootQuerySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, ModelPart modelPart, int numberColumns, Consumer<JdbcParameter> jdbcParameterConsumer, LoaderSqlAstCreationState sqlAstCreationState) {
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final NavigablePath navigablePath = rootNavigablePath.append(modelPart.getNavigableRole().getNavigableName());
    if (numberColumns == 1) {
        modelPart.forEachSelectable((columnIndex, selection) -> {
            final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
            final ColumnReference columnRef = (ColumnReference) sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), p -> new ColumnReference(tableReference, selection, creationContext.getSessionFactory()));
            if (numberOfKeysToLoad == 1) {
                final JdbcParameter jdbcParameter = new JdbcParameterImpl(selection.getJdbcMapping());
                jdbcParameterConsumer.accept(jdbcParameter);
                rootQuerySpec.applyPredicate(new ComparisonPredicate(columnRef, ComparisonOperator.EQUAL, jdbcParameter));
            } else {
                final InListPredicate predicate = new InListPredicate(columnRef);
                for (int i = 0; i < numberOfKeysToLoad; i++) {
                    for (int j = 0; j < numberColumns; j++) {
                        final JdbcParameter jdbcParameter = new JdbcParameterImpl(columnRef.getJdbcMapping());
                        jdbcParameterConsumer.accept(jdbcParameter);
                        predicate.addExpression(jdbcParameter);
                    }
                }
                rootQuerySpec.applyPredicate(predicate);
            }
        });
    } else {
        final List<ColumnReference> columnReferences = new ArrayList<>(numberColumns);
        modelPart.forEachSelectable((columnIndex, selection) -> {
            final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
            columnReferences.add((ColumnReference) sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), p -> new ColumnReference(tableReference, selection, creationContext.getSessionFactory())));
        });
        final SqlTuple tuple = new SqlTuple(columnReferences, modelPart);
        final InListPredicate predicate = new InListPredicate(tuple);
        for (int i = 0; i < numberOfKeysToLoad; i++) {
            final List<JdbcParameter> tupleParams = new ArrayList<>(numberColumns);
            for (int j = 0; j < numberColumns; j++) {
                final ColumnReference columnReference = columnReferences.get(j);
                final JdbcParameter jdbcParameter = new JdbcParameterImpl(columnReference.getJdbcMapping());
                jdbcParameterConsumer.accept(jdbcParameter);
                tupleParams.add(jdbcParameter);
            }
            final SqlTuple paramTuple = new SqlTuple(tupleParams, modelPart);
            predicate.addExpression(paramTuple);
        }
        rootQuerySpec.applyPredicate(predicate);
    }
}
Also used : Arrays(java.util.Arrays) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) GraphSemantic(org.hibernate.graph.GraphSemantic) ResultsHelper.attributeName(org.hibernate.query.results.ResultsHelper.attributeName) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) CascadingAction(org.hibernate.engine.spi.CascadingAction) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) BagSemantics(org.hibernate.collection.spi.BagSemantics) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) FetchProfile(org.hibernate.engine.profile.FetchProfile) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) Fetchable(org.hibernate.sql.results.graph.Fetchable) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) LockOptions(org.hibernate.LockOptions) AliasCollector(org.hibernate.sql.ast.spi.AliasCollector) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) OrderByFragment(org.hibernate.metamodel.mapping.ordering.OrderByFragment) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) NavigablePath(org.hibernate.spi.NavigablePath) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) List(java.util.List) EntityIdentifierNavigablePath(org.hibernate.spi.EntityIdentifierNavigablePath) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Logger(org.jboss.logging.Logger) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) ArrayList(java.util.ArrayList) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) Loader(org.hibernate.loader.ast.spi.Loader) BiConsumer(java.util.function.BiConsumer) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) Loadable(org.hibernate.loader.ast.spi.Loadable) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) Restrictable(org.hibernate.metamodel.mapping.Restrictable) EntityGraphTraversalState(org.hibernate.sql.results.graph.EntityGraphTraversalState) FetchStyle(org.hibernate.engine.FetchStyle) StandardEntityGraphTraversalStateImpl(org.hibernate.sql.results.internal.StandardEntityGraphTraversalStateImpl) AbstractMap(java.util.AbstractMap) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) NavigablePath(org.hibernate.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.spi.EntityIdentifierNavigablePath) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) ArrayList(java.util.ArrayList) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 19 with SqlExpressionResolver

use of org.hibernate.sql.ast.spi.SqlExpressionResolver in project hibernate-orm by hibernate.

the class DynamicResultBuilderBasicConverted method buildResult.

@Override
public BasicResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final TypeConfiguration typeConfiguration = domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration();
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnAlias, state -> {
        final int currentJdbcPosition = resultPosition + 1;
        final int jdbcPosition;
        if (columnAlias != null) {
            jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        } else {
            jdbcPosition = currentJdbcPosition;
        }
        final BasicType<?> basicType = jdbcResultsMetadata.resolveType(jdbcPosition, basicValueConverter.getRelationalJavaType(), domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory());
        final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
        return new ResultSetMappingSqlSelection(valuesArrayPosition, (BasicValuedMapping) basicType);
    }), basicValueConverter.getDomainJavaType(), null, typeConfiguration);
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnAlias, basicValueConverter.getDomainJavaType(), basicValueConverter);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) JpaAttributeConverterImpl(org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl) ProvidedInstanceManagedBeanImpl(org.hibernate.resource.beans.spi.ProvidedInstanceManagedBeanImpl) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) BasicType(org.hibernate.type.BasicType) BiFunction(java.util.function.BiFunction) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultsHelper(org.hibernate.query.results.ResultsHelper) ManagedBean(org.hibernate.resource.beans.spi.ManagedBean) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) AttributeConverter(jakarta.persistence.AttributeConverter) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BasicType(org.hibernate.type.BasicType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 20 with SqlExpressionResolver

use of org.hibernate.sql.ast.spi.SqlExpressionResolver in project hibernate-orm by hibernate.

the class DynamicResultBuilderEntityStandard method resolveSqlSelection.

private void resolveSqlSelection(String columnAlias, String columnKey, JdbcMapping jdbcMapping, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationState domainResultCreationState) {
    final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();
    sqlExpressionResolver.resolveSqlSelection(sqlExpressionResolver.resolveSqlExpression(columnKey, state -> {
        final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
        final int valuesArrayPosition = jdbcPosition - 1;
        return new ResultSetMappingSqlSelection(valuesArrayPosition, jdbcMapping);
    }), jdbcMapping.getMappedJavaType(), null, domainResultCreationState.getSqlAstCreationState().getCreationContext().getSessionFactory().getTypeConfiguration());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SingleAttributeIdentifierMapping(org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping) TableGroupImpl(org.hibernate.query.results.TableGroupImpl) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ArrayList(java.util.ArrayList) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) NativeQuery(org.hibernate.query.NativeQuery) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) Fetchable(org.hibernate.sql.results.graph.Fetchable) FetchTiming(org.hibernate.engine.FetchTiming) LockMode(org.hibernate.LockMode) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) NavigablePath(org.hibernate.spi.NavigablePath) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) FetchBuilder(org.hibernate.query.results.FetchBuilder) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

Aggregations

SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)46 SqlAstCreationState (org.hibernate.sql.ast.spi.SqlAstCreationState)25 TableReference (org.hibernate.sql.ast.tree.from.TableReference)24 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)23 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)22 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)21 DomainResultCreationState (org.hibernate.sql.results.graph.DomainResultCreationState)19 NavigablePath (org.hibernate.spi.NavigablePath)18 BasicResult (org.hibernate.sql.results.graph.basic.BasicResult)18 FetchTiming (org.hibernate.engine.FetchTiming)16 Fetch (org.hibernate.sql.results.graph.Fetch)16 FetchParent (org.hibernate.sql.results.graph.FetchParent)16 BiConsumer (java.util.function.BiConsumer)15 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)15 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)15 DomainResult (org.hibernate.sql.results.graph.DomainResult)15 FetchStyle (org.hibernate.engine.FetchStyle)13 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)13 MappingType (org.hibernate.metamodel.mapping.MappingType)13 NavigableRole (org.hibernate.metamodel.model.domain.NavigableRole)13