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.Objects;
19  import java.util.Optional;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.SqlTable;
23  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
24  import org.mybatis.dynamic.sql.insert.render.InsertSelectRenderer;
25  import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
26  import org.mybatis.dynamic.sql.render.RenderingStrategy;
27  import org.mybatis.dynamic.sql.select.SelectModel;
28  
29  public class InsertSelectModel {
30      private final SqlTable table;
31      private final @Nullable InsertColumnListModel columnList;
32      private final SelectModel selectModel;
33      private final StatementConfiguration statementConfiguration;
34  
35      private InsertSelectModel(Builder builder) {
36          table = Objects.requireNonNull(builder.table);
37          columnList = builder.columnList;
38          selectModel = Objects.requireNonNull(builder.selectModel);
39          statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
40      }
41  
42      public SqlTable table() {
43          return table;
44      }
45  
46      public SelectModel selectModel() {
47          return selectModel;
48      }
49  
50      public Optional<InsertColumnListModel> columnList() {
51          return Optional.ofNullable(columnList);
52      }
53  
54      public StatementConfiguration statementConfiguration() {
55          return statementConfiguration;
56      }
57  
58      public InsertSelectStatementProvider render(RenderingStrategy renderingStrategy) {
59          return InsertSelectRenderer.withInsertSelectModel(this)
60                  .withRenderingStrategy(renderingStrategy)
61                  .build()
62                  .render();
63      }
64  
65      public static Builder withTable(SqlTable table) {
66          return new Builder().withTable(table);
67      }
68  
69      public static class Builder {
70          private @Nullable SqlTable table;
71          private @Nullable InsertColumnListModel columnList;
72          private @Nullable SelectModel selectModel;
73          private @Nullable StatementConfiguration statementConfiguration;
74  
75          public Builder withTable(SqlTable table) {
76              this.table = table;
77              return this;
78          }
79  
80          public Builder withColumnList(@Nullable InsertColumnListModel columnList) {
81              this.columnList = columnList;
82              return this;
83          }
84  
85          public Builder withSelectModel(SelectModel selectModel) {
86              this.selectModel = selectModel;
87              return this;
88          }
89  
90          public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
91              this.statementConfiguration = statementConfiguration;
92              return this;
93          }
94  
95          public InsertSelectModel build() {
96              return new InsertSelectModel(this);
97          }
98      }
99  }