Class AbstractBatchQuery<T extends AbstractBatchQuery<T>>
java.lang.Object
com.github.darksoulq.abyssallib.common.database.relational.AbstractBatchQuery<T>
- Type Parameters:
T- The specific implementation type, allowing for fluent method chaining.
- Direct Known Subclasses:
BatchQuery, BatchQuery, BatchQuery, BatchQuery, BatchQuery
An abstract base class for handling batch database operations such as INSERT, REPLACE, or INSERT IGNORE.
This class provides a fluent API to build a collection of records and execute them in a single batch
to optimize performance and reduce database round-trips.
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractBatchQuery(Connection connection, String table, ExecutorService asyncPool, String... columns) Constructs a new AbstractBatchQuery instance. -
Method Summary
Modifier and TypeMethodDescriptionAdds a single row of data to the internal batch list.intexecute()Compiles the SQL statement and executes the batch against the database.Executes the batch operation asynchronously using the internal thread pool.insert()Sets the batch operation mode to standard INSERT.Sets the batch operation mode to INSERT IGNORE.replace()Sets the batch operation mode to REPLACE.
-
Constructor Details
-
AbstractBatchQuery
public AbstractBatchQuery(Connection connection, String table, ExecutorService asyncPool, String... columns) Constructs a new AbstractBatchQuery instance.- Parameters:
connection- The JDBCConnectionto be used.table- The name of the target table.asyncPool- TheExecutorServicefor asynchronous tasks.columns- The specific column names to be populated in this batch.
-
-
Method Details
-
insert
Sets the batch operation mode to standard INSERT.- Returns:
- The current instance cast to type
Tfor chaining.
-
replace
Sets the batch operation mode to REPLACE.- Returns:
- The current instance cast to type
Tfor chaining.
-
insertIgnore
Sets the batch operation mode to INSERT IGNORE.- Returns:
- The current instance cast to type
Tfor chaining.
-
add
Adds a single row of data to the internal batch list.- Parameters:
values- The values to be inserted. The length must match the number of columns defined.- Returns:
- The current instance cast to type
Tfor chaining. - Throws:
IllegalArgumentException- If the number of values does not match the number of columns.
-
execute
public int execute()Compiles the SQL statement and executes the batch against the database.- Returns:
- The sum of all rows affected by the batch operation.
- Throws:
RuntimeException- If aSQLExceptionoccurs during execution.
-
executeAsync
Executes the batch operation asynchronously using the internal thread pool.- Returns:
- A
CompletableFuturethat resolves to the total number of affected rows.
-