Bugfix orderby for an IdQueryField(ID)

Squash commits of branch bugfix/orderby-id:

- Bugfix orderby for an IdQueryField(ID)

- Add changelog for the bugfix ordering by ID field

- Add second unit test to order by name and id
This commit is contained in:
Anna Vetcininova
2025-06-04 14:58:37 +02:00
parent 72d4b65539
commit 7b32d6233c
3 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Ensure query results are ordered by the ID field

View File

@@ -451,7 +451,12 @@ class SQLiteQueryableStore<T> implements QueryableStore<T>, QueryableMaintenance
Iterator<OrderBy<T>> it = orderBy.iterator();
while (it.hasNext()) {
OrderBy<T> order = it.next();
orderByBuilder.append("json_extract(payload, '$.").append(order.field.getName()).append("') ").append(order.order.name());
if (order.field instanceof IdQueryField) {
orderByBuilder.append("ID ");
} else {
orderByBuilder.append("json_extract(payload, '$.").append(order.field.getName()).append("') ");
}
orderByBuilder.append(order.order.name());
if (it.hasNext()) {
orderByBuilder.append(", ");
}

View File

@@ -457,6 +457,39 @@ class SQLiteQueryableStoreTest {
.containsExactly("Tricia");
}
@Test
void shouldQueryForIdAndOrderByDESC() {
SQLiteQueryableMutableStore<User> store = new StoreTestBuilder(connectionString).withIds();
store.put("1", new User("trish", "Tricia", "tricia@hog.org"));
store.put("2", new User("trillian", "Trillian McMillan", "mcmillan@gmail.com"));
store.put("3", new User("arthur", "Arthur Dent", "arthur@hog.org"));
List<User> all = store.query().orderBy(ID, QueryableStore.Order.DESC)
.findAll();
assertThat(all)
.extracting("name")
.containsExactly("arthur","trillian","trish");
}
@Test
void shouldOrderIdsAndPayload() {
SQLiteQueryableMutableStore<User> store = new StoreTestBuilder(connectionString).withIds();
store.put("1", new User("trish", "Tricia", "tricia@hog.org"));
store.put("2", new User("trillian", "Trillian McMillan", "mcmillan@gmail.com"));
store.put("3", new User("trillian", "Arthur Dent", "arthur@hog.org"));
List<User> all = store.query()
.orderBy(USER_NAME, QueryableStore.Order.DESC)
.orderBy(ID, QueryableStore.Order.ASC)
.findAll();
System.out.println(all);
assertThat(all)
.extracting("displayName")
.containsExactly("Tricia","Trillian McMillan","Arthur Dent");
}
@Test
void shouldQueryForParents() {
new StoreTestBuilder(connectionString, Group.class.getName())