use of org.jdbi.v3.sqlobject.customizer.BindList in project jdbi by jdbi.
the class TestBindList method testBindListWithHashPrefixParser.
@Test
public void testBindListWithHashPrefixParser() throws Exception {
Jdbi jdbi = Jdbi.create(dbRule.getConnectionFactory());
jdbi.setSqlParser(new HashPrefixSqlParser());
jdbi.useHandle(handle -> {
handle.registerRowMapper(FieldMapper.factory(Thing.class));
handle.createUpdate("insert into thing (<columns>) values (<values>)").defineList("columns", "id", "foo").bindList("values", 3, "abc").execute();
List<Thing> list = handle.createQuery("select id, foo from thing where id in (<ids>)").bindList("ids", 1, 3).mapTo(Thing.class).list();
assertThat(list).extracting(Thing::getId, Thing::getFoo, Thing::getBar, Thing::getBaz).containsExactly(tuple(1, "foo1", null, null), tuple(3, "abc", null, null));
});
}
use of org.jdbi.v3.sqlobject.customizer.BindList in project jdbi by jdbi.
the class BindListFactory method createForParameter.
@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
final BindList bindList = (BindList) annotation;
final String name = ParameterUtil.findParameterName(bindList.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @BindList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
return (stmt, arg) -> {
if (arg == null || IterableLike.isEmpty(arg)) {
switch(bindList.onEmpty()) {
case VOID:
stmt.define(name, "");
return;
case NULL:
stmt.define(name, "null");
return;
case THROW:
throw new IllegalArgumentException(arg == null ? "argument is null; null was explicitly forbidden on this instance of BindList" : "argument is empty; emptiness was explicitly forbidden on this instance of BindList");
default:
throw new IllegalStateException(valueNotHandledMessage);
}
}
stmt.bindList(name, IterableLike.toList(arg));
};
}
Aggregations