aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gmail.com>2011-12-06 08:51:45 -0500
committerJames Moger <james.moger@gmail.com>2011-12-06 08:51:45 -0500
commit0b94e39b872246774fe2f3a0d507f7c134a9d6b4 (patch)
tree8ad2660a9f318644a5944d26f0242a8bd50f5763
parentad70685918aac114a4babd9a1a0e0217b6323df0 (diff)
downloadiciql-0b94e39b872246774fe2f3a0d507f7c134a9d6b4.tar.gz
iciql-0b94e39b872246774fe2f3a0d507f7c134a9d6b4.zip
Improved fluent/type-safety of join statements
-rw-r--r--docs/04_examples.mkd8
-rw-r--r--docs/05_releases.mkd1
-rw-r--r--src/com/iciql/Query.java2
-rw-r--r--src/com/iciql/QueryJoin.java32
-rw-r--r--src/com/iciql/QueryJoinCondition.java26
-rw-r--r--tests/com/iciql/test/JoinTest.java3
6 files changed, 40 insertions, 32 deletions
diff --git a/docs/04_examples.mkd b/docs/04_examples.mkd
index d4f98da..480e29d 100644
--- a/docs/04_examples.mkd
+++ b/docs/04_examples.mkd
@@ -80,6 +80,14 @@ db.from(p).where(p.productId).atLeast(10).delete();
%BEGINCODE%
final Customer c = new Customer();
final Order o = new Order();
+
+List&lt;Customer&gt; customersWithLargeOrders =
+ db.from(c).
+ innerJoin(o).on(c.customerId).is(o.customerId).
+ where(o.total).greaterThan(new BigDecimal("500.00")).
+ groupBy(c.customerId).select();
+
+
List&lt;CustOrder&gt; orders =
db.from(c).
innerJoin(o).on(c.customerId).is(o.customerId).
diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd
index 94d0e9a..6d6054c 100644
--- a/docs/05_releases.mkd
+++ b/docs/05_releases.mkd
@@ -8,6 +8,7 @@
- Fixed joins on primitives
- Fixed group by on primitives
+- Improved fluent/type-safety of joins
### Older Releases
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java
index 2f5141d..b43f774 100644
--- a/src/com/iciql/Query.java
+++ b/src/com/iciql/Query.java
@@ -725,7 +725,7 @@ public class Query<T> {
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
- public <U> QueryJoin innerJoin(U alias) {
+ 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);
diff --git a/src/com/iciql/QueryJoin.java b/src/com/iciql/QueryJoin.java
index dbb3d15..652d937 100644
--- a/src/com/iciql/QueryJoin.java
+++ b/src/com/iciql/QueryJoin.java
@@ -21,54 +21,54 @@ package com.iciql;
* This class represents a query with a join.
*/
-public class QueryJoin {
+public class QueryJoin<T> {
- private Query<?> query;
- private SelectTable<?> join;
+ private Query<T> query;
+ private SelectTable<T> join;
- QueryJoin(Query<?> query, SelectTable<?> join) {
+ QueryJoin(Query<T> query, SelectTable<T> join) {
this.query = query;
this.join = join;
}
- public QueryJoinCondition<Boolean> on(boolean x) {
+ public QueryJoinCondition<T, Boolean> on(boolean x) {
return addPrimitive(x);
}
- public QueryJoinCondition<Byte> on(byte x) {
+ public QueryJoinCondition<T, Byte> on(byte x) {
return addPrimitive(x);
}
- public QueryJoinCondition<Short> on(short x) {
+ public QueryJoinCondition<T, Short> on(short x) {
return addPrimitive(x);
}
- public QueryJoinCondition<Integer> on(int x) {
+ public QueryJoinCondition<T, Integer> on(int x) {
return addPrimitive(x);
}
- public QueryJoinCondition<Long> on(long x) {
+ public QueryJoinCondition<T, Long> on(long x) {
return addPrimitive(x);
}
- public QueryJoinCondition<Float> on(float x) {
+ public QueryJoinCondition<T, Float> on(float x) {
return addPrimitive(x);
}
- public QueryJoinCondition<Double> on(double x) {
+ public QueryJoinCondition<T, Double> on(double x) {
return addPrimitive(x);
}
- private <A> QueryJoinCondition<A> addPrimitive(A x) {
+ private <A> QueryJoinCondition<T, A> addPrimitive(A x) {
A alias = query.getPrimitiveAliasByValue(x);
if (alias == null) {
// this will result in an unmapped field exception
- return new QueryJoinCondition<A>(query, join, x);
+ return new QueryJoinCondition<T, A>(query, join, x);
}
- return new QueryJoinCondition<A>(query, join, alias);
+ return new QueryJoinCondition<T, A>(query, join, alias);
}
- public <A> QueryJoinCondition<A> on(A x) {
- return new QueryJoinCondition<A>(query, join, x);
+ public <A> QueryJoinCondition<T, A> on(A x) {
+ return new QueryJoinCondition<T, A>(query, join, x);
}
}
diff --git a/src/com/iciql/QueryJoinCondition.java b/src/com/iciql/QueryJoinCondition.java
index b49e35b..6dfd218 100644
--- a/src/com/iciql/QueryJoinCondition.java
+++ b/src/com/iciql/QueryJoinCondition.java
@@ -25,48 +25,48 @@ package com.iciql;
* the incomplete condition data type
*/
-public class QueryJoinCondition<A> {
+public class QueryJoinCondition<T, A> {
- private Query<?> query;
- private SelectTable<?> join;
+ private Query<T> query;
+ private SelectTable<T> join;
private A x;
- QueryJoinCondition(Query<?> query, SelectTable<?> join, A x) {
+ QueryJoinCondition(Query<T> query, SelectTable<T> join, A x) {
this.query = query;
this.join = join;
this.x = x;
}
- public Query<?> is(boolean y) {
+ public Query<T> is(boolean y) {
return addPrimitive(y);
}
- public Query<?> is(byte y) {
+ public Query<T> is(byte y) {
return addPrimitive(y);
}
- public Query<?> is(short y) {
+ public Query<T> is(short y) {
return addPrimitive(y);
}
- public Query<?> is(int y) {
+ public Query<T> is(int y) {
return addPrimitive(y);
}
- public Query<?> is(long y) {
+ public Query<T> is(long y) {
return addPrimitive(y);
}
- public Query<?> is(float y) {
+ public Query<T> is(float y) {
return addPrimitive(y);
}
- public Query<?> is(double y) {
+ public Query<T> is(double y) {
return addPrimitive(y);
}
@SuppressWarnings("unchecked")
- private Query<?> addPrimitive(Object o) {
+ private Query<T> addPrimitive(Object o) {
A alias = query.getPrimitiveAliasByValue((A) o);
if (alias == null) {
join.addConditionToken(new Condition<A>(x, (A) o, CompareType.EQUAL));
@@ -76,7 +76,7 @@ public class QueryJoinCondition<A> {
return query;
}
- public Query<?> is(A y) {
+ public Query<T> is(A y) {
join.addConditionToken(new Condition<A>(x, y, CompareType.EQUAL));
return query;
}
diff --git a/tests/com/iciql/test/JoinTest.java b/tests/com/iciql/test/JoinTest.java
index 018d721..ceb2ad5 100644
--- a/tests/com/iciql/test/JoinTest.java
+++ b/tests/com/iciql/test/JoinTest.java
@@ -28,7 +28,6 @@ import org.junit.Test;
import com.iciql.Db;
import com.iciql.Iciql.IQColumn;
import com.iciql.Iciql.IQTable;
-import com.iciql.util.IciqlLogger;
/**
* Tests of Joins.
@@ -79,7 +78,7 @@ public class JoinTest {
final UserNote n = new UserNote();
// this query returns 1 UserId if the user has a note
- List<UserId> users = (List<UserId>) db.from(u).innerJoin(n).on(u.id).is(n.userId).groupBy(u.id).where(u.id).is(2).select();
+ List<UserId> users = db.from(u).innerJoin(n).on(u.id).is(n.userId).groupBy(u.id).where(u.id).is(2).select();
db.dropTable(UserId.class);
db.dropTable(UserNote.class);