Skip to content

Instantly share code, notes, and snippets.

@pierre
Created April 9, 2013 16:58
Show Gist options
  • Save pierre/5347402 to your computer and use it in GitHub Desktop.
Save pierre/5347402 to your computer and use it in GitHub Desktop.
JDBI StringTemplate Locator
public class TestFoo {
@Test
public void testFoo() throws Exception {
final DBI dbi = new DBI("jdbc:h2:mem:");
dbi.onDemand(HoneyBadger.class).inTransaction(new Transaction<Object, HoneyBadger>() {
@Override
public Object inTransaction(final HoneyBadger transactional, final TransactionStatus status) throws Exception {
transactional.create("something");
transactional.delete("something");
transactional.delete("something");
return null;
}
});
}
@UseStringTemplate3StatementLocator("/com/acme/foo.sql.stg")
private static interface HoneyBadger extends Transactional<HoneyBadger> {
@SqlUpdate
public void create(@Define("tableName") String table);
@SqlUpdate
public void delete(@Define("tableName") String table);
}
}
-> pass
public class TestFoo {
@Test
public void testFoo() throws Exception {
final DBI dbi = new DBI("jdbc:h2:mem:");
dbi.setStatementLocator(new StringTemplate3StatementLocator("/com/acme/foo.sql.stg"));
dbi.onDemand(HoneyBadger.class).inTransaction(new Transaction<Object, HoneyBadger>() {
@Override
public Object inTransaction(final HoneyBadger transactional, final TransactionStatus status) throws Exception {
transactional.create("something");
transactional.delete("something");
transactional.delete("something");
return null;
}
});
}
private static interface HoneyBadger extends Transactional<HoneyBadger> {
@SqlUpdate
public void create(@Define("tableName") String table);
@SqlUpdate
public void delete(@Define("tableName") String table);
}
}
-> delete from somethingsomething; [42102-158] [statement:"delete", located:"delete from somethingsomething;", rewritten:"delete from somethingsomething;", arguments:{ positional:{}, named:{}, finder:[]}]
From StringTemplate.java
/** Set an attribute for this template. If you set the same
* attribute more than once, you get a multi-valued attribute.
* If you send in a StringTemplate object as a value, it's
* enclosing instance (where it will inherit values from) is
* set to 'this'. This would be the normal case, though you
* can set it back to null after this call if you want.
* If you send in a List plus other values to the same
* attribute, they all get flattened into one List of values.
* This will be a new list object so that incoming objects are
* not altered.
* If you send in an array, it is converted to an ArrayIterator.
*/
public void setAttribute(String name, Object value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment