use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.
the class AbstractTransactionalAnnotatedConfigClassTests method setDataSource.
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSourceViaInjection = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.
the class NamedParameterJdbcTemplateTests method testBatchUpdateWithInClause.
@Test
public void testBatchUpdateWithInClause() throws Exception {
@SuppressWarnings("unchecked") Map<String, Object>[] parameters = new Map[3];
parameters[0] = Collections.singletonMap("ids", Arrays.asList(1, 2));
parameters[1] = Collections.singletonMap("ids", Arrays.asList("3", "4"));
parameters[2] = Collections.singletonMap("ids", (Iterable<Integer>) () -> Arrays.asList(5, 6).iterator());
final int[] rowsAffected = new int[] { 1, 2, 3 };
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
given(connection.getMetaData()).willReturn(databaseMetaData);
JdbcTemplate template = new JdbcTemplate(dataSource, false);
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
int[] actualRowsAffected = namedParameterTemplate.batchUpdate("delete sometable where id in (:ids)", parameters);
assertThat(actualRowsAffected.length).as("executed 3 updates").isEqualTo(3);
InOrder inOrder = inOrder(preparedStatement);
inOrder.verify(preparedStatement).setObject(1, 1);
inOrder.verify(preparedStatement).setObject(2, 2);
inOrder.verify(preparedStatement).addBatch();
inOrder.verify(preparedStatement).setString(1, "3");
inOrder.verify(preparedStatement).setString(2, "4");
inOrder.verify(preparedStatement).addBatch();
inOrder.verify(preparedStatement).setObject(1, 5);
inOrder.verify(preparedStatement).setObject(2, 6);
inOrder.verify(preparedStatement).addBatch();
inOrder.verify(preparedStatement, atLeastOnce()).close();
verify(connection, atLeastOnce()).close();
}
use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.
the class H2SequenceMaxValueIncrementerTests method assertIncrements.
private void assertIncrements(DataSource dataSource) {
assertThat(new JdbcTemplate(dataSource).queryForObject("values next value for SEQ", int.class)).isEqualTo(1);
H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(dataSource, "SEQ");
assertThat(incrementer.nextIntValue()).isEqualTo(2);
assertThat(incrementer.nextStringValue()).isEqualTo("3");
}
use of cn.taketoday.jdbc.core.JdbcTemplate 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);
}
use of cn.taketoday.jdbc.core.JdbcTemplate in project today-framework by TAKETODAY.
the class InferredDataSourceTransactionalSqlScriptsTests method database1.
@Test
@Transactional("txMgr1")
@Sql(scripts = "data-add-dogbert.sql", config = @SqlConfig(transactionManager = "txMgr1"))
void database1() {
assertThatTransaction().isActive();
assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert");
}
Aggregations