Thursday 13 June 2013

Dropwizard / JDBI String Templating

At first glance the documentation seems straight forward, but with some updates to dropwizard framework and some undocumented additions to JDBI

Based on Brian McCallisters' original blog post: http://skife.org/jdbi/java/2011/12/21/jdbi_in_clauses.html


@UseStringTemplate3StatementLocator("/sql/templates/TestDao.sql.stg")
public interface TestDao {

 @SqlQuery
 Iterator getDevicesWithInClause(
   @BindIn("guids") List guids,
   @Bind("limit") Integer limit);
}


The main pieces of information that I had to hunt around for are:

  • @UseStringTemplate3StatementLocator replaces the now deprecated @ExternalizedSqlViaStringTemplate3
  • you can pass a value to the @UseStringTemplate3StatementLocator which is the location (on the classpath) of the string template file.
  • In your string template file, for normal @Bind parameters you still use the standard colonprefix
sql/templates/TestDao.sql.stg

group TestDao;

getDevicesWithInClause(guids, limit) ::= <<
 select distinct(guid) from table where guid in () limit :limit
>>