org.fluentjdbc.DatabaseBulkDeleteBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-jdbc Show documentation
Show all versions of fluent-jdbc Show documentation
A Java library used to execute JDBC statements and build SQL
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