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) boolean readOnly, @Nullable IsolationLevel isolationLevel, Duration lockWaitTimeout)
implements io.r2dbc.spi.TransactionDefinition { 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") @SuppressWarnings("unchecked")
@Override @Override
public <T> T getAttribute(Option<T> option) { 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)) { if (io.r2dbc.spi.TransactionDefinition.READ_ONLY.equals(option)) {
return this.readOnly; return this.readOnly;
} }
if (io.r2dbc.spi.TransactionDefinition.LOCK_WAIT_TIMEOUT.equals(option) if (io.r2dbc.spi.TransactionDefinition.LOCK_WAIT_TIMEOUT.equals(option) &&
&& !this.lockWaitTimeout.isZero()) { !this.lockWaitTimeout.isZero()) {
return this.lockWaitTimeout; return this.lockWaitTimeout;
} }
return null; return null;
@ -468,7 +459,7 @@ public class R2dbcTransactionManager extends AbstractReactiveTransactionManager
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(128);
sb.append(getClass().getSimpleName()); sb.append(getClass().getSimpleName());
sb.append(" [transactionName='").append(this.transactionName).append('\''); sb.append(" [transactionName='").append(this.transactionName).append('\'');
sb.append(", readOnly=").append(this.readOnly); 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. * 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 mappingFunction a function that maps from {@link Readable} to the result type
* @param <R> 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 * @since 6.0
*/ */
<R> RowsFetchSpec<R> map(Function<? super Readable, R> mappingFunction); <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} * @param mappingFunction a function that maps from {@link Row} and {@link RowMetadata}
* to the result type * to the result type
* @param <R> 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); <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 mappingFunction a function that maps from {@link Result} into a result publisher
* @param <R> the result type * @param <R> the result type
* @return a {@link Flux} that emits mapped elements * @return a {@link Flux} that emits mapped elements

View File

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

View File

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