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.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 }