use of org.jdbi.v3.sqlobject.customizer.DefineList in project jdbi by jdbi.
the class DefineListFactory method createForParameter.
@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
final DefineList d = (DefineList) annotation;
final String name = ParameterUtil.findParameterName(d.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @DefineList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
return (stmt, arg) -> {
List<?> argsList;
if (arg instanceof List) {
argsList = (List<?>) arg;
} else if (arg instanceof Object[]) {
argsList = Arrays.asList((Object[]) arg);
} else if (arg == null) {
throw new IllegalArgumentException("A null object was passed as a @DefineList parameter. " + "@DefineList is only supported on List and array arguments");
} else {
throw new IllegalArgumentException("A " + arg.getClass() + " object was passed as a @DefineList " + "parameter. @DefineList is only supported on List and array arguments");
}
if (argsList.isEmpty()) {
throw new IllegalArgumentException("An empty list was passed as a @DefineList parameter. Can't define " + "an empty attribute.");
}
if (argsList.contains(null)) {
throw new IllegalArgumentException("A @DefineList parameter was passed a list with null values in it.");
}
stmt.defineList(name, argsList);
};
}
use of org.jdbi.v3.sqlobject.customizer.DefineList 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));
});
}
Aggregations