deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johndam...@apache.org
Subject deltaspike git commit: DELTASPIKE-1153 Added support for ordering without a where clause.
Date Mon, 23 May 2016 02:10:10 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master f632269f1 -> 7c83662d0


DELTASPIKE-1153 Added support for ordering without a where clause.


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/7c83662d
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/7c83662d
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/7c83662d

Branch: refs/heads/master
Commit: 7c83662d0e981d214ffefe73ae0748540299e6ba
Parents: f632269
Author: John D. Ament <johndament@apache.org>
Authored: Sun May 22 22:10:13 2016 -0400
Committer: John D. Ament <johndament@apache.org>
Committed: Sun May 22 22:10:13 2016 -0400

----------------------------------------------------------------------
 .../data/impl/builder/part/QueryRoot.java       |  4 ++-
 .../deltaspike/data/impl/meta/MethodPrefix.java | 24 ++++++++++-----
 .../data/impl/handler/QueryHandlerTest.java     | 32 +++++++++++++++++++-
 .../data/impl/meta/MethodPrefixTest.java        | 31 +++++++++++++++++++
 .../data/test/service/SimpleRepository.java     |  4 +++
 documentation/src/main/asciidoc/data.adoc       | 19 ++++++++++++
 6 files changed, 104 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c83662d/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/part/QueryRoot.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/part/QueryRoot.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/part/QueryRoot.java
index 1d484c8..767efa1 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/part/QueryRoot.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/part/QueryRoot.java
@@ -138,7 +138,9 @@ public class QueryRoot extends QueryPart
 
     private boolean hasQueryConditions(String[] orderByParts)
     {
-        return !methodPrefix.getPrefix().equals(orderByParts[0]);
+        String orderByPart = orderByParts[0];
+        String prefix = methodPrefix.getPrefix();
+        return !prefix.equals(orderByPart) && !orderByPart.matches(prefix);
     }
 
     private String removePrefix(String queryPart)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c83662d/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/MethodPrefix.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/MethodPrefix.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/MethodPrefix.java
index 37f73b5..b93e45c 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/MethodPrefix.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/MethodPrefix.java
@@ -30,9 +30,10 @@ public class MethodPrefix
     public static final String DEFAULT_ANY_PREFIX = "findAnyBy";
     public static final String DEFAULT_DELETE_PREFIX = "deleteBy";
     public static final String DEFAULT_REMOVE_PREFIX = "removeBy";
+    private static final String FIND_ALL_PREFIX = "findAll";
 
-    private static final String FIND_FIRST_PREFIX = "find(First|Top)(\\d+)By";
-    private static final String FIND_FIRST_PREFIX_PATTERN = FIND_FIRST_PREFIX + "(.+)";
+    private static final String FIND_FIRST_PREFIX = "find(First|Top)(\\d+)(By)*";
+    private static final String FIND_FIRST_PREFIX_PATTERN = FIND_FIRST_PREFIX + "(.*)";
     private static final Pattern DIGIT_PATTERN = Pattern.compile("\\d+");
 
     private final String customPrefix;
@@ -103,7 +104,7 @@ public class MethodPrefix
                 this.getPrefix().equalsIgnoreCase(DEFAULT_REMOVE_PREFIX);
     }
 
-    public int getDefinedMaxResults()
+    int getDefinedMaxResults()
     {
         return definedMaxResults;
     }
@@ -123,9 +124,15 @@ public class MethodPrefix
     private enum KnownQueryPrefix
     {
         DEFAULT(DEFAULT_PREFIX, SingleResultType.JPA),
+        ALL(FIND_ALL_PREFIX, SingleResultType.JPA),
         FIND_FIRST(FIND_FIRST_PREFIX, SingleResultType.JPA)
         {
             @Override
+            public boolean matches(String name)
+            {
+                return name.matches(FIND_FIRST_PREFIX_PATTERN);
+            }
+            @Override
             public String removePrefix(String queryPart)
             {
                 return queryPart.replaceFirst(FIND_FIRST_PREFIX,"");
@@ -160,15 +167,16 @@ public class MethodPrefix
             return this.singleResultType;
         }
 
+        public boolean matches(String name)
+        {
+            return name.startsWith(getPrefix());
+        }
+
         public static KnownQueryPrefix fromMethodName(String name)
         {
-            if (name.matches(FIND_FIRST_PREFIX_PATTERN))
-            {
-                return FIND_FIRST;
-            }
             for (KnownQueryPrefix mapping : values())
             {
-                if (name.startsWith(mapping.getPrefix()))
+                if (mapping.matches(name))
                 {
                     return mapping;
                 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c83662d/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
index f7b8ccd..75f8543 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
@@ -61,7 +61,6 @@ public class QueryHandlerTest extends TransactionalTestCase
     @Inject
     private Simple2Repository repo2;
 
-
     private SimpleBuilder builder;
 
     @Test
@@ -486,6 +485,37 @@ public class QueryHandlerTest extends TransactionalTestCase
         assertEquals(2, result.size());
     }
 
+    @Test
+    public void should_find_top_3_ordered()
+    {
+        builder.createSimple("zebra");
+        builder.createSimple("willow");
+        builder.createSimple("kangaroo");
+        builder.createSimple("bologna");
+
+        List<Simple> result = repo.findFirst3OrderByName();
+
+        assertEquals("bologna", result.get(0).getName());
+        assertEquals("kangaroo", result.get(1).getName());
+        assertEquals("willow", result.get(2).getName());
+    }
+
+    @Test
+    public void should_find_all_ordered()
+    {
+        builder.createSimple("zebra");
+        builder.createSimple("willow");
+        builder.createSimple("kangaroo");
+        builder.createSimple("bologna");
+
+        List<Simple> result = repo.findAllOrderByName();
+
+        assertEquals("bologna", result.get(0).getName());
+        assertEquals("kangaroo", result.get(1).getName());
+        assertEquals("willow", result.get(2).getName());
+        assertEquals("zebra", result.get(3).getName());
+    }
+
     @Before
     public void setup()
     {

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c83662d/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/MethodPrefixTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/MethodPrefixTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/MethodPrefixTest.java
index 04e1aa1..bcfa0a2 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/MethodPrefixTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/MethodPrefixTest.java
@@ -63,4 +63,35 @@ public class MethodPrefixTest
 
         assertEquals(0, maxResults);
     }
+
+    @Test
+    public void shouldParseOrderByEmailDesc()
+    {
+        MethodPrefix methodPrefix = new MethodPrefix("","findFirst3OrderByEmailDesc");
+
+        String resultingQuery = methodPrefix.removePrefix("findFirst3OrderByEmailDesc");
+
+        assertEquals("OrderByEmailDesc", resultingQuery);
+    }
+
+    @Test
+    public void shouldParseOrderByEmail()
+    {
+        MethodPrefix methodPrefix = new MethodPrefix("","findFirst3OrderByEmail");
+
+        String resultingQuery = methodPrefix.removePrefix("findFirst3OrderByEmail");
+
+        assertEquals("OrderByEmail", resultingQuery);
+    }
+
+    @Test
+    public void shouldParseFindAllOrderByName()
+    {
+        MethodPrefix methodPrefix = new MethodPrefix("","findAllOrderByName");
+
+        String resultingQuery = methodPrefix.removePrefix("findAllOrderByName");
+
+        assertEquals("OrderByName", resultingQuery);
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c83662d/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepository.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepository.java
index 62dcb24..977736d 100755
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepository.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepository.java
@@ -89,6 +89,10 @@ public abstract class SimpleRepository extends AbstractEntityRepository<Simple,
 
     public abstract List<Simple> findTop2ByName(String name);
 
+    public abstract List<Simple> findFirst3OrderByName();
+
+    public abstract List<Simple> findAllOrderByName();
+
     public abstract List<Simple> findByOrderByCounterAscIdDesc();
 
     @Query(value = "SELECT * from SIMPLE_TABLE s WHERE s.name = ?1", isNative = true)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c83662d/documentation/src/main/asciidoc/data.adoc
----------------------------------------------------------------------
diff --git a/documentation/src/main/asciidoc/data.adoc b/documentation/src/main/asciidoc/data.adoc
index 904a468..e8ea354 100644
--- a/documentation/src/main/asciidoc/data.adoc
+++ b/documentation/src/main/asciidoc/data.adoc
@@ -540,6 +540,25 @@ public interface PersonRepository extends EntityRepository<Person,
Long>
 }
 ------------------------------------------------------------------------------
 
+Query Limits can be applied even when there is no where clause defined by your
+query.
+
+[source,java]
+------------------------------------------------------------------------------
+@Repository
+public interface PersonRepository extends EntityRepository<Person, Long>
+{
+
+    List<Person> findAllOrderByAgeAsc();
+
+    List<Person> findTop20OrderByLastNameDesc();
+
+}
+------------------------------------------------------------------------------
+
+The first query finding all entries ordered by age, and the second only 20
+ordered by last name.
+
 === Nested Properties
 
 To create a comparison on a nested property, the traversal parts can be


Mime
View raw message