/** * Invoke the callable statement * @return the output parameters resulting from the invocation. */ public OutParameters invoke() { try { final PreparedStatement stmt = this.internalExecute(); OutParameters out = new OutParameters(); for (OutParamArgument param : params) { Object obj = param.map((CallableStatement) stmt); // convert from JDBC 1-based position to Jdbi's 0-based int index = param.position - 1; out.getMap().put(index, obj); if (param.name != null) { out.getMap().put(param.name, obj); } } return out; } finally { close(); } }
public Timestamp getTimestamp(String name) { Long epoch = getEpoch(name); if (epoch == null) { return null; } return new Timestamp(epoch); }
public Float getFloat(int pos) { return getNumber(pos).floatValue(); }
@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")); }
/** * Type-casting convenience method which obtains an object from the map, the * object obtained should have been created with {@link CallableStatementMapper} * * @param name The out parameter name * @param type The java type to obtain * @param <T> the output parameter type * @return the output of name as type T */ public <T> T getObject(String name, Class<T> type) { return type.cast(getObject(name)); }
@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 testOutParameter() { MyDao myDao = db.onDemand(MyDao.class); OutParameters outParameters = myDao.callStoredProc(); assertThat(outParameters.getInt("outparam")).isEqualTo(100); }
/** * Type-casting convenience method which obtains an object from the map positionally * object obtained should have been created with {@link CallableStatementMapper} * * @param pos The out parameter position * @param type The java type to obtain * @param <T> the output parameter type * @return the output of name as type T */ public <T> T getObject(int pos, Class<T> type) { return type.cast(getObject(pos)); }
public Integer getInt(int pos) { return getNumber(pos).intValue(); }
@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); } }
public Timestamp getTimestamp(int pos) { Long epoch = getEpoch(pos); if (epoch == null) { return null; } return new Timestamp(epoch); }
/** * Invoke the callable statement * @return the output parameters resulting from the invocation. */ public OutParameters invoke() { try { final PreparedStatement stmt = this.internalExecute(); OutParameters out = new OutParameters(); for ( OutParamArgument param : params ) { Object obj = param.map((CallableStatement)stmt); // convert from JDBC 1-based position to Jdbi's 0-based int index = param.position - 1; out.getMap().put(index, obj); if ( param.name != null ) { out.getMap().put(param.name, obj); } } return out; } finally { close(); } }
/** * Type-casting convenience method which obtains an object from the map positionally * object obtained should have been created with {@link CallableStatementMapper} * * @param pos The out parameter position * @param type The java type to obtain * @param <T> the output parameter type * @return the output of name as type T */ public <T> T getObject(int pos, Class<T> type) { return type.cast(getObject(pos)); }
public Long getLong(int pos) { return getNumber(pos).longValue(); }
public Date getDate(String name) { Long epoch = getEpoch(name); if (epoch == null) { return null; } return new Date(epoch); }
/** * Type-casting convenience method which obtains an object from the map, the * object obtained should have been created with {@link CallableStatementMapper} * * @param name The out parameter name * @param type The java type to obtain * @param <T> the output parameter type * @return the output of name as type T */ public <T> T getObject(String name, Class<T> type) { return type.cast(getObject(name)); }
public Float getFloat(String name) { return getNumber(name).floatValue(); }
@Test public void testOutParameter() { MyDao myDao = db.onDemand(MyDao.class); OutParameters outParameters = myDao.callStoredProc(); assertThat(outParameters.getInt("outparam")).isEqualTo(100); }