Search in sources :

Example 21 with JdbcTemplate

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);
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Autowired(cn.taketoday.beans.factory.annotation.Autowired)

Example 22 with JdbcTemplate

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();
}
Also used : InOrder(org.mockito.InOrder) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 23 with JdbcTemplate

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");
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate)

Example 24 with JdbcTemplate

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);
}
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 25 with JdbcTemplate

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");
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test) Transactional(cn.taketoday.transaction.annotation.Transactional)

Aggregations

JdbcTemplate (cn.taketoday.jdbc.core.JdbcTemplate)52 Test (org.junit.jupiter.api.Test)34 DataSource (javax.sql.DataSource)18 AbstractDriverBasedDataSource (cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource)10 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)8 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)6 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 EmbeddedDatabaseFactoryBean (cn.taketoday.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean)4 Transactional (cn.taketoday.transaction.annotation.Transactional)4 List (java.util.List)4 Autowired (cn.taketoday.beans.factory.annotation.Autowired)2 Bean (cn.taketoday.context.annotation.Bean)2 Primary (cn.taketoday.context.annotation.Primary)2 ConditionalOnMissingBean (cn.taketoday.context.condition.ConditionalOnMissingBean)2 ClassRelativeResourceLoader (cn.taketoday.core.io.ClassRelativeResourceLoader)2 CallableStatementCreator (cn.taketoday.jdbc.core.CallableStatementCreator)2 SqlReturnResultSet (cn.taketoday.jdbc.core.SqlReturnResultSet)2 NamedParameterJdbcTemplate (cn.taketoday.jdbc.core.namedparam.NamedParameterJdbcTemplate)2 DriverManagerDataSource (cn.taketoday.jdbc.datasource.DriverManagerDataSource)2 SimpleDriverDataSource (cn.taketoday.jdbc.datasource.SimpleDriverDataSource)2