public static void interpretationOfNulls(Connection conn) throws Exception {
final JdbcDataContext dc = new JdbcDataContext(conn);
final Schema schema = dc.getDefaultSchema();
if (dc.getTableByQualifiedLabel("test_table") != null) {
dc.executeUpdate(new DropTable(schema, "test_table"));
}
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback cb) {
ColumnCreationBuilder createTableBuilder = cb.createTable(schema, "test_table").withColumn("id")
.ofType(ColumnType.FLOAT).withColumn("code").ofType(ColumnType.VARCHAR).ofSize(10);
Table table = createTableBuilder.execute();
cb.insertInto(table).value("id", 1.0).value("code", "C01").execute();
cb.insertInto(table).value("id", 2.0).value("code", "C02").execute();
cb.insertInto(table).value("id", 3.0).value("code", null).execute();
cb.insertInto(table).value("id", 4.0).value("code", "C02").execute();
}
});
assertEquals(1, getCount(dc.query().from("test_table").selectCount().where("code").isNull().execute()));
assertEquals(3, getCount(dc.query().from("test_table").selectCount().where("code").isNotNull().execute()));
assertEquals(2, getCount(dc.query().from("test_table").selectCount().where("code").ne("C02").execute()));
// we put the results into a map, because databases are not in agreement
// wrt. if NULL is greater than or less than other values, so ordering
// does not help
final Map<Object, Object> map = new HashMap<Object, Object>();
DataSet ds = dc.query().from("test_table").select("code").selectCount().groupBy("code").execute();
assertTrue(ds.next());
map.put(ds.getRow().getValue(0), ds.getRow().getValue(1));
assertTrue(ds.next());
map.put(ds.getRow().getValue(0), ds.getRow().getValue(1));
assertTrue(ds.next());
map.put(ds.getRow().getValue(0), ds.getRow().getValue(1));
assertFalse(ds.next());
ds.close();
dc.executeUpdate(new DropTable(schema, "test_table"));
assertEquals(1, ((Number) map.get(null)).intValue());
assertEquals(1, ((Number) map.get("C01")).intValue());
assertEquals(2, ((Number) map.get("C02")).intValue());