All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fluentjdbc.DatabaseBulkDeleteBuilder Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
package org.fluentjdbc;

import org.fluentjdbc.util.ExceptionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;

public class DatabaseBulkDeleteBuilder extends DatabaseStatement
        implements DatabaseBulkQueriable> {

    private final List whereConditions = new ArrayList<>();
    private final List> whereParameters = new ArrayList<>();
    private final DatabaseTableImpl table;
    private final Iterable objects;

    public DatabaseBulkDeleteBuilder(DatabaseTableImpl table, Iterable objects) {
        this.table = table;
        this.objects = objects;
    }

    @Override
    public DatabaseBulkDeleteBuilder where(String field, Function value) {
        whereConditions.add(field + " = ?");
        whereParameters.add(value);
        return this;
    }

    public int execute(Connection connection) {
        String deleteStatement = createDeleteStatement(table.getTableName(), whereConditions);
        try (PreparedStatement statement = connection.prepareStatement(deleteStatement)) {
            addBatch(statement, objects, whereParameters);
            int[] counts = statement.executeBatch();
            return IntStream.of(counts).sum();
        } catch (SQLException e) {
            throw ExceptionUtil.softenCheckedException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy