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.Arrays;
19  import java.util.List;
20  import java.util.Objects;
21  import java.util.function.Consumer;
22  
23  import org.jspecify.annotations.Nullable;
24  import org.mybatis.dynamic.sql.SqlColumn;
25  import org.mybatis.dynamic.sql.SqlTable;
26  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
27  import org.mybatis.dynamic.sql.select.SelectModel;
28  import org.mybatis.dynamic.sql.util.Buildable;
29  import org.mybatis.dynamic.sql.util.ConfigurableStatement;
30  
31  public class InsertSelectDSL implements Buildable<InsertSelectModel>, ConfigurableStatement<InsertSelectDSL> {
32  
33      private final SqlTable table;
34      private final @Nullable InsertColumnListModel columnList;
35      private final SelectModel selectModel;
36      private final StatementConfiguration statementConfiguration = new StatementConfiguration();
37  
38      private InsertSelectDSL(SqlTable table, InsertColumnListModel columnList, SelectModel selectModel) {
39          this.table = Objects.requireNonNull(table);
40          this.selectModel = Objects.requireNonNull(selectModel);
41          this.columnList = columnList;
42      }
43  
44      private InsertSelectDSL(SqlTable table, SelectModel selectModel) {
45          this.table = Objects.requireNonNull(table);
46          this.selectModel = Objects.requireNonNull(selectModel);
47          this.columnList = null;
48      }
49  
50      @Override
51      public InsertSelectModel build() {
52          return InsertSelectModel.withTable(table)
53                  .withColumnList(columnList)
54                  .withSelectModel(selectModel)
55                  .withStatementConfiguration(statementConfiguration)
56                  .build();
57      }
58  
59      public static InsertColumnGatherer insertInto(SqlTable table) {
60          return new InsertColumnGatherer(table);
61      }
62  
63      @Override
64      public InsertSelectDSL configureStatement(Consumer<StatementConfiguration> consumer) {
65          consumer.accept(statementConfiguration);
66          return this;
67      }
68  
69      public static class InsertColumnGatherer {
70          private final SqlTable table;
71  
72          private InsertColumnGatherer(SqlTable table) {
73              this.table = table;
74          }
75  
76          public SelectGatherer withColumnList(SqlColumn<?>... columns) {
77              return withColumnList(Arrays.asList(columns));
78          }
79  
80          public SelectGatherer withColumnList(List<SqlColumn<?>> columns) {
81              return new SelectGatherer(table, columns);
82          }
83  
84          public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
85              return new InsertSelectDSL(table, selectModelBuilder.build());
86          }
87      }
88  
89      public static class SelectGatherer {
90          private final SqlTable table;
91          private final InsertColumnListModel columnList;
92  
93          private SelectGatherer(SqlTable table, List<SqlColumn<?>> columns) {
94              this.table = table;
95              columnList = InsertColumnListModel.of(columns);
96          }
97  
98          public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
99              return new InsertSelectDSL(table, columnList, selectModelBuilder.build());
100         }
101     }
102 }