[jOOQ/jOOQ#14158] Add ExecuteContext::batchMode to indicate what type of batch is being executed

This commit is contained in:
Lukas Eder 2022-11-01 11:37:26 +01:00
parent 3e38d3b0ed
commit 3821ba5b4e
5 changed files with 60 additions and 18 deletions

View File

@ -150,6 +150,13 @@ public interface ExecuteContext extends Scope {
/**
* The batch execution mode.
*/
@NotNull
BatchMode batchMode();
/**
* The jOOQ {@link Query} objects that are being executed in batch mode, or
@ -158,6 +165,8 @@ public interface ExecuteContext extends Scope {
* If a single <code>Query</code> is executed in non-batch mode, this will
* return an array of length <code>1</code>, containing that
* <code>Query</code>
* <p>
* Refer to {@link #batchMode()} to decide how to interpret this content.
*
* @see #query()
* @see #routine()
@ -423,4 +432,29 @@ public interface ExecuteContext extends Scope {
* <code>{@link Settings#getFetchServerOutputSize()} &gt; 0</code>.
*/
void serverOutput(String[] output);
/**
* The batch mode, which helps interpret the contents of
* {@link ExecuteContext#batchQueries()}.
*/
enum BatchMode {
/**
* No {@link Batch} is being executed, but a single
* {@link ExecuteContext#query()} or a {@link ExecuteContext#routine()}.
*/
NONE,
/**
* A {@link Batch} with a single {@link PreparedStatement} and multiple
* bind variable sets is being executed.
*/
SINGLE,
/**
* A {@link Batch} with multiple static {@link Statement} and no bind
* variables is being executed.
*/
MULTIPLE
}
}

View File

@ -42,6 +42,7 @@ import java.sql.SQLException;
import org.jooq.Configuration;
import org.jooq.ExecuteContext;
import org.jooq.ExecuteContext.BatchMode;
import org.jooq.ExecuteListener;
import org.jooq.Query;
import org.jooq.conf.SettingsTools;
@ -89,7 +90,7 @@ final class BatchMultiple extends AbstractBatch {
}
static int[] execute(final Configuration configuration, final Query[] queries) {
ExecuteContext ctx = new DefaultExecuteContext(configuration, queries);
ExecuteContext ctx = new DefaultExecuteContext(configuration, BatchMode.MULTIPLE, queries);
ExecuteListener listener = ExecuteListeners.get(ctx);
Connection connection = ctx.connection();

View File

@ -57,6 +57,7 @@ import org.jooq.ExecuteContext;
import org.jooq.ExecuteListener;
import org.jooq.Param;
import org.jooq.Query;
import org.jooq.ExecuteContext.BatchMode;
import org.jooq.conf.SettingsTools;
import org.jooq.exception.ControlFlowSignal;
import org.jooq.impl.R2DBC.BatchSingleSubscriber;
@ -186,7 +187,7 @@ final class BatchSingle extends AbstractBatch implements BatchBindStep {
}
private final int[] executePrepared() {
DefaultExecuteContext ctx = new DefaultExecuteContext(configuration, new Query[] { query });
DefaultExecuteContext ctx = new DefaultExecuteContext(configuration, BatchMode.SINGLE, new Query[] { query });
ExecuteListener listener = ExecuteListeners.get(ctx);
Connection connection = ctx.connection();

View File

@ -85,7 +85,6 @@ import org.jooq.tools.JooqLogger;
import org.jooq.tools.jdbc.JDBCUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
// ...
@ -112,7 +111,7 @@ class DefaultExecuteContext implements ExecuteContext {
private final Routine<?> routine;
private String sql;
private final boolean batch;
private final BatchMode batchMode;
private final Query[] batchQueries;
private final String[] batchSQL;
private final int[] batchRows;
@ -318,22 +317,22 @@ class DefaultExecuteContext implements ExecuteContext {
// ------------------------------------------------------------------------
DefaultExecuteContext(Configuration configuration) {
this(configuration, null, null, null);
this(configuration, BatchMode.NONE, null, null, null);
}
DefaultExecuteContext(Configuration configuration, Query[] batchQueries) {
this(configuration, null, batchQueries, null);
DefaultExecuteContext(Configuration configuration, BatchMode batchMode, Query[] batchQueries) {
this(configuration, batchMode, null, batchQueries, null);
}
DefaultExecuteContext(Configuration configuration, Query query) {
this(configuration, query, null, null);
this(configuration, BatchMode.NONE, query, null, null);
}
DefaultExecuteContext(Configuration configuration, Routine<?> routine) {
this(configuration, null, null, routine);
this(configuration, BatchMode.NONE, null, null, routine);
}
private DefaultExecuteContext(Configuration configuration, Query query, Query[] batchQueries, Routine<?> routine) {
private DefaultExecuteContext(Configuration configuration, BatchMode batchMode, Query query, Query[] batchQueries, Routine<?> routine) {
// [#4277] The ExecuteContext's Configuration will always return the same Connection,
// e.g. when running statements from sub-ExecuteContexts
@ -343,6 +342,7 @@ class DefaultExecuteContext implements ExecuteContext {
this.originalConfiguration = configuration;
this.derivedConfiguration = configuration.derive(new ExecuteContextConnectionProvider());
this.data = new DataMap();
this.batchMode = batchMode;
this.query = query;
@ -351,13 +351,11 @@ class DefaultExecuteContext implements ExecuteContext {
this.converterContext = new DefaultConverterContext(derivedConfiguration, data);
if (routine != null) {
this.batch = false;
this.batchQueries = null;
this.batchRows = null;
this.batchSQL = null;
}
else if (batchQueries != null) {
this.batch = true;
this.batchQueries = batchQueries;
this.batchRows = new int[batchQueries.length];
this.batchSQL = new String[batchQueries.length];
@ -365,13 +363,11 @@ class DefaultExecuteContext implements ExecuteContext {
Arrays.fill(this.batchRows, -1);
}
else if (query == null) {
this.batch = false;
this.batchQueries = null;
this.batchRows = null;
this.batchSQL = null;
}
else {
this.batch = false;
this.batchQueries = null;
this.batchRows = null;
this.batchSQL = null;
@ -414,7 +410,7 @@ class DefaultExecuteContext implements ExecuteContext {
}
// This can only be a BatchSingle or BatchMultiple execution
else if (batch) {
else if (batchMode != BatchMode.NONE) {
return ExecuteType.BATCH;
}
@ -494,9 +490,14 @@ class DefaultExecuteContext implements ExecuteContext {
@Override
public final BatchMode batchMode() {
return batchMode;
}
@Override
public final Query[] batchQueries() {
return batch
return batchMode != BatchMode.NONE
? batchQueries
: query() != null
? new Query[] { query() }
@ -524,7 +525,7 @@ class DefaultExecuteContext implements ExecuteContext {
@Override
public final String[] batchSQL() {
return batch
return batchMode != BatchMode.NONE
? batchSQL
: routine != null || query() != null
? new String[] { sql }
@ -663,7 +664,7 @@ class DefaultExecuteContext implements ExecuteContext {
@Override
public final int[] batchRows() {
return batch
return batchMode != BatchMode.NONE
? batchRows
: routine != null || query() != null
? new int[] { rows }

View File

@ -106,6 +106,11 @@ final class SimpleExecuteContext extends AbstractScope implements ExecuteContext
@Override
public final BatchMode batchMode() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public final Query[] batchQueries() {
throw new UnsupportedOperationException("Not implemented");