\r
enum CompareType {\r
EQUAL("=", true), EXCEEDS(">", true), AT_LEAST(">=", true), LESS_THAN("<", true), AT_MOST("<=", true), NOT_EQUAL(\r
- "<>", true), IS_NOT_NULL("IS NOT NULL", false), IS_NULL("IS NULL", false), LIKE("LIKE", true);\r
+ "<>", true), IS_NOT_NULL("IS NOT NULL", false), IS_NULL("IS NULL", false), LIKE("LIKE", true), BETWEEN(\r
+ "BETWEEN", true);\r
\r
private String text;\r
private boolean hasRightExpression;\r
\r
class Condition<A> implements Token {\r
CompareType compareType;\r
- A x, y;\r
+ A x, y, z;\r
\r
Condition(A x, A y, CompareType compareType) {\r
+ this(x, y, null, compareType);\r
+ }\r
+\r
+ Condition(A x, A y, A z, CompareType compareType) {\r
this.compareType = compareType;\r
this.x = x;\r
this.y = y;\r
+ this.z = z;\r
}\r
\r
public <T> void appendSQL(SQLStatement stat, Query<T> query) {\r
stat.appendSQL(compareType.getString());\r
if (compareType.hasRightExpression()) {\r
stat.appendSQL(" ");\r
- query.appendSQL(stat, x, y);\r
+ if (z == null) {\r
+ query.appendSQL(stat, x, y);\r
+ } else {\r
+ query.appendSQL(stat, x, y, z, compareType);\r
+ }\r
}\r
}\r
}\r
return;\r
}\r
stat.appendSQL("?");\r
+ addParameter(stat, alias, value);\r
+ }\r
+ \r
+ /**\r
+ * INTERNAL\r
+ * \r
+ * @param stat\r
+ * the statement\r
+ * @param alias\r
+ * the alias object (can be null)\r
+ * @param value\r
+ * the value\r
+ */\r
+ public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight, CompareType compareType) {\r
+ stat.appendSQL("?");\r
+ stat.appendSQL(" ");\r
+ stat.appendSQL("AND");\r
+ stat.appendSQL(" ");\r
+ stat.appendSQL("?");\r
+ addParameter(stat, alias, valueLeft);\r
+ addParameter(stat, alias, valueRight);\r
+ }\r
+ \r
+ private void addParameter(SQLStatement stat, Object alias, Object value) {\r
if (alias != null && value.getClass().isEnum()) {\r
- col = aliasMap.get(alias);\r
+ SelectColumn<T> col = aliasMap.get(alias);\r
EnumType type = col.getFieldDefinition().enumType;\r
Enum<?> anEnum = (Enum<?>) value;\r
Object y = Utils.convertEnum(anEnum, type);\r
query.addConditionToken(new Condition<A>(x, y, CompareType.NOT_EQUAL));
return new QueryWhere<T>(query);
}
+
+ public QueryWhere<T> isNull() {
+ query.addConditionToken(new Condition<A>(x, null, CompareType.IS_NULL));
+ return new QueryWhere<T>(query);
+ }
+
+ public QueryWhere<T> isNotNull() {
+ query.addConditionToken(new Condition<A>(x, null, CompareType.IS_NOT_NULL));
+ return new QueryWhere<T>(query);
+ }
public QueryWhere<T> exceeds(A y) {
query.addConditionToken(new Condition<A>(x, y, CompareType.EXCEEDS));
return new QueryWhere<T>(query);
}
+ public QueryWhere<T> between(A y, A z) {
+ query.addConditionToken(new Condition<A>(x, y, z, CompareType.BETWEEN));
+ return new QueryWhere<T>(query);
+ }
+
public QueryWhere<T> like(A pattern) {
query.addConditionToken(new Condition<A>(x, pattern, CompareType.LIKE));
return new QueryWhere<T>(query);
List<EnumModels> list = db.from(e).where(e.tree()).atLeast(Tree.BIRCH).select();
assertEquals(3, list.size());
+
+ // between is an int compare
+ list = db.from(e).where(e.tree()).between(Tree.BIRCH, Tree.WALNUT).select();
+ assertEquals(2, list.size());
+
}
private void testStringEnums(EnumModels e, List<?> models) {
List<EnumModels> list = db.from(e).where(e.tree()).isNot(Tree.BIRCH).select();
assertEquals(models.size() - 1, list.size());
+
+ // between is a string compare
+ list = db.from(e).where(e.tree()).between(Tree.MAPLE, Tree.PINE).select();
+ assertEquals(3, list.size());
}
@Test