Repository: groovy Updated Branches: refs/heads/master 70ce561c1 -> 0a612c254
GROOVY-8288 executeBatch() should not be called with batchCount == 0 (closes #586) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/0a612c25 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/0a612c25 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/0a612c25 Branch: refs/heads/master Commit: 0a612c2545485c02d7d61dcbadfaa4a063c54b16 Parents: 70ce561 Author: Antoine Kapps <antoine.ka...@orange.fr> Authored: Tue Aug 15 09:42:26 2017 +0200 Committer: sunlan <sun...@apache.org> Committed: Sat Sep 16 00:40:37 2017 +0800 ---------------------------------------------------------------------- .../groovy/sql/BatchingStatementWrapper.java | 17 ++++++++- .../test/groovy/groovy/sql/SqlBatchTest.groovy | 40 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/0a612c25/subprojects/groovy-sql/src/main/java/groovy/sql/BatchingStatementWrapper.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-sql/src/main/java/groovy/sql/BatchingStatementWrapper.java b/subprojects/groovy-sql/src/main/java/groovy/sql/BatchingStatementWrapper.java index edb5212..8e6c4e1 100644 --- a/subprojects/groovy-sql/src/main/java/groovy/sql/BatchingStatementWrapper.java +++ b/subprojects/groovy-sql/src/main/java/groovy/sql/BatchingStatementWrapper.java @@ -83,8 +83,10 @@ public class BatchingStatementWrapper extends GroovyObjectSupport { } public int[] executeBatch() throws SQLException { - int[] lastResult = delegate.executeBatch(); - processResult(lastResult); + if (shouldCallDelegate()) { + int[] lastResult = delegate.executeBatch(); + processResult(lastResult); + } int[] result = new int[results.size()]; for (int i = 0; i < results.size(); i++) { result[i] = results.get(i); @@ -93,6 +95,17 @@ public class BatchingStatementWrapper extends GroovyObjectSupport { return result; } + private boolean shouldCallDelegate() { + if (batchCount > 0) { + return true; + } else if (results.isEmpty()) { + log.warning("Nothing has been added to batch. This might cause the JDBC driver to throw an exception."); + return true; + } + // Nothing added since last delegate execution. No need to call the delegate this time. + return false; + } + protected void processResult(int[] lastResult) { boolean foundError = false; for (int i : lastResult) { http://git-wip-us.apache.org/repos/asf/groovy/blob/0a612c25/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy b/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy index 02a6d65..6916113 100644 --- a/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy +++ b/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy @@ -18,6 +18,8 @@ */ package groovy.sql +import groovy.test.GroovyAssert + import javax.sql.DataSource import static groovy.sql.SqlTestConstants.* @@ -119,6 +121,44 @@ class SqlBatchTest extends GroovyTestCase { // FINE: Successfully executed batch with 1 command(s) } + void testWithBatchHavingSizeSameSizeAsStatements() { + def numRows = sql.rows("SELECT * FROM PERSON").size() + assert numRows == 3 + def myOthers = ['f4':'l4','f5':'l5','f6':'l6','f7':'l7'] + def result = sql.withBatch(myOthers.size(), "insert into PERSON (id, firstname, lastname) values (?, ?, ?)") { ps -> + myOthers.eachWithIndex { k, v, index -> + def id = index + numRows + 1 + ps.addBatch(id, k, v) + } + } + assert result == [1] * myOthers.size() + assert sql.rows("SELECT * FROM PERSON").size() == numRows + myOthers.size() + // end result the same as if no batching was in place but logging should show: + // FINE: Successfully executed batch with 4 command(s) + } + + void testWithBatchNothingAddedToBatch() { + def numRows = sql.rows("SELECT * FROM PERSON").size() + assert numRows == 3 + + def result = sql.withBatch { ps -> + // Add nothing + } + assert result == [] as int[] + } + + void testWithBatchWithPreparedStatementNothingAddedToBatch() { + def numRows = sql.rows("SELECT * FROM PERSON").size() + assert numRows == 3 + + // If you create a PreparedStatement you have to use it - or else HSQL throws an exception + GroovyAssert.shouldFail { + sql.withBatch(3, "insert into PERSON (id, firstname, lastname) values (?, ?, ?)") { ps -> + // Add nothing - not a good practice at all... + } + } + } + void testWithBatchInsideWithTransaction() { def numRows = sql.rows("SELECT * FROM PERSON").size() assert numRows == 3