Good afternoon.
I'm trying to bind a list with strings to the query that the IN operator uses. im use Oracle. I did following the example that was described by the link: How to use IN operator with JDBI?
List<String> ms = new ArrayList();
ms.add("Novosibirsk");
ms.add("Perm");
public interface CityDAO {
@RegisterMapper(CitiesMapper.class)
@SqlQuery("SELECT *
FROM Universities
WHERE Location IN (:cities)")
List<cities> getItems(@Bind("cities") List<String> cities);}
}
I created a ListArgumentFactory
public class ListArgumentFactory implements ArgumentFactory<List> {
@Override
public boolean accepts(Class<?> expectedType, Object value, StatementContext ctx) {
return value instanceof List;
}
@Override
public Argument build(Class<?> expectedType, final List value, StatementContext ctx) {
return new Argument() {
@Override
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
String type = null;
if(value.get(0).getClass() == String.class){
type = "varchar";
} else if(value.get(0).getClass() == Integer.class){
// For integer and so on...
} else {
// throw error.. type not handled
}
Array array = ctx.getConnection().createArrayOf(type, value.toArray());
statement.setArray(position, array);
}
};
}
}
I registered the factory
public class DBI extends AbstractModule {
private DBI dbi;
@Override
protected void configure() {
this.dbi = new DBI(provideConfig().url());
this.dbi.registerArgumentFactory(new ListArgumentFactory());
}
}
But when I make a request I get an exception
org.skife.jdbi.v2.exceptions.UnableToCreateStatementException: Exception while binding 'cities' [statement:"SELECT * FROM Universities WHERE Location IN (:cities)", arguments:{ positional:{}, named {cities:factory.ListArgumentFactory$1@6788168c}, finder:[]}]
Help me figure out what I'm doing wrong