[#2028] Add Batch.size() to indicate the number of queries that will be

executed by a batch operation
This commit is contained in:
Lukas Eder 2012-12-20 13:42:40 +01:00
parent fddd972465
commit d4da8c72b0
5 changed files with 40 additions and 11 deletions

View File

@ -61,6 +61,7 @@ import java.sql.Connection;
import java.sql.Date;
import java.util.Arrays;
import org.jooq.Batch;
import org.jooq.ExecuteContext;
import org.jooq.Field;
import org.jooq.Record;
@ -425,14 +426,16 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
// [#1749] TODO Firebird renders CAST(? as VARCHAR(...)) bind values with sizes
// pre-calculated. Hence the param needs to have some min length...
int[] result = create().batch(create().insertInto(TAuthor())
.set(TAuthor_ID(), 8)
.set(TAuthor_LAST_NAME(), " "))
.bind(8, "Gamma")
.bind(9, "Helm")
.bind(10, "Johnson")
.execute();
Batch batch = create().batch(create().insertInto(TAuthor())
.set(TAuthor_ID(), 8)
.set(TAuthor_LAST_NAME(), " "))
.bind(8, "Gamma")
.bind(9, "Helm")
.bind(10, "Johnson");
assertEquals(3, batch.size());
int[] result = batch.execute();
assertEquals(3, result.length);
testBatchAuthors("Gamma", "Helm", "Johnson");
}
@ -441,7 +444,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
public void testBatchMultiple() throws Exception {
jOOQAbstractTest.reset = false;
int[] result = create().batch(
Batch batch = create().batch(
create().insertInto(TAuthor())
.set(TAuthor_ID(), 8)
.set(TAuthor_LAST_NAME(), "Gamma"),
@ -460,8 +463,11 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
create().insertInto(TAuthor())
.set(TAuthor_ID(), 10)
.set(TAuthor_LAST_NAME(), "Johnson")).execute();
.set(TAuthor_LAST_NAME(), "Johnson"));
assertEquals(4, batch.size());
int[] result = batch.execute();
assertEquals(4, result.length);
assertEquals(5, create().fetch(TBook()).size());
assertEquals(1, create().fetch(TBook(), TBook_AUTHOR_ID().equal(8)).size());
@ -489,7 +495,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
b1.setValue(TBook_PUBLISHED_IN(), 2000);
b1.setValue(TBook_LANGUAGE_ID(), 1);
int[] result1 = create().batchStore(a1, b1, a2).execute();
Batch batch = create().batchStore(a1, b1, a2);
assertEquals(3, batch.size());
int[] result1 = batch.execute();
assertEquals(3, result1.length);
testBatchAuthors("XX", "YY");
assertEquals("XX 1", create()

View File

@ -70,4 +70,9 @@ public interface Batch extends Serializable {
* @throws DataAccessException if something went wrong executing the query
*/
int[] execute() throws DataAccessException;
/**
* Get the number of executed queries in this batch operation
*/
int size();
}

View File

@ -61,6 +61,11 @@ class BatchMultiple implements Batch {
this.queries = queries;
}
@Override
public final int size() {
return queries.length;
}
@Override
public final int[] execute() {
ExecuteContext ctx = new DefaultExecuteContext(create, queries);

View File

@ -74,6 +74,11 @@ class BatchSingle implements BatchBindStep {
return this;
}
@Override
public final int size() {
return allBindValues.size();
}
@Override
public final int[] execute() {

View File

@ -65,7 +65,7 @@ class BatchStore implements Batch {
*/
private static final long serialVersionUID = -2935544935267715011L;
private final Executor create;
private final Executor create;
private final UpdatableRecord<?>[] records;
BatchStore(Executor create, UpdatableRecord<?>[] records) {
@ -73,6 +73,11 @@ class BatchStore implements Batch {
this.records = records;
}
@Override
public final int size() {
return records.length;
}
@Override
public final int[] execute() throws DataAccessException {