public void testExecuteQueryInPublicSchema() throws Exception {
if (!isConfigured()) {
return;
}
DataContext dc = new JdbcDataContext(getConnection());
Query q = new Query();
Schema schema = dc.getSchemaByName("public");
Table productsTable = schema.getTableByName("products");
q.from(productsTable);
Column titleColumn = productsTable.getColumnByName("title");
Column productPriceColumn = productsTable.getColumnByName("price");
q.select(titleColumn, productPriceColumn);
q.getSelectClause().getItem(0).setAlias("product-title");
DataSet data = dc.executeQuery(q);
TableModel tableModel = new DataSetTableModel(data);
assertEquals(2, tableModel.getColumnCount());
assertEquals(10000, tableModel.getRowCount());
assertEquals("ACADEMY ACADEMY", tableModel.getValueAt(0, 0).toString());
assertEquals("25.99", tableModel.getValueAt(0, 1).toString());
assertEquals("ACADEMY HORN", tableModel.getValueAt(432, 0).toString());
assertEquals("16.99", tableModel.getValueAt(6346, 1).toString());
assertEquals("ALADDIN ZORRO", tableModel.getValueAt(9999, 0).toString());
assertEquals("10.99", tableModel.getValueAt(9999, 1).toString());
data = null;
tableModel = null;
Column prodIdColumn = productsTable.getColumnByName("prod_id");
Table orderlinesTable = schema.getTableByName("orderlines");
Column commonProdIdColumn = orderlinesTable.getColumnByName("prod_id");
Column quantityColumn = orderlinesTable.getColumnByName("quantity");
q.from(orderlinesTable);
q.where(new FilterItem(new SelectItem(prodIdColumn), OperatorType.EQUALS_TO, new SelectItem(commonProdIdColumn)));
q.groupBy(titleColumn);
q.getSelectClause().removeItem(q.getSelectClause().getSelectItem(productPriceColumn));
SelectItem quantitySum = new SelectItem(FunctionType.SUM, quantityColumn).setAlias("orderAmount");
q.select(quantitySum);
q.having(new FilterItem(quantitySum, OperatorType.GREATER_THAN, 25));
q.orderBy(new OrderByItem(q.getSelectClause().getItem(0)));
assertEquals("SELECT \"products\".\"title\" AS product-title, SUM(\"orderlines\".\"quantity\") AS orderAmount "
+ "FROM public.\"products\", public.\"orderlines\" "
+ "WHERE \"products\".\"prod_id\" = \"orderlines\".\"prod_id\" " + "GROUP BY \"products\".\"title\" "
+ "HAVING SUM(\"orderlines\".\"quantity\") > 25 " + "ORDER BY \"products\".\"title\" ASC", q.toString());
data = dc.executeQuery(q);
tableModel = new DataSetTableModel(data);
assertEquals(2, tableModel.getColumnCount());
assertEquals(136, tableModel.getRowCount());
assertEquals("ACADEMY ALABAMA", tableModel.getValueAt(0, 0).toString());