From 4d44efec43a9ad028c8976cb8110b04e82942a5c Mon Sep 17 00:00:00 2001 From: Ta Van Dung Date: Sat, 23 Nov 2024 08:54:27 +0700 Subject: [PATCH] update libs conditions mongodb (#30) --- ezydata-database/pom.xml | 2 +- .../database/query/EzyQueryMethod.java | 36 +++- .../database/query/EzyQueryOperation.java | 4 +- .../EzyAbstractRepositoryImplementerTest.java | 59 +++++++ .../test/query/EzyQueryMethodTest.java | 164 ++++++++++++++++++ ezydata-elasticsearch/pom.xml | 4 +- ezydata-hazelcast-mongodb/pom.xml | 2 +- ezydata-hazelcast/pom.xml | 4 +- ezydata-jpa/pom.xml | 2 +- ezydata-mongodb/pom.xml | 2 +- .../repository/EzySimpleMongoRepository.java | 3 + .../testing/EzyMongoDatabaseContextTest.java | 2 + ezydata-redis/pom.xml | 2 +- pom.xml | 8 +- 14 files changed, 276 insertions(+), 18 deletions(-) create mode 100644 ezydata-database/src/test/java/com/tvd12/ezydata/database/test/bean/EzyAbstractRepositoryImplementerTest.java diff --git a/ezydata-database/pom.xml b/ezydata-database/pom.xml index 8d36a24..5376c8e 100644 --- a/ezydata-database/pom.xml +++ b/ezydata-database/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-database ezydata-database diff --git a/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryMethod.java b/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryMethod.java index 6c4908e..c93b078 100644 --- a/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryMethod.java +++ b/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryMethod.java @@ -5,6 +5,9 @@ import com.tvd12.ezyfox.util.Next; import lombok.Getter; +import java.util.ArrayList; +import java.util.List; + import static com.tvd12.ezydata.database.EzyDatabaseRepository.*; @Getter @@ -52,10 +55,10 @@ private static EzyQueryConditionChain getConditionChain(EzyMethod method) { return conditionChainBuilder.build(); } - String[] conditionGroupStrings = chain.split(OR); + List conditionGroupStrings = splitConditions(chain, OR); for (String conditionGroupString : conditionGroupStrings) { - String[] conditionStrings = conditionGroupString.split(AND); + List conditionStrings = splitConditions(conditionGroupString, AND); EzyQueryConditionGroup.Builder conditionGroupBuilder = EzyQueryConditionGroup.builder(); for (String conditionString : conditionStrings) { @@ -67,6 +70,35 @@ private static EzyQueryConditionChain getConditionChain(EzyMethod method) { return conditionChainBuilder.build(); } + public static List splitConditions( + String chain, + String operator + ) { + int fromIndex = 0; + String remaining = chain; + List conditions = new ArrayList<>(); + while (true) { + int index = remaining.indexOf(operator, fromIndex); + if (index <= 0) { + break; + } + int afterIndex = index + operator.length(); + if (afterIndex >= remaining.length()) { + break; + } + char afterOperatorChar = remaining.charAt(afterIndex); + if (Character.isUpperCase(afterOperatorChar)) { + conditions.add(remaining.substring(0, index)); + fromIndex = 0; + remaining = remaining.substring(afterIndex); + } else { + fromIndex = afterIndex; + } + } + conditions.add(remaining); + return conditions; + } + public static int getQueryParameterCount(EzyMethod method) { if (isPaginationMethod(method)) { return method.getParameterCount() - 1; diff --git a/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryOperation.java b/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryOperation.java index da513da..f5be9d6 100644 --- a/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryOperation.java +++ b/ezydata-database/src/main/java/com/tvd12/ezydata/database/query/EzyQueryOperation.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.List; +@Getter public enum EzyQueryOperation { EQUAL("=", "eq", ""), IN("in", "in", "In"), @@ -14,11 +15,8 @@ public enum EzyQueryOperation { LT("<", "lt", "Lt"), LTE("<=", "lte", "Lte"); - @Getter private final String sign; - @Getter private final String signName; - @Getter private final String tag; private static final List NOT_INCLUDE_EQUAL_VALUES = diff --git a/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/bean/EzyAbstractRepositoryImplementerTest.java b/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/bean/EzyAbstractRepositoryImplementerTest.java new file mode 100644 index 0000000..6631c0e --- /dev/null +++ b/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/bean/EzyAbstractRepositoryImplementerTest.java @@ -0,0 +1,59 @@ +package com.tvd12.ezydata.database.test.bean; + +import com.tvd12.ezydata.database.EzyDatabaseRepository; +import com.tvd12.ezydata.database.bean.EzyAbstractRepositoryImplementer; +import com.tvd12.ezyfox.reflect.EzyMethod; +import com.tvd12.test.assertion.Asserts; +import com.tvd12.test.reflect.MethodInvoker; +import org.testng.annotations.Test; + +import java.util.Collections; +import java.util.List; + +public class EzyAbstractRepositoryImplementerTest { + + @Test + public void isAutoImplementMethodTest() throws Exception { + // given + ExEzyRepositoryImplementer instance = new ExEzyRepositoryImplementer( + ExRepo.class + ); + + // when + boolean answer = MethodInvoker.create() + .object(instance) + .method("isAutoImplementMethod") + .param( + EzyMethod.class, + new EzyMethod( + ExRepo.class.getDeclaredMethod("findAll") + ) + ) + .invoke(Boolean.class); + + // then + Asserts.assertFalse(answer); + } + + public static class ExEzyRepositoryImplementer + extends EzyAbstractRepositoryImplementer { + + public ExEzyRepositoryImplementer(Class clazz) { + super(clazz); + } + + @Override + protected Class getSuperClass() { + return EzyDatabaseRepository.class; + } + } + + public interface ExRepo + extends EzyDatabaseRepository { + + @Override + default List findAll() { + return Collections.emptyList(); + } + } +} diff --git a/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/query/EzyQueryMethodTest.java b/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/query/EzyQueryMethodTest.java index c57150e..70d19a0 100644 --- a/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/query/EzyQueryMethodTest.java +++ b/ezydata-database/src/test/java/com/tvd12/ezydata/database/test/query/EzyQueryMethodTest.java @@ -7,6 +7,8 @@ import com.tvd12.test.assertion.Asserts; import org.testng.annotations.Test; +import java.util.Arrays; +import java.util.Collections; import java.util.List; public class EzyQueryMethodTest { @@ -89,6 +91,45 @@ public void methodDelete() { Asserts.assertEquals(conditionGroup.getConditions().get(1).getField(), "value"); } + @Test + public void methodDeleteAndOr() { + // given + EzyMethod method = getMethod( + "deleteByNameAndValueOrEmailAndPhoneIn", + String.class, + String.class, + String.class, + String.class + ); + + // when + EzyQueryMethod queryMethod = new EzyQueryMethod(method); + + // then + Asserts.assertEquals(queryMethod.getMethod(), method); + Asserts.assertEquals(queryMethod.getType(), EzyQueryMethodType.DELETE); + + EzyQueryConditionChain conditionChain = queryMethod.getConditionChain(); + Asserts.assertEquals(conditionChain.getParameterCount(), 4); + + List conditionGroups = conditionChain.getConditionGroups(); + Asserts.assertEquals(conditionGroups.size(), 2); + + EzyQueryConditionGroup conditionGroup1 = conditionGroups.get(0); + Asserts.assertEquals(conditionGroup1.size(), 2); + Asserts.assertEquals(conditionGroup1.getConditions().get(0).getOperation(), EzyQueryOperation.EQUAL); + Asserts.assertEquals(conditionGroup1.getConditions().get(0).getField(), "name"); + Asserts.assertEquals(conditionGroup1.getConditions().get(1).getOperation(), EzyQueryOperation.EQUAL); + Asserts.assertEquals(conditionGroup1.getConditions().get(1).getField(), "value"); + + EzyQueryConditionGroup conditionGroup2 = conditionGroups.get(1); + Asserts.assertEquals(conditionGroup2.size(), 2); + Asserts.assertEquals(conditionGroup2.getConditions().get(0).getOperation(), EzyQueryOperation.EQUAL); + Asserts.assertEquals(conditionGroup2.getConditions().get(0).getField(), "email"); + Asserts.assertEquals(conditionGroup2.getConditions().get(1).getOperation(), EzyQueryOperation.IN); + Asserts.assertEquals(conditionGroup2.getConditions().get(1).getField(), "phone"); + } + @Test public void emptyParamsMethod() { // given @@ -120,6 +161,122 @@ public void notEnoughParameters() { Asserts.assertEqualsType(e, IllegalArgumentException.class); } + @Test + public void splitConditionsTest() { + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "", + "And" + ), + Collections.singletonList(""), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "And", + "And" + ), + Collections.singletonList("And"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "Anda", + "And" + ), + Collections.singletonList("Anda"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "", + "Or" + ), + Collections.singletonList(""), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "Or", + "Or" + ), + Collections.singletonList("Or"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "Ora", + "Or" + ), + Collections.singletonList("Ora"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "Name", + "And" + ), + Collections.singletonList("Name"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "NameAndAnd", + "And" + ), + Arrays.asList("Name", "And"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "NameOrOr", + "Or" + ), + Arrays.asList("Name", "Or"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "NameAndId", + "And" + ), + Arrays.asList("Name", "Id"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "NameAndIdOrEmail", + "And" + ), + Arrays.asList("Name", "IdOrEmail"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "IdOrEmail", + "Or" + ), + Arrays.asList("Id", "Email"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "DisplayOrderInOrName", + "Or" + ), + Arrays.asList("DisplayOrderIn", "Name"), + false + ); + Asserts.assertEquals( + EzyQueryMethod.splitConditions( + "DataTypeAndDataIdAndMetaKeyAndMetaValue", + "And" + ), + Arrays.asList("DataType", "DataId", "MetaKey", "MetaValue"), + false + ); + } + private EzyMethod getMethod(String name, Class... parameters) { try { return new EzyMethod(Repo.class.getDeclaredMethod(name, parameters)); @@ -138,6 +295,13 @@ private interface Repo extends EzyDatabaseRepository { int deleteByNameAndValue(String name, String value); + int deleteByNameAndValueOrEmailAndPhoneIn( + String name, + String value, + String email, + String phone + ); + @Override List findAll(); diff --git a/ezydata-elasticsearch/pom.xml b/ezydata-elasticsearch/pom.xml index b065760..eccb7db 100644 --- a/ezydata-elasticsearch/pom.xml +++ b/ezydata-elasticsearch/pom.xml @@ -4,13 +4,13 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-elasticsearch ezydata-elasticsearch - 7.17.9 + 7.17.25 diff --git a/ezydata-hazelcast-mongodb/pom.xml b/ezydata-hazelcast-mongodb/pom.xml index 2881c05..ffcb84f 100644 --- a/ezydata-hazelcast-mongodb/pom.xml +++ b/ezydata-hazelcast-mongodb/pom.xml @@ -6,7 +6,7 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-hazelcast-mongodb ezydata-hazelcast-mongodb diff --git a/ezydata-hazelcast/pom.xml b/ezydata-hazelcast/pom.xml index e280d27..ad6d4a6 100644 --- a/ezydata-hazelcast/pom.xml +++ b/ezydata-hazelcast/pom.xml @@ -6,13 +6,13 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-hazelcast ezydata-hazelcast http://maven.apache.org - 5.2.2 + 5.2.5 3.12.13 diff --git a/ezydata-jpa/pom.xml b/ezydata-jpa/pom.xml index 9749683..99cc242 100644 --- a/ezydata-jpa/pom.xml +++ b/ezydata-jpa/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-jpa ezydata-jpa diff --git a/ezydata-mongodb/pom.xml b/ezydata-mongodb/pom.xml index 84a99be..729578a 100644 --- a/ezydata-mongodb/pom.xml +++ b/ezydata-mongodb/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-mongodb ezydata-mongodb diff --git a/ezydata-mongodb/src/main/java/com/tvd12/ezydata/mongodb/repository/EzySimpleMongoRepository.java b/ezydata-mongodb/src/main/java/com/tvd12/ezydata/mongodb/repository/EzySimpleMongoRepository.java index 10426bb..1463059 100644 --- a/ezydata-mongodb/src/main/java/com/tvd12/ezydata/mongodb/repository/EzySimpleMongoRepository.java +++ b/ezydata-mongodb/src/main/java/com/tvd12/ezydata/mongodb/repository/EzySimpleMongoRepository.java @@ -90,6 +90,9 @@ public void save(E entity) { @Override public void save(Iterable entities) { List entityList = iterableToList(entities); + if (entityList.isEmpty()) { + return; + } List> request = new ArrayList<>(); for (E entity : entityList) { BsonDocument document = entityToBsonDocument(entity); diff --git a/ezydata-mongodb/src/test/java/com/tvd12/ezydata/mongodb/testing/EzyMongoDatabaseContextTest.java b/ezydata-mongodb/src/test/java/com/tvd12/ezydata/mongodb/testing/EzyMongoDatabaseContextTest.java index 553438e..f519220 100644 --- a/ezydata-mongodb/src/test/java/com/tvd12/ezydata/mongodb/testing/EzyMongoDatabaseContextTest.java +++ b/ezydata-mongodb/src/test/java/com/tvd12/ezydata/mongodb/testing/EzyMongoDatabaseContextTest.java @@ -12,6 +12,7 @@ import org.testng.annotations.Test; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -39,6 +40,7 @@ public void test() { Category category2 = new Category("category2"); assert category1.getName() == null; assert category2.getName() == null; + categoryRepo.save(Collections.emptyList()); categoryRepo.save(Arrays.asList(category1, category2)); assert category1.getName() != null; assert category2.getName() != null; diff --git a/ezydata-redis/pom.xml b/ezydata-redis/pom.xml index 8aa91a7..71c81a4 100644 --- a/ezydata-redis/pom.xml +++ b/ezydata-redis/pom.xml @@ -5,7 +5,7 @@ com.tvd12 ezydata - 1.2.8 + 1.2.9 ezydata-redis ezydata-redis diff --git a/pom.xml b/pom.xml index c0a60a4..99dab92 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 1.0.6 ezydata - 1.2.8 + 1.2.9 pom ezydata @@ -65,11 +65,11 @@ 2.2 1.3 - 4.3.1 + 4.4.8 5.6.15.Final 4.0.3 - 3.12.12 - 8.0.32 + 3.12.14 + 8.0.33