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.delete;
17  
18  import java.util.Objects;
19  import java.util.Optional;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.dynamic.sql.SqlTable;
23  import org.mybatis.dynamic.sql.common.CommonBuilder;
24  import org.mybatis.dynamic.sql.common.OrderByModel;
25  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
26  import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
27  import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
28  import org.mybatis.dynamic.sql.render.RenderingStrategy;
29  import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
30  
31  public class DeleteModel {
32      private final SqlTable table;
33      private final @Nullable String tableAlias;
34      private final @Nullable EmbeddedWhereModel whereModel;
35      private final @Nullable Long limit;
36      private final @Nullable OrderByModel orderByModel;
37      private final StatementConfiguration statementConfiguration;
38  
39      private DeleteModel(Builder builder) {
40          table = Objects.requireNonNull(builder.table());
41          whereModel = builder.whereModel();
42          tableAlias = builder.tableAlias();
43          limit = builder.limit();
44          orderByModel = builder.orderByModel();
45          statementConfiguration = Objects.requireNonNull(builder.statementConfiguration());
46      }
47  
48      public SqlTable table() {
49          return table;
50      }
51  
52      public Optional<String> tableAlias() {
53          return Optional.ofNullable(tableAlias);
54      }
55  
56      public Optional<EmbeddedWhereModel> whereModel() {
57          return Optional.ofNullable(whereModel);
58      }
59  
60      public Optional<Long> limit() {
61          return Optional.ofNullable(limit);
62      }
63  
64      public Optional<OrderByModel> orderByModel() {
65          return Optional.ofNullable(orderByModel);
66      }
67  
68      public StatementConfiguration statementConfiguration() {
69          return statementConfiguration;
70      }
71  
72      public DeleteStatementProvider render(RenderingStrategy renderingStrategy) {
73          return DeleteRenderer.withDeleteModel(this)
74                  .withRenderingStrategy(renderingStrategy)
75                  .build()
76                  .render();
77      }
78  
79      public static Builder withTable(SqlTable table) {
80          return new Builder().withTable(table);
81      }
82  
83      public static class Builder extends CommonBuilder<Builder> {
84          @Override
85          protected Builder getThis() {
86              return this;
87          }
88  
89          public DeleteModel build() {
90              return new DeleteModel(this);
91          }
92      }
93  }