]> source.dussan.org Git - iciql.git/commitdiff
Fixed joins on primitives
authorJames Moger <james.moger@gmail.com>
Mon, 5 Dec 2011 22:01:37 +0000 (17:01 -0500)
committerJames Moger <james.moger@gmail.com>
Mon, 5 Dec 2011 22:01:37 +0000 (17:01 -0500)
docs/05_releases.mkd
src/com/iciql/Constants.java
src/com/iciql/QueryJoin.java
src/com/iciql/QueryJoinCondition.java
tests/com/iciql/test/IciqlSuite.java
tests/com/iciql/test/JoinTest.java [new file with mode: 0644]

index a28751d1a1aba6e281f054d562bef823a9533da7..17b5e048ab7e0b2c592c0aa76cee4bcd8cca8d6e 100644 (file)
@@ -6,12 +6,16 @@
 \r
 **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%)) &nbsp; *released %BUILDDATE%*\r
 \r
+- Fixed joins on primitives\r
+\r
+### Older Releases\r
+\r
+**0.7.2** &nbsp; *released 2011-11-30*\r
+\r
 - generated models are now serializable with a default serial version id of 1\r
 - Updated to H2 1.3.162\r
 - Updated to HSQL 2.2.6 (100% unit test pass now that [this bug](https://sourceforge.net/tracker/?func=detail&aid=3390047&group_id=23316&atid=378131) was fixed)\r
 \r
-### Older Releases\r
-\r
 **0.7.1** &nbsp; *released 2011-08-31*\r
 \r
 - api change release (API v7)\r
index dfc18af22d7644b6c467d699172c88643bb125f1..0bd7663bcabc83aa025cc2fccdd940fb35b4c8b5 100644 (file)
@@ -25,11 +25,11 @@ public class Constants {
 \r
        // The build script extracts this exact line so be careful editing it\r
        // and only use A-Z a-z 0-9 .-_ in the string.\r
-       public static final String VERSION = "0.7.2";\r
+       public static final String VERSION = "0.7.3-SNAPSHOT";\r
 \r
        // The build script extracts this exact line so be careful editing it\r
        // and only use A-Z a-z 0-9 .-_ in the string.\r
-       public static final String VERSION_DATE = "2011-11-30";\r
+       public static final String VERSION_DATE = "PENDING";\r
 \r
        // The build script extracts this exact line so be careful editing it\r
        // and only use A-Z a-z 0-9 .-_ in the string.\r
index bb614eb00b2d9c0da61aea9adaad69e6a9b7c59f..dbb3d15ff171b338f598ccbd08ce936c4481559e 100644 (file)
@@ -31,6 +31,43 @@ public class QueryJoin {
                this.join = join;\r
        }\r
 \r
+       public QueryJoinCondition<Boolean> on(boolean x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       public QueryJoinCondition<Byte> on(byte x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       public QueryJoinCondition<Short> on(short x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       public QueryJoinCondition<Integer> on(int x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       public QueryJoinCondition<Long> on(long x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       public QueryJoinCondition<Float> on(float x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       public QueryJoinCondition<Double> on(double x) {\r
+               return addPrimitive(x);\r
+       }\r
+\r
+       private <A> QueryJoinCondition<A> addPrimitive(A x) {           \r
+               A alias = query.getPrimitiveAliasByValue(x);\r
+               if (alias == null) {\r
+                       // this will result in an unmapped field exception\r
+                       return new QueryJoinCondition<A>(query, join, x);\r
+               }\r
+               return new QueryJoinCondition<A>(query, join, alias);\r
+       }\r
+\r
        public <A> QueryJoinCondition<A> on(A x) {\r
                return new QueryJoinCondition<A>(query, join, x);\r
        }\r
index e5620d53140e17acccc9539a908e0580a1f9c86f..b49e35bdf552e432242bb7b6518a1110947ece6f 100644 (file)
@@ -17,6 +17,7 @@
 \r
 package com.iciql;\r
 \r
+\r
 /**\r
  * This class represents a query with join and an incomplete condition.\r
  * \r
@@ -36,6 +37,45 @@ public class QueryJoinCondition<A> {
                this.x = x;\r
        }\r
 \r
+       public Query<?> is(boolean y) {\r
+               return addPrimitive(y);\r
+       }       \r
+\r
+       public Query<?> is(byte y) {\r
+               return addPrimitive(y);\r
+       }       \r
+\r
+       public Query<?> is(short y) {\r
+               return addPrimitive(y);\r
+       }       \r
+\r
+       public Query<?> is(int y) {\r
+               return addPrimitive(y);\r
+       }\r
+       \r
+       public Query<?> is(long y) {\r
+               return addPrimitive(y);\r
+       }       \r
+\r
+       public Query<?> is(float y) {\r
+               return addPrimitive(y);\r
+       }       \r
+\r
+       public Query<?> is(double y) {\r
+               return addPrimitive(y);\r
+       }       \r
+\r
+       @SuppressWarnings("unchecked")\r
+       private Query<?> addPrimitive(Object o) {               \r
+               A alias = query.getPrimitiveAliasByValue((A) o);\r
+               if (alias == null) {\r
+                       join.addConditionToken(new Condition<A>(x, (A) o, CompareType.EQUAL));\r
+               } else {\r
+                       join.addConditionToken(new Condition<A>(x, alias, CompareType.EQUAL));\r
+               }\r
+               return query;           \r
+       }\r
+\r
        public Query<?> is(A y) {\r
                join.addConditionToken(new Condition<A>(x, y, CompareType.EQUAL));\r
                return query;\r
index 88b8f339886dde676e93d842f8227f1549c19155..3c98d692346a0f48252e1d9609ca46f48e24ca69 100644 (file)
@@ -84,7 +84,8 @@ import com.iciql.util.Utils;
 @RunWith(Suite.class)\r
 @SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class,\r
                ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class,\r
-               RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, UUIDTest.class })\r
+               RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, JoinTest.class,\r
+               UUIDTest.class })\r
 public class IciqlSuite {\r
 \r
        private static final TestDb[] TEST_DBS = {\r
diff --git a/tests/com/iciql/test/JoinTest.java b/tests/com/iciql/test/JoinTest.java
new file mode 100644 (file)
index 0000000..eab1376
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2011 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.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.iciql.Db;
+import com.iciql.Iciql.IQColumn;
+import com.iciql.Iciql.IQTable;
+
+/**
+ * Tests of Joins.
+ */
+public class JoinTest {
+
+       Db db;
+
+       @Before
+       public void setup() {
+               db = IciqlSuite.openNewDb();
+       }
+
+       @After
+       public void tearDown() {
+               db.close();
+       }
+
+       @Test
+       public void testPrimitiveJoin() throws Exception {
+               db.insertAll(UserId.getList());
+               db.insertAll(UserNote.getList());
+
+               final UserId u = new UserId();
+               final UserNote n = new UserNote();
+
+               List<UserNote> notes = db.from(u).innerJoin(n).on(u.id).is(n.userId).where(u.id).is(2)
+                               .select(new UserNote() {
+                                       {
+                                               userId = n.userId;
+                                               noteId = n.noteId;
+                                               text = n.text;
+                                       }
+                               });
+
+               db.dropTable(UserId.class);
+               db.dropTable(UserNote.class);
+
+               assertEquals(3, notes.size());
+       }
+
+       @IQTable
+       public static class UserId {
+
+               @IQColumn(primaryKey = true)
+               public int id;
+
+               @IQColumn(length = 10)
+               public String name;
+
+               public UserId() {
+                       // public constructor
+               }
+
+               public UserId(int id, String name) {
+                       this.id = id;
+                       this.name = name;
+               }
+
+               public String toString() {
+                       return name + " (" + id + ")";
+               }
+
+               public static List<UserId> getList() {
+                       UserId[] list = { new UserId(1, "Tom"), new UserId(2, "Dick"), new UserId(3, "Harry") };
+                       return Arrays.asList(list);
+               }
+       }
+
+       @IQTable
+       public static class UserNote {
+
+               @IQColumn(autoIncrement = true, primaryKey = true)
+               public int noteId;
+
+               @IQColumn
+               public int userId;
+
+               @IQColumn(length = 10)
+               public String text;
+
+               public UserNote() {
+                       // public constructor
+               }
+
+               public UserNote(int userId, String text) {
+                       this.userId = userId;
+                       this.text = text;
+               }
+
+               public String toString() {
+                       return text;
+               }
+
+               public static List<UserNote> getList() {
+                       UserNote[] list = { new UserNote(1, "A"), new UserNote(2, "B"), new UserNote(3, "C"),
+                                       new UserNote(1, "D"), new UserNote(2, "E"), new UserNote(3, "F"), new UserNote(1, "G"),
+                                       new UserNote(2, "H"), new UserNote(3, "I"), };
+                       return Arrays.asList(list);
+               }
+       }
+}