Skip to content

Commit

Permalink
Fix SQL generation for query with sorting.
Browse files Browse the repository at this point in the history
Closes #1919
  • Loading branch information
schauder committed Oct 25, 2024
1 parent 73e5033 commit 593d73a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ private SelectBuilder.SelectOrdered applyQueryOnSelect(Query query, MapSqlParame

if (query.isSorted()) {
List<OrderByField> sort = this.queryMapper.getMappedSort(table, query.getSort(), entity);
selectOrdered = selectBuilder.orderBy(sort);
selectOrdered = selectOrdered.orderBy(sort);
}

SelectBuilder.SelectLimitOffset limitable = (SelectBuilder.SelectLimitOffset) selectOrdered;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,42 @@ void findAllPagedAndSorted() {
"LIMIT 10");
}

@Test // GH-1919
void selectByQuery() {

Query query = Query.query(Criteria.where("id").is(23L));

String sql = sqlGenerator.selectByQuery(query, new MapSqlParameterSource());

assertThat(sql).contains( //
"SELECT", //
"FROM dummy_entity", //
"LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1", //
"LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id", //
"WHERE dummy_entity.id1 = :id1" //
);
}

@Test // GH-1919
void selectBySortedQuery() {

Query query = Query.query(Criteria.where("id").is(23L)) //
.sort(Sort.by(Sort.Order.asc("id")));

String sql = sqlGenerator.selectByQuery(query, new MapSqlParameterSource());

assertThat(sql).contains( //
"SELECT", //
"FROM dummy_entity", //
"LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1", //
"LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id", //
"WHERE dummy_entity.id1 = :id1", //
"ORDER BY dummy_entity.id1 ASC" //
);
assertThat(sql).containsOnlyOnce("LEFT OUTER JOIN referenced_entity ref ON ref.dummy_entity = dummy_entity.id1");
assertThat(sql).containsOnlyOnce("LEFT OUTER JOIN second_level_referenced_entity ref_further ON ref_further.referenced_entity = ref.x_l1id");
}

@Test // DATAJDBC-131, DATAJDBC-111
void findAllByProperty() {

Expand Down

0 comments on commit 593d73a

Please # to comment.