Skip to content

Instantly share code, notes, and snippets.

@brianm
Created July 17, 2015 16:44
Show Gist options
  • Save brianm/efad2014e91e7abbaa16 to your computer and use it in GitHub Desktop.
Save brianm/efad2014e91e7abbaa16 to your computer and use it in GitHub Desktop.
package org.skife.jdbi.v2.sqlobject;
import junit.framework.TestCase;
import org.h2.jdbcx.JdbcDataSource;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.Something;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.skife.jdbi.v2.sqlobject.stringtemplate.UseStringTemplate3StatementLocator;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.sql.SQLException;
public class TestDynamicDefineOnClass extends TestCase
{
private DBI dbi;
private Handle handle;
@Override
public void setUp() throws Exception
{
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test");
dbi = new DBI(ds);
handle = dbi.open();
handle.execute("create table something (id int primary key, name varchar(100))");
}
@Override
public void tearDown() throws Exception
{
handle.execute("drop table something");
handle.close();
}
public void testFoo() throws Exception
{
Dao dao = dbi.onDemand(Dao.class);
dao.tableName = "something";
dao.insert(1, "Brian");
Something s = dao.find(1);
assertEquals(s.getName(), "Brian");
}
@DynamicDefine(key = "table", attribute = "tableName")
@UseStringTemplate3StatementLocator
@RegisterMapper(SomethingMapper.class)
public static abstract class Dao
{
public volatile String tableName = "SET ME!";
@SqlUpdate("insert into <table> (id, name) values (:id, :name)")
public abstract int insert(@Bind("id") int id, @Bind("name") String name);
@SqlQuery("select id, name from <table> where id = :id")
public abstract Something find(@Bind("id") int id);
}
@SqlStatementCustomizingAnnotation(DynamicDefine.DynamicDefineFactory.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DynamicDefine
{
String key();
String attribute();
class DynamicDefineFactory implements SqlStatementCustomizerFactory
{
@Override
public SqlStatementCustomizer createForType(final Annotation annotation, final Class sqlObjectType)
{
return new SqlStatementCustomizer()
{
@Override
public void apply(final SQLStatement q) throws SQLException
{
DynamicDefine dd = (DynamicDefine) annotation;
// need to figure out how to get reference to the dao instance. Not sure.
q.define(dd.key(), "something");
}
};
}
@Override
public SqlStatementCustomizer createForMethod(final Annotation annotation, final Class sqlObjectType, final Method method)
{
throw new UnsupportedOperationException("Not supported");
}
@Override
public SqlStatementCustomizer createForParameter(final Annotation annotation, final Class sqlObjectType, final Method method, final Object arg)
{
throw new UnsupportedOperationException("Not supported");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment