*
* @throws Exception
*/
public void testCsvQueryResultComparison() throws Exception {
Connection connection = getTestDbConnection();
final DataContext dataContext1 = new JdbcDataContext(connection);
final DataContext dataContext2 = new QueryPostprocessDataContext() {
@Override
public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
Query q = new Query();
q.from(table, "a");
q.select(columns);
return dataContext1.executeQuery(q);
}
@Override
protected Schema getMainSchema() throws MetaModelException {
throw new UnsupportedOperationException();
}
@Override
protected String getMainSchemaName() throws MetaModelException {
return "PUBLIC";
}
};
Schema schema2 = dataContext1.getDefaultSchema();
Table customersTable = schema2.getTableByName("CUSTOMERS");
Table employeeTable = schema2.getTableByName("EMPLOYEES");
assertEquals(
"[Column[name=CUSTOMERNUMBER,columnNumber=0,type=INTEGER,nullable=false,nativeType=INTEGER,columnSize=0], "
+ "Column[name=CUSTOMERNAME,columnNumber=1,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=CONTACTLASTNAME,columnNumber=2,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=CONTACTFIRSTNAME,columnNumber=3,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=PHONE,columnNumber=4,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=ADDRESSLINE1,columnNumber=5,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=ADDRESSLINE2,columnNumber=6,type=VARCHAR,nullable=true,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=CITY,columnNumber=7,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=STATE,columnNumber=8,type=VARCHAR,nullable=true,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=POSTALCODE,columnNumber=9,type=VARCHAR,nullable=true,nativeType=VARCHAR,columnSize=15], "
+ "Column[name=COUNTRY,columnNumber=10,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=SALESREPEMPLOYEENUMBER,columnNumber=11,type=INTEGER,nullable=true,nativeType=INTEGER,columnSize=0], "
+ "Column[name=CREDITLIMIT,columnNumber=12,type=NUMERIC,nullable=true,nativeType=NUMERIC,columnSize=17]]",
Arrays.toString(customersTable.getColumns()));
assertEquals(
"[Column[name=EMPLOYEENUMBER,columnNumber=0,type=INTEGER,nullable=false,nativeType=INTEGER,columnSize=0], "
+ "Column[name=LASTNAME,columnNumber=1,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=FIRSTNAME,columnNumber=2,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50], "
+ "Column[name=EXTENSION,columnNumber=3,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=10], "
+ "Column[name=EMAIL,columnNumber=4,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=100], "
+ "Column[name=OFFICECODE,columnNumber=5,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=20], "
+ "Column[name=REPORTSTO,columnNumber=6,type=INTEGER,nullable=true,nativeType=INTEGER,columnSize=0], "
+ "Column[name=JOBTITLE,columnNumber=7,type=VARCHAR,nullable=false,nativeType=VARCHAR,columnSize=50]]",
Arrays.toString(employeeTable.getColumns()));
Column employeeNumberColumn1 = customersTable.getColumnByName("SALESREPEMPLOYEENUMBER");
Column countryColumn = customersTable.getColumnByName("COUNTRY");
Column employeeNumberColumn2 = employeeTable.getColumnByName("EMPLOYEENUMBER");
Column creditLimitColumn = customersTable.getColumnByName("CREDITLIMIT");
Query q = new Query();
q.from(customersTable, "c");
q.from(employeeTable, "o");
SelectItem countrySelect = new SelectItem(countryColumn);
q.select(countrySelect, new SelectItem(FunctionType.SUM, creditLimitColumn));
q.groupBy(countryColumn);
q.orderBy(new OrderByItem(countrySelect));
q.where(new FilterItem(new SelectItem(employeeNumberColumn1), OperatorType.EQUALS_TO, new SelectItem(
employeeNumberColumn2)));
assertEquals(
"SELECT c.\"COUNTRY\", SUM(c.\"CREDITLIMIT\") FROM PUBLIC.\"CUSTOMERS\" c, PUBLIC.\"EMPLOYEES\" o WHERE c.\"SALESREPEMPLOYEENUMBER\" = o.\"EMPLOYEENUMBER\" GROUP BY c.\"COUNTRY\" ORDER BY c.\"COUNTRY\" ASC",
q.toString());
DataSet data1 = dataContext1.executeQuery(q);
assertTrue(data1.next());
assertEquals("Row[values=[Australia, 430300.0]]", data1.getRow().toString());
DataSet data2 = dataContext2.executeQuery(q);
assertTrue(data2.next());
assertEquals("Row[values=[Australia, 430300.0]]", data2.getRow().toString());
assertEquals(new DataSetTableModel(data1), new DataSetTableModel(data2));
}