[#6148] Add Queries.executeBatch()

This commit is contained in:
lukaseder 2017-04-24 16:06:31 +02:00
parent bcf6c14f1d
commit 1b5309be62
2 changed files with 37 additions and 5 deletions

View File

@ -34,8 +34,11 @@
*/
package org.jooq;
import java.sql.Statement;
import java.util.stream.Stream;
import org.jooq.exception.DetachedException;
/**
* A wrapper for a collection of queries.
*
@ -74,9 +77,31 @@ public interface Queries extends QueryPart, Iterable<Query> {
/**
* Execute all queries one-by-one and return all results.
* <p>
* This is a convenience method for calling {@link ResultQuery#fetchMany()}
* on each individual {@link ResultQuery}, or {@link Query#execute()} on all
* others.
* This is a convenience method for executing individual {@link #queries()}.
* <p>
* If this {@link Queries} reference is attached to a {@link Configuration},
* then that <code>configuration</code> is used through
* {@link DSLContext#fetchMany(ResultQuery)} or
* {@link DSLContext#execute(Query)}. If this <code>queries</code> reference
* is unattached, then each individual {@link ResultQuery#fetchMany()} or
* {@link Query#execute()} method is called.
*
* @throws DetachedException If this <code>queries</code> reference is
* unattached and at least one of the contained
* {@link #queries()} is also unattached.
*/
Results fetchMany();
/**
* Sends the entire batch of queries to the server and executes them using a
* JDBC {@link Statement#executeBatch()} operation.
* <p>
* This {@link Queries} reference must be attached to a
* {@link Configuration}.
*
* @throws DetachedException If this <code>queries</code> reference is
* unattached.
* @see DSLContext#batch(Queries)
*/
int[] executeBatch();
}

View File

@ -44,6 +44,7 @@ import org.jooq.AttachableInternal;
import org.jooq.Clause;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.DSLContext;
import org.jooq.Queries;
import org.jooq.Query;
import org.jooq.ResultQuery;
@ -103,16 +104,22 @@ final class QueriesImpl extends AbstractQueryPart implements Queries, Attachable
@Override
public final Results fetchMany() {
ResultsImpl results = new ResultsImpl(configuration());
DSLContext ctx = DSL.using(configuration());
for (Query query : this)
if (query instanceof ResultQuery)
results.resultsOrRows.addAll(((ResultQuery<?>) query).fetchMany().resultsOrRows());
results.resultsOrRows.addAll(ctx.fetchMany((ResultQuery<?>) query).resultsOrRows());
else
results.resultsOrRows.add(new ResultOrRowsImpl(query.execute()));
results.resultsOrRows.add(new ResultOrRowsImpl(ctx.execute(query)));
return results;
}
@Override
public final int[] executeBatch() {
return DSL.using(configuration()).batch(this).execute();
}
// ------------------------------------------------------------------------
// Attachable API
// ------------------------------------------------------------------------