Skip to content

Commit

Permalink
update libs conditions mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
tvd12 committed Nov 23, 2024
1 parent 146f7f0 commit 5617415
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ezydata-database/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezydata</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
</parent>
<artifactId>ezydata-database</artifactId>
<name>ezydata-database</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,10 +55,10 @@ private static EzyQueryConditionChain getConditionChain(EzyMethod method) {
return conditionChainBuilder.build();
}

String[] conditionGroupStrings = chain.split(OR);
List<String> conditionGroupStrings = splitConditions(chain, OR);

for (String conditionGroupString : conditionGroupStrings) {
String[] conditionStrings = conditionGroupString.split(AND);
List<String> conditionStrings = splitConditions(conditionGroupString, AND);
EzyQueryConditionGroup.Builder conditionGroupBuilder = EzyQueryConditionGroup.builder();

for (String conditionString : conditionStrings) {
Expand All @@ -67,6 +70,35 @@ private static EzyQueryConditionChain getConditionChain(EzyMethod method) {
return conditionChainBuilder.build();
}

public static List<String> splitConditions(
String chain,
String operator
) {
int fromIndex = 0;
String remaining = chain;
List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.List;

@Getter
public enum EzyQueryOperation {
EQUAL("=", "eq", ""),
IN("in", "in", "In"),
Expand All @@ -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<EzyQueryOperation> NOT_INCLUDE_EQUAL_VALUES =
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long, Person> {

@Override
default List<Person> findAll() {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<EzyQueryConditionGroup> 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
Expand Down Expand Up @@ -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));
Expand All @@ -138,6 +295,13 @@ private interface Repo extends EzyDatabaseRepository<Long, Data> {

int deleteByNameAndValue(String name, String value);

int deleteByNameAndValueOrEmailAndPhoneIn(
String name,
String value,
String email,
String phone
);

@Override
List<Data> findAll();

Expand Down
4 changes: 2 additions & 2 deletions ezydata-elasticsearch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezydata</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
</parent>
<artifactId>ezydata-elasticsearch</artifactId>
<name>ezydata-elasticsearch</name>

<properties>
<elasticsearch.version>7.17.9</elasticsearch.version>
<elasticsearch.version>7.17.25</elasticsearch.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion ezydata-hazelcast-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezydata</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
</parent>
<artifactId>ezydata-hazelcast-mongodb</artifactId>
<name>ezydata-hazelcast-mongodb</name>
Expand Down
4 changes: 2 additions & 2 deletions ezydata-hazelcast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezydata</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
</parent>
<artifactId>ezydata-hazelcast</artifactId>
<name>ezydata-hazelcast</name>
<url>http://maven.apache.org</url>
<properties>
<hazelcast.version>5.2.2</hazelcast.version>
<hazelcast.version>5.2.5</hazelcast.version>
<hazelcast.client.version>3.12.13</hazelcast.client.version>
</properties>

Expand Down
2 changes: 1 addition & 1 deletion ezydata-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezydata</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
</parent>
<artifactId>ezydata-jpa</artifactId>
<name>ezydata-jpa</name>
Expand Down
2 changes: 1 addition & 1 deletion ezydata-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezydata</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
</parent>
<artifactId>ezydata-mongodb</artifactId>
<name>ezydata-mongodb</name>
Expand Down
Loading

0 comments on commit 5617415

Please sign in to comment.