@@ -55,11 +55,11 @@ class DefaultNeo4jClient implements Neo4jClient {
55
55
56
56
private final Driver driver ;
57
57
private final TypeSystem typeSystem ;
58
- private final DatabaseSelectionProvider databaseSelectionProvider ;
58
+ private @ Nullable final DatabaseSelectionProvider databaseSelectionProvider ;
59
59
private final ConversionService conversionService ;
60
60
private final Neo4jPersistenceExceptionTranslator persistenceExceptionTranslator = new Neo4jPersistenceExceptionTranslator ();
61
61
62
- DefaultNeo4jClient (Driver driver , DatabaseSelectionProvider databaseSelectionProvider ) {
62
+ DefaultNeo4jClient (Driver driver , @ Nullable DatabaseSelectionProvider databaseSelectionProvider ) {
63
63
64
64
this .driver = driver ;
65
65
this .typeSystem = driver .defaultTypeSystem ();
@@ -69,8 +69,9 @@ class DefaultNeo4jClient implements Neo4jClient {
69
69
new Neo4jConversions ().registerConvertersIn ((ConverterRegistry ) conversionService );
70
70
}
71
71
72
- DelegatingQueryRunner getQueryRunner (@ Nullable final String targetDatabase ) {
72
+ DelegatingQueryRunner getQueryRunner (DatabaseSelection databaseSelection ) {
73
73
74
+ String targetDatabase = databaseSelection .getValue ();
74
75
QueryRunner queryRunner = Neo4jTransactionManager .retrieveTransaction (driver , targetDatabase );
75
76
if (queryRunner == null ) {
76
77
queryRunner = driver .session (Neo4jTransactionUtils .defaultSessionConfig (targetDatabase ));
@@ -193,21 +194,33 @@ private static RuntimeException potentiallyConvertRuntimeException(RuntimeExcept
193
194
return resolved == null ? ex : resolved ;
194
195
}
195
196
197
+ private DatabaseSelection resolveTargetDatabaseName (@ Nullable String parameterTargetDatabase ) {
198
+
199
+ String value = Neo4jClient .verifyDatabaseName (parameterTargetDatabase );
200
+ if (value != null ) {
201
+ return DatabaseSelection .byName (value );
202
+ }
203
+ if (databaseSelectionProvider != null ) {
204
+ return databaseSelectionProvider .getDatabaseSelection ();
205
+ }
206
+ return DatabaseSelectionProvider .getDefaultSelectionProvider ().getDatabaseSelection ();
207
+ }
208
+
196
209
class DefaultRunnableSpec implements RunnableSpec {
197
210
198
211
private RunnableStatement runnableStatement ;
199
212
200
- private String targetDatabase ;
213
+ private DatabaseSelection databaseSelection ;
201
214
202
215
DefaultRunnableSpec (Supplier <String > cypherSupplier ) {
203
- this .targetDatabase = Neo4jClient . verifyDatabaseName ( resolveTargetDatabaseName (targetDatabase ) );
216
+ this .databaseSelection = resolveTargetDatabaseName (null );
204
217
this .runnableStatement = new RunnableStatement (cypherSupplier );
205
218
}
206
219
207
220
@ Override
208
- public RunnableSpecTightToDatabase in (@ SuppressWarnings ( "HiddenField" ) String targetDatabase ) {
221
+ public RunnableSpecTightToDatabase in (String targetDatabase ) {
209
222
210
- this .targetDatabase = Neo4jClient . verifyDatabaseName (targetDatabase );
223
+ this .databaseSelection = resolveTargetDatabaseName (targetDatabase );
211
224
return this ;
212
225
}
213
226
@@ -249,53 +262,40 @@ public RunnableSpecTightToDatabase bindAll(Map<String, Object> newParameters) {
249
262
@ Override
250
263
public <T > MappingSpec <T > fetchAs (Class <T > targetClass ) {
251
264
252
- return new DefaultRecordFetchSpec (this .targetDatabase , this .runnableStatement ,
265
+ return new DefaultRecordFetchSpec <> (this .databaseSelection , this .runnableStatement ,
253
266
new SingleValueMappingFunction (conversionService , targetClass ));
254
267
}
255
268
256
269
@ Override
257
270
public RecordFetchSpec <Map <String , Object >> fetch () {
258
271
259
- return new DefaultRecordFetchSpec <>(this .targetDatabase , this .runnableStatement , (t , r ) -> r .asMap ());
272
+ return new DefaultRecordFetchSpec <>(this .databaseSelection , this .runnableStatement , (t , r ) -> r .asMap ());
260
273
}
261
274
262
275
@ Override
263
276
public ResultSummary run () {
264
277
265
- try (DelegatingQueryRunner statementRunner = getQueryRunner (this .targetDatabase )) {
278
+ try (DelegatingQueryRunner statementRunner = getQueryRunner (this .databaseSelection )) {
266
279
Result result = runnableStatement .runWith (statementRunner );
267
280
return ResultSummaries .process (result .consume ());
268
281
} catch (RuntimeException e ) {
269
282
throw potentiallyConvertRuntimeException (e , persistenceExceptionTranslator );
270
283
}
271
284
}
272
-
273
- private String resolveTargetDatabaseName (@ Nullable String parameterTargetDatabase ) {
274
- if (parameterTargetDatabase != null ) {
275
- return parameterTargetDatabase ;
276
- }
277
- if (databaseSelectionProvider != null ) {
278
- String databaseSelectionProviderValue = databaseSelectionProvider .getDatabaseSelection ().getValue ();
279
- if (databaseSelectionProviderValue != null ) {
280
- return databaseSelectionProviderValue ;
281
- }
282
- }
283
- return DatabaseSelectionProvider .getDefaultSelectionProvider ().getDatabaseSelection ().getValue ();
284
- }
285
285
}
286
286
287
287
class DefaultRecordFetchSpec <T > implements RecordFetchSpec <T >, MappingSpec <T > {
288
288
289
- private final String targetDatabase ;
289
+ private final DatabaseSelection databaseSelection ;
290
290
291
291
private final RunnableStatement runnableStatement ;
292
292
293
293
private BiFunction <TypeSystem , Record , T > mappingFunction ;
294
294
295
- DefaultRecordFetchSpec (String parameterTargetDatabase , RunnableStatement runnableStatement ,
295
+ DefaultRecordFetchSpec (DatabaseSelection databaseSelection , RunnableStatement runnableStatement ,
296
296
BiFunction <TypeSystem , Record , T > mappingFunction ) {
297
297
298
- this .targetDatabase = parameterTargetDatabase ;
298
+ this .databaseSelection = databaseSelection ;
299
299
this .runnableStatement = runnableStatement ;
300
300
this .mappingFunction = mappingFunction ;
301
301
}
@@ -311,7 +311,7 @@ public RecordFetchSpec<T> mappedBy(
311
311
@ Override
312
312
public Optional <T > one () {
313
313
314
- try (DelegatingQueryRunner statementRunner = getQueryRunner (this .targetDatabase )) {
314
+ try (DelegatingQueryRunner statementRunner = getQueryRunner (this .databaseSelection )) {
315
315
Result result = runnableStatement .runWith (statementRunner );
316
316
Optional <T > optionalValue = result .hasNext () ?
317
317
Optional .ofNullable (mappingFunction .apply (typeSystem , result .single ())) :
@@ -326,7 +326,7 @@ public Optional<T> one() {
326
326
@ Override
327
327
public Optional <T > first () {
328
328
329
- try (DelegatingQueryRunner statementRunner = getQueryRunner (this .targetDatabase )) {
329
+ try (DelegatingQueryRunner statementRunner = getQueryRunner (this .databaseSelection )) {
330
330
Result result = runnableStatement .runWith (statementRunner );
331
331
Optional <T > optionalValue = result .stream ().map (partialMappingFunction (typeSystem )).findFirst ();
332
332
ResultSummaries .process (result .consume ());
@@ -339,7 +339,7 @@ public Optional<T> first() {
339
339
@ Override
340
340
public Collection <T > all () {
341
341
342
- try (DelegatingQueryRunner statementRunner = getQueryRunner (this .targetDatabase )) {
342
+ try (DelegatingQueryRunner statementRunner = getQueryRunner (this .databaseSelection )) {
343
343
Result result = runnableStatement .runWith (statementRunner );
344
344
Collection <T > values = result .stream ().map (partialMappingFunction (typeSystem )).collect (Collectors .toList ());
345
345
ResultSummaries .process (result .consume ());
@@ -360,34 +360,29 @@ private Function<Record, T> partialMappingFunction(TypeSystem typeSystem) {
360
360
361
361
class DefaultRunnableDelegation <T > implements RunnableDelegation <T >, OngoingDelegation <T > {
362
362
363
- private final Function < QueryRunner , Optional < T >> callback ;
363
+ private DatabaseSelection databaseSelection ;
364
364
365
- @ Nullable private String targetDatabase ;
365
+ private final Function < QueryRunner , Optional < T >> callback ;
366
366
367
367
DefaultRunnableDelegation (Function <QueryRunner , Optional <T >> callback ) {
368
- this (callback , null );
369
- }
370
-
371
- DefaultRunnableDelegation (Function <QueryRunner , Optional <T >> callback , @ Nullable String targetDatabase ) {
372
368
this .callback = callback ;
373
- this .targetDatabase = Neo4jClient . verifyDatabaseName ( targetDatabase );
369
+ this .databaseSelection = resolveTargetDatabaseName ( null );
374
370
}
375
371
376
372
@ Override
377
- public RunnableDelegation in (@ Nullable @ SuppressWarnings ( "HiddenField" ) String targetDatabase ) {
373
+ public RunnableDelegation in (@ Nullable String targetDatabase ) {
378
374
379
- this .targetDatabase = Neo4jClient . verifyDatabaseName (targetDatabase );
375
+ this .databaseSelection = resolveTargetDatabaseName (targetDatabase );
380
376
return this ;
381
377
}
382
378
383
379
@ Override
384
380
public Optional <T > run () {
385
- try (DelegatingQueryRunner queryRunner = getQueryRunner (targetDatabase )) {
381
+ try (DelegatingQueryRunner queryRunner = getQueryRunner (databaseSelection )) {
386
382
return callback .apply (queryRunner );
387
383
} catch (RuntimeException e ) {
388
384
throw potentiallyConvertRuntimeException (e , persistenceExceptionTranslator );
389
385
}
390
386
}
391
387
}
392
-
393
388
}
0 commit comments