View Javadoc
1   /*
2    *    Copyright 2018-2022 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.scripting.thymeleaf.processor;
17  
18  import java.util.function.UnaryOperator;
19  
20  import org.mybatis.scripting.thymeleaf.support.spring.SpringNamedParameterBindVariableRender;
21  
22  /**
23   * The interface for rendering a bind variable. <br>
24   * If you want to customize a default {@code BindVariableRender}, you implements class of this interface and you need to
25   * specify the 'dialect.bind-variable-render' property of mybatis-thymeleaf.properties. <br>
26   * <br>
27   * e.g.) Implementation class:
28   *
29   * <pre>
30   * package com.example;
31   *
32   * // ...
33   * public class R2dbcMySQLBindVariableRender extends EnclosingBasedBindVariableRender {
34   *   public R2dbcMySQLBindVariableRender() {
35   *     super("?", ""); // Render '?...' (e.g. ?id)
36   *   }
37   * }
38   * </pre>
39   *
40   * <br>
41   * e.g.) Configuration file (mybatis-thymeleaf.properties):
42   *
43   * <pre>
44   * dialect.bind-variable-render = com.example.MyBindVariableRender
45   * </pre>
46   *
47   * @author Kazuki Shimizu
48   *
49   * @version 1.0.2
50   */
51  @FunctionalInterface
52  public interface BindVariableRender extends UnaryOperator<String> {
53  
54    /**
55     * {@inheritDoc}
56     *
57     * @see #render(String)
58     */
59    @Override
60    default String apply(String name) {
61      return render(name);
62    }
63  
64    /**
65     * Render a bind variable.
66     *
67     * @param name
68     *          a bind variable name
69     *
70     * @return a bind variable
71     */
72    String render(String name);
73  
74    /**
75     * The built-in bind variable renders.
76     */
77    enum BuiltIn implements BindVariableRender {
78  
79      /**
80       * The render for MyBatis core named parameter format(.e.g {@literal #{id}}).
81       * <p>
82       * This is default.
83       * </p>
84       */
85      MYBATIS(name -> "#{" + name + "}"),
86      /**
87       * The render for Spring JDBC named parameter format(.e.g {@literal :id}).
88       */
89      SPRING_NAMED_PARAMETER(new SpringNamedParameterBindVariableRender());
90  
91      private final BindVariableRender delegate;
92  
93      BuiltIn(BindVariableRender delegate) {
94        this.delegate = delegate;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public String render(String name) {
102       return delegate.render(name);
103     }
104 
105     /**
106      * Get a type of the actual {@link BindVariableRender}.
107      *
108      * @return a type of delegating {@link BindVariableRender}
109      */
110     public Class<? extends BindVariableRender> getType() {
111       return delegate.getClass();
112     }
113 
114   }
115 
116 }