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.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Objects;
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.RowMapping;
33 import org.mybatis.dynamic.sql.util.StringConstantMapping;
34
35 public class BatchInsertDSL<T> implements Buildable<BatchInsertModel<T>> {
36
37 private final Collection<T> records;
38 private final SqlTable table;
39 private final List<AbstractColumnMapping> columnMappings;
40
41 private BatchInsertDSL(AbstractBuilder<T, ?> builder) {
42 this.records = builder.records;
43 this.table = Objects.requireNonNull(builder.table);
44 this.columnMappings = builder.columnMappings;
45 }
46
47 public <F> ColumnMappingFinisher<F> map(SqlColumn<F> column) {
48 return new ColumnMappingFinisher<>(column);
49 }
50
51 @Override
52 public BatchInsertModel<T> build() {
53 return BatchInsertModel.withRecords(records)
54 .withTable(table)
55 .withColumnMappings(columnMappings)
56 .build();
57 }
58
59 @SafeVarargs
60 public static <T> BatchInsertDSL.IntoGatherer<T> insert(T... records) {
61 return insert(Arrays.asList(records));
62 }
63
64 public static <T> BatchInsertDSL.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 BatchInsertDSL<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 BatchInsertDSL<T> toProperty(String property) {
88 columnMappings.add(PropertyMapping.of(column, property));
89 return BatchInsertDSL.this;
90 }
91
92 public BatchInsertDSL<T> toNull() {
93 columnMappings.add(NullMapping.of(column));
94 return BatchInsertDSL.this;
95 }
96
97 public BatchInsertDSL<T> toConstant(String constant) {
98 columnMappings.add(ConstantMapping.of(column, constant));
99 return BatchInsertDSL.this;
100 }
101
102 public BatchInsertDSL<T> toStringConstant(String constant) {
103 columnMappings.add(StringConstantMapping.of(column, constant));
104 return BatchInsertDSL.this;
105 }
106
107 public BatchInsertDSL<T> toRow() {
108 columnMappings.add(RowMapping.of(column));
109 return BatchInsertDSL.this;
110 }
111 }
112
113 public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> {
114 final Collection<T> records = new ArrayList<>();
115 @Nullable SqlTable table;
116 final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
117
118 public B withRecords(Collection<T> records) {
119 this.records.addAll(records);
120 return getThis();
121 }
122
123 public B withTable(SqlTable table) {
124 this.table = table;
125 return getThis();
126 }
127
128 public B withColumnMappings(Collection<? extends AbstractColumnMapping> columnMappings) {
129 this.columnMappings.addAll(columnMappings);
130 return getThis();
131 }
132
133 protected abstract B getThis();
134 }
135
136 public static class Builder<T> extends AbstractBuilder<T, Builder<T>> {
137 @Override
138 protected Builder<T> getThis() {
139 return this;
140 }
141
142 public BatchInsertDSL<T> build() {
143 return new BatchInsertDSL<>(this);
144 }
145 }
146 }