Schema schema = dc.getDefaultSchema();
Table table = schema.getTableByName("projects_project");
Query q = new Query().select(table.getColumns()).from(table, "p");
assertEquals("SELECT p.id, p.project, p.name FROM xml_input_eobjects.xml.projects_project p", q.toString());
DataSet data = dc.executeQuery(q);
TableModel tableModel = new DataSetTableModel(data);
assertEquals(3, tableModel.getColumnCount());
assertEquals(2, tableModel.getRowCount());
// ID
assertEquals(1, tableModel.getValueAt(0, 0));
assertEquals(2, tableModel.getValueAt(1, 0));
// Project (text content)
assertEquals("Some description", tableModel.getValueAt(0, 1));
assertNull(tableModel.getValueAt(1, 1));
// Name
assertEquals("datacleaner", tableModel.getValueAt(0, 2));
assertEquals("MetaModel", tableModel.getValueAt(1, 2));
// Make a new query that joins the normalized tables together
table = schema.getTableByName("contributors_person_address");
Relationship relationShip = table.getRelationships()[0];
q = new Query().select(relationShip.getPrimaryTable().getColumns())
.select(relationShip.getForeignTable().getColumns()).from(new FromItem(JoinType.INNER, relationShip));
assertEquals(
"SELECT contributors_person.id, contributors_person_address.id, "
+ "contributors_person_address.contributors_person_id, contributors_person_address.address "
+ "FROM xml_input_eobjects.xml.contributors_person INNER JOIN xml_input_eobjects.xml.contributors_person_address "
+ "ON contributors_person.id = contributors_person_address.contributors_person_id",
q.toString());
data = dc.executeQuery(q);
tableModel = new DataSetTableModel(data);
assertEquals(4, tableModel.getColumnCount());
assertEquals(4, tableModel.getRowCount());
assertEquals("1", tableModel.getValueAt(0, 0).toString());
assertEquals("1", tableModel.getValueAt(0, 1).toString());
assertEquals("1", tableModel.getValueAt(0, 2).toString());
assertEquals("My address", tableModel.getValueAt(0, 3).toString());
assertEquals("1", tableModel.getValueAt(1, 0).toString());
assertEquals("2", tableModel.getValueAt(1, 1).toString());
assertEquals("1", tableModel.getValueAt(1, 2).toString());
assertEquals("Another address", tableModel.getValueAt(1, 3).toString());
assertEquals("1", tableModel.getValueAt(2, 0).toString());
assertEquals("3", tableModel.getValueAt(2, 1).toString());
assertEquals("1", tableModel.getValueAt(2, 2).toString());
assertEquals("A third address", tableModel.getValueAt(2, 3).toString());
assertEquals("2", tableModel.getValueAt(3, 0).toString());
assertEquals("4", tableModel.getValueAt(3, 1).toString());
assertEquals("2", tableModel.getValueAt(3, 2).toString());
assertEquals("Asbjorns address", tableModel.getValueAt(3, 3).toString());
}