#1634 - ENH: Add convenience expressions "greater than or null" gtOrNull(), ltOrNull() and inRangeWith()

This commit is contained in:
rob bygrave
2019-02-09 00:30:55 +13:00
parent 25b6d2a5a0
commit 829dfcb013
7 changed files with 170 additions and 6 deletions
-3
View File
@@ -71,7 +71,6 @@ public class Expr {
* </p>
*/
public static Expression inRange(String propertyName, Object value1, Object value2) {
return Ebean.getExpressionFactory().inRange(propertyName, value1, value2);
}
@@ -79,7 +78,6 @@ public class Expr {
* Between - property between the two given values.
*/
public static Expression between(String propertyName, Object value1, Object value2) {
return Ebean.getExpressionFactory().between(propertyName, value1, value2);
}
@@ -87,7 +85,6 @@ public class Expr {
* Between - value between two given properties.
*/
public static Expression between(String lowProperty, String highProperty, Object value) {
return Ebean.getExpressionFactory().betweenProperties(lowProperty, highProperty, value);
}
@@ -157,6 +157,25 @@ public interface ExpressionFactory {
*/
Expression inRange(String propertyName, Object value1, Object value2);
/**
* Value in Range between 2 properties.
*
* <pre>{@code
*
* .startDate.inRangeWith(endDate, now)
*
* // which equates to
* startDate <= now and (endDate > now or endDate is null)
*
* }</pre>
*
* <p>
* This is a convenience expression combining a number of simple expressions.
* The most common use of this could be called "effective dating" where 2 date or
* timestamp columns represent the date range in which
*/
Expression inRangeWith(String lowProperty, String highProperty, Object value);
/**
* Between - property between the two given values.
*/
@@ -167,6 +186,14 @@ public interface ExpressionFactory {
*/
Expression betweenProperties(String lowProperty, String highProperty, Object value);
/**
* Greater Than Or Null - property greater than the given value or null.
* <p>
* A convenient expression combining GT and Is Null. Most often useful for range
* expressions where the top range value is nullable.
*/
Expression gtOrNull(String propertyName, Object value);
/**
* Greater Than - property greater than the given value.
*/
@@ -178,6 +205,14 @@ public interface ExpressionFactory {
*/
Expression ge(String propertyName, Object value);
/**
* Less Than or Null - property less than the given value or null.
* <p>
* A convenient expression combining LT and Is Null. Most often useful for range
* expressions where the bottom range value is nullable.
*/
Expression ltOrNull(String propertyName, Object value);
/**
* Less Than - property less than the given value.
*/
@@ -802,6 +802,25 @@ public interface ExpressionList<T> {
*/
ExpressionList<T> ine(String propertyName, String value);
/**
* Value in Range between 2 properties.
*
* <pre>{@code
*
* .startDate.inRangeWith(endDate, now)
*
* // which equates to
* startDate <= now and (endDate > now or endDate is null)
*
* }</pre>
*
* <p>
* This is a convenience expression combining a number of simple expressions.
* The most common use of this could be called "effective dating" where 2 date or
* timestamp columns represent the date range in which
*/
ExpressionList<T> inRangeWith(String lowProperty, String highProperty, Object value);
/**
* In Range - property >= value1 and property < value2.
* <p>
@@ -825,6 +844,11 @@ public interface ExpressionList<T> {
*/
ExpressionList<T> gt(String propertyName, Object value);
/**
* Greater Than or Null - property greater than the given value or null.
*/
ExpressionList<T> gtOrNull(String propertyName, Object value);
/**
* Greater Than or Equal to - property greater than or equal to the given
* value.
@@ -836,6 +860,11 @@ public interface ExpressionList<T> {
*/
ExpressionList<T> lt(String propertyName, Object value);
/**
* Less Than or Null - property less than the given value or null.
*/
ExpressionList<T> ltOrNull(String propertyName, Object value);
/**
* Less Than or Equal to - property less than or equal to the given value.
*/
@@ -221,6 +221,11 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return new InRangeExpression(propertyName, value1, value2);
}
@Override
public Expression inRangeWith(String lowProperty, String highProperty, Object value) {
return and(le(lowProperty, value), gtOrNull(highProperty, value));
}
/**
* Between - property between the two given values.
*/
@@ -245,6 +250,14 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return new SimpleExpression(propertyName, Op.GT, value);
}
/**
* Greater Than or null - property greater than the given value or null.
*/
@Override
public Expression gtOrNull(String propertyName, Object value) {
return or(gt(propertyName, value), isNull(propertyName));
}
/**
* Greater Than or Equal to - property greater than or equal to the given
* value.
@@ -254,6 +267,14 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return new SimpleExpression(propertyName, Op.GT_EQ, value);
}
/**
* Less Than or null - property less than the given value or null.
*/
@Override
public Expression ltOrNull(String propertyName, Object value) {
return or(lt(propertyName, value), isNull(propertyName));
}
/**
* Less Than - property less than the given value.
*/
@@ -794,6 +794,12 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return this;
}
@Override
public ExpressionList<T> inRangeWith(String lowProperty, String highProperty, Object value) {
add(expr.inRangeWith(lowProperty, highProperty, value));
return this;
}
@Override
public ExpressionList<T> inRange(String propertyName, Object value1, Object value2) {
add(expr.inRange(propertyName, value1, value2));
@@ -836,6 +842,12 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return this;
}
@Override
public ExpressionList<T> gtOrNull(String propertyName, Object value) {
add(expr.gtOrNull(propertyName, value));
return this;
}
@Override
public ExpressionList<T> icontains(String propertyName, String value) {
add(expr.icontains(propertyName, value));
@@ -990,6 +1002,12 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
return this;
}
@Override
public ExpressionList<T> ltOrNull(String propertyName, Object value) {
add(expr.ltOrNull(propertyName, value));
return this;
}
@Override
public ExpressionList<T> not(Expression exp) {
add(expr.not(exp));
@@ -280,6 +280,11 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
return exprList.and(expOne, expTwo);
}
@Override
public ExpressionList<T> inRangeWith(String lowProperty, String highProperty, Object value) {
return exprList.inRangeWith(lowProperty, highProperty, value);
}
@Override
public ExpressionList<T> inRange(String propertyName, Object value1, Object value2) {
return exprList.inRange(propertyName, value1, value2);
@@ -597,6 +602,11 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
return exprList.gt(propertyName, value);
}
@Override
public ExpressionList<T> gtOrNull(String propertyName, Object value) {
return exprList.gtOrNull(propertyName, value);
}
@Override
public ExpressionList<T> having() {
throw new IllegalStateException("having() not allowed on Junction expression list");
@@ -732,6 +742,11 @@ class JunctionExpression<T> implements SpiJunction<T>, SpiExpression, Expression
return exprList.lt(propertyName, value);
}
@Override
public ExpressionList<T> ltOrNull(String propertyName, Object value) {
return exprList.ltOrNull(propertyName, value);
}
@Override
public ExpressionList<T> ne(String propertyName, Object value) {
return exprList.ne(propertyName, value);