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.Collection;
20  import java.util.List;
21  import java.util.Objects;
22  import java.util.function.Supplier;
23  
24  import org.jspecify.annotations.Nullable;
25  import org.mybatis.dynamic.sql.SqlColumn;
26  import org.mybatis.dynamic.sql.SqlTable;
27  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
28  import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
29  import org.mybatis.dynamic.sql.util.Buildable;
30  import org.mybatis.dynamic.sql.util.ConstantMapping;
31  import org.mybatis.dynamic.sql.util.NullMapping;
32  import org.mybatis.dynamic.sql.util.StringConstantMapping;
33  import org.mybatis.dynamic.sql.util.ValueMapping;
34  import org.mybatis.dynamic.sql.util.ValueOrNullMapping;
35  import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
36  
37  public class GeneralInsertDSL implements Buildable<GeneralInsertModel> {
38      private final List<AbstractColumnMapping> columnMappings;
39      private final SqlTable table;
40  
41      private GeneralInsertDSL(Builder builder) {
42          table = Objects.requireNonNull(builder.table);
43          columnMappings = builder.columnMappings;
44      }
45  
46      public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
47          return new SetClauseFinisher<>(column);
48      }
49  
50      @Override
51      public GeneralInsertModel build() {
52          return new GeneralInsertModel.Builder()
53                  .withTable(table)
54                  .withInsertMappings(columnMappings)
55                  .withStatementConfiguration(new StatementConfiguration()) // nothing configurable in this statement yet
56                  .build();
57      }
58  
59      public static GeneralInsertDSL insertInto(SqlTable table) {
60          return new GeneralInsertDSL.Builder().withTable(table).build();
61      }
62  
63      public class SetClauseFinisher<T> {
64  
65          private final SqlColumn<T> column;
66  
67          public SetClauseFinisher(SqlColumn<T> column) {
68              this.column = column;
69          }
70  
71          public GeneralInsertDSL toNull() {
72              columnMappings.add(NullMapping.of(column));
73              return GeneralInsertDSL.this;
74          }
75  
76          public GeneralInsertDSL toConstant(String constant) {
77              columnMappings.add(ConstantMapping.of(column, constant));
78              return GeneralInsertDSL.this;
79          }
80  
81          public GeneralInsertDSL toStringConstant(String constant) {
82              columnMappings.add(StringConstantMapping.of(column, constant));
83              return GeneralInsertDSL.this;
84          }
85  
86          public GeneralInsertDSL toValue(T value) {
87              return toValue(() -> value);
88          }
89  
90          public GeneralInsertDSL toValue(Supplier<T> valueSupplier) {
91              columnMappings.add(ValueMapping.of(column, valueSupplier));
92              return GeneralInsertDSL.this;
93          }
94  
95          public GeneralInsertDSL toValueOrNull(@Nullable T value) {
96              return toValueOrNull(() -> value);
97          }
98  
99          public GeneralInsertDSL toValueOrNull(Supplier<@Nullable T> valueSupplier) {
100             columnMappings.add(ValueOrNullMapping.of(column, valueSupplier));
101             return GeneralInsertDSL.this;
102         }
103 
104         public GeneralInsertDSL toValueWhenPresent(@Nullable T value) {
105             return toValueWhenPresent(() -> value);
106         }
107 
108         public GeneralInsertDSL toValueWhenPresent(Supplier<@Nullable T> valueSupplier) {
109             columnMappings.add(ValueWhenPresentMapping.of(column, valueSupplier));
110             return GeneralInsertDSL.this;
111         }
112     }
113 
114     public static class Builder {
115         private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
116         private @Nullable SqlTable table;
117 
118         public Builder withTable(SqlTable table) {
119             this.table = table;
120             return this;
121         }
122 
123         public Builder withColumnMappings(Collection<? extends AbstractColumnMapping> columnMappings) {
124             this.columnMappings.addAll(columnMappings);
125             return this;
126         }
127 
128         public GeneralInsertDSL build() {
129             return new GeneralInsertDSL(this);
130         }
131     }
132 }