From: James Moger Date: Mon, 8 Aug 2011 11:54:28 +0000 (-0400) Subject: Refinement to BETWEEN to feel more like the rest of the API. X-Git-Tag: v0.6.3~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=19326deded5a62997962d6d612de4b857303e21b;p=iciql.git Refinement to BETWEEN to feel more like the rest of the API. --- diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java index d6ee0ef..2d31e8d 100644 --- a/src/com/iciql/Query.java +++ b/src/com/iciql/Query.java @@ -367,7 +367,7 @@ public class Query { stat.appendSQL("?"); addParameter(stat, alias, value); } - + /** * INTERNAL * @@ -375,19 +375,28 @@ public class Query { * the statement * @param alias * the alias object (can be null) - * @param value - * the value + * @param valueLeft + * the value on the left of the compound clause + * @param valueRight + * the value on the right of the compound clause + * @param compareType + * the current compare type (e.g. BETWEEN) */ - public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight, CompareType compareType) { + public void appendSQL(SQLStatement stat, Object alias, Object valueLeft, Object valueRight, + CompareType compareType) { stat.appendSQL("?"); stat.appendSQL(" "); - stat.appendSQL("AND"); + switch (compareType) { + case BETWEEN: + stat.appendSQL("AND"); + break; + } 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()) { SelectColumn col = aliasMap.get(alias); diff --git a/src/com/iciql/QueryBetween.java b/src/com/iciql/QueryBetween.java new file mode 100644 index 0000000..26ac40e --- /dev/null +++ b/src/com/iciql/QueryBetween.java @@ -0,0 +1,35 @@ +/* + * Copyright 2011 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.iciql; + +public class QueryBetween { + + private Query query; + private A x; + private A y; + + public QueryBetween(Query query, A x, A y) { + this.query = query; + this.x = x; + this.y = y; + } + + public QueryWhere and(A z) { + query.addConditionToken(new Condition(x, y, z, CompareType.BETWEEN)); + return new QueryWhere(query); + } +} diff --git a/src/com/iciql/QueryCondition.java b/src/com/iciql/QueryCondition.java index efaccb6..583f26d 100644 --- a/src/com/iciql/QueryCondition.java +++ b/src/com/iciql/QueryCondition.java @@ -45,7 +45,7 @@ public class QueryCondition { query.addConditionToken(new Condition(x, y, CompareType.NOT_EQUAL)); return new QueryWhere(query); } - + public QueryWhere isNull() { query.addConditionToken(new Condition(x, null, CompareType.IS_NULL)); return new QueryWhere(query); @@ -75,12 +75,11 @@ public class QueryCondition { query.addConditionToken(new Condition(x, y, CompareType.AT_MOST)); return new QueryWhere(query); } - - public QueryWhere between(A y, A z) { - query.addConditionToken(new Condition(x, y, z, CompareType.BETWEEN)); - return new QueryWhere(query); + + public QueryBetween between(A y) { + return new QueryBetween(query, x, y); } - + public QueryWhere like(A pattern) { query.addConditionToken(new Condition(x, pattern, CompareType.LIKE)); return new QueryWhere(query); diff --git a/tests/com/iciql/test/ModelsTest.java b/tests/com/iciql/test/ModelsTest.java index c529012..0caae4b 100644 --- a/tests/com/iciql/test/ModelsTest.java +++ b/tests/com/iciql/test/ModelsTest.java @@ -131,13 +131,13 @@ public class ModelsTest { List 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(); + 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 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(); + list = db.from(e).where(e.tree()).between(Tree.MAPLE).and(Tree.PINE).select(); assertEquals(3, list.size()); }