From 0b94e39b872246774fe2f3a0d507f7c134a9d6b4 Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 6 Dec 2011 08:51:45 -0500 Subject: [PATCH] Improved fluent/type-safety of join statements --- docs/04_examples.mkd | 8 +++++++ docs/05_releases.mkd | 1 + src/com/iciql/Query.java | 2 +- src/com/iciql/QueryJoin.java | 32 +++++++++++++-------------- src/com/iciql/QueryJoinCondition.java | 26 +++++++++++----------- tests/com/iciql/test/JoinTest.java | 3 +-- 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<Customer> 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<CustOrder> 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 { */ @SuppressWarnings({ "unchecked", "rawtypes" }) - public QueryJoin innerJoin(U alias) { + public QueryJoin innerJoin(A alias) { TableDefinition def = (TableDefinition) db.define(alias.getClass()); SelectTable 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 { - private Query query; - private SelectTable join; + private Query query; + private SelectTable join; - QueryJoin(Query query, SelectTable join) { + QueryJoin(Query query, SelectTable join) { this.query = query; this.join = join; } - public QueryJoinCondition on(boolean x) { + public QueryJoinCondition on(boolean x) { return addPrimitive(x); } - public QueryJoinCondition on(byte x) { + public QueryJoinCondition on(byte x) { return addPrimitive(x); } - public QueryJoinCondition on(short x) { + public QueryJoinCondition on(short x) { return addPrimitive(x); } - public QueryJoinCondition on(int x) { + public QueryJoinCondition on(int x) { return addPrimitive(x); } - public QueryJoinCondition on(long x) { + public QueryJoinCondition on(long x) { return addPrimitive(x); } - public QueryJoinCondition on(float x) { + public QueryJoinCondition on(float x) { return addPrimitive(x); } - public QueryJoinCondition on(double x) { + public QueryJoinCondition on(double x) { return addPrimitive(x); } - private QueryJoinCondition addPrimitive(A x) { + private QueryJoinCondition addPrimitive(A x) { A alias = query.getPrimitiveAliasByValue(x); if (alias == null) { // this will result in an unmapped field exception - return new QueryJoinCondition(query, join, x); + return new QueryJoinCondition(query, join, x); } - return new QueryJoinCondition(query, join, alias); + return new QueryJoinCondition(query, join, alias); } - public QueryJoinCondition on(A x) { - return new QueryJoinCondition(query, join, x); + public QueryJoinCondition on(A x) { + return new QueryJoinCondition(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 { +public class QueryJoinCondition { - private Query query; - private SelectTable join; + private Query query; + private SelectTable join; private A x; - QueryJoinCondition(Query query, SelectTable join, A x) { + QueryJoinCondition(Query query, SelectTable join, A x) { this.query = query; this.join = join; this.x = x; } - public Query is(boolean y) { + public Query is(boolean y) { return addPrimitive(y); } - public Query is(byte y) { + public Query is(byte y) { return addPrimitive(y); } - public Query is(short y) { + public Query is(short y) { return addPrimitive(y); } - public Query is(int y) { + public Query is(int y) { return addPrimitive(y); } - public Query is(long y) { + public Query is(long y) { return addPrimitive(y); } - public Query is(float y) { + public Query is(float y) { return addPrimitive(y); } - public Query is(double y) { + public Query is(double y) { return addPrimitive(y); } @SuppressWarnings("unchecked") - private Query addPrimitive(Object o) { + private Query addPrimitive(Object o) { A alias = query.getPrimitiveAliasByValue((A) o); if (alias == null) { join.addConditionToken(new Condition(x, (A) o, CompareType.EQUAL)); @@ -76,7 +76,7 @@ public class QueryJoinCondition { return query; } - public Query is(A y) { + public Query is(A y) { join.addConditionToken(new Condition(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 users = (List) db.from(u).innerJoin(n).on(u.id).is(n.userId).groupBy(u.id).where(u.id).is(2).select(); + List 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); -- 2.39.5