]> source.dussan.org Git - iciql.git/commitdiff
Quick implementation of BETWEEN, IS NULL, and IS NOT NULL.
authorJames Moger <james.moger@gmail.com>
Sun, 7 Aug 2011 20:10:43 +0000 (16:10 -0400)
committerJames Moger <james.moger@gmail.com>
Sun, 7 Aug 2011 20:10:43 +0000 (16:10 -0400)
src/com/iciql/CompareType.java
src/com/iciql/Condition.java
src/com/iciql/Query.java
src/com/iciql/QueryCondition.java
tests/com/iciql/test/ModelsTest.java

index eb401bbeb59f249fd74d897527ec34f9a87a9905..84e29fe34e10aa06311466d4a017600b3181da88 100644 (file)
@@ -23,7 +23,8 @@ package com.iciql;
 \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
index df9762a6f858c5277334e995eee214ad99cede04..17cb1176692150e483f6bf6627e14a54f18e94a2 100644 (file)
@@ -26,12 +26,17 @@ package com.iciql;
 \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
@@ -40,7 +45,11 @@ class Condition<A> implements Token {
                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
index c44cac793aa6de1947e6a175d8d19649a00746c7..d6ee0ef335627bd66097ff0b7b70b73fdb04ff9e 100644 (file)
@@ -365,8 +365,32 @@ public class Query<T> {
                        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
index 9d139956f7981737a1a4bd574741e3e2595a3fc9..efaccb60adf1c61eb565ca7bb9ad1b6c111ee184 100644 (file)
@@ -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);
index acaf63481c5c960c9a8759a4ae4011c4b7c7e8eb..c529012781584cb883ae6a8110cb7e75a8a438cd 100644 (file)
@@ -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