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