#422 - ENH: Add jsonBetween() expression

This commit is contained in:
Robin Bygrave
2015-09-22 12:32:46 +12:00
parent d022e4aec8
commit 11301846be
7 changed files with 70 additions and 1 deletions
@@ -76,6 +76,11 @@ public interface ExpressionFactory {
*/
Expression jsonLessOrEqualTo(String propertyName, String path, Object val);
/**
* Between - for the given path in a JSON document.
*/
Expression jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue);
/**
* Equal To - property equal to the given value.
*/
@@ -447,6 +447,17 @@ public interface ExpressionList<T> extends Serializable {
*/
ExpressionList<T> jsonLessOrEqualTo(String propertyName, String path, Object value);
/**
* Between - for the given path in a JSON document.
*
* <pre>{@code
*
* where().jsonBetween("content", "orderDate", lowerDateTime, upperDateTime)
*
* }</pre>
*/
ExpressionList<T> jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue);
/**
* Add an Expression to the list.
* <p>
@@ -68,6 +68,10 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return new JsonPathExpression(propertyName, path, Op.LT_EQ, val);
}
public Expression jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue) {
return new JsonPathExpression(propertyName, path, lowerValue, upperValue);
}
/**
* Equal To - property equal to the given value.
*/
@@ -30,11 +30,31 @@ class JsonPathExpression extends AbstractExpression {
*/
protected final Object value;
/**
* For Between this is the upper bind value.
*/
protected final Object upperValue;
/**
* Construct for Operator (not BETWEEN though).
*/
JsonPathExpression(String propertyName, String path, Op operator, Object value) {
super(propertyName);
this.path = path;
this.operator = operator;
this.value = value;
this.upperValue = null;
}
/**
* Construct for BETWEEN expression.
*/
JsonPathExpression(String propertyName, String path, Object value, Object upperValue) {
super(propertyName);
this.path = path;
this.operator = Op.BETWEEN;
this.value = value;
this.upperValue = upperValue;
}
@Override
@@ -49,7 +69,9 @@ class JsonPathExpression extends AbstractExpression {
@Override
public int queryBindHash() {
return (value == null) ? 0 : value.hashCode();
int hc = (value == null) ? 0 : value.hashCode();
hc = (upperValue == null) ? hc : hc * 31 + upperValue.hashCode();
return hc;
}
@Override
@@ -63,7 +85,12 @@ class JsonPathExpression extends AbstractExpression {
public void addBindValues(SpiExpressionRequest request) {
if (value != null) {
// value is null for EXISTS/NOT EXISTS
request.addBindValue(value);
}
if (upperValue != null) {
// upperValue only for BETWEEN operator
request.addBindValue(upperValue);
}
}
}
@@ -377,6 +377,14 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
return exprList.jsonLessOrEqualTo(propertyName, path, val);
}
/**
* Between - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue){
return exprList.jsonBetween(propertyName, path, lowerValue, upperValue);
}
@Override
public ExpressionList<T> ge(String propertyName, Object value) {
return exprList.ge(propertyName, value);
@@ -15,6 +15,11 @@ public enum Op {
*/
NOT_EXISTS(" is null "),
/**
* Between (JSON).
*/
BETWEEN(" between ? and ? "),
/**
* Equal to
*/
@@ -435,6 +435,15 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return this;
}
/**
* Between - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue){
add(expr.jsonBetween(propertyName, path, lowerValue, upperValue));
return this;
}
@Override
public ExpressionList<T> eq(String propertyName, Object value) {
add(expr.eq(propertyName, value));