@Override public void addBatch(String sql) throws SQLException { targetStatement.addBatch(sql); }
public void addBatch(String sql) throws SQLException { delegate.addBatch(sql); incrementBatchCount(); }
@Override public void addBatch(String sql) throws SQLException { statement.addBatch(sql); }
public void addBatch(String sql) throws SQLException { stmt.addBatch(sql); }
@Override public Object call() throws Exception { stmt2.addBatch(""); return null; } }, SQLException.class, "Statement is closed.");
@Override public Object call() throws Exception { stmt.addBatch(BASIC_SQL_COPY_STMT); stmt.addBatch("copy from '" + BULKLOAD_ONE_LINE_CSV_FILE + "' into " + TBL_NAME + " (_key, age, firstName, lastName)" + " format csv"); stmt.addBatch("copy from '" + BULKLOAD_UTF8_CSV_FILE + "' into " + TBL_NAME + " (_key, age, firstName, lastName)" + " format csv"); stmt.executeBatch(); return null; } }, BatchUpdateException.class, "COPY command cannot be executed in batch mode.");
@Override public Object call() throws Exception { try (Statement stmt = conn.createStatement()) { stmt.addBatch("INSERT INTO Person(_key, id, firstName, lastName, age, data) " + "VALUES ('p1', 0, 'J', 'W', 250, RAWTOHEX('W'))"); stmt.addBatch("UPDATE Person SET id = 3, firstName = 'Mike', lastName = 'Green', " + "age = 40, data = RAWTOHEX('Green') WHERE _key = 'p3'"); stmt.addBatch("SELECT id FROM Person WHERE _key = 'p1'"); return stmt.executeBatch(); } } },
@Override public final void addBatch(String sql) throws SQLException { checkOpen(); transactionRecord(sql); try { stmt.addBatch(sql); } catch (Throwable t) { throw checkException(t, sql); } }
@Override public void statement_addBatch(StatementProxy statement, String sql) throws SQLException { if (this.pos < filterSize) { nextFilter().statement_addBatch(this, statement, sql); return; } statement.getRawObject().addBatch(sql); }
@Override public Object call() throws Exception { try (Connection conn = createOrdinaryConnection()) { try (Statement s = conn.createStatement()) { s.addBatch(String.format("insert into Person(\"id\", \"name\")values (%d, '%s')", 1, nameForId(1))); s.addBatch("SET STREAMING 1 FLUSH_FREQUENCY 10000"); } } return null; } }, SQLException.class, "Streaming control commands must be executed explicitly");
/** * Populates table 'Person' with entities. * * @param size Number of entities. * @throws SQLException If failed. */ private void populateTable(int size) throws SQLException { stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values " + generateValues(0, size)); stmt.executeBatch(); }
@Override public Object call() throws Exception { try (Connection conn = createOrdinaryConnection()) { try (Statement s = conn.createStatement()) { for (int i = 1; i <= 10; i++) s.addBatch(String.format("insert into Person(\"id\", \"name\")values (%d, '%s')", i, nameForId(i))); execute(conn, "SET STREAMING 1"); s.addBatch(String.format("insert into Person(\"id\", \"name\")values (%d, '%s')", 11, nameForId(11))); } } return null; } }, SQLException.class, "Statement has non-empty batch (call executeBatch() or clearBatch() before " +
protected void truncateTables(Set<String> tableNames, DataSource dataSource) throws SQLException { if (tableNames == null || tableNames.isEmpty()) { return; } try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { for (String tableName : tableNames) { statement.addBatch(MessageFormat.format(getTrancateSql(), tableName)); } statement.executeBatch(); } }
@Test public void testBatchUpdate() throws Exception { final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1", "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 2"}; given(this.statement.executeBatch()).willReturn(new int[] {1, 1}); mockDatabaseMetaData(true); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); int[] actualRowsAffected = template.batchUpdate(sql); assertTrue("executed 2 updates", actualRowsAffected.length == 2); verify(this.statement).addBatch(sql[0]); verify(this.statement).addBatch(sql[1]); verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); }
/** * Verifies that emty batch can be performed. * * @throws Exception If failed. */ @org.junit.Test public void testBatchEmpty() throws Exception { assert conn.getMetaData().supportsBatchUpdates(); stmt.addBatch(""); stmt.clearBatch(); // Just verify that no exception have been thrown. stmt.executeBatch(); }
@Test public void testBatchUpdateWithNoBatchSupport() throws Exception { final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1", "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 2"}; given(this.statement.execute(sql[0])).willReturn(false); given(this.statement.getUpdateCount()).willReturn(1, 1); given(this.statement.execute(sql[1])).willReturn(false); mockDatabaseMetaData(false); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); int[] actualRowsAffected = template.batchUpdate(sql); assertTrue("executed 2 updates", actualRowsAffected.length == 2); verify(this.statement, never()).addBatch(anyString()); verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); }
/** * @throws SQLException If failed. */ @Test public void testBatch() throws SQLException { final int BATCH_SIZE = 10; for (int idx = 0, i = 0; i < BATCH_SIZE; ++i, idx += i) { stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values " + generateValues(idx, i + 1)); } int[] updCnts = stmt.executeBatch(); assertEquals("Invalid update counts size", BATCH_SIZE, updCnts.length); for (int i = 0; i < BATCH_SIZE; ++i) assertEquals("Invalid update count", i + 1, updCnts[i]); }
/** * @throws SQLException If failed. */ @Test public void testBatchMerge() throws SQLException { final int BATCH_SIZE = 7; for (int idx = 0, i = 0; i < BATCH_SIZE; ++i, idx += i) { stmt.addBatch("merge into Person (_key, id, firstName, lastName, age) values " + generateValues(idx, i + 1)); } int[] updCnts = stmt.executeBatch(); assertEquals("Invalid update counts size", BATCH_SIZE, updCnts.length); for (int i = 0; i < BATCH_SIZE; ++i) assertEquals("Invalid update count", i + 1, updCnts[i]); }
/** * @throws SQLException If failed. */ @Test public void testBatchClear() throws SQLException { final int BATCH_SIZE = 7; for (int idx = 0, i = 0; i < BATCH_SIZE; ++i, idx += i) { stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values " + generateValues(idx, i + 1)); } stmt.clearBatch(); int[] updates = stmt.executeBatch(); assertEquals("Returned update counts array should have no elements for empty batch.", 0, updates.length); }
@Test public void testBatchUpdateWithNoBatchSupportAndSelect() throws Exception { final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1", "SELECT * FROM NOSUCHTABLE"}; given(this.statement.execute(sql[0])).willReturn(false); given(this.statement.getUpdateCount()).willReturn(1); given(this.statement.execute(sql[1])).willReturn(true); mockDatabaseMetaData(false); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); this.thrown.expect(InvalidDataAccessApiUsageException.class); try { template.batchUpdate(sql); } finally { verify(this.statement, never()).addBatch(anyString()); verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); } }