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.Arrays;
19  import java.util.Collection;
20  import java.util.Objects;
21  import java.util.function.Consumer;
22  import java.util.function.Function;
23  
24  import org.jspecify.annotations.Nullable;
25  import org.mybatis.dynamic.sql.SortSpecification;
26  import org.mybatis.dynamic.sql.SqlTable;
27  import org.mybatis.dynamic.sql.common.OrderByModel;
28  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
29  import org.mybatis.dynamic.sql.util.Buildable;
30  import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
31  import org.mybatis.dynamic.sql.where.AbstractWhereStarter;
32  import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
33  
34  public class DeleteDSL<R> implements AbstractWhereStarter<DeleteDSL<R>.DeleteWhereBuilder, DeleteDSL<R>>,
35          Buildable<R> {
36  
37      private final Function<DeleteModel, R> adapterFunction;
38      private final SqlTable table;
39      private final @Nullable String tableAlias;
40      private @Nullable DeleteWhereBuilder whereBuilder;
41      private final StatementConfiguration statementConfiguration = new StatementConfiguration();
42      private @Nullable Long limit;
43      private @Nullable OrderByModel orderByModel;
44  
45      private DeleteDSL(SqlTable table, @Nullable String tableAlias, Function<DeleteModel, R> adapterFunction) {
46          this.table = Objects.requireNonNull(table);
47          this.tableAlias = tableAlias;
48          this.adapterFunction = Objects.requireNonNull(adapterFunction);
49      }
50  
51      @Override
52      public DeleteWhereBuilder where() {
53          whereBuilder = Objects.requireNonNullElseGet(whereBuilder, DeleteWhereBuilder::new);
54          return whereBuilder;
55      }
56  
57      public DeleteDSL<R> limit(long limit) {
58          return limitWhenPresent(limit);
59      }
60  
61      public DeleteDSL<R> limitWhenPresent(@Nullable Long limit) {
62          this.limit = limit;
63          return this;
64      }
65  
66      public DeleteDSL<R> orderBy(SortSpecification... columns) {
67          return orderBy(Arrays.asList(columns));
68      }
69  
70      public DeleteDSL<R> orderBy(Collection<? extends SortSpecification> columns) {
71          orderByModel = OrderByModel.of(columns);
72          return this;
73      }
74  
75      /**
76       * WARNING! Calling this method could result in a delete statement that deletes
77       * all rows in a table.
78       *
79       * @return the model class
80       */
81      @Override
82      public R build() {
83          DeleteModel deleteModel = DeleteModel.withTable(table)
84                  .withTableAlias(tableAlias)
85                  .withLimit(limit)
86                  .withOrderByModel(orderByModel)
87                  .withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
88                  .withStatementConfiguration(statementConfiguration)
89                  .build();
90  
91          return adapterFunction.apply(deleteModel);
92      }
93  
94      @Override
95      public DeleteDSL<R> configureStatement(Consumer<StatementConfiguration> consumer) {
96          consumer.accept(statementConfiguration);
97          return this;
98      }
99  
100     public static <R> DeleteDSL<R> deleteFrom(Function<DeleteModel, R> adapterFunction, SqlTable table,
101             @Nullable String tableAlias) {
102         return new DeleteDSL<>(table, tableAlias, adapterFunction);
103     }
104 
105     public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
106         return deleteFrom(Function.identity(), table, null);
107     }
108 
109     public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table, String tableAlias) {
110         return deleteFrom(Function.identity(), table, tableAlias);
111     }
112 
113     public class DeleteWhereBuilder extends AbstractWhereFinisher<DeleteWhereBuilder> implements Buildable<R> {
114 
115         private DeleteWhereBuilder() {
116             super(DeleteDSL.this);
117         }
118 
119         public DeleteDSL<R> limit(long limit) {
120             return limitWhenPresent(limit);
121         }
122 
123         public DeleteDSL<R> limitWhenPresent(@Nullable Long limit) {
124             return DeleteDSL.this.limitWhenPresent(limit);
125         }
126 
127         public DeleteDSL<R> orderBy(SortSpecification... columns) {
128             return orderBy(Arrays.asList(columns));
129         }
130 
131         public DeleteDSL<R> orderBy(Collection<? extends SortSpecification> columns) {
132             orderByModel = OrderByModel.of(columns);
133             return DeleteDSL.this;
134         }
135 
136         @Override
137         public R build() {
138             return DeleteDSL.this.build();
139         }
140 
141         @Override
142         protected DeleteWhereBuilder getThis() {
143             return this;
144         }
145 
146         protected EmbeddedWhereModel buildWhereModel() {
147             return buildModel();
148         }
149     }
150 }