From a414d3467f135120927cae069ab35c5db80ad2dc Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 3 Mar 2012 15:43:58 +0000 Subject: [PATCH] [#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) for convenience --- .../org/jooq/util/spring/FactoryProxy.java | 5 ++++ .../jooq/test/_/testcases/GeneralTests.java | 6 ---- .../main/java/org/jooq/FactoryOperations.java | 18 ++++++++++++ .../main/java/org/jooq/impl/BatchSingle.java | 28 +++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/Factory.java | 8 ++++++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/jOOQ-spring/src/main/java/org/jooq/util/spring/FactoryProxy.java b/jOOQ-spring/src/main/java/org/jooq/util/spring/FactoryProxy.java index ea64713afc..6b0d405dcf 100644 --- a/jOOQ-spring/src/main/java/org/jooq/util/spring/FactoryProxy.java +++ b/jOOQ-spring/src/main/java/org/jooq/util/spring/FactoryProxy.java @@ -352,6 +352,11 @@ public class FactoryProxy implements FactoryOperations, MethodInterceptor { return getDelegate().batch(queries); } + @Override + public final Batch batch(Collection queries) { + return getDelegate().batch(queries); + } + @Override public final BatchBindStep batch(Query query) { return getDelegate().batch(query); diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/GeneralTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/GeneralTests.java index cc402c934d..c50cbe293b 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/GeneralTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/GeneralTests.java @@ -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 + * This essentially runs the following logic:
+     * Statement s = connection.createStatement();
+     *
+     * for (Query query : queries) {
+     *     s.addBatch(query.getSQL(true));
+     * }
+     *
+     * s.execute();
+     * 
+ * + * @see Statement#executeBatch() + */ + @Support + Batch batch(Collection queries); + /** * Execute a set of queries in batch mode (with bind values). *

diff --git a/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java b/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java index 7e46b6ff98..a7407d8838 100644 --- a/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java +++ b/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java @@ -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 queries = new ArrayList(); + + 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(); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index c6a4cb8551..ef373b6d48 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -1195,6 +1195,14 @@ public class Factory implements FactoryOperations { return new BatchMultiple(this, queries); } + /** + * {@inheritDoc} + */ + @Override + public final Batch batch(Collection queries) { + return batch(queries.toArray(new Query[queries.size()])); + } + /** * {@inheritDoc} */