From 9abd23a91685f29a9065b3217be455f1f92f554b Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 20 Jan 2015 14:11:51 +0100 Subject: [PATCH] [#3973] Add better support for re-using named bind values --- .../src/main/java/org/jooq/BatchBindStep.java | 49 ++++++++++++++- .../main/java/org/jooq/impl/BatchSingle.java | 60 ++++++++++++++++--- 2 files changed, 99 insertions(+), 10 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/BatchBindStep.java b/jOOQ/src/main/java/org/jooq/BatchBindStep.java index 7dadef8b74..0a5ff0f01b 100644 --- a/jOOQ/src/main/java/org/jooq/BatchBindStep.java +++ b/jOOQ/src/main/java/org/jooq/BatchBindStep.java @@ -41,6 +41,7 @@ package org.jooq; import java.sql.Statement; +import java.util.Map; /** * This type is used for the {@link Batch}'s DSL API. @@ -54,14 +55,56 @@ import java.sql.Statement; public interface BatchBindStep extends Batch { /** - * Set bind values on the batch statement. + * Set indexed bind values onto the batch statement. + *

+ * The argument array of bindValues will be set onto the + * indexed bind values of the batch statement: + *

+ *

+ * "Unmatched" bind values will be left unmodified: + *

*/ BatchBindStep bind(Object... bindValues); /** - * Set several bind values on the batch statement. + * Set several indexed bind values onto the batch statement. *

* This is the same as calling {@link #bind(Object...)} several times. */ - BatchBindStep bind(Object[][] bindValues); + BatchBindStep bind(Object[]... bindValues); + + /** + * Set named bind values onto the batch statement. + *

+ * The argument map of namedBindValues will be set onto the + * named bind values of the batch statement: + *

+ *

+ * "Unmatched" bind values will be left unmodified: + *

+ */ + BatchBindStep bind(Map namedBindValues); + + /** + * Set several named bind values onto the batch statement. + *

+ * This is the same as calling {@link #bind(Map...)} several times. + */ + BatchBindStep bind(Map... namedBindValues); } diff --git a/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java b/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java index d8e543f140..3d7891bbe5 100644 --- a/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java +++ b/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java @@ -49,7 +49,10 @@ import static org.jooq.impl.Utils.visitAll; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.jooq.BatchBindStep; import org.jooq.Configuration; @@ -58,6 +61,7 @@ import org.jooq.DataType; import org.jooq.ExecuteContext; import org.jooq.ExecuteListener; import org.jooq.Field; +import org.jooq.Param; import org.jooq.Query; import org.jooq.exception.ControlFlowSignal; @@ -69,18 +73,32 @@ class BatchSingle implements BatchBindStep { /** * Generated UID */ - private static final long serialVersionUID = 3793967258181493207L; + private static final long serialVersionUID = 3793967258181493207L; - private final DSLContext create; - private final Configuration configuration; - private final Query query; - private final List allBindValues; + private final DSLContext create; + private final Configuration configuration; + private final Query query; + private final Map> nameToIndexMapping; + private final List allBindValues; public BatchSingle(Configuration configuration, Query query) { this.create = DSL.using(configuration); this.configuration = configuration; this.query = query; this.allBindValues = new ArrayList(); + this.nameToIndexMapping = new LinkedHashMap>(); + + int i = 0; + for (Entry> entry : query.getParams().entrySet()) { + List list = nameToIndexMapping.get(entry.getKey()); + + if (list == null) { + list = new ArrayList(); + nameToIndexMapping.put(entry.getKey(), list); + } + + list.add(i++); + } } @Override @@ -90,11 +108,39 @@ class BatchSingle implements BatchBindStep { } @Override - public final BatchSingle bind(Object[][] bindValues) { - for (Object[] v : bindValues) { + public final BatchSingle bind(Object[]... bindValues) { + for (Object[] v : bindValues) bind(v); + + return this; + } + + @SuppressWarnings("unchecked") + @Override + public final BatchSingle bind(Map namedBindValues) { + return bind(new Map[] { namedBindValues }); + } + + @Override + public final BatchSingle bind(Map... namedBindValues) { + List defaultValues = query.getBindValues(); + + Object[][] bindValues = new Object[namedBindValues.length][]; + for (int row = 0; row < bindValues.length; row++) { + bindValues[row] = defaultValues.toArray(); + + for (Entry entry : namedBindValues[row].entrySet()) { + List indexes = nameToIndexMapping.get(entry.getKey()); + + if (indexes != null) { + for (int index : indexes) { + bindValues[row][index] = entry.getValue(); + } + } + } } + bind(bindValues); return this; }