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 examples.mysql;
17  
18  import java.util.NoSuchElementException;
19  import java.util.function.Function;
20  import java.util.function.Predicate;
21  
22  import org.jspecify.annotations.Nullable;
23  import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
24  import org.mybatis.dynamic.sql.BindableColumn;
25  import org.mybatis.dynamic.sql.render.RenderingContext;
26  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
27  
28  public class IsLikeEscape<T> extends AbstractSingleValueCondition<T>
29          implements AbstractSingleValueCondition.Filterable<T>, AbstractSingleValueCondition.Mappable<T> {
30      private static final IsLikeEscape<?> EMPTY = new IsLikeEscape<Object>(-1, null) {
31          @Override
32          public Object value() {
33              throw new NoSuchElementException("No value present"); //$NON-NLS-1$
34          }
35  
36          @Override
37          public boolean isEmpty() {
38              return true;
39          }
40      };
41  
42      public static <T> IsLikeEscape<T> empty() {
43          @SuppressWarnings("unchecked")
44          IsLikeEscape<T> t = (IsLikeEscape<T>) EMPTY;
45          return t;
46      }
47  
48      private final @Nullable Character escapeCharacter;
49  
50      protected IsLikeEscape(T value, @Nullable Character escapeCharacter) {
51          super(value);
52          this.escapeCharacter = escapeCharacter;
53      }
54  
55      @Override
56      public String operator() {
57          return "like";
58      }
59  
60      @Override
61      public FragmentAndParameters renderCondition(RenderingContext renderingContext, BindableColumn<T> leftColumn) {
62          var fragment = super.renderCondition(renderingContext, leftColumn);
63          if (escapeCharacter != null) {
64              fragment = fragment.mapFragment(this::addEscape);
65          }
66  
67          return fragment;
68      }
69  
70      private String addEscape(String s) {
71          return s + " ESCAPE '" + escapeCharacter + "'";
72      }
73  
74      @Override
75      public IsLikeEscape<T> filter(Predicate<? super T> predicate) {
76          return filterSupport(predicate, IsLikeEscape::empty, this);
77      }
78  
79      @Override
80      public <R> IsLikeEscape<R> map(Function<? super T, ? extends R> mapper) {
81          return mapSupport(mapper, v -> new IsLikeEscape<>(v, escapeCharacter), IsLikeEscape::empty);
82      }
83  
84      public static <T> IsLikeEscape<T> isLike(T value) {
85          return new IsLikeEscape<>(value, null);
86      }
87  
88      public static <T> IsLikeEscape<T> isLike(T value, Character escapeCharacter) {
89          return new IsLikeEscape<>(value, escapeCharacter);
90      }
91  }