]> source.dussan.org Git - iciql.git/commitdiff
simple join select (#39)
authoruchicom <2980920+uchicom@users.noreply.github.com>
Sun, 2 May 2021 16:09:41 +0000 (01:09 +0900)
committerGitHub <noreply@github.com>
Sun, 2 May 2021 16:09:41 +0000 (12:09 -0400)
* simple join select

* test for simple join select

* test for simple join select

src/main/java/com/iciql/Query.java
src/main/java/com/iciql/TableDefinition.java
src/test/java/com/iciql/test/JoinTest.java

index d1528e8c7a6bcc2c18cab3e5a21f97bdd2c068c9..e9404ec0c9bd3417ab1a38745b45ef8389698c5f 100644 (file)
@@ -291,7 +291,11 @@ public class Query<T> {
         List<T> result = Utils.newArrayList();
         TableDefinition<T> def = from.getAliasDefinition();
         SQLStatement stat = getSelectStatement(distinct);
-        def.appendSelectList(stat);
+        if (isJoin()) {
+            def.appendSelectList(stat, from.getAs());
+        } else {
+            def.appendSelectList(stat);
+        }
         appendFromWhere(stat);
         ResultSet rs = stat.executeQuery();
         try {
index fe273c07a04c8ea8a2762208af37848f88c951cd..05cdbf692261fd938c7225da0a7fa57453b0faa3 100644 (file)
@@ -1150,6 +1150,17 @@ public class TableDefinition<T> {
         }
     }
 
+    void appendSelectList(SQLStatement stat, String as) {
+        for (int i = 0; i < fields.size(); i++) {
+            if (i > 0) {
+                stat.appendSQL(", ");
+            }
+            stat.appendSQL(as + ".");
+            FieldDefinition def = fields.get(i);
+            stat.appendColumn(def.columnName);
+        }
+    }
+
     <Y, X> void appendSelectList(SQLStatement stat, Query<Y> query, X x) {
         // select t0.col1, t0.col2, t0.col3...
         // select table1.col1, table1.col2, table1.col3...
index e3370b1dea3a7ab384ee49c9fcfb30a59bf064a2..77523c5d06402fa88a3145db9c53e49872a58f90 100644 (file)
@@ -58,13 +58,24 @@ public class JoinTest {
                 .select(new UserNote() {
                     {
                         userId = n.userId;
-                        noteId = n.noteId;
+                        id = n.id;
                         text = n.text;
                     }
                 });
         assertEquals(3, notes.size());
     }
 
+    @Test
+    public void testPrimitiveJoinAndSimpleSelect() throws Exception {
+        final UserId u = new UserId();
+        final UserNote n = new UserNote();
+
+        List<UserId> userIds = db.from(u).innerJoin(n).on(u.id).is(n.userId).where(n.text).is("D")
+                .select();
+        assertEquals(1, userIds.size());
+        assertEquals(1, userIds.get(0).id);
+    }
+
     @Test
     public void testJoin() throws Exception {
         final UserId u = new UserId();
@@ -140,7 +151,7 @@ public class JoinTest {
     public static class UserNote {
 
         @IQColumn(autoIncrement = true, primaryKey = true)
-        public int noteId;
+        public int id;
 
         @IQColumn
         public int userId;