Skip to content

Commit ee9b4b9

Browse files
committed
Apply sorting to select and slice queries only.
StatementFactory now only applies ordering to select and slice queries without applying ordering to count and exists projections. Closes #2190
1 parent 4a44507 commit ee9b4b9

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StatementFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ public String build(MapSqlParameterSource parameterSource) {
201201
SelectBuilder.SelectWhere whereBuilder = applyLimitAndOffset(limitOffsetBuilder);
202202
SelectBuilder.SelectOrdered selectOrderBuilder = applyCriteria(criteria, entity, table, parameterSource,
203203
whereBuilder);
204-
selectOrderBuilder = applyOrderBy(sort, entity, table, selectOrderBuilder);
204+
205+
if (mode == Mode.SLICE || mode == Mode.SELECT) {
206+
selectOrderBuilder = applyOrderBy(sort, entity, table, selectOrderBuilder);
207+
}
205208

206209
SelectBuilder.BuildSelect completedBuildSelect = selectOrderBuilder;
207210
if (this.lockMode != null) {

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StatementFactoryUnitTests.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.data.annotation.Id;
26+
import org.springframework.data.domain.PageRequest;
27+
import org.springframework.data.domain.Sort;
2628
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
2729
import org.springframework.data.jdbc.core.convert.JdbcTypeFactory;
2830
import org.springframework.data.jdbc.core.convert.MappingJdbcConverter;
@@ -41,30 +43,76 @@ class StatementFactoryUnitTests {
4143
JdbcMappingContext mappingContext = new JdbcMappingContext();
4244
MappingJdbcConverter converter;
4345

46+
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
47+
StatementFactory statementFactory;
48+
4449
@BeforeEach
4550
void setUp() {
4651

4752
JdbcCustomConversions conversions = JdbcCustomConversions.of(JdbcH2Dialect.INSTANCE, List.of());
4853
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
54+
mappingContext.setForceQuote(false);
4955
mappingContext.afterPropertiesSet();
5056
converter = new MappingJdbcConverter(mappingContext, (identifier, path) -> null, conversions,
5157
JdbcTypeFactory.unsupported());
58+
statementFactory = new StatementFactory(converter, JdbcH2Dialect.INSTANCE);
5259
}
5360

5461
@Test // GH-2162
55-
void statementFactoryConsidersQualifiedTableName() {
62+
void sliceConsiderSort() {
63+
64+
StatementFactory.SelectionBuilder selection = statementFactory.slice(User.class);
65+
selection.page(PageRequest.of(0, 1, Sort.by("id")));
66+
67+
String sql = selection.build(parameterSource);
68+
assertThat(sql).contains("SELECT user.ID").contains("ORDER BY user.ID");
69+
}
70+
71+
@Test // GH-2162
72+
void selectConsiderSort() {
73+
74+
StatementFactory.SelectionBuilder selection = statementFactory.select(User.class);
75+
selection.page(PageRequest.of(0, 1, Sort.by("id")));
76+
77+
String sql = selection.build(parameterSource);
78+
assertThat(sql).contains("SELECT user.ID").contains("ORDER BY user.ID");
79+
}
80+
81+
@Test // GH-2162
82+
void countDoesNotConsiderSort() {
5683

57-
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
84+
StatementFactory.SelectionBuilder selection = statementFactory.count(User.class);
85+
selection.page(PageRequest.of(0, 1, Sort.by("id")));
86+
87+
String sql = selection.build(parameterSource);
88+
assertThat(sql).isEqualTo("SELECT COUNT(*) FROM user");
89+
}
90+
91+
@Test // GH-2162
92+
void existsDoesNotConsiderSort() {
93+
94+
StatementFactory.SelectionBuilder selection = statementFactory.exists(User.class);
95+
selection.page(PageRequest.of(0, 1, Sort.by("id")));
96+
97+
String sql = selection.build(parameterSource);
98+
assertThat(sql).isEqualTo("SELECT user.ID FROM user OFFSET 0 ROWS FETCH FIRST 1 ROWS ONLY");
99+
}
100+
101+
@Test // GH-2162
102+
void statementFactoryConsidersQualifiedTableName() {
58103

59-
StatementFactory statementFactory = new StatementFactory(converter, JdbcH2Dialect.INSTANCE);
60104
StatementFactory.SelectionBuilder selection = statementFactory.select(Media.class);
61105

62106
String sql = selection.build(parameterSource);
63-
assertThat(sql).contains("SELECT \"archive\".\"media\".").contains("ROM \"archive\".\"media\"");
107+
assertThat(sql).contains("SELECT archive.media.").contains("ROM archive.media");
64108
}
65109

66110
@Table(schema = "archive", name = "media")
67111
public record Media(@Id Long id, String objectType, Long objectId) {
68112
}
69113

114+
@Table(name = "user")
115+
public record User(@Id Long id, String objectType, Long objectId) {
116+
}
117+
70118
}

0 commit comments

Comments
 (0)