15
15
import org .hibernate .SharedSessionContract ;
16
16
import org .hibernate .cache .spi .QueryKey ;
17
17
import org .hibernate .cache .spi .QueryResultsCache ;
18
+ import org .hibernate .engine .spi .LoadQueryInfluencers ;
18
19
import org .hibernate .engine .spi .PersistenceContext ;
19
20
import org .hibernate .engine .spi .SessionFactoryImplementor ;
20
21
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
21
- import org .hibernate .internal .util .collections .ArrayHelper ;
22
22
import org .hibernate .query .TupleTransformer ;
23
23
import org .hibernate .engine .internal .ReactivePersistenceContextAdapter ;
24
24
import org .hibernate .query .spi .QueryOptions ;
43
43
import org .hibernate .sql .results .internal .RowTransformerTupleTransformerAdapter ;
44
44
import org .hibernate .sql .results .jdbc .internal .CachedJdbcValuesMetadata ;
45
45
import org .hibernate .sql .results .jdbc .internal .JdbcValuesSourceProcessingStateStandardImpl ;
46
+ import org .hibernate .sql .results .jdbc .spi .JdbcValuesMapping ;
46
47
import org .hibernate .sql .results .jdbc .spi .JdbcValuesMetadata ;
47
48
import org .hibernate .sql .results .jdbc .spi .JdbcValuesSourceProcessingOptions ;
48
49
import org .hibernate .sql .results .spi .RowTransformer ;
52
53
import org .hibernate .type .spi .TypeConfiguration ;
53
54
54
55
import static org .hibernate .internal .util .NullnessHelper .coalesceSuppliedValues ;
56
+ import static org .hibernate .internal .util .collections .ArrayHelper .indexOf ;
55
57
56
58
/**
57
59
* @see org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl
@@ -148,8 +150,8 @@ public <T, R> CompletionStage<T> executeQuery(
148
150
ReactiveResultsConsumer <T , R > resultsConsumer ) {
149
151
150
152
final PersistenceContext persistenceContext = executionContext .getSession ().getPersistenceContext ();
151
- boolean defaultReadOnlyOrig = persistenceContext .isDefaultReadOnly ();
152
- Boolean readOnly = executionContext .getQueryOptions ().isReadOnly ();
153
+ final boolean defaultReadOnlyOrig = persistenceContext .isDefaultReadOnly ();
154
+ final Boolean readOnly = executionContext .getQueryOptions ().isReadOnly ();
153
155
if ( readOnly != null ) {
154
156
// The read-only/modifiable mode for the query was explicitly set.
155
157
// Temporarily set the default read-only/modifiable setting to the query's setting.
@@ -179,7 +181,8 @@ private <T, R> CompletionStage<T> doExecuteQuery(
179
181
JdbcSelectExecutor .StatementCreator statementCreator ,
180
182
ReactiveResultsConsumer <T , R > resultsConsumer ) {
181
183
182
- final ReactiveDeferredResultSetAccess deferredResultSetAccess = new ReactiveDeferredResultSetAccess ( jdbcSelect , jdbcParameterBindings , executionContext , statementCreator , resultCountEstimate );
184
+ final ReactiveDeferredResultSetAccess deferredResultSetAccess =
185
+ new ReactiveDeferredResultSetAccess ( jdbcSelect , jdbcParameterBindings , executionContext , statementCreator , resultCountEstimate );
183
186
184
187
return resolveJdbcValuesSource (
185
188
executionContext .getQueryIdentifier ( deferredResultSetAccess .getFinalSql () ),
@@ -216,10 +219,8 @@ public boolean shouldReturnProxies() {
216
219
}
217
220
};
218
221
219
- final JdbcValuesSourceProcessingStateStandardImpl valuesProcessingState = new JdbcValuesSourceProcessingStateStandardImpl (
220
- executionContext ,
221
- processingOptions
222
- );
222
+ final JdbcValuesSourceProcessingStateStandardImpl valuesProcessingState =
223
+ new JdbcValuesSourceProcessingStateStandardImpl ( executionContext , processingOptions );
223
224
224
225
final ReactiveRowReader <R > rowReader = ReactiveResultsHelper .createRowReader (
225
226
executionContext .getSession ().getSessionFactory (),
@@ -257,26 +258,25 @@ private static <R> RowTransformer<R> rowTransformer(
257
258
ExecutionContext executionContext ,
258
259
RowTransformer <R > transformer ,
259
260
ReactiveValuesResultSet jdbcValues ) {
260
- RowTransformer <R > rowTransformer = transformer ;
261
- if ( rowTransformer == null ) {
262
- @ SuppressWarnings ("unchecked" ) final TupleTransformer <R > tupleTransformer = (TupleTransformer <R >) executionContext
263
- .getQueryOptions ()
264
- .getTupleTransformer ();
265
-
261
+ if ( transformer == null ) {
262
+ @ SuppressWarnings ("unchecked" )
263
+ final TupleTransformer <R > tupleTransformer =
264
+ (TupleTransformer <R >) executionContext .getQueryOptions ().getTupleTransformer ();
266
265
if ( tupleTransformer == null ) {
267
- rowTransformer = RowTransformerStandardImpl .instance ();
266
+ return RowTransformerStandardImpl .instance ();
268
267
}
269
268
else {
270
- final List <DomainResult <?>> domainResults = jdbcValues .getValuesMapping ()
271
- .getDomainResults ();
269
+ final List <DomainResult <?>> domainResults = jdbcValues .getValuesMapping ().getDomainResults ();
272
270
final String [] aliases = new String [domainResults .size ()];
273
271
for ( int i = 0 ; i < domainResults .size (); i ++ ) {
274
272
aliases [i ] = domainResults .get ( i ).getResultVariable ();
275
273
}
276
- rowTransformer = new RowTransformerTupleTransformerAdapter <>( aliases , tupleTransformer );
274
+ return new RowTransformerTupleTransformerAdapter <>( aliases , tupleTransformer );
277
275
}
278
276
}
279
- return rowTransformer ;
277
+ else {
278
+ return transformer ;
279
+ }
280
280
}
281
281
282
282
public CompletionStage <ReactiveValuesResultSet > resolveJdbcValuesSource (
@@ -290,9 +290,10 @@ public CompletionStage<ReactiveValuesResultSet> resolveJdbcValuesSource(
290
290
final boolean queryCacheEnabled = factory .getSessionFactoryOptions ().isQueryCacheEnabled ();
291
291
292
292
final CacheMode cacheMode = resolveCacheMode ( executionContext );
293
+ final QueryOptions queryOptions = executionContext .getQueryOptions ();
293
294
final boolean cacheable = queryCacheEnabled
294
295
&& canBeCached
295
- && executionContext . getQueryOptions () .isResultCachingEnabled () == Boolean .TRUE ;
296
+ && queryOptions .isResultCachingEnabled () == Boolean .TRUE ;
296
297
297
298
final QueryKey queryResultsCacheKey ;
298
299
final List <?> cachedResults ;
@@ -307,10 +308,10 @@ public CompletionStage<ReactiveValuesResultSet> resolveJdbcValuesSource(
307
308
}
308
309
309
310
final QueryResultsCache queryCache = factory .getCache ()
310
- .getQueryResultsCache ( executionContext . getQueryOptions () .getResultCacheRegionName () );
311
+ .getQueryResultsCache ( queryOptions .getResultCacheRegionName () );
311
312
312
313
queryResultsCacheKey = QueryKey
313
- .from ( jdbcSelect .getSqlString (), executionContext . getQueryOptions () .getLimit (), executionContext .getQueryParameterBindings (), session );
314
+ .from ( jdbcSelect .getSqlString (), queryOptions .getLimit (), executionContext .getQueryParameterBindings (), session );
314
315
315
316
cachedResults = queryCache .get (
316
317
// todo (6.0) : QueryCache#get takes the `queryResultsCacheKey` see tat discussion above
@@ -345,7 +346,7 @@ public CompletionStage<ReactiveValuesResultSet> resolveJdbcValuesSource(
345
346
if ( cacheable && cacheMode .isPutEnabled () ) {
346
347
queryResultsCacheKey = QueryKey .from (
347
348
jdbcSelect .getSqlString (),
348
- executionContext . getQueryOptions () .getLimit (),
349
+ queryOptions .getLimit (),
349
350
executionContext .getQueryParameterBindings (),
350
351
session
351
352
);
@@ -355,16 +356,18 @@ public CompletionStage<ReactiveValuesResultSet> resolveJdbcValuesSource(
355
356
}
356
357
}
357
358
358
- ReactiveValuesMappingProducer mappingProducer = (ReactiveValuesMappingProducer ) jdbcSelect .getJdbcValuesMappingProducer ();
359
+ final LoadQueryInfluencers loadQueryInfluencers = session .getLoadQueryInfluencers ();
360
+
361
+ final ReactiveValuesMappingProducer mappingProducer =
362
+ (ReactiveValuesMappingProducer ) jdbcSelect .getJdbcValuesMappingProducer ();
359
363
if ( cachedResults == null ) {
360
364
if ( queryResultsCacheKey == null ) {
361
- return mappingProducer
362
- .reactiveResolve ( resultSetAccess , session .getLoadQueryInfluencers (), factory )
365
+ return mappingProducer .reactiveResolve ( resultSetAccess , loadQueryInfluencers , factory )
363
366
.thenApply ( jdbcValuesMapping -> new ReactiveValuesResultSet (
364
367
resultSetAccess ,
365
368
null ,
366
369
queryIdentifier ,
367
- executionContext . getQueryOptions () ,
370
+ queryOptions ,
368
371
resultSetAccess .usesFollowOnLocking (),
369
372
jdbcValuesMapping ,
370
373
null ,
@@ -374,13 +377,12 @@ public CompletionStage<ReactiveValuesResultSet> resolveJdbcValuesSource(
374
377
else {
375
378
// If we need to put the values into the cache, we need to be able to capture the JdbcValuesMetadata
376
379
final CapturingJdbcValuesMetadata capturingMetadata = new CapturingJdbcValuesMetadata ( resultSetAccess );
377
- return mappingProducer
378
- .reactiveResolve ( resultSetAccess , session .getLoadQueryInfluencers (), factory )
380
+ return mappingProducer .reactiveResolve ( resultSetAccess , loadQueryInfluencers , factory )
379
381
.thenApply ( jdbcValuesMapping -> new ReactiveValuesResultSet (
380
382
resultSetAccess ,
381
383
queryResultsCacheKey ,
382
384
queryIdentifier ,
383
- executionContext . getQueryOptions () ,
385
+ queryOptions ,
384
386
resultSetAccess .usesFollowOnLocking (),
385
387
jdbcValuesMapping ,
386
388
capturingMetadata .resolveMetadataForCache (),
@@ -392,34 +394,22 @@ public CompletionStage<ReactiveValuesResultSet> resolveJdbcValuesSource(
392
394
// TODO: Implements JdbcValuesCacheHit for reactive, see JdbcSelectExecutorStandardImpl#resolveJdbcValuesSource
393
395
// If we need to put the values into the cache, we need to be able to capture the JdbcValuesMetadata
394
396
final CapturingJdbcValuesMetadata capturingMetadata = new CapturingJdbcValuesMetadata ( resultSetAccess );
395
- if ( cachedResults .isEmpty () || !( cachedResults .get ( 0 ) instanceof JdbcValuesMetadata ) ) {
396
- return mappingProducer .reactiveResolve ( resultSetAccess , session .getLoadQueryInfluencers (), factory )
397
- .thenApply ( jdbcValuesMapping -> new ReactiveValuesResultSet (
398
- resultSetAccess ,
399
- queryResultsCacheKey ,
400
- queryIdentifier ,
401
- executionContext .getQueryOptions (),
402
- resultSetAccess .usesFollowOnLocking (),
403
- jdbcValuesMapping ,
404
- capturingMetadata .resolveMetadataForCache (),
405
- executionContext
406
- ) );
407
- }
408
- else {
409
- return mappingProducer
410
- .reactiveResolve ( (JdbcValuesMetadata ) cachedResults .get ( 0 ), session .getLoadQueryInfluencers (), factory )
411
- .thenApply ( jdbcValuesMapping -> new ReactiveValuesResultSet (
412
- resultSetAccess ,
413
- queryResultsCacheKey ,
414
- queryIdentifier ,
415
- executionContext .getQueryOptions (),
416
- resultSetAccess .usesFollowOnLocking (),
417
- jdbcValuesMapping ,
418
- capturingMetadata .resolveMetadataForCache (),
419
- executionContext
420
- ) );
421
- }
422
- }
397
+ final CompletionStage <JdbcValuesMapping > stage =
398
+ !cachedResults .isEmpty ()
399
+ && cachedResults .get (0 ) instanceof JdbcValuesMetadata jdbcValuesMetadata
400
+ ? mappingProducer .reactiveResolve ( jdbcValuesMetadata , loadQueryInfluencers , factory )
401
+ : mappingProducer .reactiveResolve ( resultSetAccess , loadQueryInfluencers , factory );
402
+ return stage .thenApply ( jdbcValuesMapping -> new ReactiveValuesResultSet (
403
+ resultSetAccess ,
404
+ queryResultsCacheKey ,
405
+ queryIdentifier ,
406
+ queryOptions ,
407
+ resultSetAccess .usesFollowOnLocking (),
408
+ jdbcValuesMapping ,
409
+ capturingMetadata .resolveMetadataForCache (),
410
+ executionContext
411
+ ) );
412
+ }
423
413
}
424
414
425
415
private static CacheMode resolveCacheMode (ExecutionContext executionContext ) {
@@ -468,7 +458,7 @@ public int resolveColumnPosition(String columnName) {
468
458
position = resultSetAccess .resolveColumnPosition ( columnName );
469
459
columnNames [position - 1 ] = columnName ;
470
460
}
471
- else if ( ( position = ArrayHelper . indexOf ( columnNames , columnName ) + 1 ) == 0 ) {
461
+ else if ( ( position = indexOf ( columnNames , columnName ) + 1 ) == 0 ) {
472
462
position = resultSetAccess .resolveColumnPosition ( columnName );
473
463
columnNames [position - 1 ] = columnName ;
474
464
}
@@ -556,9 +546,7 @@ public <T> void end(JdbcOperationQuerySelect jdbcSelect, T result) {
556
546
}
557
547
558
548
private <T > int getResultSize (T result ) {
559
- return result instanceof Collection
560
- ? ( (Collection <?>) result ).size ()
561
- : -1 ;
549
+ return result instanceof Collection <?> collection ? collection .size () : -1 ;
562
550
}
563
551
}
564
552
}
0 commit comments