|
18 | 18 | */
|
19 | 19 | package groovy.sql
|
20 | 20 |
|
| 21 | +import groovy.test.GroovyAssert |
| 22 | + |
21 | 23 | import javax.sql.DataSource
|
22 | 24 |
|
23 | 25 | import static groovy.sql.SqlTestConstants.*
|
@@ -119,6 +121,44 @@ class SqlBatchTest extends GroovyTestCase {
|
119 | 121 | // FINE: Successfully executed batch with 1 command(s)
|
120 | 122 | }
|
121 | 123 |
|
| 124 | + void testWithBatchHavingSizeSameSizeAsStatements() { |
| 125 | + def numRows = sql.rows("SELECT * FROM PERSON").size() |
| 126 | + assert numRows == 3 |
| 127 | + def myOthers = ['f4':'l4','f5':'l5','f6':'l6','f7':'l7'] |
| 128 | + def result = sql.withBatch(myOthers.size(), "insert into PERSON (id, firstname, lastname) values (?, ?, ?)") { ps -> |
| 129 | + myOthers.eachWithIndex { k, v, index -> |
| 130 | + def id = index + numRows + 1 |
| 131 | + ps.addBatch(id, k, v) |
| 132 | + } |
| 133 | + } |
| 134 | + assert result == [1] * myOthers.size() |
| 135 | + assert sql.rows("SELECT * FROM PERSON").size() == numRows + myOthers.size() |
| 136 | + // end result the same as if no batching was in place but logging should show: |
| 137 | + // FINE: Successfully executed batch with 4 command(s) |
| 138 | + } |
| 139 | + |
| 140 | + void testWithBatchNothingAddedToBatch() { |
| 141 | + def numRows = sql.rows("SELECT * FROM PERSON").size() |
| 142 | + assert numRows == 3 |
| 143 | + |
| 144 | + def result = sql.withBatch { ps -> |
| 145 | + // Add nothing |
| 146 | + } |
| 147 | + assert result == [] as int[] |
| 148 | + } |
| 149 | + |
| 150 | + void testWithBatchWithPreparedStatementNothingAddedToBatch() { |
| 151 | + def numRows = sql.rows("SELECT * FROM PERSON").size() |
| 152 | + assert numRows == 3 |
| 153 | + |
| 154 | + // If you create a PreparedStatement you have to use it - or else HSQL throws an exception |
| 155 | + GroovyAssert.shouldFail { |
| 156 | + sql.withBatch(3, "insert into PERSON (id, firstname, lastname) values (?, ?, ?)") { ps -> |
| 157 | + // Add nothing - not a good practice at all... |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
122 | 162 | void testWithBatchInsideWithTransaction() {
|
123 | 163 | def numRows = sql.rows("SELECT * FROM PERSON").size()
|
124 | 164 | assert numRows == 3
|
|
0 commit comments