Skip to content

Instantly share code, notes, and snippets.

@ericdcobb
Created April 13, 2015 21:07
Show Gist options
  • Save ericdcobb/82e4cf86ca5cb7b49e2e to your computer and use it in GitHub Desktop.
Save ericdcobb/82e4cf86ca5cb7b49e2e to your computer and use it in GitHub Desktop.
package com.levelsbeyond.assetService.jdbi;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.sql.Array;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.sqlobject.Binder;
import org.skife.jdbi.v2.sqlobject.BinderFactory;
import org.skife.jdbi.v2.sqlobject.BindingAnnotation;
@BindingAnnotation(BindListIntegers.BindArrayBinderFactory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface BindListIntegers {
String value();
public static class BindArrayBinderFactory implements BinderFactory {
@Override
public Binder build(Annotation annotation) {
return new Binder<BindListIntegers, List<Integer>>() {
@Override
public void bind(SQLStatement<?> sqlStatement, BindListIntegers bindArray, List<Integer> list) {
try {
Array ary = sqlStatement.getContext()
.getConnection()
.createArrayOf("integer", list != null ? list.toArray() : new Integer[0]);
sqlStatement.bindBySqlType(bindArray.value(), ary, Types.ARRAY);
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment