Search in sources :

Example 1 with AbstractInterruptibleBatchPreparedStatementSetter

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

the class JdbcTemplateTests method testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport.

@Test
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() 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 };
    given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]);
    mockDatabaseMetaData(false);
    BatchPreparedStatementSetter setter = new AbstractInterruptibleBatchPreparedStatementSetter() {

        @Override
        protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
            if (i < ids.length) {
                ps.setInt(1, ids[i]);
                return true;
            } else {
                return false;
            }
        }
    };
    JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
    int[] actualRowsAffected = template.batchUpdate(sql, setter);
    assertThat(actualRowsAffected.length == 2).as("executed 2 updates").isTrue();
    assertThat(actualRowsAffected[0]).isEqualTo(rowsAffected[0]);
    assertThat(actualRowsAffected[1]).isEqualTo(rowsAffected[1]);
    verify(this.preparedStatement, never()).addBatch();
    verify(this.preparedStatement).setInt(1, ids[0]);
    verify(this.preparedStatement).setInt(1, ids[1]);
    verify(this.preparedStatement).close();
    verify(this.connection, atLeastOnce()).close();
}
Also used : AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) Test(org.junit.jupiter.api.Test)

Example 2 with AbstractInterruptibleBatchPreparedStatementSetter

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

the class JdbcTemplateTests method testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport.

@Test
public void testInterruptibleBatchUpdateWithBaseClassAndNoBatchSupport() 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 };
    given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected[0], rowsAffected[1]);
    mockDatabaseMetaData(false);
    BatchPreparedStatementSetter setter = new AbstractInterruptibleBatchPreparedStatementSetter() {

        @Override
        protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
            if (i < ids.length) {
                ps.setInt(1, ids[i]);
                return true;
            } else {
                return false;
            }
        }
    };
    JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
    int[] actualRowsAffected = template.batchUpdate(sql, setter);
    assertThat(actualRowsAffected.length == 2).as("executed 2 updates").isTrue();
    assertThat(actualRowsAffected[0]).isEqualTo(rowsAffected[0]);
    assertThat(actualRowsAffected[1]).isEqualTo(rowsAffected[1]);
    verify(this.preparedStatement, never()).addBatch();
    verify(this.preparedStatement).setInt(1, ids[0]);
    verify(this.preparedStatement).setInt(1, ids[1]);
    verify(this.preparedStatement).close();
    verify(this.connection, atLeastOnce()).close();
}
Also used : AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) Test(org.junit.jupiter.api.Test)

Example 3 with AbstractInterruptibleBatchPreparedStatementSetter

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

the class JdbcTemplateTests method testInterruptibleBatchUpdateWithBaseClass.

@Test
public void testInterruptibleBatchUpdateWithBaseClass() 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 };
    given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
    mockDatabaseMetaData(true);
    BatchPreparedStatementSetter setter = new AbstractInterruptibleBatchPreparedStatementSetter() {

        @Override
        protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
            if (i < ids.length) {
                ps.setInt(1, ids[i]);
                return true;
            } else {
                return false;
            }
        }
    };
    JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
    int[] actualRowsAffected = template.batchUpdate(sql, setter);
    assertThat(actualRowsAffected.length == 2).as("executed 2 updates").isTrue();
    assertThat(actualRowsAffected[0]).isEqualTo(rowsAffected[0]);
    assertThat(actualRowsAffected[1]).isEqualTo(rowsAffected[1]);
    verify(this.preparedStatement, times(2)).addBatch();
    verify(this.preparedStatement).setInt(1, ids[0]);
    verify(this.preparedStatement).setInt(1, ids[1]);
    verify(this.preparedStatement).close();
    verify(this.connection, atLeastOnce()).close();
}
Also used : AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) Test(org.junit.jupiter.api.Test)

Example 4 with AbstractInterruptibleBatchPreparedStatementSetter

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

the class JdbcTemplateTests method testInterruptibleBatchUpdateWithBaseClass.

@Test
public void testInterruptibleBatchUpdateWithBaseClass() 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 };
    given(this.preparedStatement.executeBatch()).willReturn(rowsAffected);
    mockDatabaseMetaData(true);
    BatchPreparedStatementSetter setter = new AbstractInterruptibleBatchPreparedStatementSetter() {

        @Override
        protected boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException {
            if (i < ids.length) {
                ps.setInt(1, ids[i]);
                return true;
            } else {
                return false;
            }
        }
    };
    JdbcTemplate template = new JdbcTemplate(this.dataSource, false);
    int[] actualRowsAffected = template.batchUpdate(sql, setter);
    assertThat(actualRowsAffected.length == 2).as("executed 2 updates").isTrue();
    assertThat(actualRowsAffected[0]).isEqualTo(rowsAffected[0]);
    assertThat(actualRowsAffected[1]).isEqualTo(rowsAffected[1]);
    verify(this.preparedStatement, times(2)).addBatch();
    verify(this.preparedStatement).setInt(1, ids[0]);
    verify(this.preparedStatement).setInt(1, ids[1]);
    verify(this.preparedStatement).close();
    verify(this.connection, atLeastOnce()).close();
}
Also used : AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractInterruptibleBatchPreparedStatementSetter (cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter)4 PreparedStatement (java.sql.PreparedStatement)4 Test (org.junit.jupiter.api.Test)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4