}
public static void createInsertAndUpdateDateTypes(final JdbcDataContext dc, final Schema schema, final String tableName)
throws Exception {
if (schema.getTableByName(tableName) != null) {
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.dropTable(schema.getTableByName(tableName)).execute();
}
});
}
dc.executeUpdate(new BatchUpdateScript() {
@Override
public void run(UpdateCallback cb) {
Table table = cb.createTable(schema, tableName).withColumn("id").asPrimaryKey().ofType(ColumnType.INTEGER)
.withColumn("birthdate").ofType(ColumnType.DATE).withColumn("wakemeup").ofType(ColumnType.TIME).execute();
// insert record 1
{
// create a 7:55 time.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(Calendar.HOUR_OF_DAY, 7);
cal.set(Calendar.MINUTE, 55);
Date wakeUpTime = cal.getTime();
cb.insertInto(table).value("id", 1).value("birthdate", DateUtils.get(1982, Month.APRIL, 20))
.value("wakemeup", wakeUpTime).execute();
}
// insert record 2
{
// create a 18:35 time.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 35);
Date wakeUpTime = cal.getTime();
cb.insertInto(table).value("id", 2).value("birthdate", DateUtils.get(1982, Month.APRIL, 21))
.value("wakemeup", wakeUpTime).execute();
}
}
});
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback cb) {
cb.insertInto(schema.getTableByName(tableName)).value("id", 3).value("birthdate", "2011-12-21")
.value("wakemeup", "12:00").execute();
}
});
DataSet ds = dc.query().from(schema.getTableByName(tableName)).select("id", "birthdate", "wakemeup").orderBy("id")
.execute();
assertTrue(ds.next());
assertEquals("1", ds.getRow().getValue(0).toString());
assertEquals("1982-04-20", ds.getRow().getValue(1).toString());
assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("07:55:00"));
assertTrue(ds.next());
assertEquals("2", ds.getRow().getValue(0).toString());
assertEquals("1982-04-21", ds.getRow().getValue(1).toString());
assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("18:35:00"));
assertTrue(ds.next());
assertEquals("3", ds.getRow().getValue(0).toString());
assertEquals("2011-12-21", ds.getRow().getValue(1).toString());
assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("12:00"));
assertFalse(ds.next());
ds.close();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
// update record 1
// create a 08:00 time.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 00);
Date wakeUpTime = cal.getTime();
callback.update(schema.getTableByName(tableName)).value("birthdate", DateUtils.get(1982, Month.APRIL, 21))
.value("wakemeup", wakeUpTime).where("birthdate").isEquals(DateUtils.get(1982, Month.APRIL, 20))
.execute();
}
});
ds = dc.query().from(schema.getTableByName(tableName)).select("id", "birthdate", "wakemeup").orderBy("id").execute();
assertTrue(ds.next());
assertEquals("1", ds.getRow().getValue(0).toString());
assertEquals("1982-04-21", ds.getRow().getValue(1).toString());
assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("08:00:00"));
assertTrue(ds.next());
assertEquals("2", ds.getRow().getValue(0).toString());
assertEquals("1982-04-21", ds.getRow().getValue(1).toString());
assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("18:35:00"));
assertTrue(ds.next());
assertEquals("3", ds.getRow().getValue(0).toString());
assertEquals("2011-12-21", ds.getRow().getValue(1).toString());
assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("12:00"));
assertFalse(ds.next());
ds.close();
if (schema.getTableByName(tableName) != null) {
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.dropTable(schema.getTableByName(tableName)).execute();
}
});