Skip to content

Commit

Permalink
Allow non-domain sort orders to be used with R2dbcQueryCreator.
Browse files Browse the repository at this point in the history
Closes #1548
  • Loading branch information
mp911de committed Jun 30, 2023
1 parent f1ece35 commit e2514a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private PreparedOperation<?> select(@Nullable Criteria criteria, Sort sort, Stat
}

if (sort.isSorted()) {
selectSpec = selectSpec.withSort(getSort(sort));
selectSpec = selectSpec.withSort(sort);
}

if (tree.isDistinct()) {
Expand Down Expand Up @@ -186,15 +186,4 @@ private Expression[] getSelectProjection() {
return expressions.toArray(new Expression[0]);
}

private Sort getSort(Sort sort) {

RelationalPersistentEntity<?> tableEntity = entityMetadata.getTableEntity();

List<Sort.Order> orders = sort.get().map(order -> {
RelationalPersistentProperty property = tableEntity.getRequiredPersistentProperty(order.getProperty());
return order.isAscending() ? Sort.Order.asc(property.getName()) : Sort.Order.desc(property.getName());
}).collect(Collectors.toList());

return Sort.by(orders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.r2dbc.convert.R2dbcConverter;
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
Expand All @@ -53,6 +54,7 @@
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.relational.core.sql.LockMode;
import org.springframework.data.relational.domain.SqlSort;
import org.springframework.data.relational.repository.Lock;
import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor;
import org.springframework.data.repository.Repository;
Expand Down Expand Up @@ -599,6 +601,21 @@ void throwsExceptionWhenConditionKeywordIsUnsupported() throws Exception {
.isThrownBy(() -> createQuery(r2dbcQuery, getAccessor(queryMethod, new Object[0])));
}

@Test // GH-1548
void allowsSortingByNonDomainProperties() throws Exception {

R2dbcQueryMethod queryMethod = getQueryMethod("findAllByFirstName", String.class, Sort.class);
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, operations, r2dbcConverter, dataAccessStrategy);

PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery, "foo", Sort.by("foobar"));
PreparedOperationAssert.assertThat(preparedOperation) //
.orderBy("users.foobar ASC");

preparedOperation = createQuery(queryMethod, r2dbcQuery, "foo", SqlSort.unsafe(Direction.ASC, "sum(foobar)"));
PreparedOperationAssert.assertThat(preparedOperation) //
.orderBy("sum(foobar) ASC");
}

@Test // GH-282
void throwsExceptionWhenInvalidNumberOfParameterIsGiven() throws Exception {

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

Flux<User> findAllByIdIsEmpty();

Flux<User> findAllByFirstName(String firstName, Sort sort);

Flux<User> findTop3ByFirstName(String firstName);

Mono<User> findFirstByFirstName(String firstName);
Expand Down

0 comments on commit e2514a3

Please sign in to comment.