Skip to content

Commit

Permalink
#5 added IN operator in criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
eaxdev committed Aug 29, 2020
1 parent 9760e9e commit b91f4f0
Show file tree
Hide file tree
Showing 22 changed files with 382 additions and 25 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ dependencies {
signing {
sign publishing.publications
}
/*spotbugsMain {
reports {
xml.enabled = false
html.enabled = true
}
}*/

publishing {
publications {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public And(List<Criteria> criteria) {
public GroupConditionalOperator getGroupConditionalOperator() {
return GroupConditionalOperator.AND;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.GROUP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value = SimpleCriteria.class),
@JsonSubTypes.Type(value = GroupCriteria.class)
@JsonSubTypes.Type(value = GroupCriteria.class),
@JsonSubTypes.Type(value = MultiValueCriteria.class)
})
public abstract class Criteria {
public interface Criteria {

public abstract boolean isGroup();
CriteriaType getCriteriaType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.eaxdev.jsonsql4j.model.criteria;

/**
* @author eaxdev
*/
public enum CriteriaType {

GROUP,

SIMPLE,

MULTI_VALUE

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public Eq(String fieldName, String value) {
public SimpleConditionalOperator getSimpleConditionalOperator() {
return SimpleConditionalOperator.EQUALS_TO;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.SIMPLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public Ge(String fieldName, String value) {
public SimpleConditionalOperator getSimpleConditionalOperator() {
return SimpleConditionalOperator.GREATER_THAN_EQUALS;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.SIMPLE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
@Getter
@AllArgsConstructor
@JsonDeserialize(using = GroupCriteriaDeserializer.class)
public abstract class GroupCriteria extends Criteria {
public abstract class GroupCriteria implements Criteria {

protected final List<Criteria> criteria;

public abstract GroupConditionalOperator getGroupConditionalOperator();

@Override
public boolean isGroup() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public Gt(String fieldName, String value) {
public SimpleConditionalOperator getSimpleConditionalOperator() {
return SimpleConditionalOperator.GREATER_THAN;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.SIMPLE;
}

}
30 changes: 30 additions & 0 deletions src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/In.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.eaxdev.jsonsql4j.model.criteria;

import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* @author eaxdev
*/
@Getter
@JsonTypeName("in")
@NoArgsConstructor
public class In extends MultiValueCriteria {

public In(String fieldName, List<String> values) {
super(fieldName, values);
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.MULTI_VALUE;
}

@Override
public MultiValueConditionalOperator getMultiValueConditionalOperator() {
return MultiValueConditionalOperator.IN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public Le(String fieldName, String value) {
public SimpleConditionalOperator getSimpleConditionalOperator() {
return SimpleConditionalOperator.LESS_THAN_EQUALS;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.SIMPLE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public Lt(String fieldName, String value) {
public SimpleConditionalOperator getSimpleConditionalOperator() {
return SimpleConditionalOperator.LESS_THAN;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.SIMPLE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.eaxdev.jsonsql4j.model.criteria;

import lombok.Getter;

/**
* @author eaxdev
*/
@Getter
public enum MultiValueConditionalOperator {

IN(" IN ");

private final String queryView;

MultiValueConditionalOperator(String queryView) {
this.queryView = queryView;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.eaxdev.jsonsql4j.model.criteria;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.github.eaxdev.jsonsql4j.exception.JsonSQL4JException;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author eaxdev
*/
@Getter
@NoArgsConstructor
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(value = In.class)
})
public abstract class MultiValueCriteria implements Criteria {

@JsonProperty(value = "fieldName", required = true)
private String fieldName;

@JsonProperty(value = "values", required = true)
private List<String> values;

public MultiValueCriteria(String fieldName, List<String> values) {
this.fieldName = fieldName;
this.values = Collections.unmodifiableList(values);
}

public List<String> getValues() {
return Collections.unmodifiableList(values);
}

public abstract MultiValueConditionalOperator getMultiValueConditionalOperator();

public String getValuesView() {
validateValues();

return values.stream().map(value -> {
if (Util.isNumeric(value)) {
return value;
}
return "'" + value + "'";
}).collect(Collectors.joining(", "));

}

private void validateValues() {
if (values.isEmpty()) {
throw new JsonSQL4JException("IN operator must contains one or more elements");
}
String firstValue = values.get(0);
boolean isNumericFirst = Util.isNumeric(firstValue);
boolean allNumericOrNot = values.stream().anyMatch(v -> Util.isNumeric(v) != isNumericFirst);

if (allNumericOrNot) {
throw new JsonSQL4JException("Values in IN operator must be same type");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public Ne(String fieldName, String value) {
public SimpleConditionalOperator getSimpleConditionalOperator() {
return SimpleConditionalOperator.NOT_EQUALS_TO;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.SIMPLE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public GroupConditionalOperator getGroupConditionalOperator() {
return GroupConditionalOperator.OR;
}

@Override
public CriteriaType getCriteriaType() {
return CriteriaType.GROUP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
@JsonSubTypes.Type(value = Le.class),
@JsonSubTypes.Type(value = Lt.class),
@JsonSubTypes.Type(value = Ne.class),
@JsonSubTypes.Type(value = Or.class),})
public abstract class SimpleCriteria extends Criteria {
@JsonSubTypes.Type(value = Or.class)})
public abstract class SimpleCriteria implements Criteria {

@JsonProperty(value = "fieldName", required = true)
private String fieldName;
Expand All @@ -32,8 +32,4 @@ public abstract class SimpleCriteria extends Criteria {

public abstract SimpleConditionalOperator getSimpleConditionalOperator();

@Override
public boolean isGroup() {
return false;
}
}
23 changes: 23 additions & 0 deletions src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.eaxdev.jsonsql4j.model.criteria;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.regex.Pattern;

/**
* @author eaxdev
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
class Util {

private static final Pattern NUMERIC_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?");

public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
return NUMERIC_PATTERN.matcher(strNum).matches();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,33 @@ public String build() {
if (Objects.isNull(criteria)) {
return "";
}
return " WHERE " + (criteria.isGroup()
? constructGroupCriteria((GroupCriteria) criteria)
: constructExpression((SimpleCriteria) criteria));
return " WHERE " + constructExpression(criteria);
}

private String constructExpression(SimpleCriteria criteria) {
private String constructExpression(Criteria criteria) {
switch (criteria.getCriteriaType()) {
case GROUP:
return constructByGroupCriteria((GroupCriteria) criteria);
case SIMPLE:
return constructBySimpleCriteria((SimpleCriteria) criteria);
case MULTI_VALUE:
return constructByMultiValueCriteria((MultiValueCriteria) criteria);
default:
throw new JsonSQL4JException("Conditional operator [" + criteria.getCriteriaType() + "] is unexpected");
}
}

private String constructByMultiValueCriteria(MultiValueCriteria criteria) {
switch (criteria.getMultiValueConditionalOperator()) {
case IN:
return criteria.getFieldName() + MultiValueConditionalOperator.IN.getQueryView()
+ "(" + criteria.getValuesView() + ")";
default:
throw new JsonSQL4JException("Conditional operator [" + criteria.getMultiValueConditionalOperator() + "] is unexpected");
}
}

private String constructBySimpleCriteria(SimpleCriteria criteria) {
switch (criteria.getSimpleConditionalOperator()) {
case EQUALS_TO:
return criteria.getFieldName() + SimpleConditionalOperator.EQUALS_TO.getQueryView() + criteria.getValue();
Expand All @@ -47,14 +68,14 @@ private String constructExpression(SimpleCriteria criteria) {
}
}

private String constructGroupCriteria(GroupCriteria criteria) {
private String constructByGroupCriteria(GroupCriteria criteria) {
if (criteria.getGroupConditionalOperator() == GroupConditionalOperator.AND) {
String leftExpression = constructExpression((SimpleCriteria) criteria.getCriteria().get(0));
String rightExpression = constructExpression((SimpleCriteria) criteria.getCriteria().get(1));
String leftExpression = constructExpression(criteria.getCriteria().get(0));
String rightExpression = constructExpression(criteria.getCriteria().get(1));
return "(" + leftExpression + GroupConditionalOperator.AND.getQueryView() + rightExpression + ")";
} else {
String leftExpression = constructExpression((SimpleCriteria) criteria.getCriteria().get(0));
String rightExpression = constructExpression((SimpleCriteria) criteria.getCriteria().get(1));
String leftExpression = constructExpression(criteria.getCriteria().get(0));
String rightExpression = constructExpression(criteria.getCriteria().get(1));
return "(" + leftExpression + GroupConditionalOperator.OR.getQueryView() + rightExpression + ")";
}
}
Expand Down
Loading

0 comments on commit b91f4f0

Please sign in to comment.