1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.dynamic.sql.insert.render;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Objects;
22
23 import org.jspecify.annotations.Nullable;
24
25 public class BatchInsert<T> {
26 private final String insertStatement;
27 private final List<T> records;
28
29 private BatchInsert(Builder<T> builder) {
30 insertStatement = Objects.requireNonNull(builder.insertStatement);
31 records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
32 }
33
34
35
36
37
38
39 public List<InsertStatementProvider<T>> insertStatements() {
40 return records.stream()
41 .map(this::toInsertStatement)
42 .toList();
43 }
44
45 private InsertStatementProvider<T> toInsertStatement(T row) {
46 return DefaultInsertStatementProvider.withRow(row)
47 .withInsertStatement(insertStatement)
48 .build();
49 }
50
51
52
53
54
55
56 public String getInsertStatementSQL() {
57 return insertStatement;
58 }
59
60 public List<T> getRecords() {
61 return records;
62 }
63
64 public static <T> Builder<T> withRecords(List<T> records) {
65 return new Builder<T>().withRecords(records);
66 }
67
68 public static class Builder<T> {
69 private @Nullable String insertStatement;
70 private final List<T> records = new ArrayList<>();
71
72 public Builder<T> withInsertStatement(String insertStatement) {
73 this.insertStatement = insertStatement;
74 return this;
75 }
76
77 public Builder<T> withRecords(List<T> records) {
78 this.records.addAll(records);
79 return this;
80 }
81
82 public BatchInsert<T> build() {
83 return new BatchInsert<>(this);
84 }
85 }
86 }