View Javadoc
1   /*
2    *    Copyright 2016-2025 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.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"); //$NON-NLS-1$
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 }