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.Objects;
19  import java.util.function.Function;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.SqlTable;
23  import org.mybatis.dynamic.sql.dsl.AbstractUpdateDSL;
24  
25  public class UpdateDSL<R> extends AbstractUpdateDSL<R, UpdateDSL<R>> {
26  
27      private final Function<UpdateModel, R> adapterFunction;
28  
29      private UpdateDSL(SqlTable table, @Nullable String tableAlias, Function<UpdateModel, R> adapterFunction) {
30          super(table, tableAlias);
31          this.adapterFunction = Objects.requireNonNull(adapterFunction);
32      }
33  
34      /**
35       * WARNING! Calling this method could result in an update statement that updates
36       * all rows in a table.
37       *
38       * @return the update model
39       */
40      @Override
41      public R build() {
42          return buildUpdateModel().map(adapterFunction);
43      }
44  
45      @Override
46      protected UpdateDSL<R> getThis() {
47          return this;
48      }
49  
50      public static <R> UpdateDSL<R> update(Function<UpdateModel, R> adapterFunction, SqlTable table,
51                                            @Nullable String tableAlias) {
52          return new UpdateDSL<>(table, tableAlias, adapterFunction);
53      }
54  
55      public static UpdateDSL<UpdateModel> update(SqlTable table) {
56          return update(Function.identity(), table, null);
57      }
58  
59      public static UpdateDSL<UpdateModel> update(SqlTable table, String tableAlias) {
60          return update(Function.identity(), table, tableAlias);
61      }
62  }