Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[#878] Add OrderBy support in simplified queries

  • Loading branch information...
commit b91659a7a71313e6248cb5eee18e9c7d74f347f7 1 parent 39aac78
Loic Descotte authored mbknor committed
View
2  documentation/manual/jpa.textile
@@ -115,6 +115,8 @@ 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", "%hello%").fetch(); //default for order is asc
+Post.find("byTitleOrderByNbCommentsDesc", "%hello%").fetch();
Simple queries follow the following syntax <code>==[Property][Comparator]And?==</code> where Comparator can be the following:
View
22 framework/src/play/db/jpa/JPQL.java
@@ -211,7 +211,11 @@ public Query bindParameters(Query q, Map<String,Object> params) {
public String findByToJPQL(String findBy) {
findBy = findBy.substring(2);
StringBuffer jpql = new StringBuffer();
- String[] parts = findBy.split("And");
+ String subRequest;
+ if (findBy.contains("OrderBy"))
+ subRequest = findBy.split("OrderBy")[0];
+ else subRequest = findBy;
+ String[] parts = subRequest.split("And");
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
if (part.endsWith("NotEqual")) {
@@ -267,6 +271,22 @@ public String findByToJPQL(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();
}
View
52 framework/test-src/play/db/jpa/JPQLTest.java
@@ -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(null);
+ }
+
+ @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"));
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.