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.
This commit is contained in:
Thomas Darimont
2014-08-26 13:38:19 +02:00
committed by Oliver Gierke
parent edfd341d64
commit 9fc57ea3bf
3 changed files with 26 additions and 6 deletions

View File

@@ -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!");

View File

@@ -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.<Order> 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.<Order> iterableWithSize(2)));
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString())));
}
}

View File

@@ -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;
}