From: uchicom <2980920+uchicom@users.noreply.github.com> Date: Sun, 2 May 2021 16:09:41 +0000 (+0900) Subject: simple join select (#39) X-Git-Tag: release-2.2.1~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3ae6f66919787d279a78818605126bcdec8f9139;p=iciql.git simple join select (#39) * simple join select * test for simple join select * test for simple join select --- diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index d1528e8..e9404ec 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -291,7 +291,11 @@ public class Query { List result = Utils.newArrayList(); TableDefinition 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 { diff --git a/src/main/java/com/iciql/TableDefinition.java b/src/main/java/com/iciql/TableDefinition.java index fe273c0..05cdbf6 100644 --- a/src/main/java/com/iciql/TableDefinition.java +++ b/src/main/java/com/iciql/TableDefinition.java @@ -1150,6 +1150,17 @@ public class TableDefinition { } } + 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); + } + } + void appendSelectList(SQLStatement stat, Query query, X x) { // select t0.col1, t0.col2, t0.col3... // select table1.col1, table1.col2, table1.col3... diff --git a/src/test/java/com/iciql/test/JoinTest.java b/src/test/java/com/iciql/test/JoinTest.java index e3370b1..77523c5 100644 --- a/src/test/java/com/iciql/test/JoinTest.java +++ b/src/test/java/com/iciql/test/JoinTest.java @@ -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 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;