/** * Create a statement locator intended for setting on a DBI or Handle instance which will * lookup a template group to use based on the name of the sql object type for a particular query, using * the same logic as {@link UseST4StatementLocator}. * <p> * Supports a fallback template group for statements created not using a sql object. */ public static StatementLocator perType(final UseSTGroupCache useCache, final String fallbackTemplateGroupPath) { return perType(useCache, ST4StatementLocator.class.getResource(fallbackTemplateGroupPath)); }
/** * Create a statement locator intended for setting on a DBI or Handle instance which will * lookup a template group to use based on the name of the sql object type for a particular query, using * the same logic as {@link UseST4StatementLocator}. */ public static StatementLocator perType(final UseSTGroupCache useCache) { return perType(useCache, new STGroup('<', '>')); }
/** * Create a statement locator intended for setting on a DBI or Handle instance which will * lookup a template group to use based on the name of the sql object type for a particular query, using * the same logic as {@link UseST4StatementLocator}. * <p> * Supports a fallback template group for statements created not using a sql object. */ public static StatementLocator perType(final UseSTGroupCache useCache, final URL baseTemplate) { return perType(useCache, urlToSTGroup(baseTemplate)); }
@Test @Category(JDBITests.class) public void testFallbackTemplate() throws Exception { StatementLocator sl = ST4StatementLocator.perType(ST4StatementLocator.UseSTGroupCache.YES, "/explicit/sql.stg"); DBI dbi = new DBI(h2); dbi.setStatementLocator(sl); dbi.withHandle(new HandleCallback<Object>() { @Override public Object withHandle(final Handle h) throws Exception { h.execute("create"); h.execute("insert", 1, "Brian"); String brian = h.createQuery("findNameById").bind("0", 1).mapTo(String.class).first(); assertThat(brian).isEqualTo("Brian"); return null; } }); }
@Test @Category(JDBIQuarantineTests.class) public void testNoFallback() throws Exception { StatementLocator sl = ST4StatementLocator.perType(ST4StatementLocator.UseSTGroupCache.YES); DBI dbi = new DBI(h2); dbi.setStatementLocator(sl); dbi.withHandle(new HandleCallback<Object>() { @Override public Object withHandle(final Handle h) throws Exception { h.createStatement("create table <name> (id int primary key, name text)") .define("name", "something") .execute(); h.execute("insert into something (id, name) values (3, 'Carlos')"); return null; } }); Dao dao = dbi.onDemand(Dao.class); dao.insertFixtures(); Something francisco = dao.findById(1); assertThat(francisco.getName()).isEqualTo("Francisco"); }