Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 15, 2023
1 parent 2ce75dc commit 08bc7ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,6 @@ private record ExtendedTransactionDefinition(@Nullable String transactionName,
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) {
Expand All @@ -459,16 +450,16 @@ private Object doGetValue(Option<?> option) {
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;
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ default GenericExecuteSpec filter(Function<? super Statement, ? extends Statemen
* 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);
Expand All @@ -232,12 +232,12 @@ default GenericExecuteSpec filter(Function<? super Statement, ? extends Statemen
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,24 +392,20 @@ private ResultFunction getResultFunction(Supplier<String> sqlSupplier) {
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);
}

Expand Down Expand Up @@ -448,8 +444,7 @@ private Parameter getParameter(Map<String, Parameter> remainderByName,

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");
}
}

Expand Down Expand Up @@ -497,8 +492,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void shouldCloseConnectionOnlyOnce() {
DefaultDatabaseClient databaseClient = (DefaultDatabaseClient) databaseClientBuilder.build();
Flux<Object> flux = databaseClient.inConnectionMany(connection -> Flux.empty());

flux.subscribe(new CoreSubscriber<Object>() {
flux.subscribe(new CoreSubscriber<>() {

Subscription subscription;

Expand Down Expand Up @@ -136,13 +136,15 @@ void executeShouldBindNullValues() {

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));
}
Expand All @@ -153,15 +155,15 @@ void executeShouldBindSettableValues() {
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));
}
Expand All @@ -171,8 +173,9 @@ void executeShouldBindNamedNullValues() {
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));
}
Expand All @@ -185,9 +188,9 @@ void executeShouldBindNamedValuesFromIndexes() {
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");
Expand All @@ -207,8 +210,9 @@ void executeShouldBindValues() {

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"));
}
Expand Down

0 comments on commit 08bc7ed

Please sign in to comment.