Polishing

This commit is contained in:
Juergen Hoeller 2023-08-15 23:51:41 +02:00
parent 2ce75dc415
commit 08bc7ed8f0
4 changed files with 36 additions and 47 deletions

View File

@ -433,15 +433,6 @@ public class R2dbcTransactionManager extends AbstractReactiveTransactionManager
boolean readOnly, @Nullable IsolationLevel isolationLevel, Duration lockWaitTimeout)
implements io.r2dbc.spi.TransactionDefinition {
private ExtendedTransactionDefinition(@Nullable String transactionName, boolean readOnly,
@Nullable IsolationLevel isolationLevel, Duration lockWaitTimeout) {
this.transactionName = transactionName;
this.readOnly = readOnly;
this.isolationLevel = isolationLevel;
this.lockWaitTimeout = lockWaitTimeout;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getAttribute(Option<T> option) {
@ -459,8 +450,8 @@ public class R2dbcTransactionManager extends AbstractReactiveTransactionManager
if (io.r2dbc.spi.TransactionDefinition.READ_ONLY.equals(option)) {
return this.readOnly;
}
if (io.r2dbc.spi.TransactionDefinition.LOCK_WAIT_TIMEOUT.equals(option)
&& !this.lockWaitTimeout.isZero()) {
if (io.r2dbc.spi.TransactionDefinition.LOCK_WAIT_TIMEOUT.equals(option) &&
!this.lockWaitTimeout.isZero()) {
return this.lockWaitTimeout;
}
return null;
@ -468,7 +459,7 @@ public class R2dbcTransactionManager extends AbstractReactiveTransactionManager
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(128);
sb.append(getClass().getSimpleName());
sb.append(" [transactionName='").append(this.transactionName).append('\'');
sb.append(", readOnly=").append(this.readOnly);

View File

@ -222,7 +222,7 @@ public interface DatabaseClient extends ConnectionAccessor {
* Configure a result mapping {@link Function function} and enter the execution stage.
* @param mappingFunction a function that maps from {@link Readable} to the result type
* @param <R> the result type
* @return a {@link FetchSpec} for configuration what to fetch
* @return a {@link RowsFetchSpec} for configuration what to fetch
* @since 6.0
*/
<R> RowsFetchSpec<R> map(Function<? super Readable, R> mappingFunction);
@ -232,12 +232,12 @@ public interface DatabaseClient extends ConnectionAccessor {
* @param mappingFunction a function that maps from {@link Row} and {@link RowMetadata}
* to the result type
* @param <R> the result type
* @return a {@link FetchSpec} for configuration what to fetch
* @return a {@link RowsFetchSpec} for configuration what to fetch
*/
<R> RowsFetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
/**
* Perform the SQL call and apply {@link BiFunction function} to the {@link Result}.
* Perform the SQL call and apply {@link BiFunction function} to the {@link Result}.
* @param mappingFunction a function that maps from {@link Result} into a result publisher
* @param <R> the result type
* @return a {@link Flux} that emits mapped elements

View File

@ -392,24 +392,20 @@ final class DefaultDatabaseClient implements DatabaseClient {
return statement;
};
return new ResultFunction(sqlSupplier, statementFunction, this.filterFunction, DefaultDatabaseClient.this.executeFunction);
return new ResultFunction(sqlSupplier, statementFunction, this.filterFunction,
DefaultDatabaseClient.this.executeFunction);
}
private <T> FetchSpec<T> execute(Supplier<String> sqlSupplier, Function<Result, Publisher<T>> resultAdapter) {
ResultFunction resultHandler = getResultFunction(sqlSupplier);
return new DefaultFetchSpec<>(
DefaultDatabaseClient.this,
resultHandler,
connection -> sumRowsUpdated(resultHandler, connection),
resultAdapter);
return new DefaultFetchSpec<>(DefaultDatabaseClient.this, resultHandler,
connection -> sumRowsUpdated(resultHandler, connection), resultAdapter);
}
private <T> Flux<T> flatMap(Supplier<String> sqlSupplier, Function<Result, Publisher<T>> mappingFunction) {
ResultFunction resultHandler = getResultFunction(sqlSupplier);
ConnectionFunction<Flux<T>> connectionFunction = new DelegateConnectionFunction<>(resultHandler, cx -> resultHandler
.apply(cx)
.flatMap(mappingFunction));
ConnectionFunction<Flux<T>> connectionFunction = new DelegateConnectionFunction<>(resultHandler,
cx -> resultHandler.apply(cx).flatMap(mappingFunction));
return inConnectionMany(connectionFunction);
}
@ -448,8 +444,7 @@ final class DefaultDatabaseClient implements DatabaseClient {
private void assertNotPreparedOperation() {
if (this.sqlSupplier instanceof PreparedOperation<?>) {
throw new InvalidDataAccessApiUsageException(
"Cannot add bindings to a PreparedOperation");
throw new InvalidDataAccessApiUsageException("Cannot add bindings to a PreparedOperation");
}
}
@ -497,8 +492,7 @@ final class DefaultDatabaseClient implements DatabaseClient {
return this.target;
case "close":
// Handle close method: suppress, not valid.
return Mono.error(
new UnsupportedOperationException("Close is not supported!"));
return Mono.error(new UnsupportedOperationException("Close is not supported!"));
}
// Invoke method on target Connection.

View File

@ -103,7 +103,7 @@ class DefaultDatabaseClientUnitTests {
DefaultDatabaseClient databaseClient = (DefaultDatabaseClient) databaseClientBuilder.build();
Flux<Object> flux = databaseClient.inConnectionMany(connection -> Flux.empty());
flux.subscribe(new CoreSubscriber<Object>() {
flux.subscribe(new CoreSubscriber<>() {
Subscription subscription;
@ -136,13 +136,15 @@ class DefaultDatabaseClientUnitTests {
DatabaseClient databaseClient = databaseClientBuilder.namedParameters(false).build();
databaseClient.sql("SELECT * FROM table WHERE key = $1").bindNull(0,
String.class).then().as(StepVerifier::create).verifyComplete();
databaseClient.sql("SELECT * FROM table WHERE key = $1")
.bindNull(0, String.class)
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind(0, Parameters.in(String.class));
databaseClient.sql("SELECT * FROM table WHERE key = $1").bindNull("$1",
String.class).then().as(StepVerifier::create).verifyComplete();
databaseClient.sql("SELECT * FROM table WHERE key = $1")
.bindNull("$1", String.class)
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind("$1", Parameters.in(String.class));
}
@ -153,15 +155,15 @@ class DefaultDatabaseClientUnitTests {
Statement statement = mockStatementFor("SELECT * FROM table WHERE key = $1");
DatabaseClient databaseClient = databaseClientBuilder.namedParameters(false).build();
databaseClient.sql("SELECT * FROM table WHERE key = $1").bind(0,
Parameter.empty(String.class)).then().as(
StepVerifier::create).verifyComplete();
databaseClient.sql("SELECT * FROM table WHERE key = $1")
.bind(0, Parameter.empty(String.class))
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind(0, Parameters.in(String.class));
databaseClient.sql("SELECT * FROM table WHERE key = $1").bind("$1",
Parameter.empty(String.class)).then().as(
StepVerifier::create).verifyComplete();
databaseClient.sql("SELECT * FROM table WHERE key = $1")
.bind("$1", Parameter.empty(String.class))
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind("$1", Parameters.in(String.class));
}
@ -171,8 +173,9 @@ class DefaultDatabaseClientUnitTests {
Statement statement = mockStatementFor("SELECT * FROM table WHERE key = $1");
DatabaseClient databaseClient = databaseClientBuilder.build();
databaseClient.sql("SELECT * FROM table WHERE key = :key").bindNull("key",
String.class).then().as(StepVerifier::create).verifyComplete();
databaseClient.sql("SELECT * FROM table WHERE key = :key")
.bindNull("key", String.class)
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind(0, Parameters.in(String.class));
}
@ -185,9 +188,9 @@ class DefaultDatabaseClientUnitTests {
DatabaseClient databaseClient = databaseClientBuilder.build();
databaseClient.sql(
"SELECT id, name, manual FROM legoset WHERE name IN (:name)").bind(0,
Arrays.asList("unknown", "dunno", "other")).then().as(
StepVerifier::create).verifyComplete();
"SELECT id, name, manual FROM legoset WHERE name IN (:name)")
.bind(0, Arrays.asList("unknown", "dunno", "other"))
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind(0, "unknown");
verify(statement).bind(1, "dunno");
@ -207,8 +210,9 @@ class DefaultDatabaseClientUnitTests {
verify(statement).bind(0, Parameters.in("foo"));
databaseClient.sql("SELECT * FROM table WHERE key = $1").bind("$1",
"foo").then().as(StepVerifier::create).verifyComplete();
databaseClient.sql("SELECT * FROM table WHERE key = $1")
.bind("$1", "foo")
.then().as(StepVerifier::create).verifyComplete();
verify(statement).bind("$1", Parameters.in("foo"));
}