Skip to content

Commit

Permalink
Merge pull request #406 from stevenschlansker/out-param-index
Browse files Browse the repository at this point in the history
@OutParameter annotation for @SqlCall methods
  • Loading branch information
Matthew Hall committed Jul 14, 2016
2 parents 87153e0 + a103581 commit 837fa09
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES
@@ -1,6 +1,7 @@
2.74
- cglib 3.2.2, asm 5.1; fixes codegen for new Java 8 bridge methods
- @UseStringTemplate3StatementLocator now caches created locators
- new @OutParameter annotation for fetching named out params on @SqlCall methods

2.73
- Allow clearing of bindings in SQLStatement
Expand Down
@@ -0,0 +1,77 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skife.jdbi.v2.sqlobject.customizers;

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 org.skife.jdbi.v2.Call;
import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizer;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizerFactory;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizingAnnotation;

/**
* Declare a named out parameter on an {@code @SqlCall} annotated method.
* Note that you *must* include the parameter name in the SQL text to
* ensure that the binding is activated, this is a limitation that
* may be fixed at a future date.
*
* Example usage, using PostgreSQL call syntax:
* <pre>
* {@code
* handle.execute("CREATE FUNCTION set100(OUT outparam INT) AS $$ BEGIN outparam \\:= 100; END; $$ LANGUAGE plpgsql");
*
* @SqlCall("{call myStoredProc(:outparam)}")
* @OutParameter(name="outparam", sqlType = Types.INTEGER)
* OutParameters callStoredProc();
* }
* </pre>
*/
@SqlStatementCustomizingAnnotation(OutParameter.Factory.class)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OutParameter {
String name();
int sqlType();

class Factory implements SqlStatementCustomizerFactory {

@Override
public SqlStatementCustomizer createForType(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType) {
throw new UnsupportedOperationException("Not allowed on Type");
}

@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 {
((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");
}
}
}
@@ -0,0 +1,65 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skife.jdbi.v2.sqlobject;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

import java.sql.Types;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.OutParameters;
import org.skife.jdbi.v2.sqlobject.customizers.OutParameter;
import org.skife.jdbi.v2.tweak.HandleCallback;

public class TestOutParameterAnnotation {
@BeforeClass
public static void isPostgresInstalled() {
assumeTrue(Boolean.parseBoolean(System.getenv("TRAVIS")));
}

private DBI dbi;

@Before
public void setUp() throws Exception {
dbi = new DBI("jdbc:postgresql:jdbi_test", "postgres", "");
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();
}
}

0 comments on commit 837fa09

Please sign in to comment.