Skip to content

Commit 90b6d8e

Browse files
committed
Polishing.
Use FilterFunction instead of nullable fetchSize to avoid unconditional capturing lambdas and improve defaulting. Add since tag. See #1652 Original pull request: #1898
1 parent 96a4121 commit 90b6d8e

File tree

5 files changed

+44
-47
lines changed

5 files changed

+44
-47
lines changed

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplate.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.r2dbc.spi.ConnectionFactory;
1919
import io.r2dbc.spi.Row;
2020
import io.r2dbc.spi.RowMetadata;
21+
import io.r2dbc.spi.Statement;
2122
import reactor.core.publisher.Flux;
2223
import reactor.core.publisher.Mono;
2324

@@ -320,7 +321,8 @@ public <T> Flux<T> select(Query query, Class<T> entityClass) throws DataAccessEx
320321
<T, P extends Publisher<T>> P doSelect(Query query, Class<?> entityClass, SqlIdentifier tableName,
321322
Class<T> returnType, Function<RowsFetchSpec<T>, P> resultHandler, @Nullable Integer fetchSize) {
322323

323-
RowsFetchSpec<T> fetchSpec = doSelect(query, entityClass, tableName, returnType, fetchSize);
324+
RowsFetchSpec<T> fetchSpec = doSelect(query, entityClass, tableName, returnType,
325+
fetchSize != null ? statement -> statement.fetchSize(fetchSize) : Function.identity());
324326

325327
P result = resultHandler.apply(fetchSpec);
326328

@@ -332,7 +334,7 @@ <T, P extends Publisher<T>> P doSelect(Query query, Class<?> entityClass, SqlIde
332334
}
333335

334336
private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityType, SqlIdentifier tableName,
335-
Class<T> returnType, @Nullable Integer fetchSize) {
337+
Class<T> returnType, Function<? super Statement, ? extends Statement> filterFunction) {
336338

337339
StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityType);
338340

@@ -360,7 +362,7 @@ private <T> RowsFetchSpec<T> doSelect(Query query, Class<?> entityType, SqlIdent
360362
PreparedOperation<?> operation = statementMapper.getMappedObject(selectSpec);
361363

362364
return getRowsFetchSpec(
363-
databaseClient.sql(operation).filter((statement) -> statement.fetchSize(Optional.ofNullable(fetchSize).orElse(0))),
365+
databaseClient.sql(operation).filter(filterFunction),
364366
entityType,
365367
returnType
366368
);

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/core/ReactiveSelectOperation.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ interface SelectWithProjection<T> extends SelectWithQuery<T> {
118118
interface SelectWithQuery<T> extends TerminatingSelect<T> {
119119

120120
/**
121-
* Specifies the fetch size for this query
121+
* Specifies the fetch size for this query.
122122
*
123123
* @param fetchSize
124-
* @return
124+
* @return new instance of {@link SelectWithQuery}.
125+
* @since 3.4
126+
* @see io.r2dbc.spi.Statement#fetchSize(int)
125127
*/
126128
SelectWithQuery<T> withFetchSize(int fetchSize);
127129

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/core/ReactiveSelectOperationSupport.java

-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ static class ReactiveSelectSupport<T> implements ReactiveSelect<T> {
5454
private final Class<T> returnType;
5555
private final Query query;
5656
private final @Nullable SqlIdentifier tableName;
57-
5857
private final @Nullable Integer fetchSize;
5958

6059
ReactiveSelectSupport(R2dbcEntityTemplate template, Class<?> domainType, Class<T> returnType, Query query,
@@ -86,9 +85,6 @@ public <R> SelectWithQuery<R> as(Class<R> returnType) {
8685

8786
@Override
8887
public SelectWithQuery<T> withFetchSize(int fetchSize) {
89-
90-
Assert.notNull(returnType, "FetchSize must not be null");
91-
9288
return new ReactiveSelectSupport<>(template, domainType, returnType, query, tableName, fetchSize);
9389
}
9490

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplateUnitTests.java

+23-24
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void before() {
9999
new MappingR2dbcConverter(new R2dbcMappingContext(), conversions));
100100
}
101101

102-
@Test // gh-220
102+
@Test // GH-220
103103
void shouldCountBy() {
104104

105105
MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Long.class, 1L).build()).build();
@@ -117,8 +117,7 @@ void shouldCountBy() {
117117
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
118118
}
119119

120-
@Test
121-
// GH-1690
120+
@Test // GH-1690
122121
void shouldApplyInterfaceProjection() {
123122

124123
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -165,7 +164,7 @@ void shouldProjectEntityUsingInheritedInterface() {
165164
assertThat(statement.getSql()).isEqualTo("SELECT foo.* FROM foo WHERE foo.THE_NAME = $1");
166165
}
167166

168-
@Test // gh-469
167+
@Test // GH-469
169168
void shouldProjectExistsResult() {
170169

171170
MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Object.class, null).build()).build();
@@ -180,7 +179,7 @@ void shouldProjectExistsResult() {
180179
.verifyComplete();
181180
}
182181

183-
@Test // gh-1310
182+
@Test // GH-1310
184183
void shouldProjectExistsResultWithoutId() {
185184

186185
MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Object.class, null).build()).build();
@@ -192,7 +191,7 @@ void shouldProjectExistsResultWithoutId() {
192191
.expectNext(true).verifyComplete();
193192
}
194193

195-
@Test // gh-1310
194+
@Test // GH-1310
196195
void shouldProjectCountResultWithoutId() {
197196

198197
MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Long.class, 1L).build()).build();
@@ -204,7 +203,7 @@ void shouldProjectCountResultWithoutId() {
204203
.expectNext(1L).verifyComplete();
205204
}
206205

207-
@Test // gh-469
206+
@Test // GH-469
208207
void shouldExistsByCriteria() {
209208

210209
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -224,7 +223,7 @@ void shouldExistsByCriteria() {
224223
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
225224
}
226225

227-
@Test // gh-220
226+
@Test // GH-220
228227
void shouldSelectByCriteria() {
229228

230229
recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());
@@ -240,7 +239,7 @@ void shouldSelectByCriteria() {
240239
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
241240
}
242241

243-
@Test // gh-215
242+
@Test // GH-215
244243
void selectShouldInvokeCallback() {
245244

246245
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -266,7 +265,7 @@ void selectShouldInvokeCallback() {
266265
assertThat(callback.getValues()).hasSize(1);
267266
}
268267

269-
@Test // gh-220
268+
@Test // GH-220
270269
void shouldSelectOne() {
271270

272271
recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());
@@ -282,7 +281,7 @@ void shouldSelectOne() {
282281
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
283282
}
284283

285-
@Test // gh-220, gh-758
284+
@Test // GH-220, GH-758
286285
void shouldSelectOneDoNotOverrideExistingLimit() {
287286

288287
recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());
@@ -299,7 +298,7 @@ void shouldSelectOneDoNotOverrideExistingLimit() {
299298
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
300299
}
301300

302-
@Test // gh-220
301+
@Test // GH-220
303302
void shouldUpdateByQuery() {
304303

305304
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -321,7 +320,7 @@ void shouldUpdateByQuery() {
321320
Parameter.from("Walter"));
322321
}
323322

324-
@Test // gh-220
323+
@Test // GH-220
325324
void shouldDeleteByQuery() {
326325

327326
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -341,7 +340,7 @@ void shouldDeleteByQuery() {
341340
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
342341
}
343342

344-
@Test // gh-220
343+
@Test // GH-220
345344
void shouldDeleteEntity() {
346345

347346
Person person = Person.empty() //
@@ -358,7 +357,7 @@ void shouldDeleteEntity() {
358357
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
359358
}
360359

361-
@Test // gh-365
360+
@Test // GH-365
362361
void shouldInsertVersioned() {
363362

364363
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -379,7 +378,7 @@ void shouldInsertVersioned() {
379378
Parameter.from(1L));
380379
}
381380

382-
@Test // gh-557, gh-402
381+
@Test // GH-557, GH-402
383382
void shouldSkipDefaultIdValueOnInsert() {
384383

385384
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -397,7 +396,7 @@ void shouldSkipDefaultIdValueOnInsert() {
397396
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("bar"));
398397
}
399398

400-
@Test // gh-557, gh-402
399+
@Test // GH-557, GH-402
401400
void shouldSkipDefaultIdValueOnVersionedInsert() {
402401

403402
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -419,7 +418,7 @@ void shouldSkipDefaultIdValueOnVersionedInsert() {
419418
Parameter.from("bar"));
420419
}
421420

422-
@Test // gh-451
421+
@Test // GH-451
423422
void shouldInsertCorrectlyVersionedAndAudited() {
424423

425424
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -447,7 +446,7 @@ void shouldInsertCorrectlyVersionedAndAudited() {
447446
"INSERT INTO with_auditing_and_optimistic_locking (version, name, created_date, last_modified_date) VALUES ($1, $2, $3, $4)");
448447
}
449448

450-
@Test // gh-451
449+
@Test // GH-451
451450
void shouldUpdateCorrectlyVersionedAndAudited() {
452451

453452
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -476,7 +475,7 @@ void shouldUpdateCorrectlyVersionedAndAudited() {
476475
"UPDATE with_auditing_and_optimistic_locking SET version = $1, name = $2, created_date = $3, last_modified_date = $4");
477476
}
478477

479-
@Test // gh-215
478+
@Test // GH-215
480479
void insertShouldInvokeCallback() {
481480

482481
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -504,7 +503,7 @@ void insertShouldInvokeCallback() {
504503
Parameter.from("before-save"));
505504
}
506505

507-
@Test // gh-365
506+
@Test // GH-365
508507
void shouldUpdateVersioned() {
509508

510509
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -526,7 +525,7 @@ void shouldUpdateVersioned() {
526525
Parameter.from(1L));
527526
}
528527

529-
@Test // gh-215
528+
@Test // GH-215
530529
void updateShouldInvokeCallback() {
531530

532531
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -559,7 +558,7 @@ void updateShouldInvokeCallback() {
559558
Parameter.from("before-save"));
560559
}
561560

562-
@Test // gh-637
561+
@Test // GH-637
563562
void insertIncludesInsertOnlyColumns() {
564563

565564
MockRowMetadata metadata = MockRowMetadata.builder().build();
@@ -578,7 +577,7 @@ void insertIncludesInsertOnlyColumns() {
578577
Parameter.from("insert this"));
579578
}
580579

581-
@Test // gh-637
580+
@Test // GH-637
582581
void updateExcludesInsertOnlyColumns() {
583582

584583
MockRowMetadata metadata = MockRowMetadata.builder().build();

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/core/ReactiveSelectOperationUnitTests.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import io.r2dbc.spi.test.MockResult;
2525
import io.r2dbc.spi.test.MockRow;
2626
import io.r2dbc.spi.test.MockRowMetadata;
27-
import reactor.core.publisher.Flux;
2827
import reactor.test.StepVerifier;
2928

3029
import org.junit.jupiter.api.BeforeEach;
3130
import org.junit.jupiter.api.Test;
31+
3232
import org.springframework.data.annotation.Id;
3333
import org.springframework.data.r2dbc.dialect.PostgresDialect;
3434
import org.springframework.data.r2dbc.testing.StatementRecorder;
@@ -56,7 +56,7 @@ void before() {
5656
entityTemplate = new R2dbcEntityTemplate(client, new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE));
5757
}
5858

59-
@Test // gh-220
59+
@Test // GH-220
6060
void shouldSelectAll() {
6161

6262
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -80,7 +80,7 @@ void shouldSelectAll() {
8080
.isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 LIMIT 10 OFFSET 20");
8181
}
8282

83-
@Test // gh-220
83+
@Test // GH-220
8484
void shouldSelectAs() {
8585

8686
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -128,7 +128,7 @@ void shouldSelectAsWithColumnName() {
128128
assertThat(statement.getSql()).isEqualTo("SELECT person.id, person.a_different_name FROM person WHERE person.THE_NAME = $1");
129129
}
130130

131-
@Test // gh-220
131+
@Test // GH-220
132132
void shouldSelectFromTable() {
133133

134134
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -152,7 +152,7 @@ void shouldSelectFromTable() {
152152
assertThat(statement.getSql()).isEqualTo("SELECT the_table.* FROM the_table WHERE the_table.THE_NAME = $1");
153153
}
154154

155-
@Test // gh-220
155+
@Test // GH-220
156156
void shouldSelectFirst() {
157157

158158
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -175,7 +175,7 @@ void shouldSelectFirst() {
175175
assertThat(statement.getSql()).isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 LIMIT 1");
176176
}
177177

178-
@Test // gh-220
178+
@Test // GH-220
179179
void shouldSelectOne() {
180180

181181
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -198,7 +198,7 @@ void shouldSelectOne() {
198198
assertThat(statement.getSql()).isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 LIMIT 2");
199199
}
200200

201-
@Test // gh-220
201+
@Test // GH-220
202202
void shouldSelectExists() {
203203

204204
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -221,7 +221,7 @@ void shouldSelectExists() {
221221
assertThat(statement.getSql()).isEqualTo("SELECT 1 FROM person WHERE person.THE_NAME = $1 LIMIT 1");
222222
}
223223

224-
@Test // gh-220
224+
@Test // GH-220
225225
void shouldSelectCount() {
226226

227227
MockRowMetadata metadata = MockRowMetadata.builder()
@@ -244,8 +244,9 @@ void shouldSelectCount() {
244244
assertThat(statement.getSql()).isEqualTo("SELECT COUNT(*) FROM person WHERE person.THE_NAME = $1");
245245
}
246246

247-
@Test // gh-1652
248-
void shouldBeAbleToProvideFetchSize() {
247+
@Test // GH-1652
248+
void shouldConsiderFetchSize() {
249+
249250
MockRowMetadata metadata = MockRowMetadata.builder()
250251
.columnMetadata(MockColumnMetadata.builder().name("id").type(R2dbcType.INTEGER).build())
251252
.build();
@@ -256,17 +257,14 @@ void shouldBeAbleToProvideFetchSize() {
256257
recorder.addStubbing(s -> s.startsWith("SELECT"), result);
257258

258259
entityTemplate.select(Person.class) //
259-
.withFetchSize(10)
260-
.matching(query(where("name").is("Walter")).limit(10).offset(20)) //
260+
.withFetchSize(10) //
261261
.all() //
262262
.as(StepVerifier::create) //
263263
.expectNextCount(1) //
264264
.verifyComplete();
265265

266266
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("SELECT"));
267267

268-
assertThat(statement.getSql())
269-
.isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 LIMIT 10 OFFSET 20");
270268
assertThat(statement.getFetchSize()).isEqualTo(10);
271269
}
272270

0 commit comments

Comments
 (0)