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.List;
20 import java.util.Objects;
21 import java.util.stream.Stream;
22
23 import org.jspecify.annotations.Nullable;
24 import org.mybatis.dynamic.sql.SqlTable;
25 import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
26 import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer;
27 import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
28 import org.mybatis.dynamic.sql.render.RenderingStrategy;
29 import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
30 import org.mybatis.dynamic.sql.util.Validator;
31
32 public class GeneralInsertModel {
33
34 private final SqlTable table;
35 private final List<AbstractColumnMapping> insertMappings;
36 private final StatementConfiguration statementConfiguration;
37
38 private GeneralInsertModel(Builder builder) {
39 table = Objects.requireNonNull(builder.table);
40 Validator.assertNotEmpty(builder.insertMappings, "ERROR.6");
41 insertMappings = builder.insertMappings;
42 statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
43 }
44
45 public Stream<AbstractColumnMapping> columnMappings() {
46 return insertMappings.stream();
47 }
48
49 public SqlTable table() {
50 return table;
51 }
52
53 public StatementConfiguration statementConfiguration() {
54 return statementConfiguration;
55 }
56
57 public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) {
58 return GeneralInsertRenderer.withInsertModel(this)
59 .withRenderingStrategy(renderingStrategy)
60 .build()
61 .render();
62 }
63
64 public static class Builder {
65 private @Nullable SqlTable table;
66 private final List<AbstractColumnMapping> insertMappings = new ArrayList<>();
67 private @Nullable StatementConfiguration statementConfiguration;
68
69 public Builder withTable(SqlTable table) {
70 this.table = table;
71 return this;
72 }
73
74 public Builder withInsertMappings(List<? extends AbstractColumnMapping> insertMappings) {
75 this.insertMappings.addAll(insertMappings);
76 return this;
77 }
78
79 public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
80 this.statementConfiguration = statementConfiguration;
81 return this;
82 }
83
84 public GeneralInsertModel build() {
85 return new GeneralInsertModel(this);
86 }
87 }
88 }