[#1180] Execute BatchMultiple (multi-query batch query), when executing BindSimple (single-query, multi-bind value query) with StatementType == STATIC_STATEMENT
[#1207] Add Factory.batch(Collection<? extends Query>) for convenience
This commit is contained in:
parent
86402653ed
commit
a414d3467f
@ -352,6 +352,11 @@ public class FactoryProxy implements FactoryOperations, MethodInterceptor {
|
||||
return getDelegate().batch(queries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Batch batch(Collection<? extends Query> queries) {
|
||||
return getDelegate().batch(queries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final BatchBindStep batch(Query query) {
|
||||
return getDelegate().batch(query);
|
||||
|
||||
@ -51,7 +51,6 @@ import static org.jooq.SQLDialect.ORACLE;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SQLDialect.SQLITE;
|
||||
import static org.jooq.SQLDialect.SYBASE;
|
||||
import static org.jooq.conf.SettingsTools.executePreparedStatements;
|
||||
import static org.jooq.impl.Factory.castNull;
|
||||
import static org.jooq.impl.Factory.count;
|
||||
import static org.jooq.impl.Factory.deg;
|
||||
@ -699,11 +698,6 @@ extends BaseTest<A, B, S, B2S, BS, L, X, DATE, D, T, U, I, IPK, T658, T725, T639
|
||||
|
||||
@Test
|
||||
public void testBatchSingle() throws Exception {
|
||||
if (!executePreparedStatements(create().getSettings())) {
|
||||
log.info("SKIPPING", "Single batch tests with statement type = STATEMENT");
|
||||
return;
|
||||
}
|
||||
|
||||
jOOQAbstractTest.reset = false;
|
||||
|
||||
int[] result = create().batch(create().insertInto(TAuthor())
|
||||
|
||||
@ -565,6 +565,24 @@ public interface FactoryOperations extends Configuration {
|
||||
@Support
|
||||
Batch batch(Query... queries);
|
||||
|
||||
/**
|
||||
* Execute a set of queries in batch mode (without bind values).
|
||||
* <p>
|
||||
* This essentially runs the following logic: <code><pre>
|
||||
* Statement s = connection.createStatement();
|
||||
*
|
||||
* for (Query query : queries) {
|
||||
* s.addBatch(query.getSQL(true));
|
||||
* }
|
||||
*
|
||||
* s.execute();
|
||||
* </pre></code>
|
||||
*
|
||||
* @see Statement#executeBatch()
|
||||
*/
|
||||
@Support
|
||||
Batch batch(Collection<? extends Query> queries);
|
||||
|
||||
/**
|
||||
* Execute a set of queries in batch mode (with bind values).
|
||||
* <p>
|
||||
|
||||
@ -35,6 +35,8 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.conf.SettingsTools.executeStaticStatements;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
@ -68,6 +70,18 @@ class BatchSingle implements BatchBindStep {
|
||||
|
||||
@Override
|
||||
public final int[] execute() {
|
||||
|
||||
// [#1180] Run batch queries with BatchMultiple, if no bind variables
|
||||
// should be used...
|
||||
if (executeStaticStatements(create.getSettings())) {
|
||||
return executeStatic();
|
||||
}
|
||||
else {
|
||||
return executePrepared();
|
||||
}
|
||||
}
|
||||
|
||||
private final int[] executePrepared() {
|
||||
Connection connection = create.getConnection();
|
||||
|
||||
ExecuteContext ctx = new DefaultExecuteContext(create, new Query[] { query });
|
||||
@ -103,4 +117,18 @@ class BatchSingle implements BatchBindStep {
|
||||
Util.safeClose(listener, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
private final int[] executeStatic() {
|
||||
List<Query> queries = new ArrayList<Query>();
|
||||
|
||||
for (Object[] bindValues : allBindValues) {
|
||||
for (int i = 0; i < bindValues.length; i++) {
|
||||
query.bind(i + 1, bindValues[i]);
|
||||
}
|
||||
|
||||
queries.add(create.query(query.getSQL(true)));
|
||||
}
|
||||
|
||||
return create.batch(queries).execute();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1195,6 +1195,14 @@ public class Factory implements FactoryOperations {
|
||||
return new BatchMultiple(this, queries);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public final Batch batch(Collection<? extends Query> queries) {
|
||||
return batch(queries.toArray(new Query[queries.size()]));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user