View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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       * Returns a list of InsertStatement objects. This is useful for MyBatis batch support.
36       *
37       * @return a List of InsertStatements
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       * Returns the generated SQL for this batch. This is useful for Spring JDBC batch support.
53       *
54       * @return the generated INSERT statement
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  }