-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
382 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/CriteriaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/In.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/MultiValueConditionalOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/MultiValueCriteria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/io/github/eaxdev/jsonsql4j/model/criteria/Util.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.