final Schema schema = dc.getDefaultSchema();
assertEquals(0, schema.getTableCount());
final MutableRef<Table> tableRef = new MutableRef<Table>();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback cb) {
Table table = cb.createTable(schema, "foobar").withColumn("foo").withColumn("bar").execute();
tableRef.set(table);
assertEquals(schema, table.getSchema());
assertEquals(schema.getTables()[0], table);
assertTrue(file.exists());
assertEquals("[foo, bar]", Arrays.toString(table.getColumnNames()));
cb.insertInto(table).value(0, "f").value(1, "b").execute();
cb.insertInto(table).value(0, "o").value(table.getColumnByName("bar"), "a").execute();
cb.insertInto(table).value(0, "o").value("bar", "r").execute();
}
});
// query the file to check results
final Table readTable = schema.getTables()[0];
assertEquals(tableRef.get(), readTable);
assertEquals("[foo, bar]", Arrays.toString(readTable.getColumnNames()));
final Query query = dc.query().from(readTable).select("bar").and("foo").toQuery();
DataSet ds = dc.executeQuery(query);
assertTrue(ds.next());
assertEquals("Row[values=[b, f]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[a, o]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[r, o]]", ds.getRow().toString());
assertFalse(ds.next());
// do the same trick on an existing file
dc = new CsvDataContext(file);
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback cb) {
cb.insertInto(tableRef.get()).value("foo", "hello").value("bar", "world").execute();
}
});
ds = dc.executeQuery(query);
assertTrue(ds.next());
assertEquals("Row[values=[b, f]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[a, o]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[r, o]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[world, hello]]", ds.getRow().toString());
assertFalse(ds.next());
ds.close();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.deleteFrom(readTable).where("bar").eq("a").execute();
callback.deleteFrom(readTable).where("bar").eq("r").execute();
}
});
ds = dc.executeQuery(query);
assertTrue(ds.next());
assertEquals("Row[values=[b, f]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[world, hello]]", ds.getRow().toString());
assertFalse(ds.next());
ds.close();
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.update(readTable).value("foo", "universe").execute();
callback.update(readTable).value("bar", "c").where("bar").isEquals("b").execute();
}
});
ds = dc.executeQuery(query);
assertTrue(ds.next());
assertEquals("Row[values=[world, universe]]", ds.getRow().toString());
assertTrue(ds.next());
assertEquals("Row[values=[c, universe]]", ds.getRow().toString());
assertFalse(ds.next());
ds.close();
// drop table
dc.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback callback) {
callback.dropTable(readTable).execute();
}
});