Example usage for org.springframework.jdbc.object StoredProcedure execute

List of usage examples for org.springframework.jdbc.object StoredProcedure execute

Introduction

In this page you can find the example usage for org.springframework.jdbc.object StoredProcedure execute.

Prototype

public Map<String, Object> execute(ParameterMapper inParamMapper) throws DataAccessException 

Source Link

Document

Execute the stored procedure.

Usage

From source file:org.codehaus.grepo.procedure.repository.GenericProcedureRepositoryImpl.java

/**
 * Execute the procedure.//  www.  ja  va 2s .  c o  m
 *
 * @param pmpi The method parameter info.
 * @param genericProcedure The annotation.
 * @return Returns the result map of the procedure call.
 */
protected Map<String, Object> executeProcedure(final ProcedureMethodParameterInfo pmpi,
        GenericProcedure genericProcedure) {
    TransactionCallback<Object> callback = new TransactionCallback<Object>() {

        public Object doInTransaction(final TransactionStatus status) {
            ProcedureExecutionContext context = createProcedureExecutionContext();

            StoredProcedure sp = prepareProcedure(pmpi, context);

            Map<String, Object> input = generateInputMap(pmpi, context);

            if (logger.isDebugEnabled()) {
                logger.debug("About to execute procedure: {}", sp.getSql());
                logger.debug("Using input map: {}", input);
            }

            Object result = sp.execute(input);
            logger.debug("Procedure result is '{}'", result);
            return result;
        }
    };

    return executeCallback(callback, genericProcedure.isReadOnly());
}

From source file:org.springframework.jdbc.object.GenericStoredProcedureTests.java

@Test
public void testAddInvoices() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
            new ClassPathResource("org/springframework/jdbc/object/GenericStoredProcedureTests-context.xml"));
    Connection connection = mock(Connection.class);
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getConnection()).willReturn(connection);
    CallableStatement callableStatement = mock(CallableStatement.class);
    TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) bf.getBean("dataSource");
    testDataSource.setTarget(dataSource);

    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(3)).willReturn(new Integer(4));

    given(connection.prepareCall("{call " + "add_invoice" + "(?, ?, ?)}")).willReturn(callableStatement);

    StoredProcedure adder = (StoredProcedure) bf.getBean("genericProcedure");
    Map<String, Object> in = new HashMap<String, Object>(2);
    in.put("amount", 1106);
    in.put("custid", 3);
    Map out = adder.execute(in);
    Integer id = (Integer) out.get("newid");
    assertEquals(4, id.intValue());/*from   w w w  . j  a  v a2  s  .  c om*/

    verify(callableStatement).setObject(1, new Integer(1106), Types.INTEGER);
    verify(callableStatement).setObject(2, new Integer(3), Types.INTEGER);
    verify(callableStatement).registerOutParameter(3, Types.INTEGER);
    verify(callableStatement).close();
}