Skip to content

Commit e2514a3

Browse files
committed
Allow non-domain sort orders to be used with R2dbcQueryCreator.
Closes #1548
1 parent f1ece35 commit e2514a3

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private PreparedOperation<?> select(@Nullable Criteria criteria, Sort sort, Stat
136136
}
137137

138138
if (sort.isSorted()) {
139-
selectSpec = selectSpec.withSort(getSort(sort));
139+
selectSpec = selectSpec.withSort(sort);
140140
}
141141

142142
if (tree.isDistinct()) {
@@ -186,15 +186,4 @@ private Expression[] getSelectProjection() {
186186
return expressions.toArray(new Expression[0]);
187187
}
188188

189-
private Sort getSort(Sort sort) {
190-
191-
RelationalPersistentEntity<?> tableEntity = entityMetadata.getTableEntity();
192-
193-
List<Sort.Order> orders = sort.get().map(order -> {
194-
RelationalPersistentProperty property = tableEntity.getRequiredPersistentProperty(order.getProperty());
195-
return order.isAscending() ? Sort.Order.asc(property.getName()) : Sort.Order.desc(property.getName());
196-
}).collect(Collectors.toList());
197-
198-
return Sort.by(orders);
199-
}
200189
}

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
import org.mockito.junit.jupiter.MockitoExtension;
3939
import org.mockito.junit.jupiter.MockitoSettings;
4040
import org.mockito.quality.Strictness;
41-
4241
import org.springframework.beans.factory.annotation.Value;
4342
import org.springframework.data.annotation.Id;
43+
import org.springframework.data.domain.Sort;
44+
import org.springframework.data.domain.Sort.Direction;
4445
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
4546
import org.springframework.data.r2dbc.convert.R2dbcConverter;
4647
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
@@ -53,6 +54,7 @@
5354
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
5455
import org.springframework.data.relational.core.mapping.Table;
5556
import org.springframework.data.relational.core.sql.LockMode;
57+
import org.springframework.data.relational.domain.SqlSort;
5658
import org.springframework.data.relational.repository.Lock;
5759
import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor;
5860
import org.springframework.data.repository.Repository;
@@ -599,6 +601,21 @@ void throwsExceptionWhenConditionKeywordIsUnsupported() throws Exception {
599601
.isThrownBy(() -> createQuery(r2dbcQuery, getAccessor(queryMethod, new Object[0])));
600602
}
601603

604+
@Test // GH-1548
605+
void allowsSortingByNonDomainProperties() throws Exception {
606+
607+
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByFirstName", String.class, Sort.class);
608+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, operations, r2dbcConverter, dataAccessStrategy);
609+
610+
PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery, "foo", Sort.by("foobar"));
611+
PreparedOperationAssert.assertThat(preparedOperation) //
612+
.orderBy("users.foobar ASC");
613+
614+
preparedOperation = createQuery(queryMethod, r2dbcQuery, "foo", SqlSort.unsafe(Direction.ASC, "sum(foobar)"));
615+
PreparedOperationAssert.assertThat(preparedOperation) //
616+
.orderBy("sum(foobar) ASC");
617+
}
618+
602619
@Test // GH-282
603620
void throwsExceptionWhenInvalidNumberOfParameterIsGiven() throws Exception {
604621

@@ -960,6 +977,8 @@ interface UserRepository extends Repository<User, Long> {
960977

961978
Flux<User> findAllByIdIsEmpty();
962979

980+
Flux<User> findAllByFirstName(String firstName, Sort sort);
981+
963982
Flux<User> findTop3ByFirstName(String firstName);
964983

965984
Mono<User> findFirstByFirstName(String firstName);

0 commit comments

Comments
 (0)