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;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Objects;
21  import java.util.stream.Stream;
22  
23  import org.jspecify.annotations.Nullable;
24  import org.mybatis.dynamic.sql.SqlTable;
25  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
26  import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer;
27  import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
28  import org.mybatis.dynamic.sql.render.RenderingStrategy;
29  import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
30  import org.mybatis.dynamic.sql.util.Validator;
31  
32  public class GeneralInsertModel {
33  
34      private final SqlTable table;
35      private final List<AbstractColumnMapping> insertMappings;
36      private final StatementConfiguration statementConfiguration;
37  
38      private GeneralInsertModel(Builder builder) {
39          table = Objects.requireNonNull(builder.table);
40          Validator.assertNotEmpty(builder.insertMappings, "ERROR.6"); //$NON-NLS-1$
41          insertMappings = builder.insertMappings;
42          statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
43      }
44  
45      public Stream<AbstractColumnMapping> columnMappings() {
46          return insertMappings.stream();
47      }
48  
49      public SqlTable table() {
50          return table;
51      }
52  
53      public StatementConfiguration statementConfiguration() {
54          return statementConfiguration;
55      }
56  
57      public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) {
58          return GeneralInsertRenderer.withInsertModel(this)
59                  .withRenderingStrategy(renderingStrategy)
60                  .build()
61                  .render();
62      }
63  
64      public static class Builder {
65          private @Nullable SqlTable table;
66          private final List<AbstractColumnMapping> insertMappings = new ArrayList<>();
67          private @Nullable StatementConfiguration statementConfiguration;
68  
69          public Builder withTable(SqlTable table) {
70              this.table = table;
71              return this;
72          }
73  
74          public Builder withInsertMappings(List<? extends AbstractColumnMapping> insertMappings) {
75              this.insertMappings.addAll(insertMappings);
76              return this;
77          }
78  
79          public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
80              this.statementConfiguration = statementConfiguration;
81              return this;
82          }
83  
84          public GeneralInsertModel build() {
85              return new GeneralInsertModel(this);
86          }
87      }
88  }