Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Draft support for "where xxx in(select bbb from...)"

  • Loading branch information...
commit c5ea6785a5ce16ce9c856e220ced92091b9e1ef5 1 parent dd8d894
@gitblit authored
View
5 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*
View
6 src/com/iciql/Constants.java
@@ -25,14 +25,14 @@
// 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";
}
View
9 src/com/iciql/Query.java
@@ -203,6 +203,15 @@ public String toSQL(boolean distinct) {
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();
View
5 src/com/iciql/QueryCondition.java
@@ -36,6 +36,11 @@
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);
View
32 src/com/iciql/QueryWhere.java
@@ -289,7 +289,39 @@ public String toSQL(boolean distinct) {
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);
}
View
32 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));
+ }
+}
View
41 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(")");
+ }
+}
View
12 tests/com/iciql/test/JoinTest.java
@@ -28,6 +28,7 @@
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 void testLeftJoin() throws Exception {
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 {
Please sign in to comment.
Something went wrong with that request. Please try again.