deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject deltaspike git commit: DELTASPIKE-714 Fix count query generation
Date Fri, 23 Jan 2015 09:40:46 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 15d021dde -> 81f47b326


DELTASPIKE-714 Fix count query generation

Simplistic approach - we don't care about the order when counting, so
just cutting it off.


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

Branch: refs/heads/master
Commit: 81f47b3260a4fffc7883d50ac2fc868001fb4bb7
Parents: 15d021d
Author: Thomas Hug <Thomas.Hug@ctp.com>
Authored: Fri Jan 23 10:39:28 2015 +0100
Committer: Thomas Hug <Thomas.Hug@ctp.com>
Committed: Fri Jan 23 10:39:28 2015 +0100

----------------------------------------------------------------------
 .../postprocessor/CountQueryPostProcessor.java       |  5 +++++
 .../apache/deltaspike/data/impl/QueryResultTest.java | 15 +++++++++++++++
 .../data/test/service/SimpleRepository.java          |  3 +++
 3 files changed, 23 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/81f47b32/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/postprocessor/CountQueryPostProcessor.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/postprocessor/CountQueryPostProcessor.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/postprocessor/CountQueryPostProcessor.java
index 118a583..c781d21 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/postprocessor/CountQueryPostProcessor.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/postprocessor/CountQueryPostProcessor.java
@@ -112,6 +112,7 @@ public class CountQueryPostProcessor implements JpaQueryPostProcessor
             int selectIndex = lower.indexOf("select");
             int fromIndex = lower.indexOf("from");
             int whereIndex = lower.indexOf("where");
+            int orderByIndex = lower.indexOf("order by");
             if (selectIndex >= 0)
             {
                 select = query.substring("select".length(), fromIndex);
@@ -120,6 +121,10 @@ public class CountQueryPostProcessor implements JpaQueryPostProcessor
             {
                 from = query.substring(fromIndex, whereIndex);
                 where = query.substring(whereIndex);
+                if (orderByIndex > 0)
+                {
+                    where = where.substring(0, orderByIndex - whereIndex);
+                }
             }
             else
             {

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/81f47b32/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/QueryResultTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/QueryResultTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/QueryResultTest.java
index 860763e..e300ea0 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/QueryResultTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/QueryResultTest.java
@@ -298,6 +298,21 @@ public class QueryResultTest extends TransactionalTestCase
     }
 
     @Test
+    public void should_count_with_orderby()
+    {
+        // given
+        final String name = "testCountWithOrderBy";
+        builder.createSimple(name);
+        builder.createSimple(name);
+
+        // when
+        long result = repo.findByQueryWithOrderBy(name).count();
+
+        // then
+        assertEquals(2L, result);
+    }
+
+    @Test
     public void should_query_optional()
     {
         // given

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/81f47b32/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 8cc16eb..4f1092e 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
@@ -71,6 +71,9 @@ public abstract class SimpleRepository extends AbstractEntityRepository<Simple,
     @Query("select count(s) from Simple s where s.name = ?1")
     public abstract Long findCountByQuery(String name);
 
+    @Query("select s from Simple s where s.name = ?1 order by s.counter desc")
+    public abstract QueryResult<Simple> findByQueryWithOrderBy(String name);
+
     public abstract Simple findByNameAndEnabled(String name, Boolean enabled);
 
     public abstract Simple findOptionalByName(String name);


Mime
View raw message