Skip to content

Commit

Permalink
添加查询条件的去重判断
Browse files Browse the repository at this point in the history
  • Loading branch information
yangziwen committed Mar 9, 2020
1 parent d51e2dd commit 75de6bf
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.yangziwen</groupId>
<artifactId>quick-dao</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
<packaging>pom</packaging>
<name>quick-dao</name>
<url>http://maven.apache.org</url>
Expand Down
2 changes: 1 addition & 1 deletion quick-dao-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.yangziwen</groupId>
<artifactId>quick-dao</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
</parent>
<artifactId>quick-dao-core</artifactId>
<name>quick-dao-core</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.yangziwen.quickdao.core;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import io.github.yangziwen.quickdao.core.util.StringWrapper;
Expand All @@ -23,24 +25,41 @@ public class Criterion {
@Getter
private Operator operator = Operator.eq;

@Getter
private boolean valid;

Criterion(String name, Criteria criteria) {
this(name, criteria, true);
}

Criterion(String name, Criteria criteria, boolean valid) {
this.name = StringUtils.replacePattern(name, "\\s+", "");
this.criteria = criteria;
if (valid) {
criteria.getCriterionList().add(this);
}
this.valid = valid;
}

Criteria op(Operator operator, Object value) {
this.operator = operator;
this.value = value;
if (valid && !isRedundant()) {
criteria.getCriterionList().add(this);
}
return this.criteria;
}

private boolean isRedundant() {
for (Criterion criterion : criteria.getCriterionList()) {
if (Objects.equals(criterion.getName(), getName())
&& Objects.equals(criterion.getJsonField(), getJsonField())
// be aware that the value comparation is based on the shallow equals
&& Objects.equals(criterion.getValue(), getValue())
&& Objects.equals(criterion.getOperator(), getOperator())) {
return true;
}
}
return false;
}

public Criterion jsonField(String jsonField) {
this.jsonField = StringUtils.replacePattern(jsonField, "\\s+", "");
return this;
Expand Down
2 changes: 1 addition & 1 deletion quick-dao-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.yangziwen</groupId>
<artifactId>quick-dao</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
</parent>
<artifactId>quick-dao-example</artifactId>
<name>quick-dao-example</name>
Expand Down
2 changes: 1 addition & 1 deletion quick-dao-mybatis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.yangziwen</groupId>
<artifactId>quick-dao</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
</parent>
<artifactId>quick-dao-mybatis</artifactId>
<name>quick-dao-mybatis</name>
Expand Down
2 changes: 1 addition & 1 deletion quick-dao-spring-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.yangziwen</groupId>
<artifactId>quick-dao</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
</parent>
<artifactId>quick-dao-spring-jdbc</artifactId>
<name>quick-dao-spring-jdbc</name>
Expand Down
2 changes: 1 addition & 1 deletion quick-dao-sql2o/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.yangziwen</groupId>
<artifactId>quick-dao</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
</parent>
<artifactId>quick-dao-sql2o</artifactId>
<name>quick-dao-sql2o</name>
Expand Down

0 comments on commit 75de6bf

Please sign in to comment.