#417 - ENH: Add JSON expressions ... path exists and value at path equals value

This commit is contained in:
Robin Bygrave committed 2015-09-21 16:20:14 +12:00
1 parent 054f7dc86f
commit 5726410f6f
18 files changed
+584 -33

No files matched your search

@@ -211,7 +211,7 @@ public class DefaultExampleExpression implements SpiExpression, ExampleExpressio
// exclude the zero values typically to weed out
// primitive int and long that initialise to 0
if (includeZeros || !isZero(value)) {
list.add(new SimpleExpression(propName, SimpleExpression.Op.EQ, value));
list.add(new SimpleExpression(propName, Op.EQ, value));
}
}
}
@@ -1,9 +1,5 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.avaje.ebean.ExampleExpression;
import com.avaje.ebean.Expression;
import com.avaje.ebean.ExpressionFactory;
@@ -15,6 +11,10 @@ import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.api.SpiExpressionFactory;
import com.avaje.ebeaninternal.api.SpiQuery;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Default Expression factory for creating standard expressions.
*/
@@ -36,6 +36,38 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return "sql";
}
public Expression jsonExists(String propertyName, String path) {
return new JsonPathExpression(propertyName, path, Op.EXISTS, null);
}
public Expression jsonNotExists(String propertyName, String path) {
return new JsonPathExpression(propertyName, path, Op.NOT_EXISTS, null);
}
public Expression jsonEqualTo(String propertyName, String path, Object val) {
return new JsonPathExpression(propertyName, path, Op.EQ, val);
}
public Expression jsonNotEqualTo(String propertyName, String path, Object val) {
return new JsonPathExpression(propertyName, path, Op.NOT_EQ, val);
}
public Expression jsonGreaterThan(String propertyName, String path, Object val) {
return new JsonPathExpression(propertyName, path, Op.GT, val);
}
public Expression jsonGreaterOrEqual(String propertyName, String path, Object val) {
return new JsonPathExpression(propertyName, path, Op.GT_EQ, val);
}
public Expression jsonLessThan(String propertyName, String path, Object val) {
return new JsonPathExpression(propertyName, path, Op.LT, val);
}
public Expression jsonLessOrEqualTo(String propertyName, String path, Object val) {
return new JsonPathExpression(propertyName, path, Op.LT_EQ, val);
}
/**
* Equal To - property equal to the given value.
*/
@@ -43,7 +75,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
if (value == null) {
return equalsWithNullAsNoop ? NoopExpression.INSTANCE : isNull(propertyName);
}
return new SimpleExpression(propertyName, SimpleExpression.Op.EQ, value);
return new SimpleExpression(propertyName, Op.EQ, value);
}
/**
@@ -53,7 +85,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
if (value == null) {
return equalsWithNullAsNoop ? NoopExpression.INSTANCE : isNotNull(propertyName);
}
return new SimpleExpression(propertyName, SimpleExpression.Op.NOT_EQ, value);
return new SimpleExpression(propertyName, Op.NOT_EQ, value);
}
/**
@@ -88,7 +120,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
*/
public Expression gt(String propertyName, Object value) {
return new SimpleExpression(propertyName, SimpleExpression.Op.GT, value);
return new SimpleExpression(propertyName, Op.GT, value);
}
/**
@@ -97,7 +129,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
*/
public Expression ge(String propertyName, Object value) {
return new SimpleExpression(propertyName, SimpleExpression.Op.GT_EQ, value);
return new SimpleExpression(propertyName, Op.GT_EQ, value);
}
/**
@@ -105,7 +137,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
*/
public Expression lt(String propertyName, Object value) {
return new SimpleExpression(propertyName, SimpleExpression.Op.LT, value);
return new SimpleExpression(propertyName, Op.LT, value);
}
/**
@@ -113,7 +145,7 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
*/
public Expression le(String propertyName, Object value) {
return new SimpleExpression(propertyName, SimpleExpression.Op.LT_EQ, value);
return new SimpleExpression(propertyName, Op.LT_EQ, value);
}
/**
@@ -0,0 +1,69 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
/**
* Generally speaking tests the value at a given path in the JSON document.
* <p>
* Supports the usual operators (equal to, greater than etc).
* </p>
* <p>
* The value passed in is expected to be a valid JSON type so string, number, boolean.
* </p>
*/
class JsonPathExpression extends AbstractExpression {
/**
* The path in the JSON document in dot notation form.
*/
protected final String path;
/**
* The expression operator.
*/
protected final Op operator;
/**
* The bind value used to compare against the document path value.
*/
protected final Object value;
JsonPathExpression(String propertyName, String path, Op operator, Object value) {
super(propertyName);
this.path = path;
this.operator = operator;
this.value = value;
}
@Override
public void queryAutoTuneHash(HashQueryPlanBuilder builder) {
builder.add(JsonPathExpression.class).add(propName).add(path).add(operator);
}
@Override
public void queryPlanHash(BeanQueryRequest<?> request, HashQueryPlanBuilder builder) {
queryAutoTuneHash(builder);
}
@Override
public int queryBindHash() {
return (value == null) ? 0 : value.hashCode();
}
@Override
public void addSql(SpiExpressionRequest request) {
// Use DB specific expression handling (Postgres and Oracle supported)
request.getJsonHander().addSql(request, propName, path, operator, value);
}
@Override
public void addBindValues(SpiExpressionRequest request) {
if (value != null) {
request.addBindValue(value);
}
}
}
@@ -313,6 +313,70 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
return exprList.findUnique();
}
/**
* Path exists - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonExists(String propertyName, String path) {
return exprList.jsonExists(propertyName, path);
}
/**
* Path does not exist - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonNotExists(String propertyName, String path){
return exprList.jsonNotExists(propertyName, path);
}
/**
* Equal to - for the value at the given path in the JSON document.
*/
@Override
public ExpressionList<T> jsonEqualTo(String propertyName, String path, Object value){
return exprList.jsonEqualTo(propertyName, path, value);
}
/**
* Not Equal to - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonNotEqualTo(String propertyName, String path, Object val){
return exprList.jsonNotEqualTo(propertyName, path, val);
}
/**
* Greater than - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonGreaterThan(String propertyName, String path, Object val){
return exprList.jsonGreaterThan(propertyName, path, val);
}
/**
* Greater than or equal to - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonGreaterOrEqual(String propertyName, String path, Object val){
return exprList.jsonGreaterOrEqual(propertyName, path, val);
}
/**
* Less than - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonLessThan(String propertyName, String path, Object val){
return exprList.jsonLessThan(propertyName, path, val);
}
/**
* Less than or equal to - for the given path in a JSON document.
*/
@Override
public ExpressionList<T> jsonLessOrEqualTo(String propertyName, String path, Object val){
return exprList.jsonLessOrEqualTo(propertyName, path, val);
}
@Override
public ExpressionList<T> ge(String propertyName, Object value) {
return exprList.ge(propertyName, value);
@@ -0,0 +1,62 @@
package com.avaje.ebeaninternal.server.expression;
/**
* Simple operators - equals, greater than, less than etc.
*/
public enum Op {
/**
* Exists (JSON).
*/
EXISTS(" is not null "),
/**
* Not Exists (JSON).
*/
NOT_EXISTS(" is null "),
/**
* Equal to
*/
EQ(" = ? "),
/**
* Not equal to.
*/
NOT_EQ(" <> ? "),
/**
*
* Less than.
*/
LT(" < ? "),
/**
* Less than or equal to.
*/
LT_EQ(" <= ? "),
/**
* Greater than.
*/
GT(" > ? "),
/**
* Greater than or equal to.
*/
GT_EQ(" >= ? ");
final String exp;
Op(String exp) {
this.exp = exp;
}
/**
* Return the bind expression include JDBC ? bind placeholder.
*/
public String bind() {
return exp;
}
}
@@ -10,20 +10,6 @@ public class SimpleExpression extends AbstractExpression {
private static final long serialVersionUID = -382881395755603790L;
enum Op {
EQ(" = ? "), NOT_EQ(" <> ? "), LT(" < ? "), LT_EQ(" <= ? "), GT(" > ? "), GT_EQ(" >= ? ");
final String exp;
Op(String exp) {
this.exp = exp;
}
public String bind() {
return exp;
}
}
private final Op type;
private final Object value;