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
20 import org.mybatis.dynamic.sql.SortSpecification;
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 select 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 utility methods like the select methods in
30 * {@link MyBatis3Utils}.
31 *
32 * <p>For example, you can create mapper interface methods like this:
33 *
34 * <pre>
35 * @SelectProvider(type=SqlProviderAdapter.class, method="select")
36 * List<PersonRecord> selectMany(SelectStatementProvider selectStatement);
37 *
38 * BasicColumn[] selectList =
39 * BasicColumn.columnList(id, firstName, lastName, birthDate, employed, occupation, addressId);
40 *
41 * default List<PersonRecord> select(SelectDSLCompleter completer) {
42 * return MyBatis3Utils.select(this::selectMany, selectList, person, completer);
43 * }
44 * </pre>
45 *
46 * <p>And then call the simplified default method like this:
47 *
48 * <pre>
49 * List<PersonRecord> rows = mapper.select(c ->
50 * c.where(occupation, isNull()));
51 * </pre>
52 *
53 * <p>You can implement a "select all" with the following code:
54 *
55 * <pre>
56 * List<PersonRecord> rows = mapper.select(c -> c);
57 * </pre>
58 *
59 * <p>Or
60 *
61 * <pre>
62 * List<PersonRecord> rows = mapper.select(SelectDSLCompleter.allRows());
63 * </pre>
64 *
65 * <p>There is also a utility method to support selecting all rows in a specified order:
66 *
67 * <pre>
68 * List<PersonRecord> rows = mapper.select(SelectDSLCompleter.allRowsOrderedBy(lastName, firstName));
69 * </pre>
70 *
71 * @author Jeff Butler
72 */
73 @FunctionalInterface
74 public interface SelectDSLCompleter extends
75 Function<QueryExpressionDSL<SelectModel>, Buildable<SelectModel>> {
76
77 /**
78 * Returns a completer that can be used to select every row in a table.
79 *
80 * @return the completer that will select every row in a table
81 */
82 static SelectDSLCompleter allRows() {
83 return c -> c;
84 }
85
86 /**
87 * Returns a completer that can be used to select every row in a table with specified order.
88 *
89 * @param columns list of sort specifications for an order by clause
90 * @return the completer that will select every row in a table with specified order
91 */
92 static SelectDSLCompleter allRowsOrderedBy(SortSpecification... columns) {
93 return c -> c.orderBy(columns);
94 }
95 }