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 org.mybatis.dynamic.sql;
17  
18  import java.sql.JDBCType;
19  import java.util.Optional;
20  
21  import org.mybatis.dynamic.sql.render.RenderingContext;
22  import org.mybatis.dynamic.sql.render.RenderingStrategy;
23  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
24  
25  /**
26   * Describes attributes of columns that are necessary for rendering if the column is not expected to
27   * be bound as a JDBC parameter.  Columns in select lists, join expressions, and group by expressions
28   * are typically not bound.
29   *
30   * @author Jeff Butler
31   */
32  public interface BasicColumn {
33  
34      /**
35       * Returns the columns alias if one has been specified.
36       *
37       * @return the column alias
38       */
39      Optional<String> alias();
40  
41      /**
42       * Returns a new instance of a BasicColumn with the alias set.
43       *
44       * @param alias
45       *            the column alias to set
46       *
47       * @return new instance with alias set
48       */
49      BasicColumn as(String alias);
50  
51      /**
52       * Returns a rendering of the column.
53       * The rendered fragment should include the table alias based on the TableAliasCalculator
54       * in the RenderingContext. The fragment could contain prepared statement parameter
55       * markers and associated parameter values if desired.
56       *
57       * @param renderingContext the rendering context (strategy, sequence, etc.)
58       * @return a rendered SQL fragment and, optionally, parameters associated with the fragment
59       * @since 1.5.1
60       */
61      FragmentAndParameters render(RenderingContext renderingContext);
62  
63      default Optional<JDBCType> jdbcType() {
64          return Optional.empty();
65      }
66  
67      default Optional<String> typeHandler() {
68          return Optional.empty();
69      }
70  
71      default Optional<RenderingStrategy> renderingStrategy() {
72          return Optional.empty();
73      }
74  
75      /**
76       * Utility method to make it easier to build column lists for methods that require an
77       * array rather than the varargs method.
78       *
79       * @param columns list of BasicColumn
80       * @return an array of BasicColumn
81       */
82      static BasicColumn[] columnList(BasicColumn... columns) {
83          return columns;
84      }
85  }