1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }