diff options
author | James Moger <james.moger@gitblit.com> | 2012-04-09 05:52:20 -0700 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2012-04-09 05:52:20 -0700 |
commit | dd8d894824afb8ba677e838dbab2781e06d643f5 (patch) | |
tree | 146a136c4be850ff40d0e7cd6db95c0b42079bcb | |
parent | 25e92e1b20d58b523c8a1e2090241552bc4489cd (diff) | |
parent | 1fcbeeb471ecde1b487bece52644ddc6713592ac (diff) | |
download | iciql-dd8d894824afb8ba677e838dbab2781e06d643f5.tar.gz iciql-dd8d894824afb8ba677e838dbab2781e06d643f5.zip |
Merge pull request #1 from backpaper0/master
Added support for left outer join.
-rw-r--r-- | src/com/iciql/Query.java | 20 | ||||
-rw-r--r-- | tests/com/iciql/test/JoinTest.java | 12 |
2 files changed, 25 insertions, 7 deletions
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java index aa0ce4b..c2f81af 100644 --- a/src/com/iciql/Query.java +++ b/src/com/iciql/Query.java @@ -836,15 +836,23 @@ public class Query<T> { * @return the joined query
*/
- @SuppressWarnings({ "unchecked", "rawtypes" })
public <A> QueryJoin<T> innerJoin(A alias) {
- TableDefinition<T> def = (TableDefinition<T>) db.define(alias.getClass());
- SelectTable<T> join = new SelectTable(db, this, alias, false);
- def.initSelectObject(join, alias, aliasMap);
- joins.add(join);
- return new QueryJoin(this, join);
+ return join(alias, false);
}
+ public <A> QueryJoin<T> leftJoin(A alias) {
+ return join(alias, true);
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private <A> QueryJoin<T> join(A alias, boolean outerJoin) {
+ TableDefinition<T> def = (TableDefinition<T>) db.define(alias.getClass());
+ SelectTable<T> join = new SelectTable(db, this, alias, outerJoin);
+ def.initSelectObject(join, alias, aliasMap);
+ joins.add(join);
+ return new QueryJoin(this, join);
+ }
+
Db getDb() {
return db;
}
diff --git a/tests/com/iciql/test/JoinTest.java b/tests/com/iciql/test/JoinTest.java index 07de098..1fea444 100644 --- a/tests/com/iciql/test/JoinTest.java +++ b/tests/com/iciql/test/JoinTest.java @@ -81,6 +81,16 @@ public class JoinTest { assertEquals(2, users.get(0).id); } + @Test + public void testLeftJoin() throws Exception { + final UserId u = new UserId(); + final UserNote n = new UserNote(); + + List<UserId> notes = db.from(u).leftJoin(n).on(u.id).is(n.userId).where(u.id).is(4).select(); + assertEquals(1, notes.size()); + assertEquals(4, notes.get(0).id); + } + @IQTable public static class UserId { @@ -104,7 +114,7 @@ public class JoinTest { } public static List<UserId> getList() { - UserId[] list = { new UserId(1, "Tom"), new UserId(2, "Dick"), new UserId(3, "Harry") }; + UserId[] list = { new UserId(1, "Tom"), new UserId(2, "Dick"), new UserId(3, "Harry"), new UserId(4, "Jack") }; return Arrays.asList(list); } } |