@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { registry.get(SqlObjects.class).setSqlLocator(UseClasspathSqlLocatorImpl::locate); }
@Override public SqlObjects createCopy() { return new SqlObjects(this); } }
private static ParameterCustomizerFactory getDefaultParameterCustomizerFactory(SqlStatement<?> stmt) { return stmt.getConfig(SqlObjects.class).getDefaultParameterCustomizerFactory(); }
@Before public void setUp() { Jdbi db = dbRule.getJdbi(); ParameterCustomizerFactory defaultParameterCustomizerFactory = (sqlObjectType, method, param, index, type) -> { invocationCounter.incrementAndGet(); return (stmt, arg) -> stmt.bind("mybind" + index, arg); }; db.configure(SqlObjects.class, c -> c.setDefaultParameterCustomizerFactory(defaultParameterCustomizerFactory)); handle = db.open(); }
String locateSql(final Handle h) { return h.getConfig(SqlObjects.class).getSqlLocator().locate(sqlObjectType, method, h.getConfig()); }
@Before public void setUp() { Jdbi db = dbRule.getJdbi(); ParameterCustomizerFactory defaultParameterCustomizerFactory = (sqlObjectType, method, param, index, type) -> { invocationCounter.incrementAndGet(); return (stmt, arg) -> stmt.bind("mybind" + index, arg); }; db.configure(SqlObjects.class, c -> c.setDefaultParameterCustomizerFactory(defaultParameterCustomizerFactory)); handle = db.open(); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { registry.get(SqlObjects.class).setSqlLocator(new AnnotationSqlLocator()); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { SqlLocator locator = (type, method, config) -> { String templateName = SqlAnnotations.getAnnotationValue(method).orElseGet(method::getName); findTemplate(sqlObjectType, templateName); return templateName; }; TemplateEngine templateEngine = (templateName, ctx) -> { Template template = findTemplate(sqlObjectType, templateName); StringWriter writer = new StringWriter(); try { template.process(ctx.getAttributes(), writer); return writer.toString(); } catch (TemplateException | IOException e) { throw new IllegalStateException("Failed to render template " + templateName, e); } }; registry.get(SqlObjects.class).setSqlLocator(locator); registry.get(SqlStatements.class).setTemplateEngine(templateEngine); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { SqlLocator locator = (type, method, config) -> { String templateName = SqlAnnotations.getAnnotationValue(method).orElseGet(method::getName); STGroup group = findStringTemplateGroup(type); if (!group.isDefined(templateName)) { throw new IllegalStateException("No StringTemplate group " + templateName + " for class " + sqlObjectType); } return templateName; }; TemplateEngine templateEngine = (templateName, ctx) -> { STGroup group = findStringTemplateGroup(sqlObjectType); ST template = group.getInstanceOf(templateName); ctx.getAttributes().forEach(template::add); return template.render(); }; registry.get(SqlObjects.class).setSqlLocator(locator); registry.get(SqlStatements.class).setTemplateEngine(templateEngine); }
@Test public void testLocateConfigDriven() throws Exception { Jdbi jdbi = dbRule.getJdbi(); jdbi.useHandle(h -> { h.execute("create table something (id int, name text)"); h.execute("insert into something (id, name) values (?, ?)", 2, "Alice"); h.execute("insert into something (id, name) values (?, ?)", 1, "Bob"); }); jdbi.getConfig(SqlObjects.class).setSqlLocator( (type, method, config) -> config.get(TestConfig.class).sql); jdbi.getConfig(TestConfig.class).sql = "select * from something order by id"; assertThat(jdbi.withExtension(TestDao.class, TestDao::list)) .containsExactly(new Something(1, "Bob"), new Something(2, "Alice")); jdbi.getConfig(TestConfig.class).sql = "select * from something order by name"; assertThat(jdbi.withExtension(TestDao.class, TestDao::list)) .containsExactly(new Something(2, "Alice"), new Something(1, "Bob")); }
@Test public void testLocateConfigDriven() throws Exception { Jdbi jdbi = dbRule.getJdbi(); jdbi.useHandle(h -> { h.execute("create table something (id int, name text)"); h.execute("insert into something (id, name) values (?, ?)", 2, "Alice"); h.execute("insert into something (id, name) values (?, ?)", 1, "Bob"); }); jdbi.getConfig(SqlObjects.class).setSqlLocator( (type, method, config) -> config.get(TestConfig.class).sql); jdbi.getConfig(TestConfig.class).sql = "select * from something order by id"; assertThat(jdbi.withExtension(TestDao.class, TestDao::list)) .containsExactly(new Something(1, "Bob"), new Something(2, "Alice")); jdbi.getConfig(TestConfig.class).sql = "select * from something order by name"; assertThat(jdbi.withExtension(TestDao.class, TestDao::list)) .containsExactly(new Something(2, "Alice"), new Something(1, "Bob")); }