Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package org.skife.jdbi.v2.sqlobject;
  2.  
  3. import junit.framework.TestCase;
  4. import org.h2.jdbcx.JdbcDataSource;
  5. import org.skife.jdbi.v2.DBI;
  6. import org.skife.jdbi.v2.Handle;
  7. import org.skife.jdbi.v2.SQLStatement;
  8. import org.skife.jdbi.v2.Something;
  9. import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
  10. import org.skife.jdbi.v2.sqlobject.stringtemplate.UseStringTemplate3StatementLocator;
  11.  
  12. import java.lang.annotation.Annotation;
  13. import java.lang.annotation.ElementType;
  14. import java.lang.annotation.Retention;
  15. import java.lang.annotation.RetentionPolicy;
  16. import java.lang.annotation.Target;
  17. import java.lang.reflect.Method;
  18. import java.sql.SQLException;
  19.  
  20. public class TestDynamicDefineOnClass extends TestCase
  21. {
  22. private DBI dbi;
  23. private Handle handle;
  24.  
  25. @Override
  26. public void setUp() throws Exception
  27. {
  28. JdbcDataSource ds = new JdbcDataSource();
  29. ds.setURL("jdbc:h2:mem:test");
  30. dbi = new DBI(ds);
  31. handle = dbi.open();
  32.  
  33. handle.execute("create table something (id int primary key, name varchar(100))");
  34. }
  35.  
  36. @Override
  37. public void tearDown() throws Exception
  38. {
  39. handle.execute("drop table something");
  40. handle.close();
  41. }
  42.  
  43.  
  44. public void testFoo() throws Exception
  45. {
  46. Dao dao = dbi.onDemand(Dao.class);
  47. dao.tableName = "something";
  48.  
  49. dao.insert(1, "Brian");
  50. Something s = dao.find(1);
  51. assertEquals(s.getName(), "Brian");
  52. }
  53.  
  54. @DynamicDefine(key = "table", attribute = "tableName")
  55. @UseStringTemplate3StatementLocator
  56. @RegisterMapper(SomethingMapper.class)
  57. public static abstract class Dao
  58. {
  59. public volatile String tableName = "SET ME!";
  60.  
  61. @SqlUpdate("insert into <table> (id, name) values (:id, :name)")
  62. public abstract int insert(@Bind("id") int id, @Bind("name") String name);
  63.  
  64. @SqlQuery("select id, name from <table> where id = :id")
  65. public abstract Something find(@Bind("id") int id);
  66. }
  67.  
  68. @SqlStatementCustomizingAnnotation(DynamicDefine.DynamicDefineFactory.class)
  69. @Target(ElementType.TYPE)
  70. @Retention(RetentionPolicy.RUNTIME)
  71. public @interface DynamicDefine
  72. {
  73. String key();
  74.  
  75. String attribute();
  76.  
  77. class DynamicDefineFactory implements SqlStatementCustomizerFactory
  78. {
  79.  
  80. @Override
  81. public SqlStatementCustomizer createForType(final Annotation annotation, final Class sqlObjectType)
  82. {
  83. return new SqlStatementCustomizer()
  84. {
  85. @Override
  86. public void apply(final SQLStatement q) throws SQLException
  87. {
  88. DynamicDefine dd = (DynamicDefine) annotation;
  89.  
  90. // need to figure out how to get reference to the dao instance. Not sure.
  91. q.define(dd.key(), "something");
  92. }
  93. };
  94. }
  95.  
  96. @Override
  97. public SqlStatementCustomizer createForMethod(final Annotation annotation, final Class sqlObjectType, final Method method)
  98. {
  99. throw new UnsupportedOperationException("Not supported");
  100. }
  101.  
  102. @Override
  103. public SqlStatementCustomizer createForParameter(final Annotation annotation, final Class sqlObjectType, final Method method, final Object arg)
  104. {
  105. throw new UnsupportedOperationException("Not supported");
  106. }
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement