use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.
the class BatchSqlUpdateTests method doTestBatchUpdate.
private void doTestBatchUpdate(boolean flushThroughBatchSize) throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final int[] ids = new int[] { 100, 200 };
final int[] rowsAffected = new int[] { 1, 2 };
Connection connection = mock(Connection.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).willReturn(connection);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
given(preparedStatement.getConnection()).willReturn(connection);
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
DatabaseMetaData mockDatabaseMetaData = mock(DatabaseMetaData.class);
given(mockDatabaseMetaData.supportsBatchUpdates()).willReturn(true);
given(connection.prepareStatement(sql)).willReturn(preparedStatement);
given(connection.getMetaData()).willReturn(mockDatabaseMetaData);
BatchSqlUpdate update = new BatchSqlUpdate(dataSource, sql);
update.declareParameter(new SqlParameter(Types.INTEGER));
if (flushThroughBatchSize) {
update.setBatchSize(2);
}
update.update(ids[0]);
update.update(ids[1]);
if (flushThroughBatchSize) {
assertEquals(0, update.getQueueCount());
assertEquals(2, update.getRowsAffected().length);
} else {
assertEquals(2, update.getQueueCount());
assertEquals(0, update.getRowsAffected().length);
}
int[] actualRowsAffected = update.flush();
assertEquals(0, update.getQueueCount());
if (flushThroughBatchSize) {
assertTrue("flush did not execute updates", actualRowsAffected.length == 0);
} else {
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
}
actualRowsAffected = update.getRowsAffected();
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
update.reset();
assertEquals(0, update.getRowsAffected().length);
verify(preparedStatement).setObject(1, ids[0], Types.INTEGER);
verify(preparedStatement).setObject(1, ids[1], Types.INTEGER);
verify(preparedStatement, times(2)).addBatch();
verify(preparedStatement).close();
}
use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.
the class RdbmsOperationTests method parametersSetWithList.
@Test
public void parametersSetWithList() {
DataSource ds = new DriverManagerDataSource();
operation.setDataSource(ds);
operation.setSql("select * from mytable where one = ? and two = ?");
operation.setParameters(new SqlParameter[] { new SqlParameter("one", Types.NUMERIC), new SqlParameter("two", Types.NUMERIC) });
operation.afterPropertiesSet();
operation.validateParameters(new Object[] { 1, "2" });
assertEquals(2, operation.getDeclaredParameters().size());
}
use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.
the class SimpleJdbcCallTests method testAddInvoiceFuncWithoutMetaDataUsingArrayParams.
@Test
public void testAddInvoiceFuncWithoutMetaDataUsingArrayParams() throws Exception {
initializeAddInvoiceWithoutMetaData(true);
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
adder.declareParameters(new SqlOutParameter("return", Types.INTEGER), new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER));
Number newId = adder.executeFunction(Number.class, 1103, 3);
assertEquals(4, newId.intValue());
verifyAddInvoiceWithoutMetaData(true);
verify(connection, atLeastOnce()).close();
}
use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.
the class SimpleJdbcCallTests method testAddInvoiceProcWithoutMetaDataUsingMapParamSource.
@Test
public void testAddInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception {
initializeAddInvoiceWithoutMetaData(false);
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
adder.declareParameters(new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER), new SqlOutParameter("newid", Types.INTEGER));
Number newId = adder.executeObject(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
assertEquals(4, newId.intValue());
verifyAddInvoiceWithoutMetaData(false);
verify(connection, atLeastOnce()).close();
}
use of org.springframework.jdbc.core.SqlParameter in project spring-framework by spring-projects.
the class SimpleJdbcCallTests method testAddInvoiceFuncWithoutMetaDataUsingMapParamSource.
@Test
public void testAddInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception {
initializeAddInvoiceWithoutMetaData(true);
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
adder.declareParameters(new SqlOutParameter("return", Types.INTEGER), new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER));
Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
assertEquals(4, newId.intValue());
verifyAddInvoiceWithoutMetaData(true);
verify(connection, atLeastOnce()).close();
}
Aggregations