deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [3/3] git commit: DELTASPIKE-581 Handle count queries without where-clause
Date Wed, 30 Apr 2014 09:15:05 GMT
DELTASPIKE-581 Handle count queries without where-clause


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

Branch: refs/heads/master
Commit: 356f8f7d9f4ddf996b705e6167e5b725dbdcf881
Parents: b68774d
Author: Thomas Hug <Thomas.Hug@ctp.com>
Authored: Wed Apr 30 10:55:17 2014 +0200
Committer: Thomas Hug <Thomas.Hug@ctp.com>
Committed: Wed Apr 30 10:55:17 2014 +0200

----------------------------------------------------------------------
 .../postprocessor/CountQueryPostProcessor.java       | 14 +++++++++++---
 .../apache/deltaspike/data/impl/util/QueryUtils.java | 10 ++++++++++
 .../apache/deltaspike/data/impl/QueryResultTest.java | 15 +++++++++++++++
 .../data/test/service/SimpleRepository.java          |  3 +++
 4 files changed, 39 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/356f8f7d/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 c34e269..118a583 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
@@ -18,6 +18,9 @@
  */
 package org.apache.deltaspike.data.impl.builder.postprocessor;
 
+import static org.apache.deltaspike.data.impl.util.QueryUtils.isNotEmpty;
+import static org.apache.deltaspike.data.impl.util.QueryUtils.nullSafeValue;
+
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -26,7 +29,6 @@ import javax.persistence.Query;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
 import org.apache.deltaspike.data.impl.handler.JpaQueryPostProcessor;
 import org.apache.deltaspike.data.impl.param.Parameters;
-import org.apache.deltaspike.data.impl.util.QueryUtils;
 import org.apache.deltaspike.data.impl.util.jpa.QueryStringExtractorFactory;
 
 public class CountQueryPostProcessor implements JpaQueryPostProcessor
@@ -51,7 +53,7 @@ public class CountQueryPostProcessor implements JpaQueryPostProcessor
 
     private String getQueryString(CdiQueryInvocationContext context, Query query)
     {
-        if (QueryUtils.isNotEmpty(context.getQueryString()))
+        if (isNotEmpty(context.getQueryString()))
         {
             return context.getQueryString();
         }
@@ -82,7 +84,13 @@ public class CountQueryPostProcessor implements JpaQueryPostProcessor
 
         private String rewrite()
         {
-            return "select count(" + (select != null ? select : entityName) + ") " + from
+ where;
+            return new StringBuilder()
+                    .append("select count(")
+                        .append(nullSafeValue(select, entityName))
+                    .append(") ")
+                    .append(from)
+                    .append(nullSafeValue(where))
+                    .toString();
         }
 
         private void extractEntityName()

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/356f8f7d/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/QueryUtils.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/QueryUtils.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/QueryUtils.java
index ad96a28..adea94d 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/QueryUtils.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/QueryUtils.java
@@ -73,6 +73,16 @@ public final class QueryUtils
         return value != null && value instanceof String;
     }
 
+    public static String nullSafeValue(String value)
+    {
+        return nullSafeValue(value, null);
+    }
+
+    public static String nullSafeValue(String value, String fallback)
+    {
+        return value != null ? value : (fallback != null ? fallback : "");
+    }
+
     public static boolean contains(Class<?> clazz, Method method)
     {
         return extract(clazz, method) != null;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/356f8f7d/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 55db66f..5e13512 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
@@ -283,6 +283,21 @@ public class QueryResultTest extends TransactionalTestCase
     }
 
     @Test
+    public void should_count_without_whereclause()
+    {
+        // given
+        final String name = "testCountWithoutWhereclause";
+        builder.createSimple(name);
+        builder.createSimple(name);
+
+        // when
+        long result = repo.queryAll().count();
+
+        // then
+        assertEquals(2L, result);
+    }
+
+    @Test
     public void should_query_optional()
     {
         // given

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/356f8f7d/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 f3d1d07..45301ea 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,9 @@ public abstract class SimpleRepository extends AbstractEntityRepository<Simple,
     @Query(named = Simple.BY_NAME_LIKE)
     public abstract QueryResult<Simple> queryResultWithNamed(String name);
 
+    @Query("select s from Simple s")
+    public abstract QueryResult<Simple> queryAll();
+
     public abstract QueryResult<Simple> findByName(String name);
 
     @Override


Mime
View raw message