View Javadoc
1   /*
2    *    Copyright 2016-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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.function.Function;
23  import java.util.stream.Stream;
24  
25  import org.jspecify.annotations.Nullable;
26  import org.mybatis.dynamic.sql.SqlTable;
27  import org.mybatis.dynamic.sql.common.CommonBuilder;
28  import org.mybatis.dynamic.sql.common.OrderByModel;
29  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
30  import org.mybatis.dynamic.sql.render.RenderingStrategy;
31  import org.mybatis.dynamic.sql.update.render.UpdateRenderer;
32  import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
33  import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
34  import org.mybatis.dynamic.sql.util.Validator;
35  import org.mybatis.dynamic.sql.where.WhereModel;
36  
37  public class UpdateModel {
38      private final SqlTable table;
39      private final @Nullable String tableAlias;
40      private final @Nullable WhereModel whereModel;
41      private final List<AbstractColumnMapping> columnMappings;
42      private final @Nullable Long limit;
43      private final @Nullable OrderByModel orderByModel;
44      private final StatementConfiguration statementConfiguration;
45  
46      private UpdateModel(Builder builder) {
47          table = Objects.requireNonNull(builder.table());
48          whereModel = builder.whereModel();
49          columnMappings = Objects.requireNonNull(builder.columnMappings);
50          tableAlias = builder.tableAlias();
51          limit = builder.limit();
52          orderByModel = builder.orderByModel();
53          Validator.assertNotEmpty(columnMappings, "ERROR.17"); //$NON-NLS-1$
54          statementConfiguration = Objects.requireNonNull(builder.statementConfiguration());
55      }
56  
57      public SqlTable table() {
58          return table;
59      }
60  
61      public Optional<String> tableAlias() {
62          return Optional.ofNullable(tableAlias);
63      }
64  
65      public Optional<WhereModel> whereModel() {
66          return Optional.ofNullable(whereModel);
67      }
68  
69      public Stream<AbstractColumnMapping> columnMappings() {
70          return columnMappings.stream();
71      }
72  
73      public Optional<Long> limit() {
74          return Optional.ofNullable(limit);
75      }
76  
77      public Optional<OrderByModel> orderByModel() {
78          return Optional.ofNullable(orderByModel);
79      }
80  
81      public StatementConfiguration statementConfiguration() {
82          return statementConfiguration;
83      }
84  
85      public UpdateStatementProvider render(RenderingStrategy renderingStrategy) {
86          return UpdateRenderer.withUpdateModel(this)
87                  .withRenderingStrategy(renderingStrategy)
88                  .build()
89                  .render();
90      }
91  
92      public <R> R map(Function<UpdateModel, R> mapper) {
93          return mapper.apply(this);
94      }
95  
96      public static Builder withTable(SqlTable table) {
97          return new Builder().withTable(table);
98      }
99  
100     public static class Builder extends CommonBuilder<Builder> {
101         private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
102 
103         public Builder withColumnMappings(List<? extends AbstractColumnMapping> columnMappings) {
104             this.columnMappings.addAll(columnMappings);
105             return this;
106         }
107 
108         @Override
109         protected Builder getThis() {
110             return this;
111         }
112 
113         public UpdateModel build() {
114             return new UpdateModel(this);
115         }
116     }
117 }