Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #406 from stevenschlansker/out-param-index
@OutParameter annotation for @SqlCall methods
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/org/skife/jdbi/v2/sqlobject/customizers/OutParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/org/skife/jdbi/v2/sqlobject/TestOutParameterAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |