Skip to content

Instantly share code, notes, and snippets.

@stevenschlansker
Created July 11, 2016 21:04
Show Gist options
  • Save stevenschlansker/42b410cbaa2b3015db3c9c0d1026ce41 to your computer and use it in GitHub Desktop.
Save stevenschlansker/42b410cbaa2b3015db3c9c0d1026ce41 to your computer and use it in GitHub Desktop.
package org.skife.jdbi.v2.sqlobject;
import static org.junit.Assert.assertEquals;
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.lang.reflect.Method;
import java.sql.SQLException;
import java.sql.Types;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.skife.jdbi.v2.Call;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.OutParameters;
import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.tweak.HandleCallback;
import com.opentable.db.postgres.junit.EmbeddedPostgresRules;
import com.opentable.db.postgres.junit.SingleInstancePostgresRule;
public class TestOutParameters {
@Rule
public SingleInstancePostgresRule pg = EmbeddedPostgresRules.singleInstance();
private DBI dbi;
@Before
public void setUp() throws Exception {
dbi = new DBI(pg.getEmbeddedPostgres().getPostgresDatabase());
dbi.withHandle(new HandleCallback<Object>() {
@Override
public Object withHandle(Handle handle) throws Exception
{
handle.execute("CREATE FUNCTION set100(OUT outparam INT) AS $$ BEGIN outparam \\:= 100; END; $$ LANGUAGE plpgsql");
return null;
}
});
}
@Test
public void testOutParameter() {
MyDao myDao = dbi.onDemand(MyDao.class);
OutParameters outParameters = myDao.callStoredProc();
assertEquals(Integer.valueOf(100), outParameters.getInt("outparam"));
}
public interface MyDao{
@SqlCall("{call set100(:outparam)}")
@OutParameter(name="outparam", sqlType = Types.INTEGER)
OutParameters callStoredProc();
}
@SqlStatementCustomizingAnnotation(OutParameter.Factory.class)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OutParameter {
String name();
int sqlType();
static class Factory implements SqlStatementCustomizerFactory {
@Override
public SqlStatementCustomizer createForType(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType) {
throw new UnsupportedOperationException("Not allowed on Type"); //$NON-NLS-1$
}
@Override
public SqlStatementCustomizer createForMethod(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType, Method method) {
final OutParameter outParam = (OutParameter) annotation;
return new SqlStatementCustomizer() {
@Override
public void apply(@SuppressWarnings("rawtypes") SQLStatement q) throws SQLException {
assert q instanceof Call;
((Call) q).registerOutParameter(outParam.name(), outParam.sqlType());
}
};
}
@Override
public SqlStatementCustomizer createForParameter(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType, Method method, final Object arg) {
throw new UnsupportedOperationException("Not defined for parameter");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment