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.jetbrains.annotations.NotNull;
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 @NotNull
52 @Override
53 public BatchInsertModel<T> build() {
54 return BatchInsertModel.withRecords(records)
55 .withTable(table)
56 .withColumnMappings(columnMappings)
57 .build();
58 }
59
60 @SafeVarargs
61 public static <T> IntoGatherer<T> insert(T... records) {
62 return BatchInsertDSL.insert(Arrays.asList(records));
63 }
64
65 public static <T> IntoGatherer<T> insert(Collection<T> records) {
66 return new IntoGatherer<>(records);
67 }
68
69 public static class IntoGatherer<T> {
70 private final Collection<T> records;
71
72 private IntoGatherer(Collection<T> records) {
73 this.records = records;
74 }
75
76 public BatchInsertDSL<T> into(SqlTable table) {
77 return new Builder<T>().withRecords(records).withTable(table).build();
78 }
79 }
80
81 public class ColumnMappingFinisher<F> {
82 private final SqlColumn<F> column;
83
84 public ColumnMappingFinisher(SqlColumn<F> column) {
85 this.column = column;
86 }
87
88 public BatchInsertDSL<T> toProperty(String property) {
89 columnMappings.add(PropertyMapping.of(column, property));
90 return BatchInsertDSL.this;
91 }
92
93 public BatchInsertDSL<T> toNull() {
94 columnMappings.add(NullMapping.of(column));
95 return BatchInsertDSL.this;
96 }
97
98 public BatchInsertDSL<T> toConstant(String constant) {
99 columnMappings.add(ConstantMapping.of(column, constant));
100 return BatchInsertDSL.this;
101 }
102
103 public BatchInsertDSL<T> toStringConstant(String constant) {
104 columnMappings.add(StringConstantMapping.of(column, constant));
105 return BatchInsertDSL.this;
106 }
107
108 public BatchInsertDSL<T> toRow() {
109 columnMappings.add(RowMapping.of(column));
110 return BatchInsertDSL.this;
111 }
112 }
113
114 public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> {
115 final Collection<T> records = new ArrayList<>();
116 SqlTable table;
117 final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
118
119 public B withRecords(Collection<T> records) {
120 this.records.addAll(records);
121 return getThis();
122 }
123
124 public B withTable(SqlTable table) {
125 this.table = table;
126 return getThis();
127 }
128
129 public B withColumnMappings(Collection<? extends AbstractColumnMapping> columnMappings) {
130 this.columnMappings.addAll(columnMappings);
131 return getThis();
132 }
133
134 protected abstract B getThis();
135 }
136
137 public static class Builder<T> extends AbstractBuilder<T, Builder<T>> {
138 @Override
139 protected Builder<T> getThis() {
140 return this;
141 }
142
143 public BatchInsertDSL<T> build() {
144 return new BatchInsertDSL<>(this);
145 }
146 }
147 }