diff --git a/RELEASE_NOTES b/RELEASE_NOTES index 70c6758f51..f038edc0ce 100644 --- a/RELEASE_NOTES +++ b/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 diff --git a/src/main/java/org/skife/jdbi/v2/sqlobject/customizers/OutParameter.java b/src/main/java/org/skife/jdbi/v2/sqlobject/customizers/OutParameter.java new file mode 100644 index 0000000000..16985a2ad5 --- /dev/null +++ b/src/main/java/org/skife/jdbi/v2/sqlobject/customizers/OutParameter.java @@ -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: + *
+ * {@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();
+ * }
+ * 
+ */ +@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"); + } + } +} diff --git a/src/test/java/org/skife/jdbi/v2/sqlobject/TestOutParameterAnnotation.java b/src/test/java/org/skife/jdbi/v2/sqlobject/TestOutParameterAnnotation.java new file mode 100644 index 0000000000..e1cfc494d9 --- /dev/null +++ b/src/test/java/org/skife/jdbi/v2/sqlobject/TestOutParameterAnnotation.java @@ -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() { + @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(); + } +}