diff options
author | Sotaro SUZUKI <sotaro.suzuki@architector.jp> | 2014-10-01 15:32:06 +0900 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-10-06 10:08:50 -0400 |
commit | dd21bce82da9617ff8f748f8567b0329aca07b83 (patch) | |
tree | 730a2b85b54537a016219b9a08f4d16fb82897e1 /src/main/java | |
parent | 06c1374cfc17bdad621f6ad5154f4de08905da99 (diff) | |
download | iciql-dd21bce82da9617ff8f748f8567b0329aca07b83.tar.gz iciql-dd21bce82da9617ff8f748f8567b0329aca07b83.zip |
add support condition oneOf / noneOf
var-length parameterized IN(?, ?, ...), NOT IN(?, ?, ...) support
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/iciql/CompareType.java | 2 | ||||
-rw-r--r-- | src/main/java/com/iciql/Condition.java | 29 | ||||
-rw-r--r-- | src/main/java/com/iciql/Query.java | 16 | ||||
-rw-r--r-- | src/main/java/com/iciql/QueryCondition.java | 24 | ||||
-rw-r--r-- | src/main/java/com/iciql/util/Utils.java | 5 |
5 files changed, 68 insertions, 8 deletions
diff --git a/src/main/java/com/iciql/CompareType.java b/src/main/java/com/iciql/CompareType.java index 84e29fe..87ce77e 100644 --- a/src/main/java/com/iciql/CompareType.java +++ b/src/main/java/com/iciql/CompareType.java @@ -24,7 +24,7 @@ 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), BETWEEN(
- "BETWEEN", true);
+ "BETWEEN", true), IN("IN", true), NOT_IN("NOT IN", true);
private String text;
private boolean hasRightExpression;
diff --git a/src/main/java/com/iciql/Condition.java b/src/main/java/com/iciql/Condition.java index 17cb117..0ed1d06 100644 --- a/src/main/java/com/iciql/Condition.java +++ b/src/main/java/com/iciql/Condition.java @@ -27,28 +27,47 @@ package com.iciql; class Condition<A> implements Token {
CompareType compareType;
A x, y, z;
+ Iterable<A> i;
+
+ Condition(A x, CompareType compareType) {
+ this(x, null, null, null, compareType);
+ }
Condition(A x, A y, CompareType compareType) {
- this(x, y, null, compareType);
+ this(x, y, null, null, compareType);
}
Condition(A x, A y, A z, CompareType compareType) {
+ this(x, y, z, null, compareType);
+ }
+
+ Condition(A x, Iterable<A> i, CompareType compareType) {
+ this(x, null, null, i, compareType);
+ }
+
+ Condition(A x, A y, A z, Iterable<A> i, CompareType compareType) {
this.compareType = compareType;
this.x = x;
this.y = y;
this.z = z;
+ this.i = i;
}
+ @SuppressWarnings("unchecked")
public <T> void appendSQL(SQLStatement stat, Query<T> query) {
query.appendSQL(stat, null, x);
stat.appendSQL(" ");
stat.appendSQL(compareType.getString());
if (compareType.hasRightExpression()) {
- stat.appendSQL(" ");
- if (z == null) {
- query.appendSQL(stat, x, y);
+ if (i == null) {
+ stat.appendSQL(" ");
+ if (z == null) {
+ query.appendSQL(stat, x, y);
+ } else {
+ query.appendSQL(stat, x, y, z, compareType);
+ }
} else {
- query.appendSQL(stat, x, y, z, compareType);
+ query.appendSQL(stat, x, (Iterable<Object>)i, compareType);
}
}
}
diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index 45de08b..5f29edf 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -792,6 +792,22 @@ public class Query<T> { addParameter(stat, alias, valueRight);
}
+ public void appendSQL(SQLStatement stat, Object alias, Iterable<Object> values,
+ CompareType compareType) {
+ boolean first = true;
+ stat.appendSQL("(");
+ for (Object value : values) {
+ if (first) {
+ first = false;
+ } else {
+ stat.appendSQL(", ");
+ }
+ stat.appendSQL("?");
+ addParameter(stat, alias, value);
+ }
+ stat.appendSQL(")");
+ }
+
private void addParameter(SQLStatement stat, Object alias, Object value) {
if (alias != null && value.getClass().isEnum()) {
SelectColumn<T> col = getColumnByReference(alias);
diff --git a/src/main/java/com/iciql/QueryCondition.java b/src/main/java/com/iciql/QueryCondition.java index 9613b1b..cef95c1 100644 --- a/src/main/java/com/iciql/QueryCondition.java +++ b/src/main/java/com/iciql/QueryCondition.java @@ -17,6 +17,8 @@ package com.iciql; +import com.iciql.util.Utils; + /** * This class represents a query with an incomplete condition. * @@ -41,6 +43,24 @@ public class QueryCondition<T, A> { return new QueryWhere<T>(query); } + public QueryWhere<T> oneOf(A... a) { + return oneOf(Utils.newArrayIterable(a)); + } + + public QueryWhere<T> oneOf(Iterable<A> i) { + query.addConditionToken(new Condition<A>(x, i, CompareType.IN)); + return new QueryWhere<T>(query); + } + + public QueryWhere<T> noneOf(A... a) { + return noneOf(Utils.newArrayIterable(a)); + } + + public QueryWhere<T> noneOf(Iterable<A> i) { + query.addConditionToken(new Condition<A>(x, i, CompareType.NOT_IN)); + return new QueryWhere<T>(query); + } + public QueryWhere<T> is(A y) { query.addConditionToken(new Condition<A>(x, y, CompareType.EQUAL)); return new QueryWhere<T>(query); @@ -52,12 +72,12 @@ public class QueryCondition<T, A> { } public QueryWhere<T> isNull() { - query.addConditionToken(new Condition<A>(x, null, CompareType.IS_NULL)); + query.addConditionToken(new Condition<A>(x, CompareType.IS_NULL)); return new QueryWhere<T>(query); } public QueryWhere<T> isNotNull() { - query.addConditionToken(new Condition<A>(x, null, CompareType.IS_NOT_NULL)); + query.addConditionToken(new Condition<A>(x, CompareType.IS_NOT_NULL)); return new QueryWhere<T>(query); } diff --git a/src/main/java/com/iciql/util/Utils.java b/src/main/java/com/iciql/util/Utils.java index 77110b8..bc636d1 100644 --- a/src/main/java/com/iciql/util/Utils.java +++ b/src/main/java/com/iciql/util/Utils.java @@ -28,6 +28,7 @@ import java.math.BigInteger; import java.sql.Blob;
import java.sql.Clob;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -78,6 +79,10 @@ public class Utils { }
}
+ public static <T> Iterable<T> newArrayIterable(final T[] a) {
+ return Arrays.asList(a);
+ }
+
public static <T> ArrayList<T> newArrayList() {
return new ArrayList<T>();
}
|