From 9fc57ea3bf4795050b3ab874fc77a96cdf07098d Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 26 Aug 2014 13:38:19 +0200 Subject: [PATCH] DATACMNS-566 - Allow sorting by QueryDsl operator expressions. Previously we only allowed to sort by QueryDsl path expressions. With this change we now also support ordering by operator expressions, e.g. yearMonth() on a date property. Original pull request: #94. --- .../springframework/data/querydsl/QSort.java | 6 ++++-- .../data/querydsl/QSortUnitTests.java | 21 ++++++++++++++++--- .../springframework/data/querydsl/User.java | 5 ++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/querydsl/QSort.java b/src/main/java/org/springframework/data/querydsl/QSort.java index 2144e8fc0..1b8af09a6 100644 --- a/src/main/java/org/springframework/data/querydsl/QSort.java +++ b/src/main/java/org/springframework/data/querydsl/QSort.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import org.springframework.util.Assert; import com.mysema.query.types.Expression; import com.mysema.query.types.OrderSpecifier; +import com.mysema.query.types.Path; /** * Sort option for queries that wraps a querydsl {@link OrderSpecifier}. @@ -86,7 +87,8 @@ public class QSort extends Sort implements Serializable { Assert.notNull(orderSpecifier, "Order specifier must not be null!"); Expression target = orderSpecifier.getTarget(); - Object targetElement = ((com.mysema.query.types.Path) target).getMetadata().getElement(); + Object targetElement = target instanceof Path ? ((com.mysema.query.types.Path) target).getMetadata() + .getElement() : target; Assert.notNull(targetElement, "Target element must not be null!"); diff --git a/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java b/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java index e97da5b07..477ab3083 100644 --- a/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java +++ b/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,9 @@ */ package org.springframework.data.querydsl; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; import java.util.List; @@ -146,4 +147,18 @@ public class QSortUnitTests { assertThat(result, is(Matchers. iterableWithSize(2))); assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname"))); } + + /** + * @see DATACMNS-566 + */ + @Test + public void shouldSupportSortByOperatorExpressions() { + + QUser user = QUser.user; + QSort sort = new QSort(user.dateOfBirth.yearMonth().asc()); + + Sort result = sort.and(new Sort(Direction.ASC, "lastname")); + assertThat(result, is(Matchers. iterableWithSize(2))); + assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString()))); + } } diff --git a/src/test/java/org/springframework/data/querydsl/User.java b/src/test/java/org/springframework/data/querydsl/User.java index dec8b693c..09245a728 100644 --- a/src/test/java/org/springframework/data/querydsl/User.java +++ b/src/test/java/org/springframework/data/querydsl/User.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ */ package org.springframework.data.querydsl; +import java.util.Date; + import com.mysema.query.annotations.QueryEntity; /** @@ -25,4 +27,5 @@ import com.mysema.query.annotations.QueryEntity; public class User { String firstname; String lastname; + Date dateOfBirth; }