summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2011-08-07 16:10:43 -0400
committerJames Moger <james.moger@gmail.com>2011-08-07 16:10:43 -0400
commit99359ad5077e416c897e9596f081fc694f9ae5df (patch)
tree523842d1899c91e88729c3dcb0b23e2ea8e78ebd
parent684838def9e1646c266ffb46bbc65b5dfdc8f14d (diff)
downloadiciql-99359ad5077e416c897e9596f081fc694f9ae5df.tar.gz
iciql-99359ad5077e416c897e9596f081fc694f9ae5df.zip
Quick implementation of BETWEEN, IS NULL, and IS NOT NULL.
-rw-r--r--src/com/iciql/CompareType.java3
-rw-r--r--src/com/iciql/Condition.java13
-rw-r--r--src/com/iciql/Query.java26
-rw-r--r--src/com/iciql/QueryCondition.java15
-rw-r--r--tests/com/iciql/test/ModelsTest.java9
5 files changed, 62 insertions, 4 deletions
diff --git a/src/com/iciql/CompareType.java b/src/com/iciql/CompareType.java
index eb401bb..84e29fe 100644
--- a/src/com/iciql/CompareType.java
+++ b/src/com/iciql/CompareType.java
@@ -23,7 +23,8 @@ package com.iciql;
enum CompareType {
EQUAL("=", true), EXCEEDS(">", true), AT_LEAST(">=", true), LESS_THAN("<", true), AT_MOST("<=", true), NOT_EQUAL(
- "<>", true), IS_NOT_NULL("IS NOT NULL", false), IS_NULL("IS NULL", false), LIKE("LIKE", true);
+ "<>", true), IS_NOT_NULL("IS NOT NULL", false), IS_NULL("IS NULL", false), LIKE("LIKE", true), BETWEEN(
+ "BETWEEN", true);
private String text;
private boolean hasRightExpression;
diff --git a/src/com/iciql/Condition.java b/src/com/iciql/Condition.java
index df9762a..17cb117 100644
--- a/src/com/iciql/Condition.java
+++ b/src/com/iciql/Condition.java
@@ -26,12 +26,17 @@ package com.iciql;
class Condition<A> implements Token {
CompareType compareType;
- A x, y;
+ A x, y, z;
Condition(A x, A y, CompareType compareType) {
+ this(x, y, null, compareType);
+ }
+
+ Condition(A x, A y, A z, CompareType compareType) {
this.compareType = compareType;
this.x = x;
this.y = y;
+ this.z = z;
}
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
@@ -40,7 +45,11 @@ class Condition<A> implements Token {
stat.appendSQL(compareType.getString());
if (compareType.hasRightExpression()) {
stat.appendSQL(" ");
- query.appendSQL(stat, x, y);
+ if (z == null) {
+ query.appendSQL(stat, x, y);
+ } else {
+ query.appendSQL(stat, x, y, z, compareType);
+ }
}
}
}
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java
index c44cac7..d6ee0ef 100644
--- a/src/com/iciql/Query.java
+++ b/src/com/iciql/Query.java
@@ -365,8 +365,32 @@ public class Query<T> {
return;
}
stat.appendSQL("?");
+ addParameter(stat, alias, value);
+ }
+
+ /**
+ * INTERNAL
+ *
+ * @param stat
+ * the statement
+ * @param alias
+ * the alias object (can be null)
+ * @param value
+ * the value
+ */
+ public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight, CompareType compareType) {
+ stat.appendSQL("?");
+ stat.appendSQL(" ");
+ stat.appendSQL("AND");
+ stat.appendSQL(" ");
+ stat.appendSQL("?");
+ addParameter(stat, alias, valueLeft);
+ addParameter(stat, alias, valueRight);
+ }
+
+ private void addParameter(SQLStatement stat, Object alias, Object value) {
if (alias != null && value.getClass().isEnum()) {
- col = aliasMap.get(alias);
+ SelectColumn<T> col = aliasMap.get(alias);
EnumType type = col.getFieldDefinition().enumType;
Enum<?> anEnum = (Enum<?>) value;
Object y = Utils.convertEnum(anEnum, type);
diff --git a/src/com/iciql/QueryCondition.java b/src/com/iciql/QueryCondition.java
index 9d13995..efaccb6 100644
--- a/src/com/iciql/QueryCondition.java
+++ b/src/com/iciql/QueryCondition.java
@@ -45,6 +45,16 @@ public class QueryCondition<T, A> {
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));
@@ -66,6 +76,11 @@ public class QueryCondition<T, A> {
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);
diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java
index acaf634..c529012 100644
--- a/tests/com/iciql/test/ModelsTest.java
+++ b/tests/com/iciql/test/ModelsTest.java
@@ -131,6 +131,11 @@ public class ModelsTest {
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) {
@@ -143,6 +148,10 @@ public class ModelsTest {
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