]> source.dussan.org Git - iciql.git/commitdiff
Refinement to BETWEEN to feel more like the rest of the API.
authorJames Moger <james.moger@gmail.com>
Mon, 8 Aug 2011 11:54:28 +0000 (07:54 -0400)
committerJames Moger <james.moger@gmail.com>
Mon, 8 Aug 2011 11:54:28 +0000 (07:54 -0400)
src/com/iciql/Query.java
src/com/iciql/QueryBetween.java [new file with mode: 0644]
src/com/iciql/QueryCondition.java
tests/com/iciql/test/ModelsTest.java

index d6ee0ef335627bd66097ff0b7b70b73fdb04ff9e..2d31e8df0751137825033c5852279f5557be04c8 100644 (file)
@@ -367,7 +367,7 @@ public class Query<T> {
                stat.appendSQL("?");\r
                addParameter(stat, alias, value);\r
        }\r
-       \r
+\r
        /**\r
         * INTERNAL\r
         * \r
@@ -375,19 +375,28 @@ public class Query<T> {
         *            the statement\r
         * @param alias\r
         *            the alias object (can be null)\r
-        * @param value\r
-        *            the value\r
+        * @param valueLeft\r
+        *            the value on the left of the compound clause\r
+        * @param valueRight\r
+        *            the value on the right of the compound clause\r
+        * @param compareType\r
+        *            the current compare type (e.g. BETWEEN)\r
         */\r
-       public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight, CompareType compareType) {\r
+       public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight,\r
+                       CompareType compareType) {\r
                stat.appendSQL("?");\r
                stat.appendSQL(" ");\r
-               stat.appendSQL("AND");\r
+               switch (compareType) {\r
+               case BETWEEN:\r
+                       stat.appendSQL("AND");\r
+                       break;\r
+               }\r
                stat.appendSQL(" ");\r
                stat.appendSQL("?");\r
                addParameter(stat, alias, valueLeft);\r
                addParameter(stat, alias, valueRight);\r
        }\r
-       \r
+\r
        private void addParameter(SQLStatement stat, Object alias, Object value) {\r
                if (alias != null && value.getClass().isEnum()) {\r
                        SelectColumn<T> col = aliasMap.get(alias);\r
diff --git a/src/com/iciql/QueryBetween.java b/src/com/iciql/QueryBetween.java
new file mode 100644 (file)
index 0000000..26ac40e
--- /dev/null
@@ -0,0 +1,35 @@
+/*\r
+ * Copyright 2011 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql;\r
+\r
+public class QueryBetween<T, A> {\r
+       \r
+       private Query<T> query;\r
+       private A x;\r
+       private A y;\r
+\r
+       public QueryBetween(Query<T> query, A x, A y) {\r
+               this.query = query;\r
+               this.x = x;\r
+               this.y = y;\r
+       }\r
+       \r
+       public QueryWhere<T> and(A z) {\r
+               query.addConditionToken(new Condition<A>(x, y, z, CompareType.BETWEEN));\r
+               return new QueryWhere<T>(query);\r
+       }\r
+}\r
index efaccb60adf1c61eb565ca7bb9ad1b6c111ee184..583f26d5c574997d3dc4ad20ccedd1687074efaf 100644 (file)
@@ -45,7 +45,7 @@ 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);
@@ -75,12 +75,11 @@ public class QueryCondition<T, A> {
                query.addConditionToken(new Condition<A>(x, y, CompareType.AT_MOST));
                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 QueryBetween<T, A> between(A y) {
+               return new QueryBetween<T, A>(query, x, y);
        }
-       
+
        public QueryWhere<T> like(A pattern) {
                query.addConditionToken(new Condition<A>(x, pattern, CompareType.LIKE));
                return new QueryWhere<T>(query);
index c529012781584cb883ae6a8110cb7e75a8a438cd..0caae4b1cadc65b869ec37b6708886163fac67a2 100644 (file)
@@ -131,13 +131,13 @@ 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.BIRCHTree.WALNUT).select();
+               list = db.from(e).where(e.tree()).between(Tree.BIRCH).and(Tree.WALNUT).select();
                assertEquals(2, list.size());
 
        }
-       
+
        private void testStringEnums(EnumModels e, List<?> models) {
                db.insertAll(models);
 
@@ -148,9 +148,9 @@ 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.MAPLETree.PINE).select();
+               list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select();
                assertEquals(3, list.size());
        }