Skip to content

Commit 5a5310a

Browse files
mp911deschauder
authored andcommitted
#8 - Replace exchange() method with map(…) and then() methods.
DatabaseClient now no longer exposes the exchange() method but rather provides map(…) and then(…) methods. Calling map(…) extracts values from tabular results by propagating the mapping function to Result.map(…). then() allows statement execution without returning the result propagating only completion and error signals. Original pull request: #33.
1 parent 536d484 commit 5a5310a

File tree

8 files changed

+291
-113
lines changed

8 files changed

+291
-113
lines changed

README.adoc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The state of R2DBC is incubating to evaluate how an reactive integration could l
88

99
== This is NOT an ORM
1010

11-
Spring Data R2DBC does not try to be an ORM.
11+
Spring Data R2DBC does not try to be an ORM.
1212
Instead it is more of a construction kit for your personal reactive relational data access component that you can define the way you like or need it.
1313

1414
== Maven Coordinates
@@ -64,12 +64,13 @@ Mono<Integer> count = databaseClient.execute()
6464
6565
Flux<Map<String, Object>> rows = databaseClient.execute()
6666
.sql("SELECT id, name, manual FROM legoset")
67-
.fetch().all();
67+
.fetch()
68+
.all();
6869
6970
Flux<Long> result = db.execute()
7071
.sql("SELECT txid_current();")
71-
.exchange()
72-
.flatMapMany(it -> it.extract((r, md) -> r.get(0, Long.class)).all());
72+
.map((r, md) -> r.get(0, Long.class))
73+
.all();
7374
----
7475

7576
=== Examples selecting data
@@ -100,14 +101,13 @@ Flux<Integer> ids = databaseClient.insert()
100101
.value("id", 42055)
101102
.value("name", "Description")
102103
.nullValue("manual", Integer.class)
103-
.exchange() //
104-
.flatMapMany(it -> it.extract((r, m) -> r.get("id", Integer.class)).all())
104+
.map((r, m) -> r.get("id", Integer.class)
105+
.all();
105106
106-
Flux<Integer> ids = databaseClient.insert()
107+
Mono<Void> completion = databaseClient.insert()
107108
.into(LegoSet.class)
108109
.using(legoSet)
109-
.exchange()
110-
.flatMapMany(it -> it.extract((r, m) -> r.get("id", Integer.class)).all())
110+
.then();
111111
----
112112

113113
=== Examples using reactive repositories
@@ -162,7 +162,7 @@ Flux<Integer> rowsUpdated = databaseClient.inTransaction(db -> {
162162
[source,java]
163163
----
164164
Flux<Long> txId = databaseClient.execute().sql("SELECT txid_current();").exchange()
165-
.flatMapMany(it -> it.extract((r, md) -> r.get(0, Long.class)).all());
165+
.flatMapMany(it -> it.map((r, md) -> r.get(0, Long.class)).all());
166166
167167
Mono<Void> then = databaseClient.enableTransactionSynchronization(databaseClient.beginTransaction() //
168168
.thenMany(txId)) //

src/main/java/org/springframework/data/r2dbc/function/DatabaseClient.java

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,26 @@ interface GenericExecuteSpec extends BindSpec<GenericExecuteSpec> {
156156
*/
157157
<R> TypedExecuteSpec<R> as(Class<R> resultType);
158158

159+
/**
160+
* Configure a result mapping {@link java.util.function.BiFunction function}.
161+
*
162+
* @param mappingFunction must not be {@literal null}.
163+
* @param <R> result type.
164+
* @return
165+
*/
166+
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
167+
159168
/**
160169
* Perform the SQL call and retrieve the result.
161170
*/
162171
FetchSpec<Map<String, Object>> fetch();
163172

164173
/**
165-
* Perform the SQL request and return a {@link SqlResult}.
174+
* Perform the SQL call and return a {@link Mono} that completes without result on statement completion.
166175
*
167-
* @return a {@code Mono} for the result
176+
* @return a {@link Mono} ignoring its payload (actively dropping).
168177
*/
169-
Mono<SqlResult<Map<String, Object>>> exchange();
178+
Mono<Void> then();
170179
}
171180

172181
/**
@@ -183,17 +192,26 @@ interface TypedExecuteSpec<T> extends BindSpec<TypedExecuteSpec<T>> {
183192
*/
184193
<R> TypedExecuteSpec<R> as(Class<R> resultType);
185194

195+
/**
196+
* Configure a result mapping {@link java.util.function.BiFunction function}.
197+
*
198+
* @param mappingFunction must not be {@literal null}.
199+
* @param <R> result type.
200+
* @return
201+
*/
202+
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
203+
186204
/**
187205
* Perform the SQL call and retrieve the result.
188206
*/
189207
FetchSpec<T> fetch();
190208

191209
/**
192-
* Perform the SQL request and return a {@link SqlResult}.
210+
* Perform the SQL call and return a {@link Mono} that completes without result on statement completion.
193211
*
194-
* @return a {@code Mono} for the result
212+
* @return a {@link Mono} ignoring its payload (actively dropping).
195213
*/
196-
Mono<SqlResult<T>> exchange();
214+
Mono<Void> then();
197215
}
198216

199217
/**
@@ -229,7 +247,7 @@ interface InsertIntoSpec {
229247
* @param table must not be {@literal null} or empty.
230248
* @return
231249
*/
232-
GenericInsertSpec into(String table);
250+
GenericInsertSpec<Map<String, Object>> into(String table);
233251

234252
/**
235253
* Specify the target table to insert to using the {@link Class entity class}.
@@ -255,16 +273,18 @@ interface GenericSelectSpec extends SelectSpec<GenericSelectSpec> {
255273
<R> TypedSelectSpec<R> as(Class<R> resultType);
256274

257275
/**
258-
* Perform the SQL call and retrieve the result.
276+
* Configure a result mapping {@link java.util.function.BiFunction function}.
277+
*
278+
* @param mappingFunction must not be {@literal null}.
279+
* @param <R> result type.
280+
* @return
259281
*/
260-
FetchSpec<Map<String, Object>> fetch();
282+
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
261283

262284
/**
263-
* Perform the SQL request and return a {@link SqlResult}.
264-
*
265-
* @return a {@code Mono} for the result
285+
* Perform the SQL call and retrieve the result.
266286
*/
267-
Mono<SqlResult<Map<String, Object>>> exchange();
287+
FetchSpec<Map<String, Object>> fetch();
268288
}
269289

270290
/**
@@ -279,28 +299,21 @@ interface TypedSelectSpec<T> extends SelectSpec<TypedSelectSpec<T>> {
279299
* @param resultType must not be {@literal null}.
280300
* @param <R> result type.
281301
*/
282-
<R> TypedSelectSpec<R> as(Class<R> resultType);
302+
<R> FetchSpec<R> as(Class<R> resultType);
283303

284304
/**
285-
* Configure a result mapping {@link java.util.function.Function}.
305+
* Configure a result mapping {@link java.util.function.BiFunction function}.
286306
*
287307
* @param mappingFunction must not be {@literal null}.
288308
* @param <R> result type.
289309
* @return
290310
*/
291-
<R> TypedSelectSpec<R> extract(BiFunction<Row, RowMetadata, R> mappingFunction);
311+
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
292312

293313
/**
294314
* Perform the SQL call and retrieve the result.
295315
*/
296316
FetchSpec<T> fetch();
297-
298-
/**
299-
* Perform the SQL request and return a {@link SqlResult}.
300-
*
301-
* @return a {@code Mono} for the result
302-
*/
303-
Mono<SqlResult<T>> exchange();
304317
}
305318

306319
/**
@@ -332,24 +345,26 @@ interface SelectSpec<S extends SelectSpec<S>> {
332345

333346
/**
334347
* Contract for specifying {@code INSERT} options leading to the exchange.
348+
*
349+
* @param <T> Result type of tabular insert results.
335350
*/
336-
interface GenericInsertSpec extends InsertSpec {
351+
interface GenericInsertSpec<T> extends InsertSpec<T> {
337352

338353
/**
339354
* Specify a field and non-{@literal null} value to insert.
340355
*
341356
* @param field must not be {@literal null} or empty.
342357
* @param value must not be {@literal null}
343358
*/
344-
GenericInsertSpec value(String field, Object value);
359+
GenericInsertSpec<T> value(String field, Object value);
345360

346361
/**
347362
* Specify a {@literal null} value to insert.
348363
*
349364
* @param field must not be {@literal null} or empty.
350365
* @param type must not be {@literal null}.
351366
*/
352-
GenericInsertSpec nullValue(String field, Class<?> type);
367+
GenericInsertSpec<T> nullValue(String field, Class<?> type);
353368
}
354369

355370
/**
@@ -363,7 +378,7 @@ interface TypedInsertSpec<T> {
363378
* @param objectToInsert
364379
* @return
365380
*/
366-
InsertSpec using(T objectToInsert);
381+
InsertSpec<Map<String, Object>> using(T objectToInsert);
367382

368383
/**
369384
* Use the given {@code tableName} as insert target.
@@ -374,30 +389,43 @@ interface TypedInsertSpec<T> {
374389
TypedInsertSpec<T> table(String tableName);
375390

376391
/**
377-
* Insert the given {@link Publisher} to insert one or more objects.
392+
* Insert the given {@link Publisher} to insert one or more objects. Inserts only a single object when calling
393+
* {@link FetchSpec#one()} or {@link FetchSpec#first()}.
378394
*
379395
* @param objectToInsert
380396
* @return
397+
* @see InsertSpec#fetch()
381398
*/
382-
InsertSpec using(Publisher<T> objectToInsert);
399+
InsertSpec<Map<String, Object>> using(Publisher<T> objectToInsert);
383400
}
384401

385402
/**
386403
* Contract for specifying {@code INSERT} options leading to the exchange.
404+
*
405+
* @param <T> Result type of tabular insert results.
387406
*/
388-
interface InsertSpec {
407+
interface InsertSpec<T> {
408+
409+
/**
410+
* Configure a result mapping {@link java.util.function.BiFunction function}.
411+
*
412+
* @param mappingFunction must not be {@literal null}.
413+
* @param <R> result type.
414+
* @return
415+
*/
416+
<R> FetchSpec<R> map(BiFunction<Row, RowMetadata, R> mappingFunction);
389417

390418
/**
391-
* Perform the SQL call.
419+
* Perform the SQL call and retrieve the result.
392420
*/
393-
Mono<Void> then();
421+
FetchSpec<T> fetch();
394422

395423
/**
396-
* Perform the SQL request and return a {@link SqlResult}.
424+
* Perform the SQL call and return a {@link Mono} that completes without result on statement completion.
397425
*
398-
* @return a {@code Mono} for the result
426+
* @return a {@link Mono} ignoring its payload (actively dropping).
399427
*/
400-
Mono<SqlResult<Map<String, Object>>> exchange();
428+
Mono<Void> then();
401429
}
402430

403431
/**

0 commit comments

Comments
 (0)