Search in sources :

Example 6 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class CompleteFetchBuilderEmbeddableValuedModelPart method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    assert fetchPath.equals(navigablePath);
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
    modelPart.forEachSelectable((selectionIndex, selectableMapping) -> {
        final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, selectableMapping.getContainingTableExpression());
        final String mappedColumn = selectableMapping.getSelectionExpression();
        final String columnAlias = columnAliases.get(selectionIndex);
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mappedColumn), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
            final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
        }), modelPart.getJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    });
    return parent.generateFetchableFetch(modelPart, fetchPath, FetchTiming.IMMEDIATE, true, null, domainResultCreationState);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) FetchTiming(org.hibernate.engine.FetchTiming) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) ResultsHelper.jdbcPositionToValuesArrayPosition(org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition) FetchBuilder(org.hibernate.query.results.FetchBuilder) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchParent(org.hibernate.sql.results.graph.FetchParent) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) TableReference(org.hibernate.sql.ast.tree.from.TableReference) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

Example 7 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class JdbcSelectExecutorStandardImpl method resolveJdbcValuesSource.

private JdbcValues resolveJdbcValuesSource(String queryIdentifier, JdbcSelect jdbcSelect, boolean canBeCached, ExecutionContext executionContext, ResultSetAccess resultSetAccess) {
    final SharedSessionContractImplementor session = executionContext.getSession();
    final SessionFactoryImplementor factory = session.getFactory();
    final boolean queryCacheEnabled = factory.getSessionFactoryOptions().isQueryCacheEnabled();
    final List<?> cachedResults;
    final CacheMode cacheMode = JdbcExecHelper.resolveCacheMode(executionContext);
    final JdbcValuesMappingProducer mappingProducer = jdbcSelect.getJdbcValuesMappingProducer();
    final boolean cacheable = queryCacheEnabled && canBeCached && executionContext.getQueryOptions().isResultCachingEnabled() == Boolean.TRUE;
    final QueryKey queryResultsCacheKey;
    if (cacheable && cacheMode.isGetEnabled()) {
        SqlExecLogger.INSTANCE.debugf("Reading Query result cache data per CacheMode#isGetEnabled [%s]", cacheMode.name());
        final Set<String> querySpaces = jdbcSelect.getAffectedTableNames();
        if (querySpaces == null || querySpaces.size() == 0) {
            SqlExecLogger.INSTANCE.tracev("Unexpected querySpaces is {0}", (querySpaces == null ? querySpaces : "empty"));
        } else {
            SqlExecLogger.INSTANCE.tracev("querySpaces is {0}", querySpaces);
        }
        final QueryResultsCache queryCache = factory.getCache().getQueryResultsCache(executionContext.getQueryOptions().getResultCacheRegionName());
        // todo (6.0) : not sure that it is at all important that we account for QueryResults
        // these cached values are "lower level" than that, representing the
        // "raw" JDBC values.
        // 
        // todo (6.0) : relatedly ^^, pretty sure that SqlSelections are also irrelevant
        queryResultsCacheKey = QueryKey.from(jdbcSelect.getSql(), executionContext.getQueryOptions().getLimit(), executionContext.getQueryParameterBindings(), session);
        cachedResults = queryCache.get(// todo (6.0) : QueryCache#get takes the `queryResultsCacheKey` see tat discussion above
        queryResultsCacheKey, // atm we do not even collect querySpaces, but we need to
        querySpaces, session);
        // todo (6.0) : `querySpaces` and `session` are used in QueryCache#get to verify "up-to-dateness" via UpdateTimestampsCache
        // better imo to move UpdateTimestampsCache handling here and have QueryCache be a simple access to
        // the underlying query result cache region.
        // 
        // todo (6.0) : if we go this route (^^), still beneficial to have an abstraction over different UpdateTimestampsCache-based
        // invalidation strategies - QueryCacheInvalidationStrategy
        final StatisticsImplementor statistics = factory.getStatistics();
        if (statistics.isStatisticsEnabled()) {
            if (cachedResults == null) {
                statistics.queryCacheMiss(queryIdentifier, queryCache.getRegion().getName());
            } else {
                statistics.queryCacheHit(queryIdentifier, queryCache.getRegion().getName());
            }
        }
    } else {
        SqlExecLogger.INSTANCE.debugf("Skipping reading Query result cache data: cache-enabled = %s, cache-mode = %s", queryCacheEnabled, cacheMode.name());
        cachedResults = null;
        if (cacheable && cacheMode.isPutEnabled()) {
            queryResultsCacheKey = QueryKey.from(jdbcSelect.getSql(), executionContext.getQueryOptions().getLimit(), executionContext.getQueryParameterBindings(), session);
        } else {
            queryResultsCacheKey = null;
        }
    }
    if (cachedResults == null) {
        final JdbcValuesMetadata metadataForCache;
        final JdbcValuesMapping jdbcValuesMapping;
        if (queryResultsCacheKey == null) {
            jdbcValuesMapping = mappingProducer.resolve(resultSetAccess, factory);
            metadataForCache = null;
        } else {
            // If we need to put the values into the cache, we need to be able to capture the JdbcValuesMetadata
            final CapturingJdbcValuesMetadata capturingMetadata = new CapturingJdbcValuesMetadata(resultSetAccess);
            jdbcValuesMapping = mappingProducer.resolve(capturingMetadata, factory);
            metadataForCache = capturingMetadata.resolveMetadataForCache();
        }
        return new JdbcValuesResultSetImpl(resultSetAccess, queryResultsCacheKey, queryIdentifier, executionContext.getQueryOptions(), jdbcValuesMapping, metadataForCache, executionContext);
    } else {
        final JdbcValuesMapping jdbcValuesMapping;
        if (cachedResults.isEmpty() || !(cachedResults.get(0) instanceof JdbcValuesMetadata)) {
            jdbcValuesMapping = mappingProducer.resolve(resultSetAccess, factory);
        } else {
            jdbcValuesMapping = mappingProducer.resolve((JdbcValuesMetadata) cachedResults.get(0), factory);
        }
        return new JdbcValuesCacheHit(cachedResults, jdbcValuesMapping);
    }
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryKey(org.hibernate.cache.spi.QueryKey) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CacheMode(org.hibernate.CacheMode) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) QueryResultsCache(org.hibernate.cache.spi.QueryResultsCache) JdbcValuesResultSetImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl) JdbcValuesMappingProducer(org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducer) JdbcValuesMapping(org.hibernate.sql.results.jdbc.spi.JdbcValuesMapping) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) JdbcValuesCacheHit(org.hibernate.sql.results.jdbc.internal.JdbcValuesCacheHit)

Example 8 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class CompleteResultBuilderBasicValuedStandard method buildResult.

@Override
public BasicResult<?> buildResult(JdbcValuesMetadata jdbcResultsMetadata, int resultPosition, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final SessionFactoryImplementor sessionFactory = creationStateImpl.getSessionFactory();
    final String columnName;
    if (explicitColumnName != null) {
        columnName = explicitColumnName;
    } else {
        columnName = jdbcResultsMetadata.resolveColumnName(resultPosition + 1);
    }
    // final int jdbcPosition;
    // if ( explicitColumnName != null ) {
    // jdbcPosition = jdbcResultsMetadata.resolveColumnPosition( explicitColumnName );
    // }
    // else {
    // jdbcPosition = resultPosition + 1;
    // }
    // 
    // final BasicValuedMapping basicType;
    // if ( explicitType != null ) {
    // basicType = explicitType;
    // }
    // else {
    // basicType = jdbcResultsMetadata.resolveType( jdbcPosition, explicitJavaType );
    // }
    // 
    // final SqlSelection sqlSelection = creationStateImpl.resolveSqlSelection(
    // creationStateImpl.resolveSqlExpression(
    // columnName,
    // processingState -> {
    // final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition( jdbcPosition );
    // return new SqlSelectionImpl( valuesArrayPosition, basicType );
    // }
    // ),
    // basicType.getExpressibleJavaType(),
    // sessionFactory.getTypeConfiguration()
    // );
    final SqlSelection sqlSelection = creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(columnName, processingState -> {
        final int jdbcPosition;
        if (explicitColumnName != null) {
            jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(explicitColumnName);
        } else {
            jdbcPosition = resultPosition + 1;
        }
        final BasicValuedMapping basicType;
        if (explicitType != null) {
            basicType = explicitType;
        } else {
            basicType = jdbcResultsMetadata.resolveType(jdbcPosition, explicitJavaType, sessionFactory);
        }
        final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
        return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
    }), explicitJavaType, sessionFactory.getTypeConfiguration());
    return new BasicResult<>(sqlSelection.getValuesArrayPosition(), columnName, sqlSelection.getExpressionType().getJdbcMappings().get(0).getMappedJavaType());
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) Objects(java.util.Objects) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) ResultsHelper(org.hibernate.query.results.ResultsHelper) ResultBuilder(org.hibernate.query.results.ResultBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 9 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class CompleteResultBuilderCollectionStandard method resolveSelections.

private void resolveSelections(TableGroup tableGroup, ModelPart modelPart, String[] columnNames, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationStateImpl creationStateImpl) {
    final SelectableConsumer consumer = (selectionIndex, selectableMapping) -> {
        final String columnName = columnNames[selectionIndex];
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnName);
            final BasicValuedMapping basicType = (BasicValuedMapping) selectableMapping.getJdbcMapping();
            final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
        }), selectableMapping.getJdbcMapping().getMappedJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    };
    if (modelPart instanceof EntityValuedModelPart) {
        final EntityMappingType entityMappingType = ((EntityValuedModelPart) modelPart).getEntityMappingType();
        int index = entityMappingType.getIdentifierMapping().forEachSelectable(consumer);
        if (entityMappingType.getDiscriminatorMapping() != null) {
            index += entityMappingType.getDiscriminatorMapping().forEachSelectable(index, consumer);
        }
        entityMappingType.forEachSelectable(index, consumer);
    } else {
        modelPart.forEachSelectable(consumer);
    }
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) Arrays(java.util.Arrays) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) DomainResult(org.hibernate.sql.results.graph.DomainResult) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) FromClauseAccessImpl(org.hibernate.query.results.FromClauseAccessImpl) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) ModelPart(org.hibernate.metamodel.mapping.ModelPart) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ResultsHelper(org.hibernate.query.results.ResultsHelper) NativeQuery(org.hibernate.query.NativeQuery) ResultBuilder(org.hibernate.query.results.ResultBuilder) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 10 with JdbcValuesMetadata

use of org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata in project hibernate-orm by hibernate.

the class CompleteFetchBuilderEntityValuedModelPart method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    assert fetchPath.equals(navigablePath);
    final DomainResultCreationStateImpl creationStateImpl = impl(domainResultCreationState);
    final TableGroup tableGroup = creationStateImpl.getFromClauseAccess().getTableGroup(navigablePath.getParent());
    modelPart.forEachSelectable((selectionIndex, selectableMapping) -> {
        final TableReference tableReference = tableGroup.resolveTableReference(navigablePath, selectableMapping.getContainingTableExpression());
        final String mappedColumn = selectableMapping.getSelectionExpression();
        final String columnAlias = columnAliases.get(selectionIndex);
        creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mappedColumn), processingState -> {
            final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnAlias);
            final int valuesArrayPosition = jdbcPositionToValuesArrayPosition(jdbcPosition);
            return new ResultSetMappingSqlSelection(valuesArrayPosition, selectableMapping.getJdbcMapping());
        }), modelPart.getJavaType(), creationStateImpl.getSessionFactory().getTypeConfiguration());
    });
    return parent.generateFetchableFetch(modelPart, fetchPath, FetchTiming.DELAYED, true, null, domainResultCreationState);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) FetchTiming(org.hibernate.engine.FetchTiming) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection) BiFunction(java.util.function.BiFunction) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) Fetch(org.hibernate.sql.results.graph.Fetch) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ResultsHelper.impl(org.hibernate.query.results.ResultsHelper.impl) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) List(java.util.List) ResultsHelper.jdbcPositionToValuesArrayPosition(org.hibernate.query.results.ResultsHelper.jdbcPositionToValuesArrayPosition) FetchBuilder(org.hibernate.query.results.FetchBuilder) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchParent(org.hibernate.sql.results.graph.FetchParent) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) TableReference(org.hibernate.sql.ast.tree.from.TableReference) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) ResultSetMappingSqlSelection(org.hibernate.query.results.ResultSetMappingSqlSelection)

Aggregations

JdbcValuesMetadata (org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata)12 BiFunction (java.util.function.BiFunction)11 ResultSetMappingSqlSelection (org.hibernate.query.results.ResultSetMappingSqlSelection)11 DomainResultCreationState (org.hibernate.sql.results.graph.DomainResultCreationState)11 DomainResultCreationStateImpl (org.hibernate.query.results.DomainResultCreationStateImpl)9 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)9 ResultsHelper.impl (org.hibernate.query.results.ResultsHelper.impl)7 NavigablePath (org.hibernate.query.spi.NavigablePath)7 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)7 ResultsHelper (org.hibernate.query.results.ResultsHelper)6 DynamicFetchBuilderLegacy (org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy)6 List (java.util.List)5 Objects (java.util.Objects)5 FetchTiming (org.hibernate.engine.FetchTiming)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)5 NativeQuery (org.hibernate.query.NativeQuery)5 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)5 BasicResult (org.hibernate.sql.results.graph.basic.BasicResult)5 FetchBuilder (org.hibernate.query.results.FetchBuilder)4