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.util.AbstractColumnMapping;
28  import org.mybatis.dynamic.sql.util.Buildable;
29  import org.mybatis.dynamic.sql.util.ConstantMapping;
30  import org.mybatis.dynamic.sql.util.NullMapping;
31  import org.mybatis.dynamic.sql.util.PropertyMapping;
32  import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping;
33  import org.mybatis.dynamic.sql.util.RowMapping;
34  import org.mybatis.dynamic.sql.util.StringConstantMapping;
35  
36  public class InsertDSL<T> implements Buildable<InsertModel<T>> {
37  
38      private final T row;
39      private final SqlTable table;
40      private final List<AbstractColumnMapping> columnMappings;
41  
42      private InsertDSL(Builder<T> builder) {
43          this.row = Objects.requireNonNull(builder.row);
44          this.table = Objects.requireNonNull(builder.table);
45          columnMappings = builder.columnMappings;
46      }
47  
48      public <F> ColumnMappingFinisher<F> map(SqlColumn<F> column) {
49          return new ColumnMappingFinisher<>(column);
50      }
51  
52      @Override
53      public InsertModel<T> build() {
54          return InsertModel.withRow(row)
55                  .withTable(table)
56                  .withColumnMappings(columnMappings)
57                  .build();
58      }
59  
60      public static <T> IntoGatherer<T> insert(T row) {
61          return new IntoGatherer<>(row);
62      }
63  
64      public static class IntoGatherer<T> {
65          private final T row;
66  
67          private IntoGatherer(T row) {
68              this.row = row;
69          }
70  
71          public InsertDSL<T> into(SqlTable table) {
72              return new InsertDSL.Builder<T>().withRow(row).withTable(table).build();
73          }
74      }
75  
76      public class ColumnMappingFinisher<F> {
77          private final SqlColumn<F> column;
78  
79          public ColumnMappingFinisher(SqlColumn<F> column) {
80              this.column = column;
81          }
82  
83          public InsertDSL<T> toProperty(String property) {
84              columnMappings.add(PropertyMapping.of(column, property));
85              return InsertDSL.this;
86          }
87  
88          public InsertDSL<T> toPropertyWhenPresent(String property, Supplier<?> valueSupplier) {
89              columnMappings.add(PropertyWhenPresentMapping.of(column, property, valueSupplier));
90              return InsertDSL.this;
91          }
92  
93          public InsertDSL<T> toNull() {
94              columnMappings.add(NullMapping.of(column));
95              return InsertDSL.this;
96          }
97  
98          public InsertDSL<T> toConstant(String constant) {
99              columnMappings.add(ConstantMapping.of(column, constant));
100             return InsertDSL.this;
101         }
102 
103         public InsertDSL<T> toStringConstant(String constant) {
104             columnMappings.add(StringConstantMapping.of(column, constant));
105             return InsertDSL.this;
106         }
107 
108         public InsertDSL<T> toRow() {
109             columnMappings.add(RowMapping.of(column));
110             return InsertDSL.this;
111         }
112     }
113 
114     public static class Builder<T> {
115         private @Nullable T row;
116         private @Nullable SqlTable table;
117         private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
118 
119         public Builder<T> withRow(T row) {
120             this.row = row;
121             return this;
122         }
123 
124         public Builder<T> withTable(SqlTable table) {
125             this.table = table;
126             return this;
127         }
128 
129         public Builder<T> withColumnMappings(Collection<? extends AbstractColumnMapping> columnMappings) {
130             this.columnMappings.addAll(columnMappings);
131             return this;
132         }
133 
134         public InsertDSL<T> build() {
135             return new InsertDSL<>(this);
136         }
137     }
138 }