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 DefaultMultiRowInsertStatementProvider<T> implements MultiRowInsertStatementProvider<T> {
26  
27      private final List<T> records;
28      private final String insertStatement;
29  
30      private DefaultMultiRowInsertStatementProvider(Builder<T> builder) {
31          insertStatement = Objects.requireNonNull(builder.insertStatement);
32          records = Collections.unmodifiableList(builder.records);
33      }
34  
35      @Override
36      public String getInsertStatement() {
37          return insertStatement;
38      }
39  
40      @Override
41      public List<T> getRecords() {
42          return records;
43      }
44  
45      public static class Builder<T> {
46          private final List<T> records = new ArrayList<>();
47          private @Nullable String insertStatement;
48  
49          public Builder<T> withRecords(List<T> records) {
50              this.records.addAll(records);
51              return this;
52          }
53  
54          public Builder<T> withInsertStatement(String insertStatement) {
55              this.insertStatement = insertStatement;
56              return this;
57          }
58  
59          public DefaultMultiRowInsertStatementProvider<T> build() {
60              return new DefaultMultiRowInsertStatementProvider<>(this);
61          }
62      }
63  }