Skip to content

Commit 53d01ba

Browse files
committed
spring-projects#282 - Add support of limiting query results
1 parent d1d55d7 commit 53d01ba

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ protected String complete(Condition condition, Sort sort) {
131131

132132
if (tree.isExistsProjection()) {
133133
selectBuilder.limit(1);
134+
} else if (tree.isLimiting()) {
135+
selectBuilder.limit(tree.getMaxResults());
134136
}
135137

136138
if (condition != null) {

Diff for: src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryIntegrationTests.java

+28
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,30 @@ public void throwsExceptionWhenInvalidNumberOfParameterIsGiven() throws Exceptio
515515
r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[0]));
516516
}
517517

518+
@Test
519+
public void createsQueryWithLimitToFindEntitiesByStringAttribute() throws Exception {
520+
R2dbcQueryMethod queryMethod = getQueryMethod("findTop3ByFirstName", String.class);
521+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
522+
dataAccessStrategy);
523+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
524+
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
525+
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE
526+
+ " WHERE " + TABLE + ".first_name = :firstName LIMIT 3";
527+
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
528+
}
529+
530+
@Test
531+
public void createsQueryToFindFirstEntityByStringAttribute() throws Exception {
532+
R2dbcQueryMethod queryMethod = getQueryMethod("findFirstByFirstName", String.class);
533+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
534+
dataAccessStrategy);
535+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
536+
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
537+
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE
538+
+ " WHERE " + TABLE + ".first_name = :firstName LIMIT 1";
539+
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
540+
}
541+
518542
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
519543
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
520544
return new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
@@ -587,6 +611,10 @@ private interface UserRepository extends Repository<User, Long> {
587611
Flux<User> findAllById(Collection<Long> ids);
588612

589613
Flux<User> findAllByIdIsEmpty();
614+
615+
Flux<User> findTop3ByFirstName(String firstName);
616+
617+
Mono<User> findFirstByFirstName(String firstName);
590618
}
591619

592620
@Table("users")

0 commit comments

Comments
 (0)