deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rafab...@apache.org
Subject deltaspike git commit: Fixed: DELTASPIKE-603 removeBy* - similiar to findBy*
Date Fri, 22 May 2015 21:24:45 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 62c99d28a -> 1238dbaee


Fixed: DELTASPIKE-603 removeBy* - similiar to findBy*


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

Branch: refs/heads/master
Commit: 1238dbaee3fb8c52d99661ada6f4df200d065507
Parents: 62c99d2
Author: Daniel Cunha (soro) <danielsoro@gmail.com>
Authored: Fri May 22 16:26:45 2015 -0300
Committer: Daniel Cunha (soro) <danielsoro@gmail.com>
Committed: Fri May 22 16:44:13 2015 -0300

----------------------------------------------------------------------
 .../data/impl/builder/QueryBuilder.java         |  6 ++++
 .../data/impl/builder/part/QueryRoot.java       |  9 +++++-
 .../builder/result/QueryProcessorFactory.java   | 16 +++++++++-
 .../deltaspike/data/impl/meta/MethodPrefix.java | 14 +++++++++
 .../data/impl/meta/RepositoryMethod.java        |  2 +-
 .../data/impl/builder/part/QueryRootTest.java   | 32 ++++++++++++++++++++
 .../data/impl/handler/QueryHandlerTest.java     | 32 ++++++++++++++++++++
 .../data/test/service/SimpleRepository.java     |  5 ++-
 8 files changed, 112 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
index acdc984..02d7796 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
@@ -39,6 +39,7 @@ public abstract class QueryBuilder
 
     public static final String QUERY_SELECT = "select e from {0} e";
     public static final String QUERY_COUNT = "select count(e) from {0} e";
+    public static final String QUERY_DELETE = "delete from {0} e";
     public static final String ENTITY_NAME = "e";
 
     public static String selectQuery(String entityName)
@@ -46,6 +47,11 @@ public abstract class QueryBuilder
         return MessageFormat.format(QUERY_SELECT, entityName);
     }
 
+    public static String deleteQuery(String entityName)
+    {
+        return MessageFormat.format(QUERY_DELETE, entityName);
+    }
+
     public static String countQuery(String entityName)
     {
         return MessageFormat.format(QUERY_COUNT, entityName);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/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 1f734b2..1d484c8 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
@@ -103,7 +103,14 @@ public class QueryRoot extends QueryPart
     @Override
     protected QueryPart buildQuery(QueryBuilderContext ctx)
     {
-        ctx.append(QueryBuilder.selectQuery(entityName));
+        if (methodPrefix.isDelete())
+        {
+            ctx.append(QueryBuilder.deleteQuery(entityName));
+        }
+        else
+        {
+            ctx.append(QueryBuilder.selectQuery(entityName));
+        }
         if (hasChildren(excludedForWhereCheck()))
         {
             ctx.append(" where ");

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
index 022c22a..e623133 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/result/QueryProcessorFactory.java
@@ -28,15 +28,24 @@ import org.apache.deltaspike.data.api.Modifying;
 import org.apache.deltaspike.data.api.QueryResult;
 import org.apache.deltaspike.data.api.SingleResultType;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
+import org.apache.deltaspike.data.impl.meta.MethodPrefix;
 
 public final class QueryProcessorFactory
 {
 
     private final Method method;
+    private final MethodPrefix methodPrefix;
 
     private QueryProcessorFactory(Method method)
     {
         this.method = method;
+        this.methodPrefix = new MethodPrefix("", method.getName());
+    }
+
+    private QueryProcessorFactory(Method method, MethodPrefix methodPrefix)
+    {
+        this.method = method;
+        this.methodPrefix = methodPrefix;
     }
 
     public static QueryProcessorFactory newInstance(Method method)
@@ -44,6 +53,11 @@ public final class QueryProcessorFactory
         return new QueryProcessorFactory(method);
     }
 
+    public static QueryProcessorFactory newInstance(Method method, MethodPrefix methodPrefix)
+    {
+        return new QueryProcessorFactory(method, methodPrefix);
+    }
+
     public QueryProcessor build()
     {
         if (returns(QueryResult.class))
@@ -66,7 +80,7 @@ public final class QueryProcessorFactory
         boolean matchesType = Void.TYPE.equals(method.getReturnType()) ||
                 int.class.equals(method.getReturnType()) ||
                 Integer.class.equals(method.getReturnType());
-        return method.isAnnotationPresent(Modifying.class) && matchesType;
+        return (method.isAnnotationPresent(Modifying.class) && matchesType) || methodPrefix.isDelete();
     }
 
     private boolean returns(Class<?> clazz)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/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 81d8ea2..4bfb914 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
@@ -25,6 +25,7 @@ public class MethodPrefix
     public static final String DEFAULT_PREFIX = "findBy";
     public static final String DEFAULT_OPT_PREFIX = "findOptionalBy";
     public static final String DEFAULT_ANY_PREFIX = "findAnyBy";
+    public static final String DEFAULT_DELETE_PREFIX = "deleteBy";
 
     private final String customPrefix;
     private final String methodName;
@@ -83,6 +84,11 @@ public class MethodPrefix
         return SingleResultType.JPA;
     }
 
+    public boolean isDelete()
+    {
+        return this.getPrefix().equalsIgnoreCase(DEFAULT_DELETE_PREFIX);
+    }
+
     private static enum KnownQueryPrefix
     {
         DEFAULT(DEFAULT_PREFIX)
@@ -108,6 +114,14 @@ public class MethodPrefix
             {
                 return SingleResultType.ANY;
             }
+        },
+        DELETE_DEFAULT(DEFAULT_DELETE_PREFIX)
+        {
+            @Override
+            public SingleResultType getStyle()
+            {
+                return SingleResultType.ANY;
+            }
         };
 
         private final String prefix;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethod.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethod.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethod.java
index ff051fe..a98efb8 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethod.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/RepositoryMethod.java
@@ -73,7 +73,7 @@ public class RepositoryMethod
         this.methodPrefix = new MethodPrefix(repo.getCustomMethodPrefix(), method.getName());
         this.methodType = extractMethodType();
         this.queryRoot = initQueryRoot();
-        this.queryProcessor = QueryProcessorFactory.newInstance(method).build();
+        this.queryProcessor = QueryProcessorFactory.newInstance(method, methodPrefix).build();
         this.mapper = extractMapper(method, repo);
     }
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java
index 809954f..70edf67 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/builder/part/QueryRootTest.java
@@ -135,6 +135,38 @@ public class QueryRootTest
         assertEquals(expected, result);
     }
 
+    @Test
+    public void should_create_delete_query_by_name()
+    {
+        // given
+        final String name = "deleteByName";
+        final String expected =
+                "delete from Simple e " +
+                        "where e.name = ?1";
+
+        // when
+        String result = QueryRoot.create(name, repo, prefix(name)).getJpqlQuery().trim();
+
+        // then
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void should_create_delete_query_by_name_and_enabled()
+    {
+        // given
+        final String name = "deleteByNameAndEnabled";
+        final String expected =
+                "delete from Simple e " +
+                        "where e.name = ?1 and e.enabled = ?2";
+
+        // when
+        String result = QueryRoot.create(name, repo, prefix(name)).getJpqlQuery().trim();
+
+        // then
+        assertEquals(expected, result);
+    }
+
     private MethodPrefix prefix(final String name)
     {
         return new MethodPrefix("", name);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/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 b12e344..6cffe98 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
@@ -147,6 +147,38 @@ public class QueryHandlerTest extends TransactionalTestCase
     }
 
     @Test
+    public void should_create_query_delete_by_method_name()
+    {
+        // given
+        final String name = "testCreateQueryByMethodName";
+        builder.createSimple(name);
+
+        // when
+        repo.deleteByName(name);
+        repo.flush();
+        Simple result = repo.findAnyByName(name);
+
+        // then
+        assertNull(result);
+    }
+
+    @Test
+    public void should_create_query_delete_by_method_name_with_multiply_params()
+    {
+        // given
+        final String name = "testCreateQueryByMethodName";
+        builder.createSimple(name);
+
+        // when
+        repo.deleteByNameAndEnabled(name, Boolean.TRUE);
+        repo.flush();
+        Simple result = repo.findAnyByName(name);
+
+        // then
+        assertNull(result);
+    }
+
+    @Test
     public void should_restrict_result_size_by_annotation()
     {
         // given

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1238dbae/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 39b9311..2b1a8d5 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
@@ -105,7 +105,10 @@ public abstract class SimpleRepository extends AbstractEntityRepository<Simple,
     @Query(named = SimpleStringId.FIND_ALL_ORDER_BY_ID)
     public abstract QueryResult<SimpleStringId> findAllOrderByIdPaginate(@FirstResult
int start, @MaxResults int pageSize);
 
+    public abstract void deleteByName(String name);
+
+    public abstract void deleteByNameAndEnabled(String name, boolean enable);
+
     @Override
     protected abstract EntityManager entityManager();
-
 }


Mime
View raw message