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.function.Function;
19  import java.util.function.ToIntFunction;
20  
21  import org.mybatis.dynamic.sql.SqlTable;
22  import org.mybatis.dynamic.sql.util.Buildable;
23  import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
24  
25  /**
26   * Represents a function that can be used to create a simplified delete method. When using this function
27   * you can create a method that does not require a user to call the build() and render() methods - making
28   *  client code look a bit cleaner.
29   *
30   * <p>This function is intended to be used in conjunction with a utility method like
31   *  {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)}
32   *
33   * <p>For example, you can create mapper interface methods like this:
34   *
35   * <pre>
36   * &#64;DeleteProvider(type=SqlProviderAdapter.class, method="delete")
37   * int delete(DeleteStatementProvider deleteStatement);
38   *
39   * default int delete(DeleteDSLCompleter completer) {
40   *     return MyBatis3Utils.deleteFrom(this::delete, person, completer);
41   * }
42   * </pre>
43   *
44   * <p>And then call the simplified default method like this:
45   *
46   * <pre>
47   * int rows = mapper.delete(c -&gt;
48   *           c.where(occupation, isNull()));
49   * </pre>
50   *
51   * <p>You can implement a "delete all" with the following code:
52   *
53   * <pre>
54   * int rows = mapper.delete(c -&gt; c);
55   * </pre>
56   *
57   * <p>Or
58   *
59   * <pre>
60   * long rows = mapper.delete(DeleteDSLCompleter.allRows());
61   * </pre>
62  
63   * @author Jeff Butler
64   */
65  @FunctionalInterface
66  public interface DeleteDSLCompleter extends
67          Function<DeleteDSL<DeleteModel>, Buildable<DeleteModel>> {
68  
69      /**
70       * Returns a completer that can be used to delete every row in a table.
71       *
72       * @return the completer that will delete every row in a table
73       */
74      static DeleteDSLCompleter allRows() {
75          return c -> c;
76      }
77  }