@Override @Nullable public DBI apply(@Nullable DBI input) { if (input == null) { return null; } input.setStatementLocator(new AnnotatedStatementLocator(annotatedClass, input.getStatementLocator())); return input; } }
public static DBI getDBI(Class<?> clazz, DataSource datasource) { DBI dbi = new DBI(datasource); final ClasspathGroupLoader theLoader = new ClasspathGroupLoader(AngleBracketTemplateLexer.class, clazz.getPackage().getName() .replaceAll("\\.", "/")); dbi.setStatementLocator(new StatementLocator() { private final StringTemplateGroupLoader loader = theLoader; public String locate(String name, StatementContext ctx) throws Exception { if (ClasspathStatementLocator.looksLikeSql(name)) { return name; } final StringTokenizer tok = new StringTokenizer(name, ":"); final String group_name = tok.nextToken(); final String template_name = tok.nextToken(); final StringTemplateGroup group = loader.loadGroup(group_name); final StringTemplate template = group.getInstanceOf(template_name); template.setAttributes(ctx.getAttributes()); return template.toString(); } }); return dbi; }
/** * See org.springframework.beans.factory.FactoryBean#getObject */ public Object getObject() throws Exception { final DBI dbi = new DBI(new SpringDataSourceConnectionFactory(dataSource)); if (statementLocator != null) { dbi.setStatementLocator(statementLocator); } for (Map.Entry<String, Object> entry : globalDefines.entrySet()) { dbi.define(entry.getKey(), entry.getValue()); } return dbi; }
/** * See org.springframework.beans.factory.FactoryBean#getObject */ @Override public Object getObject() throws Exception { final DBI dbi = new DBI(new SpringDataSourceConnectionFactory(dataSource)); if (statementLocator != null) { dbi.setStatementLocator(statementLocator); } for (Map.Entry<String, Object> entry : globalDefines.entrySet()) { dbi.define(entry.getKey(), entry.getValue()); } return dbi; }
@Test(groups = "slow") public void testMultipleInvocationsWithoutLiterals() throws IOException { dbi.setStatementLocator(new ReusableStringTemplate3StatementLocator("/org/killbill/commons/jdbi/SomethingNonLiteralSqlDao.sql.stg", true, true)); final SomethingNonLiteralSqlDao somethingNonLiteralSqlDao = dbi.onDemand(SomethingNonLiteralSqlDao.class); somethingNonLiteralSqlDao.delete(TABLE_NAME); somethingNonLiteralSqlDao.delete(TABLE_NAME); }
@Test(groups = "slow") public void testMultipleInvocationsWithLiterals() throws IOException { dbi.setStatementLocator(new ReusableStringTemplate3StatementLocator(SomethingLiteralSqlDao.class, true, true)); final SomethingLiteralSqlDao somethingLiteralSqlDao = dbi.onDemand(SomethingLiteralSqlDao.class); somethingLiteralSqlDao.delete(TABLE_NAME); somethingLiteralSqlDao.delete(TABLE_NAME); }
@Test(groups = "slow") public void testMultipleInvocationsWithLiterals() throws IOException { dbi.setStatementLocator(new ReusableStringTemplate3StatementLocator(SomethingLiteralSqlDao.class, true, true)); final SomethingLiteralSqlDao somethingLiteralSqlDao = dbi.onDemand(SomethingLiteralSqlDao.class); somethingLiteralSqlDao.delete(TABLE_NAME); somethingLiteralSqlDao.delete(TABLE_NAME); }
@Test(groups = "slow") public void testMultipleInvocationsWithoutLiterals() throws IOException { dbi.setStatementLocator(new ReusableStringTemplate3StatementLocator("/com/ning/billing/commons/jdbi/SomethingNonLiteralSqlDao.sql.stg", true, true)); final SomethingNonLiteralSqlDao somethingNonLiteralSqlDao = dbi.onDemand(SomethingNonLiteralSqlDao.class); somethingNonLiteralSqlDao.delete(TABLE_NAME); somethingNonLiteralSqlDao.delete(TABLE_NAME); }
@Test @Category(JDBITests.class) public void testFoo() throws Exception { DBI dbi = new DBI(h2); dbi.setStatementLocator(ST4StatementLocator.fromClasspath("/explicit/sql.stg")); Dao dao = dbi.onDemand(Dao.class); dao.create(); dao.insert(1, "Brian"); String brian = dao.findNameById(1); assertThat(brian).isEqualTo("Brian"); }
@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(JDBITests.class) public void testFluent() throws Exception { DBI dbi = new DBI(h2); dbi.setStatementLocator(ST4StatementLocator.fromClasspath("/org/skife/jdbi/v2/st4/ExampleTest.Dao.sql.stg")); dbi.withHandle(new HandleCallback<Object>() { @Override public Object withHandle(final Handle h) throws Exception { h.execute("createSomethingTable"); int numCreated = h.createStatement("insertSomething") .bind("0", 0) .bind("1", "Jan") .execute(); assertThat(numCreated).as("number of rows inserted").isEqualTo(1); String name = h.createQuery("findById") .bind("0", 0) .define("columns", "name") .mapTo(String.class) .first(); assertThat(name).as("Jan's Name").isEqualTo("Jan"); 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"); }
@Test @Category(JDBITests.class) public void testFoo() throws Exception { final DBI dbi = new DBI(this.h2.getDataSource()); dbi.setStatementLocator(new ST4StatementLocator(new STGroupFile("org/skife/jdbi/v2/st4/DaoTest.InnerDao.sql.stg"))); dbi.withHandle(new HandleCallback<Object>() { @Override public Object withHandle(final Handle h) throws Exception { h.execute("createSomething"); h.createStatement("insert") .define("table", "something") .bind("id", 1) .bind("name", "Ven") .execute(); final Something s = h.createQuery("findById") .bind("id", 1) .map(new ResultSetMapper<Something>() { @Override public Something map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException { return new Something(r.getInt("id"), r.getString("name")); } }) .first(); assertThat(s).isEqualTo(new Something(1, "Ven")); return null; } }); } }