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);
}
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);
}
}
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());
}
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);
}
}
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);
}
Aggregations