Browse Source

simple join select (#39)

* simple join select

* test for simple join select

* test for simple join select
tags/release-2.2.1
uchicom 3 years ago
parent
commit
3ae6f66919
No account linked to committer's email address

+ 5
- 1
src/main/java/com/iciql/Query.java View 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 {

+ 11
- 0
src/main/java/com/iciql/TableDefinition.java View 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...

+ 13
- 2
src/test/java/com/iciql/test/JoinTest.java View 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;

Loading…
Cancel
Save