private void allowUnused() { statements.setUnusedBindingAllowed(true); } }
@Override public SqlStatementCustomizer createForType(Annotation annotation, Class<?> sqlObjectType) { return s -> s.getConfig(SqlStatements.class).setUnusedBindingAllowed(((AllowUnusedBindings) annotation).value()); } }
@Test public void testUnannotated() { dbRule.getJdbi().getConfig(SqlStatements.class).setUnusedBindingAllowed(true); assertThat(dao.unannotated("42")).isTrue(); }
@Test public void testDisallowed() { dao.getHandle().getConfig(SqlStatements.class).setUnusedBindingAllowed(true); assertThatThrownBy(() -> dao.disallowed("43")) .isInstanceOf(UnableToCreateStatementException.class) .hasMessageContaining("named parameter"); }
@Test public void theIntuitiveWayDoesntWork() { Query query = db.getSharedHandle().createQuery("select bar from foo where bar like '%:hello%'") .bind("hello", "am"); assertThatThrownBy(() -> query.mapTo(String.class).findOnly()) .isInstanceOf(UnableToCreateStatementException.class); String name = query // this lovely safeguard :) .configure(SqlStatements.class, sqls -> sqls.setUnusedBindingAllowed(true)) .mapTo(String.class) .findOnly(); assertThat(name).isEqualTo(":hello"); } }
@Test public void testPermittedUnusedBinding() { assertThatCode(() -> h.configure(SqlStatements.class, s -> s.setUnusedBindingAllowed(true)) .createQuery("select * from something") .bind("id", 1) .collectRows(Collectors.counting())).doesNotThrowAnyException(); }
@Test public void testPermittedUsedAndUnusedBinding() { assertThatCode(() -> h.configure(SqlStatements.class, s -> s.setUnusedBindingAllowed(true)) .createQuery("select * from something where id = :id") .bind("id", 1) .bind("name", "jack") .collectRows(Collectors.counting())).doesNotThrowAnyException(); }
@Test public void testPermittedUnusedBindingWithOutParameter() { h.execute("CREATE ALIAS TO_DEGREES FOR \"java.lang.Math.toDegrees\""); Call call = h.configure(SqlStatements.class, stmts -> stmts.setUnusedBindingAllowed(true)) .createCall("? = CALL TO_DEGREES(?)") .registerOutParameter(0, Types.DOUBLE) .bind(1, 100.0d) .bind(2, "foo"); assertThatCode(call::invoke).doesNotThrowAnyException(); } }