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.type_conversion;
17  
18  import java.sql.JDBCType;
19  import java.util.Optional;
20  
21  import org.mybatis.dynamic.sql.BasicColumn;
22  import org.mybatis.dynamic.sql.BindableColumn;
23  import org.mybatis.dynamic.sql.render.RenderingContext;
24  import org.mybatis.dynamic.sql.select.function.AbstractTypeConvertingFunction;
25  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
26  
27  public class ToBase64 extends AbstractTypeConvertingFunction<byte[], String, ToBase64> {
28  
29      protected ToBase64(BasicColumn column) {
30          super(column);
31      }
32  
33      @Override
34      public Optional<JDBCType> jdbcType() {
35          return Optional.of(JDBCType.VARCHAR);
36      }
37  
38      @Override
39      public FragmentAndParameters render(RenderingContext renderingContext) {
40          return column.render(renderingContext)
41                  .mapFragment(f -> "TO_BASE64(" + f + ")"); //$NON-NLS-1$ //$NON-NLS-2$
42      }
43  
44      @Override
45      protected ToBase64 copy() {
46          return new ToBase64(column);
47      }
48  
49      public static ToBase64 toBase64(BindableColumn<byte[]> column) {
50          return new ToBase64(column);
51      }
52  }