Search in sources :

Example 1 with DeferredResultSetAccess

use of org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess in project hibernate-orm by hibernate.

the class JdbcSelectExecutorStandardImpl method doExecuteQuery.

private <T, R> T doExecuteQuery(JdbcSelect jdbcSelect, JdbcParameterBindings jdbcParameterBindings, ExecutionContext executionContext, RowTransformer<R> rowTransformer, Function<String, PreparedStatement> statementCreator, ResultsConsumer<T, R> resultsConsumer) {
    final DeferredResultSetAccess deferredResultSetAccess = new DeferredResultSetAccess(jdbcSelect, jdbcParameterBindings, executionContext, statementCreator);
    final JdbcValues jdbcValues = resolveJdbcValuesSource(executionContext.getQueryIdentifier(deferredResultSetAccess.getFinalSql()), jdbcSelect, resultsConsumer.canResultsBeCached(), executionContext, deferredResultSetAccess);
    if (rowTransformer == null) {
        @SuppressWarnings("unchecked") final TupleTransformer<R> tupleTransformer = (TupleTransformer<R>) executionContext.getQueryOptions().getTupleTransformer();
        if (tupleTransformer == null) {
            rowTransformer = RowTransformerPassThruImpl.instance();
        } else {
            final List<DomainResult<?>> domainResults = jdbcValues.getValuesMapping().getDomainResults();
            final String[] aliases = new String[domainResults.size()];
            for (int i = 0; i < domainResults.size(); i++) {
                aliases[i] = domainResults.get(i).getResultVariable();
            }
            rowTransformer = new RowTransformerTupleTransformerAdapter<>(aliases, tupleTransformer);
        }
    }
    final boolean stats;
    long startTime = 0;
    final StatisticsImplementor statistics = executionContext.getSession().getFactory().getStatistics();
    if (executionContext.hasQueryExecutionToBeAddedToStatistics() && jdbcValues instanceof JdbcValuesResultSetImpl) {
        stats = statistics.isStatisticsEnabled();
        if (stats) {
            startTime = System.nanoTime();
        }
    } else {
        stats = false;
    }
    /*
		 * Processing options effectively are only used for entity loading.  Here we don't need these values.
		 */
    final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() {

        @Override
        public Object getEffectiveOptionalObject() {
            return executionContext.getEntityInstance();
        }

        @Override
        public String getEffectiveOptionalEntityName() {
            return null;
        }

        @Override
        public Object getEffectiveOptionalId() {
            return executionContext.getEntityId();
        }

        @Override
        public boolean shouldReturnProxies() {
            return true;
        }
    };
    final JdbcValuesSourceProcessingStateStandardImpl valuesProcessingState = new JdbcValuesSourceProcessingStateStandardImpl(executionContext, processingOptions, executionContext::registerLoadingEntityEntry);
    final RowReader<R> rowReader = ResultsHelper.createRowReader(executionContext, // because the EntityEntrys would already have the desired lock mode
    deferredResultSetAccess.usesFollowOnLocking() ? LockOptions.NONE : executionContext.getQueryOptions().getLockOptions(), rowTransformer, jdbcValues);
    final RowProcessingStateStandardImpl rowProcessingState = new RowProcessingStateStandardImpl(valuesProcessingState, executionContext, rowReader, jdbcValues);
    final T result = resultsConsumer.consume(jdbcValues, executionContext.getSession(), processingOptions, valuesProcessingState, rowProcessingState, rowReader);
    if (stats) {
        final long endTime = System.nanoTime();
        final long milliseconds = TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
        statistics.queryExecuted(executionContext.getQueryIdentifier(jdbcSelect.getSql()), getResultSize(result), milliseconds);
    }
    return result;
}
Also used : JdbcValuesSourceProcessingOptions(org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions) JdbcValues(org.hibernate.sql.results.jdbc.spi.JdbcValues) DeferredResultSetAccess(org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess) DomainResult(org.hibernate.sql.results.graph.DomainResult) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) JdbcValuesResultSetImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl) TupleTransformer(org.hibernate.query.TupleTransformer) JdbcValuesSourceProcessingStateStandardImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl) RowProcessingStateStandardImpl(org.hibernate.sql.results.internal.RowProcessingStateStandardImpl)

Aggregations

TupleTransformer (org.hibernate.query.TupleTransformer)1 DomainResult (org.hibernate.sql.results.graph.DomainResult)1 RowProcessingStateStandardImpl (org.hibernate.sql.results.internal.RowProcessingStateStandardImpl)1 DeferredResultSetAccess (org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess)1 JdbcValuesResultSetImpl (org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl)1 JdbcValuesSourceProcessingStateStandardImpl (org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl)1 JdbcValues (org.hibernate.sql.results.jdbc.spi.JdbcValues)1 JdbcValuesSourceProcessingOptions (org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions)1 StatisticsImplementor (org.hibernate.stat.spi.StatisticsImplementor)1