/** * Register a named output parameter * @param name the parameter name * @param sqlType an SQL type constant as defined by {@link java.sql.Types} or by the JDBC vendor. * @return self */ public Call registerOutParameter(String name, int sqlType) { return registerOutParameter(name, sqlType, null); }
/** * Register a positional output parameter * @param position the parameter position (zero-based) * @param sqlType an SQL type constant as defined by {@link java.sql.Types} or by the JDBC vendor. * @return self */ public Call registerOutParameter(int position, int sqlType) { return registerOutParameter(position, sqlType, null); }
@Override public SqlStatementCustomizer createForMethod(Annotation annotation, Class<?> sqlObjectType, Method method) { final OutParameter outParam = (OutParameter) annotation; return stmt -> ((Call) stmt).registerOutParameter(outParam.name(), outParam.sqlType()); } }
@Override public SqlStatementCustomizer createForMethod(Annotation annotation, Class<?> sqlObjectType, Method method) { final OutParameterList outParams = (OutParameterList) annotation; return stmt -> { Call call = (Call) stmt; for (OutParameter outParam : outParams.value()) { call.registerOutParameter(outParam.name(), outParam.sqlType()); } }; } }
@Test public void testUnusedBindingWithOutParameter() { h.execute("CREATE ALIAS TO_DEGREES FOR \"java.lang.Math.toDegrees\""); Call call = h.createCall("? = CALL TO_DEGREES(?)") .registerOutParameter(0, Types.DOUBLE) .bind(1, 100.0d) .bind(2, "foo"); assertThatThrownBy(call::invoke).isInstanceOf(UnableToCreateStatementException.class); }
@Test public void testStatement() { OutParameters ret = h.createCall("? = CALL TO_DEGREES(?)") .registerOutParameter(0, Types.DOUBLE) .bind(1, 100.0d) .invoke(); Double expected = Math.toDegrees(100.0d); assertThat(ret.getDouble(0)).isEqualTo(expected, Offset.offset(0.001)); assertThat(ret.getLong(0).longValue()).isEqualTo(expected.longValue()); assertThat(ret.getShort(0).shortValue()).isEqualTo(expected.shortValue()); assertThat(ret.getInt(0).intValue()).isEqualTo(expected.intValue()); assertThat(ret.getFloat(0).floatValue()).isEqualTo(expected.floatValue(), Offset.offset(0.001f)); assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate(1)); assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate(2)); }
@Test public void testStatementWithNamedParam() { OutParameters ret = h.createCall(":x = CALL TO_DEGREES(:y)") .registerOutParameter("x", Types.DOUBLE) .bind("y", 100.0d) .invoke(); Double expected = Math.toDegrees(100.0d); assertThat(ret.getDouble("x")).isEqualTo(expected, Offset.offset(0.001)); assertThat(ret.getLong("x").longValue()).isEqualTo(expected.longValue()); assertThat(ret.getShort("x").shortValue()).isEqualTo(expected.shortValue()); assertThat(ret.getInt("x").intValue()).isEqualTo(expected.intValue()); assertThat(ret.getFloat("x")).isEqualTo(expected.floatValue()); assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate("x")); assertThatExceptionOfType(Exception.class).isThrownBy(() -> ret.getDate("y")); }
@Test @Ignore // TODO(scs): how do we test out parameters with h2? public void testWithNullReturnWithNamedParam() { OutParameters ret = h.createCall("CALL TEST_PROCEDURE(:x, :y)") .bind("x", (String) null) .registerOutParameter("y", Types.VARCHAR) .invoke(); String out = ret.getString("y"); assertThat(out).isNull(); }
@Test @Ignore // TODO(scs): how do we test out parameters with h2? public void testWithNullReturn() { OutParameters ret = h.createCall("CALL TEST_PROCEDURE(?, ?)") .bind(0, (String) null) .registerOutParameter(1, Types.VARCHAR) .invoke(); String out = ret.getString(1); assertThat(out).isNull(); }
@Test public void testCall() { Handle handle = db.getHandle(); handle.execute(findSqlOnClasspath("create_stored_proc_add")); // tag::invokeProcedure[] OutParameters result = handle .createCall("{:sum = call add(:a, :b)}") // <1> .bind("a", 13) // <2> .bind("b", 9) // <2> .registerOutParameter("sum", Types.INTEGER) // <3> <4> .invoke(); // <5> // end::invokeProcedure[] // tag::getOutParameters[] int sum = result.getInt("sum"); // end::getOutParameters[] assertThat(sum).isEqualTo(22); } }
@Test public void testPermittedUnusedBindingWithOutParameter() { h.execute("CREATE ALIAS TO_DEGREES FOR \"java.lang.Math.toDegrees\""); Call call = h.configure(SqlStatements.class, stmts -> stmts.setUnusedBindingAllowed(true)) .createCall("? = CALL TO_DEGREES(?)") .registerOutParameter(0, Types.DOUBLE) .bind(1, 100.0d) .bind(2, "foo"); assertThatCode(call::invoke).doesNotThrowAnyException(); } }
/** * Register a named output parameter * @param name the parameter name * @param sqlType an SQL type constant as defined by {@link java.sql.Types} or by the JDBC vendor. * @return self */ public Call registerOutParameter(String name, int sqlType) { return registerOutParameter(name, sqlType, null); }
/** * Register a positional output parameter * @param position the parameter position (zero-based) * @param sqlType an SQL type constant as defined by {@link java.sql.Types} or by the JDBC vendor. * @return self */ public Call registerOutParameter(int position, int sqlType) { return registerOutParameter(position, sqlType, null); }