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;
17
18 import org.mybatis.dynamic.sql.render.RenderingContext;
19 import org.mybatis.dynamic.sql.util.FragmentAndParameters;
20
21 @FunctionalInterface
22 public interface RenderableCondition<T> {
23 /**
24 * Render a condition - typically a condition in a WHERE clause.
25 *
26 * <p>A rendered condition includes an SQL fragment, and any associated parameters. For example,
27 * the <code>isEqual</code> condition should be rendered as "= ?" where "?" is a properly formatted
28 * parameter marker (the parameter marker can be computed from the <code>RenderingContext</code>).
29 * Note that a rendered condition should NOT include the left side of the phrase - that is rendered
30 * by the {@link RenderableCondition#renderLeftColumn(RenderingContext, BindableColumn)} method.
31 *
32 * @param renderingContext the current rendering context
33 * @param leftColumn the column related to this condition in a where clause
34 * @return the rendered condition. Should NOT include the column.
35 */
36 FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn);
37
38 /**
39 * Render the column in a column and condition phrase - typically in a WHERE clause.
40 *
41 * <p>By default, the column will be rendered as the column alias if it exists, or the column name.
42 * This can be complicated if the column has a table qualifier, or if the "column" is a function or
43 * part of a CASE expression. Columns know how to render themselves, so we just call their "render"
44 * methods.
45 *
46 * @param renderingContext the current rendering context
47 * @param leftColumn the column related to this condition in a where clause
48 * @return the rendered column
49 */
50 default FragmentAndParameters renderLeftColumn(RenderingContext renderingContext, BindableColumn<T> leftColumn) {
51 return leftColumn.alias()
52 .map(FragmentAndParameters::fromFragment)
53 .orElseGet(() -> leftColumn.render(renderingContext));
54 }
55
56 /**
57 * Subclasses can override this to inform the renderer if the condition should not be included
58 * in the rendered SQL. Typically, conditions will not render if they are empty.
59 *
60 * @return true if the condition should render.
61 */
62 default boolean shouldRender(RenderingContext renderingContext) {
63 return !isEmpty();
64 }
65
66 /**
67 * Subclasses can override this to indicate whether the condition is considered empty. This is primarily used in
68 * map and filter operations - the map and filter functions will not be applied if the condition is empty.
69 *
70 * @return true if the condition is empty.
71 */
72 default boolean isEmpty() {
73 return false;
74 }
75
76 /**
77 * This method will be called during rendering when {@link RenderableCondition#shouldRender(RenderingContext)}
78 * returns false.
79 */
80 default void renderingSkipped() {}
81 }