final Schema schema = dc.getDefaultSchema();
Table readTable = dc.getDefaultSchema().getTableByName("test_table");
assertNull(readTable);
final MutableRef<Table> writtenTableRef = new MutableRef<Table>();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback cb) {
JdbcCreateTableBuilder createTableBuilder = (JdbcCreateTableBuilder) cb.createTable(schema, "test_table");
Table writtenTable = createTableBuilder.withColumn("id").asPrimaryKey().ofType(ColumnType.INTEGER)
.withColumn("name").ofSize(255).ofType(ColumnType.VARCHAR).withColumn("age").ofType(ColumnType.INTEGER)
.execute();
String sql = createTableBuilder.createSqlStatement();
assertEquals("CREATE TABLE PUBLIC.test_table (id INTEGER PRIMARY KEY, name VARCHAR(255), age INTEGER)", sql);
assertNotNull(writtenTable);
assertEquals("[ID, NAME, AGE]", Arrays.toString(writtenTable.getColumnNames()));
writtenTableRef.set(writtenTable);
}
});
assertEquals("[TEST_TABLE]", Arrays.toString(dc.getDefaultSchema().getTableNames()));
readTable = dc.getDefaultSchema().getTableByName("test_table");
assertEquals("[ID, NAME, AGE]", Arrays.toString(readTable.getColumnNames()));
assertEquals("[Column[name=ID,columnNumber=0,type=INTEGER,nullable=false,nativeType=INTEGER,columnSize=10]]",
Arrays.toString(readTable.getPrimaryKeys()));
assertEquals(writtenTableRef.get(), readTable);
assertFalse(conn.isReadOnly());
dc = new JdbcDataContext(conn);
assertSame(conn, dc.getConnection());
readTable = dc.getDefaultSchema().getTableByName("test_table");
assertEquals("[ID, NAME, AGE]", Arrays.toString(readTable.getColumnNames()));
assertTrue(writtenTableRef.get().getQualifiedLabel().equalsIgnoreCase(readTable.getQualifiedLabel()));
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback cb) {
cb.insertInto(writtenTableRef.get()).value("age", 14).value("name", "hello").value("id", 1).execute();
JdbcInsertBuilder insertBuilder = (JdbcInsertBuilder) cb.insertInto(writtenTableRef.get()).value("age", 15)
.value("name", "wor'ld").value("id", 2);
assertEquals("INSERT INTO PUBLIC.\"TEST_TABLE\" (ID,NAME,AGE) VALUES (?,?,?)", insertBuilder.createSqlStatement());
insertBuilder.execute();
cb.insertInto(writtenTableRef.get()).value("age", 16).value("name", "escobar!").value("id", 3).execute();
}
});
DataSet ds = dc.query().from(readTable).select(readTable.getColumns()).orderBy("id").execute();
assertTrue(ds.next());
assertEquals("Row[values=[1, hello, 14]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[2, wor'ld, 15]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[3, escobar!, 16]]", ds.getRow().toString());
assertFalse(ds.next());
ds.close();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
JdbcUpdateBuilder updateCallback = (JdbcUpdateBuilder) callback.update("test_table").value("age", 18).where("id")
.greaterThan(1);
assertEquals("UPDATE PUBLIC.\"TEST_TABLE\" SET AGE=? WHERE \"TEST_TABLE\".\"ID\" > ?",
updateCallback.createSqlStatement());
updateCallback.execute();
}
});
ds = dc.query().from(readTable).select(readTable.getColumns()).orderBy("id").execute();
assertTrue(ds.next());
assertEquals("Row[values=[1, hello, 14]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[2, wor'ld, 18]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[3, escobar!, 18]]", ds.getRow().toString());
assertFalse(ds.next());
ds.close();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.deleteFrom("test_table").where("age").greaterThan(15).execute();
}
});
ds = dc.query().from(readTable).select(readTable.getColumns()).orderBy("id").execute();
assertTrue(ds.next());
assertEquals("Row[values=[1, hello, 14]]", ds.getRow().toString());
assertFalse(ds.next());
ds.close();
assertEquals("[TEST_TABLE]", Arrays.toString(dc.getDefaultSchema().getTableNames()));
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.dropTable("test_table").execute();
}
});