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