Query q1 = new Query();
q1.from(table1);
q1.select(table1.getColumns());
Query q2 = new Query();
FromItem fromItem = new FromItem(q1);
q2.from(fromItem);
SelectItem selectItem = new SelectItem(q1.getSelectClause().getItems().get(1), fromItem);
selectItem.setAlias("e");
q2.select(selectItem);
assertEquals(
"SELECT name AS e FROM (SELECT contributor.contributor_id, contributor.name, contributor.country FROM MetaModelSchema.contributor)",
q2.toString());
fromItem.setAlias("c");
assertEquals(
"SELECT c.name AS e FROM (SELECT contributor.contributor_id, contributor.name, contributor.country FROM MetaModelSchema.contributor) c",
q2.toString());
DataContext dc = getDataContext();
DataSet data = dc.executeQuery(q2);
assertEquals(1, data.getSelectItems().length);
assertTrue(data.next());
assertEquals("Row[values=[kasper]]", data.getRow().toString());
assertTrue(data.next());
assertEquals("Row[values=[asbjorn]]", data.getRow().toString());
assertTrue(data.next());
assertEquals("Row[values=[johny]]", data.getRow().toString());
assertTrue(data.next());
assertEquals("Row[values=[daniel]]", data.getRow().toString());
assertTrue(data.next());
assertEquals("Row[values=[sasidhar]]", data.getRow().toString());
assertTrue(data.next());
assertEquals("Row[values=[jesper]]", data.getRow().toString());
assertFalse(data.next());
// Create a sub-query for a sub-query
Query q3 = new Query();
fromItem = new FromItem(q2);
q3.from(fromItem);
selectItem = new SelectItem(q2.getSelectClause().getItems().get(0), fromItem);
selectItem.setAlias("f");
q3.select(selectItem);
fromItem.setAlias("d");
assertEquals(
"SELECT d.e AS f FROM (SELECT c.name AS e FROM (SELECT contributor.contributor_id, contributor.name, contributor.country FROM MetaModelSchema.contributor) c) d",
q3.toString());
data = dc.executeQuery(q3);
assertEquals(1, data.getSelectItems().length);