|
| 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 | + FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn); |
| 24 | + |
| 25 | + default FragmentAndParameters renderLeftColumn(RenderingContext renderingContext, BindableColumn<T> leftColumn) { |
| 26 | + return leftColumn.alias() |
| 27 | + .map(FragmentAndParameters::fromFragment) |
| 28 | + .orElseGet(() -> leftColumn.render(renderingContext)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Subclasses can override this to inform the renderer if the condition should not be included |
| 33 | + * in the rendered SQL. Typically, conditions will not render if they are empty. |
| 34 | + * |
| 35 | + * @return true if the condition should render. |
| 36 | + */ |
| 37 | + default boolean shouldRender(RenderingContext renderingContext) { |
| 38 | + return !isEmpty(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Subclasses can override this to indicate whether the condition is considered empty. This is primarily used in |
| 43 | + * map and filter operations - the map and filter functions will not be applied if the condition is empty. |
| 44 | + * |
| 45 | + * @return true if the condition is empty. |
| 46 | + */ |
| 47 | + default boolean isEmpty() { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * This method will be called during rendering when {@link RenderableCondition#shouldRender(RenderingContext)} |
| 53 | + * returns false. |
| 54 | + */ |
| 55 | + default void renderingSkipped() {} |
| 56 | +} |
0 commit comments