Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub issue #1278: OrderBy support in simplified JPA queries for 1.5.x #1343

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion documentation/manual/jpa.textile
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ bc. Post.find("byTitle", "My first post").fetch();
Post.find("byTitleLike", "%hello%").fetch();
Post.find("byAuthorIsNull").fetch();
Post.find("byTitleLikeAndAuthor", "%hello%", connectedUser).fetch();
Post.find("byTitleOrderByTitle", "A nice post").fetch();
Post.find("byTitleOrderByNbCommentsDesc", "A nice post").fetch();

Simple queries follow the following syntax <code>==[Property][Comparator]And?==</code> where Comparator can be the following:
Simple queries follow the following syntax <code>==[Property][Comparator]And?[Orderdirective]?==</code> where Comparator can be the following:

* @LessThan@ - less than the given value
* @LessThanEquals@ - less than or equal a give value
Expand All @@ -133,6 +135,15 @@ Simple queries follow the following syntax <code>==[Property][Comparator]And?==<
* @IsNotNull@ - Not a null value (doesn't require an argument)
* @IsNull@ - Is a null value (doesn't require an argument)

and Orderdirective can be (e.g for a property named @name@) :

* @OrderByName@ - default ascending order
* @OrderByNameDesc@ - descending order

you can also use several attributes in Orderdirective :
* @OrderByNameAndAge@
* @OrderByNameAndAgeDesc@

h3. Find using a JPQL query

You can use a JPQL query:
Expand Down
18 changes: 17 additions & 1 deletion framework/src/play/db/jpa/JPQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public String findByToJPQL(String dbName, String findBy) {
StringBuilder jpql = new StringBuilder();
String subRequest;
if (findBy.contains("OrderBy"))
subRequest = findBy.split("OrderBy")[0];
subRequest = findBy.split("OrderBy")[0];
else subRequest = findBy;
String[] parts = subRequest.split("And");
int index = 1;
Expand Down Expand Up @@ -309,6 +309,22 @@ public String findByToJPQL(String dbName, String findBy) {
jpql.append(" AND ");
}
}
// ORDER BY clause
if (findBy.contains("OrderBy")) {
jpql.append(" ORDER BY ");
String orderQuery = findBy.split("OrderBy")[1];
parts = orderQuery.split("And");
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
String orderProp;
if (part.endsWith("Desc"))
orderProp = extractProp(part, "Desc") + " DESC";
else orderProp = part.toLowerCase();
if (i > 0)
jpql.append(", ");
jpql.append(orderProp);
}
}
return jpql.toString();
}

Expand Down
52 changes: 52 additions & 0 deletions framework/test-src/play/db/jpa/JPQLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package play.db.jpa;


import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Properties;
import java.util.Set;

import static play.db.jpa.JPQL.extractProp;

import static org.junit.Assert.*;

public class JPQLTest {

static JPQL jpql;

@BeforeClass
public static void setup(){
jpql = new JPQL();
}

@Test
public void testOrder() {

String query = "ByNameOrderByName";
String result = jpql.findByToJPQL(query);
assertTrue(result.endsWith(" ORDER BY name"));

query = "ByNameOrderByNameAndAge";
result = jpql.findByToJPQL(query);
assertTrue(result.endsWith(" ORDER BY name, age"));

query = "ByNameOrderByNameDesc";
result = jpql.findByToJPQL(query);
assertTrue(result.endsWith(" ORDER BY name DESC"));

query = "ByNameOrderByNameDescAndAge";
result = jpql.findByToJPQL(query);
assertTrue(result.endsWith(" ORDER BY name DESC, age"));

query = "ByNameOrderByNameAndAgeDesc";
result = jpql.findByToJPQL(query);
assertTrue(result.endsWith(" ORDER BY name, age DESC"));

query = "ByNameOrderByNameDescAndAgeDesc";
result = jpql.findByToJPQL(query);
assertTrue(result.endsWith(" ORDER BY name DESC, age DESC"));

}

}