1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.dynamic.sql.update;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.stream.Stream;
23
24 import org.jspecify.annotations.Nullable;
25 import org.mybatis.dynamic.sql.SqlTable;
26 import org.mybatis.dynamic.sql.common.CommonBuilder;
27 import org.mybatis.dynamic.sql.common.OrderByModel;
28 import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
29 import org.mybatis.dynamic.sql.render.RenderingStrategy;
30 import org.mybatis.dynamic.sql.update.render.UpdateRenderer;
31 import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
32 import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
33 import org.mybatis.dynamic.sql.util.Validator;
34 import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
35
36 public class UpdateModel {
37 private final SqlTable table;
38 private final @Nullable String tableAlias;
39 private final @Nullable EmbeddedWhereModel whereModel;
40 private final List<AbstractColumnMapping> columnMappings;
41 private final @Nullable Long limit;
42 private final @Nullable OrderByModel orderByModel;
43 private final StatementConfiguration statementConfiguration;
44
45 private UpdateModel(Builder builder) {
46 table = Objects.requireNonNull(builder.table());
47 whereModel = builder.whereModel();
48 columnMappings = Objects.requireNonNull(builder.columnMappings);
49 tableAlias = builder.tableAlias();
50 limit = builder.limit();
51 orderByModel = builder.orderByModel();
52 Validator.assertNotEmpty(columnMappings, "ERROR.17");
53 statementConfiguration = Objects.requireNonNull(builder.statementConfiguration());
54 }
55
56 public SqlTable table() {
57 return table;
58 }
59
60 public Optional<String> tableAlias() {
61 return Optional.ofNullable(tableAlias);
62 }
63
64 public Optional<EmbeddedWhereModel> whereModel() {
65 return Optional.ofNullable(whereModel);
66 }
67
68 public Stream<AbstractColumnMapping> columnMappings() {
69 return columnMappings.stream();
70 }
71
72 public Optional<Long> limit() {
73 return Optional.ofNullable(limit);
74 }
75
76 public Optional<OrderByModel> orderByModel() {
77 return Optional.ofNullable(orderByModel);
78 }
79
80 public StatementConfiguration statementConfiguration() {
81 return statementConfiguration;
82 }
83
84 public UpdateStatementProvider render(RenderingStrategy renderingStrategy) {
85 return UpdateRenderer.withUpdateModel(this)
86 .withRenderingStrategy(renderingStrategy)
87 .build()
88 .render();
89 }
90
91 public static Builder withTable(SqlTable table) {
92 return new Builder().withTable(table);
93 }
94
95 public static class Builder extends CommonBuilder<Builder> {
96 private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
97
98 public Builder withColumnMappings(List<? extends AbstractColumnMapping> columnMappings) {
99 this.columnMappings.addAll(columnMappings);
100 return this;
101 }
102
103 @Override
104 protected Builder getThis() {
105 return this;
106 }
107
108 public UpdateModel build() {
109 return new UpdateModel(this);
110 }
111 }
112 }