Skip to content

Commit a549ed7

Browse files
committed
GH-2234 - Move database selection into Runnable-Spec.
Fixes the problem that certain operations did not take the database selection into consideration when executing queries. Closes #2234
1 parent 94fc00d commit a549ed7

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/main/java/org/springframework/data/neo4j/core/DefaultNeo4jClient.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class DefaultRunnableSpec implements RunnableSpec {
189189
private String targetDatabase;
190190

191191
DefaultRunnableSpec(Supplier<String> cypherSupplier) {
192+
this.targetDatabase = Neo4jClient.verifyDatabaseName(resolveTargetDatabaseName(targetDatabase));
192193
this.runnableStatement = new RunnableStatement(cypherSupplier);
193194
}
194195

@@ -257,6 +258,19 @@ public ResultSummary run() {
257258
throw potentiallyConvertRuntimeException(e, persistenceExceptionTranslator);
258259
}
259260
}
261+
262+
private String resolveTargetDatabaseName(@Nullable String parameterTargetDatabase) {
263+
if (parameterTargetDatabase != null) {
264+
return parameterTargetDatabase;
265+
}
266+
if (databaseSelectionProvider != null) {
267+
String databaseSelectionProviderValue = databaseSelectionProvider.getDatabaseSelection().getValue();
268+
if (databaseSelectionProviderValue != null) {
269+
return databaseSelectionProviderValue;
270+
}
271+
}
272+
return DatabaseSelectionProvider.getDefaultSelectionProvider().getDatabaseSelection().getValue();
273+
}
260274
}
261275

262276
class DefaultRecordFetchSpec<T> implements RecordFetchSpec<T>, MappingSpec<T> {
@@ -270,24 +284,11 @@ class DefaultRecordFetchSpec<T> implements RecordFetchSpec<T>, MappingSpec<T> {
270284
DefaultRecordFetchSpec(String parameterTargetDatabase, RunnableStatement runnableStatement,
271285
BiFunction<TypeSystem, Record, T> mappingFunction) {
272286

273-
this.targetDatabase = resolveTargetDatabaseName(parameterTargetDatabase);
287+
this.targetDatabase = parameterTargetDatabase;
274288
this.runnableStatement = runnableStatement;
275289
this.mappingFunction = mappingFunction;
276290
}
277291

278-
private String resolveTargetDatabaseName(@Nullable String parameterTargetDatabase) {
279-
if (parameterTargetDatabase != null) {
280-
return parameterTargetDatabase;
281-
}
282-
if (databaseSelectionProvider != null) {
283-
String databaseSelectionProviderValue = databaseSelectionProvider.getDatabaseSelection().getValue();
284-
if (databaseSelectionProviderValue != null) {
285-
return databaseSelectionProviderValue;
286-
}
287-
}
288-
return DatabaseSelectionProvider.getDefaultSelectionProvider().getDatabaseSelection().getValue();
289-
}
290-
291292
@Override
292293
public RecordFetchSpec<T> mappedBy(
293294
@SuppressWarnings("HiddenField") BiFunction<TypeSystem, Record, T> mappingFunction) {
@@ -358,7 +359,7 @@ class DefaultRunnableDelegation<T> implements RunnableDelegation<T>, OngoingDele
358359

359360
DefaultRunnableDelegation(Function<QueryRunner, Optional<T>> callback, @Nullable String targetDatabase) {
360361
this.callback = callback;
361-
this.targetDatabase = targetDatabase;
362+
this.targetDatabase = Neo4jClient.verifyDatabaseName(targetDatabase);
362363
}
363364

364365
@Override
@@ -375,4 +376,5 @@ public Optional<T> run() {
375376
}
376377
}
377378
}
379+
378380
}

src/test/java/org/springframework/data/neo4j/integration/imperative/RepositoryIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4285,7 +4285,7 @@ public Neo4jMappingContext neo4jMappingContext(Neo4jConversions neo4JConversions
42854285
}
42864286

42874287
@Bean
4288-
public DatabaseSelectionProvider databaseNameProvider() {
4288+
public DatabaseSelectionProvider databaseSelectionProvider() {
42894289
return () -> databaseSelection;
42904290
}
42914291
}

0 commit comments

Comments
 (0)