How can I execute somethings like this in jDBI ?
@SqlQuery(\"select id from foo where name in \")
List getIds(@Bind(
With PostgreSQL, I was able to use the ANY comparison and bind the collection to an array to achieve this.
public interface Foo {
@SqlQuery("SELECT id FROM foo WHERE name = ANY (:nameList)")
List getIds(@BindStringList("nameList") List nameList);
}
@BindingAnnotation(BindStringList.BindFactory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface BindStringList {
String value() default "it";
class BindFactory implements BinderFactory {
@Override
public Binder build(Annotation annotation) {
return new Binder>() {
@Override
public void bind(SQLStatement> q, BindStringList bind, Collection arg) {
try {
Array array = q.getContext().getConnection().createArrayOf("varchar", arg.toArray());
q.bindBySqlType(bind.value(), array, Types.ARRAY);
} catch (SQLException e) {
// handle error
}
}
};
}
}
}
NB: ANY is not part of the ANSI SQL standard, so this creates a hard dependency on PostgreSQL.