diff options
-rw-r--r-- | docs/05_releases.mkd | 5 | ||||
-rw-r--r-- | src/com/iciql/Constants.java | 6 | ||||
-rw-r--r-- | src/com/iciql/Query.java | 9 | ||||
-rw-r--r-- | src/com/iciql/QueryCondition.java | 5 | ||||
-rw-r--r-- | src/com/iciql/QueryWhere.java | 32 | ||||
-rw-r--r-- | src/com/iciql/SubQuery.java | 32 | ||||
-rw-r--r-- | src/com/iciql/SubQueryCondition.java | 41 | ||||
-rw-r--r-- | tests/com/iciql/test/JoinTest.java | 12 |
8 files changed, 139 insertions, 3 deletions
diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd index 934e710..37de385 100644 --- a/docs/05_releases.mkd +++ b/docs/05_releases.mkd @@ -6,6 +6,11 @@ **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%)) *released %BUILDDATE%*
+- Added support for single column subquery *select name, address from user_table where user_id in (select user_id from invoice table where paid = false)*
+- Added support for left outer join (Github/backpaper0)
+
+**0.7.10** *released 2012-01-27*
+
- Fixed default String value bug where a default empty string threw an IndexOutOfBounds exception
**0.7.9** *released 2012-01-24*
diff --git a/src/com/iciql/Constants.java b/src/com/iciql/Constants.java index 2ce664a..fc27ace 100644 --- a/src/com/iciql/Constants.java +++ b/src/com/iciql/Constants.java @@ -25,14 +25,14 @@ public class Constants { // The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String VERSION = "0.7.10";
+ public static final String VERSION = "1.0.0-SNAPSHOT";
// The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String VERSION_DATE = "2012-01-27";
+ public static final String VERSION_DATE = "2012-05-07";
// The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String API_CURRENT = "13";
+ public static final String API_CURRENT = "14";
}
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java index c2f81af..6c0ecf9 100644 --- a/src/com/iciql/Query.java +++ b/src/com/iciql/Query.java @@ -203,6 +203,15 @@ public class Query<T> { return stat.toSQL().trim();
}
+ <Z> String toSubQuery(Z z) {
+ SQLStatement stat = getSelectStatement(false);
+ SelectColumn<T> col = aliasMap.get(z);
+ String columnName = col.getFieldDefinition().columnName;
+ stat.appendColumn(columnName);
+ appendFromWhere(stat);
+ return stat.toSQL();
+ }
+
private List<T> select(boolean distinct) {
List<T> result = Utils.newArrayList();
TableDefinition<T> def = from.getAliasDefinition();
diff --git a/src/com/iciql/QueryCondition.java b/src/com/iciql/QueryCondition.java index 2595531..9613b1b 100644 --- a/src/com/iciql/QueryCondition.java +++ b/src/com/iciql/QueryCondition.java @@ -36,6 +36,11 @@ public class QueryCondition<T, A> { this.x = x; } + public <Q, Z> QueryWhere<T> in(SubQuery<Q, Z> q) { + query.addConditionToken(new SubQueryCondition<A, Q, Z>(x, q)); + 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); diff --git a/src/com/iciql/QueryWhere.java b/src/com/iciql/QueryWhere.java index 31228c9..3f1afe1 100644 --- a/src/com/iciql/QueryWhere.java +++ b/src/com/iciql/QueryWhere.java @@ -289,7 +289,39 @@ public class QueryWhere<T> { public <K> String toSQL(boolean distinct, K k) {
return query.toSQL(distinct, k);
}
+
+ public <Z> SubQuery<T, Z> subQuery(Z x) {
+ return new SubQuery<T, Z>(query, x);
+ }
+
+ public SubQuery<T, Boolean> subQuery(boolean x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+
+ public SubQuery<T, Byte> subQuery(byte x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+
+ public SubQuery<T, Short> subQuery(short x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+ public SubQuery<T, Integer> subQuery(int x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+
+ public SubQuery<T, Long> subQuery(long x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+
+ public SubQuery<T, Float> subQuery(float x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+
+ public SubQuery<T, Double> subQuery(double x) {
+ return subQuery(query.getPrimitiveAliasByValue(x));
+ }
+
public <X, Z> List<X> select(Z x) {
return query.select(x);
}
diff --git a/src/com/iciql/SubQuery.java b/src/com/iciql/SubQuery.java new file mode 100644 index 0000000..398d214 --- /dev/null +++ b/src/com/iciql/SubQuery.java @@ -0,0 +1,32 @@ +/*
+ * Copyright 2012 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 SubQuery<T, Z> {
+
+ final Query<T> query;
+ final Z z;
+
+ public SubQuery(Query<T> query, Z x) {
+ this.query = query;
+ this.z = x;
+ }
+
+ public void appendSQL(SQLStatement stat) {
+ stat.appendSQL(query.toSubQuery(z));
+ }
+}
diff --git a/src/com/iciql/SubQueryCondition.java b/src/com/iciql/SubQueryCondition.java new file mode 100644 index 0000000..effea3b --- /dev/null +++ b/src/com/iciql/SubQueryCondition.java @@ -0,0 +1,41 @@ +/*
+ * Copyright 2012 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;
+
+/**
+ * A condition that contains a subquery.
+ *
+ * @param <A>
+ * the operand type
+ */
+
+class SubQueryCondition<A, Y, Z> implements Token {
+ A x;
+ SubQuery<Y, Z> subquery;
+
+ SubQueryCondition(A x, SubQuery<Y, Z> subquery) {
+ this.x = x;
+ this.subquery = subquery;
+ }
+
+ public <T> void appendSQL(SQLStatement stat, Query<T> query) {
+ query.appendSQL(stat, null, x);
+ stat.appendSQL(" in (");
+ subquery.appendSQL(stat);
+ stat.appendSQL(")");
+ }
+}
diff --git a/tests/com/iciql/test/JoinTest.java b/tests/com/iciql/test/JoinTest.java index 1fea444..3e36eff 100644 --- a/tests/com/iciql/test/JoinTest.java +++ b/tests/com/iciql/test/JoinTest.java @@ -28,6 +28,7 @@ import org.junit.Test; import com.iciql.Db; import com.iciql.Iciql.IQColumn; import com.iciql.Iciql.IQTable; +import com.iciql.QueryWhere; /** * Tests of Joins. @@ -91,6 +92,17 @@ public class JoinTest { assertEquals(4, notes.get(0).id); } + @Test + public void testSubQuery() throws Exception { + final UserId u = new UserId(); + final UserNote n = new UserNote(); + + QueryWhere<UserId> q = db.from(u).where(u.id).in(db.from(n).where(n.userId).exceeds(0).subQuery(n.userId)); + assertEquals("SELECT * FROM UserId WHERE id in (SELECT userId FROM UserNote WHERE userId > 0 )", q.toSQL()); + List<UserId> notes = q.select(); + assertEquals(3, notes.size()); + } + @IQTable public static class UserId { |