Search in sources :

Example 1 with CallableStatementCreator

use of cn.taketoday.jdbc.core.CallableStatementCreator in project today-infrastructure by TAKETODAY.

the class AbstractJdbcCall method executeCallInternal.

/**
 * Delegate method to perform the actual call processing.
 */
private Map<String, Object> executeCallInternal(Map<String, ?> args) {
    CallableStatementCreator csc = getCallableStatementFactory().newCallableStatementCreator(args);
    if (logger.isDebugEnabled()) {
        logger.debug("The following parameters are used for call " + getCallString() + " with " + args);
        int i = 1;
        for (SqlParameter param : getCallParameters()) {
            logger.debug(i + ": " + param.getName() + ", SQL type " + param.getSqlType() + ", type name " + param.getTypeName() + ", parameter class [" + param.getClass().getName() + "]");
            i++;
        }
    }
    return getJdbcTemplate().call(csc, getCallParameters());
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) SqlParameter(cn.taketoday.jdbc.core.SqlParameter)

Example 2 with CallableStatementCreator

use of cn.taketoday.jdbc.core.CallableStatementCreator in project today-infrastructure by TAKETODAY.

the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.

/**
 * Confirm no connection was used to get metadata. Does not use superclass replay
 * mechanism.
 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(2)).willReturn(5);
    given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
    class TestJdbcTemplate extends JdbcTemplate {

        int calls;

        @Override
        public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
            calls++;
            return super.call(csc, declaredParameters);
        }
    }
    TestJdbcTemplate t = new TestJdbcTemplate();
    t.setDataSource(dataSource);
    // Will fail without the following, because we're not able to get a connection
    // from the DataSource here if we need to create an ExceptionTranslator
    t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
    assertThat(sp.execute(11)).isEqualTo(5);
    assertThat(t.calls).isEqualTo(1);
    verify(callableStatement).setObject(1, 11, Types.INTEGER);
    verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 3 with CallableStatementCreator

use of cn.taketoday.jdbc.core.CallableStatementCreator in project today-framework by TAKETODAY.

the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.

/**
 * Confirm no connection was used to get metadata. Does not use superclass replay
 * mechanism.
 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(2)).willReturn(5);
    given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
    class TestJdbcTemplate extends JdbcTemplate {

        int calls;

        @Override
        public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
            calls++;
            return super.call(csc, declaredParameters);
        }
    }
    TestJdbcTemplate t = new TestJdbcTemplate();
    t.setDataSource(dataSource);
    // Will fail without the following, because we're not able to get a connection
    // from the DataSource here if we need to create an ExceptionTranslator
    t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
    assertThat(sp.execute(11)).isEqualTo(5);
    assertThat(t.calls).isEqualTo(1);
    verify(callableStatement).setObject(1, 11, Types.INTEGER);
    verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 4 with CallableStatementCreator

use of cn.taketoday.jdbc.core.CallableStatementCreator in project today-framework by TAKETODAY.

the class AbstractJdbcCall method executeCallInternal.

/**
 * Delegate method to perform the actual call processing.
 */
private Map<String, Object> executeCallInternal(Map<String, ?> args) {
    CallableStatementCreator csc = getCallableStatementFactory().newCallableStatementCreator(args);
    if (logger.isDebugEnabled()) {
        logger.debug("The following parameters are used for call " + getCallString() + " with " + args);
        int i = 1;
        for (SqlParameter param : getCallParameters()) {
            logger.debug(i + ": " + param.getName() + ", SQL type " + param.getSqlType() + ", type name " + param.getTypeName() + ", parameter class [" + param.getClass().getName() + "]");
            i++;
        }
    }
    return getJdbcTemplate().call(csc, getCallParameters());
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) SqlParameter(cn.taketoday.jdbc.core.SqlParameter)

Aggregations

CallableStatementCreator (cn.taketoday.jdbc.core.CallableStatementCreator)4 JdbcTemplate (cn.taketoday.jdbc.core.JdbcTemplate)2 SqlParameter (cn.taketoday.jdbc.core.SqlParameter)2 SQLStateSQLExceptionTranslator (cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2