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.Collection;
20  import java.util.List;
21  import java.util.Objects;
22  
23  import org.mybatis.dynamic.sql.SqlColumn;
24  import org.mybatis.dynamic.sql.SqlTable;
25  import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
26  import org.mybatis.dynamic.sql.util.Buildable;
27  import org.mybatis.dynamic.sql.util.ConstantMapping;
28  import org.mybatis.dynamic.sql.util.NullMapping;
29  import org.mybatis.dynamic.sql.util.PropertyMapping;
30  import org.mybatis.dynamic.sql.util.RowMapping;
31  import org.mybatis.dynamic.sql.util.StringConstantMapping;
32  
33  public class MultiRowInsertDSL<T> implements Buildable<MultiRowInsertModel<T>> {
34  
35      private final Collection<T> records;
36      private final SqlTable table;
37      private final List<AbstractColumnMapping> columnMappings;
38  
39      private MultiRowInsertDSL(BatchInsertDSL.AbstractBuilder<T, ?> builder) {
40          this.records = builder.records;
41          this.table = Objects.requireNonNull(builder.table);
42          this.columnMappings = builder.columnMappings;
43      }
44  
45      public <F> ColumnMappingFinisher<F> map(SqlColumn<F> column) {
46          return new ColumnMappingFinisher<>(column);
47      }
48  
49      @Override
50      public MultiRowInsertModel<T> build() {
51          return MultiRowInsertModel.withRecords(records)
52                  .withTable(table)
53                  .withColumnMappings(columnMappings)
54                  .build();
55      }
56  
57      @SafeVarargs
58      public static <T> MultiRowInsertDSL.IntoGatherer<T> insert(T... records) {
59          return insert(Arrays.asList(records));
60      }
61  
62      public static <T> MultiRowInsertDSL.IntoGatherer<T> insert(Collection<T> records) {
63          return new IntoGatherer<>(records);
64      }
65  
66      public static class IntoGatherer<T> {
67          private final Collection<T> records;
68  
69          private IntoGatherer(Collection<T> records) {
70              this.records = records;
71          }
72  
73          public MultiRowInsertDSL<T> into(SqlTable table) {
74              return new Builder<T>().withRecords(records).withTable(table).build();
75          }
76      }
77  
78      public class ColumnMappingFinisher<F> {
79          private final SqlColumn<F> column;
80  
81          public ColumnMappingFinisher(SqlColumn<F> column) {
82              this.column = column;
83          }
84  
85          public MultiRowInsertDSL<T> toProperty(String property) {
86              columnMappings.add(PropertyMapping.of(column, property));
87              return MultiRowInsertDSL.this;
88          }
89  
90          public MultiRowInsertDSL<T> toNull() {
91              columnMappings.add(NullMapping.of(column));
92              return MultiRowInsertDSL.this;
93          }
94  
95          public MultiRowInsertDSL<T> toConstant(String constant) {
96              columnMappings.add(ConstantMapping.of(column, constant));
97              return MultiRowInsertDSL.this;
98          }
99  
100         public MultiRowInsertDSL<T> toStringConstant(String constant) {
101             columnMappings.add(StringConstantMapping.of(column, constant));
102             return MultiRowInsertDSL.this;
103         }
104 
105         public MultiRowInsertDSL<T> toRow() {
106             columnMappings.add(RowMapping.of(column));
107             return MultiRowInsertDSL.this;
108         }
109     }
110 
111     public static class Builder<T> extends BatchInsertDSL.AbstractBuilder<T, Builder<T>> {
112 
113         @Override
114         protected Builder<T> getThis() {
115             return this;
116         }
117 
118         public MultiRowInsertDSL<T> build() {
119             return new MultiRowInsertDSL<>(this);
120         }
121     }
122 }