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
This commit is contained in:
Gerrit Meier
2021-04-17 08:01:51 +02:00
parent 94fc00de83
commit a549ed7404
2 changed files with 18 additions and 16 deletions

View File

@@ -189,6 +189,7 @@ class DefaultNeo4jClient implements Neo4jClient {
private String targetDatabase;
DefaultRunnableSpec(Supplier<String> cypherSupplier) {
this.targetDatabase = Neo4jClient.verifyDatabaseName(resolveTargetDatabaseName(targetDatabase));
this.runnableStatement = new RunnableStatement(cypherSupplier);
}
@@ -257,6 +258,19 @@ class DefaultNeo4jClient implements Neo4jClient {
throw potentiallyConvertRuntimeException(e, persistenceExceptionTranslator);
}
}
private String resolveTargetDatabaseName(@Nullable String parameterTargetDatabase) {
if (parameterTargetDatabase != null) {
return parameterTargetDatabase;
}
if (databaseSelectionProvider != null) {
String databaseSelectionProviderValue = databaseSelectionProvider.getDatabaseSelection().getValue();
if (databaseSelectionProviderValue != null) {
return databaseSelectionProviderValue;
}
}
return DatabaseSelectionProvider.getDefaultSelectionProvider().getDatabaseSelection().getValue();
}
}
class DefaultRecordFetchSpec<T> implements RecordFetchSpec<T>, MappingSpec<T> {
@@ -270,24 +284,11 @@ class DefaultNeo4jClient implements Neo4jClient {
DefaultRecordFetchSpec(String parameterTargetDatabase, RunnableStatement runnableStatement,
BiFunction<TypeSystem, Record, T> mappingFunction) {
this.targetDatabase = resolveTargetDatabaseName(parameterTargetDatabase);
this.targetDatabase = parameterTargetDatabase;
this.runnableStatement = runnableStatement;
this.mappingFunction = mappingFunction;
}
private String resolveTargetDatabaseName(@Nullable String parameterTargetDatabase) {
if (parameterTargetDatabase != null) {
return parameterTargetDatabase;
}
if (databaseSelectionProvider != null) {
String databaseSelectionProviderValue = databaseSelectionProvider.getDatabaseSelection().getValue();
if (databaseSelectionProviderValue != null) {
return databaseSelectionProviderValue;
}
}
return DatabaseSelectionProvider.getDefaultSelectionProvider().getDatabaseSelection().getValue();
}
@Override
public RecordFetchSpec<T> mappedBy(
@SuppressWarnings("HiddenField") BiFunction<TypeSystem, Record, T> mappingFunction) {
@@ -358,7 +359,7 @@ class DefaultNeo4jClient implements Neo4jClient {
DefaultRunnableDelegation(Function<QueryRunner, Optional<T>> callback, @Nullable String targetDatabase) {
this.callback = callback;
this.targetDatabase = targetDatabase;
this.targetDatabase = Neo4jClient.verifyDatabaseName(targetDatabase);
}
@Override
@@ -375,4 +376,5 @@ class DefaultNeo4jClient implements Neo4jClient {
}
}
}
}

View File

@@ -4285,7 +4285,7 @@ class RepositoryIT {
}
@Bean
public DatabaseSelectionProvider databaseNameProvider() {
public DatabaseSelectionProvider databaseSelectionProvider() {
return () -> databaseSelection;
}
}