001package org.itsallcode.jdbc.batch;
002
003import org.itsallcode.jdbc.SimpleStatement;
004
005/**
006 * A batch handler for SQL statements.
007 */
008public class StatementBatch implements AutoCloseable {
009
010    private final Batch batch;
011    private final SimpleStatement statement;
012
013    /**
014     * Create a new instance.
015     * 
016     * @param statement    the statement
017     * @param maxBatchSize maximum batch size
018     */
019    public StatementBatch(final SimpleStatement statement, final int maxBatchSize) {
020        this.statement = statement;
021        this.batch = new Batch(maxBatchSize, statement, statement::executeBatch);
022    }
023
024    /**
025     * Add a new SQL statement to the batch.
026     * 
027     * @param sql SQL statement
028     */
029    public void addBatch(final String sql) {
030        statement.addBatch(sql);
031        this.batch.addBatch();
032    }
033
034    @Override
035    public void close() {
036        this.batch.close();
037    }
038}