Skip to content

Commit

Permalink
Fix sonar warning
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Oct 13, 2024
1 parent 60a8726 commit d8b6f33
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/main/java/org/itsallcode/jdbc/BatchInsertBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.sql.PreparedStatement;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Stream;

Expand All @@ -17,15 +18,15 @@
public class BatchInsertBuilder<T> {
private static final Logger LOG = Logger.getLogger(BatchInsertBuilder.class.getName());
private static final int DEFAULT_MAX_BATCH_SIZE = 200_000;
private final SimpleConnection connection;
private final Function<String, SimplePreparedStatement> statementFactory;
private final Context context;
private String sql;
private RowPreparedStatementSetter<T> mapper;
private Iterator<T> rows;
private int maxBatchSize = DEFAULT_MAX_BATCH_SIZE;

BatchInsertBuilder(final SimpleConnection connection, final Context context) {
this.connection = connection;
BatchInsertBuilder(final Function<String, SimplePreparedStatement> statementFactory, final Context context) {
this.statementFactory = statementFactory;
this.context = context;
}

Expand Down Expand Up @@ -129,7 +130,7 @@ public void start() {
Objects.requireNonNull(this.mapper, "mapper");
Objects.requireNonNull(this.rows, "rows");
LOG.finest(() -> "Running insert statement '" + sql + "'...");
final SimplePreparedStatement statement = connection.prepareStatement(sql);
final SimplePreparedStatement statement = statementFactory.apply(sql);
try (BatchInsert<T> batch = new BatchInsert<>(statement, this.mapper, this.maxBatchSize)) {
while (rows.hasNext()) {
batch.add(rows.next());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/itsallcode/jdbc/SimpleConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SimplePreparedStatement prepareStatement(final String sql) {
* @return batch insert builder
*/
public <T> BatchInsertBuilder<T> batchInsert(final Class<T> rowType) {
return new BatchInsertBuilder<>(this, context);
return new BatchInsertBuilder<>(this::prepareStatement, context);
}

private PreparedStatement prepare(final String sql) {
Expand Down

0 comments on commit d8b6f33

Please sign in to comment.