View Javadoc
1   /*
2    *    Copyright 2016-2024 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.util.Optional;
19  
20  import org.mybatis.dynamic.sql.exception.DynamicSqlException;
21  import org.mybatis.dynamic.sql.render.RenderingContext;
22  import org.mybatis.dynamic.sql.render.TableAliasCalculator;
23  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
24  import org.mybatis.dynamic.sql.util.Messages;
25  
26  /**
27   * Describes attributes of columns that are necessary for rendering if the column is not expected to
28   * be bound as a JDBC parameter.  Columns in select lists, join expressions, and group by expressions
29   * are typically not bound.
30   *
31   * @author Jeff Butler
32   */
33  public interface BasicColumn {
34  
35      /**
36       * Returns the columns alias if one has been specified.
37       *
38       * @return the column alias
39       */
40      Optional<String> alias();
41  
42      /**
43       * Returns a new instance of a BasicColumn with the alias set.
44       *
45       * @param alias
46       *            the column alias to set
47       *
48       * @return new instance with alias set
49       */
50      BasicColumn as(String alias);
51  
52      /**
53       * Returns a rendering of the column.
54       * The rendered fragment should include the table alias based on the TableAliasCalculator
55       * in the RenderingContext. The fragment could contain prepared statement parameter
56       * markers and associated parameter values if desired.
57       *
58       * @param renderingContext the rendering context (strategy, sequence, etc.)
59       * @return a rendered SQL fragment and, optionally, parameters associated with the fragment
60       * @since 1.5.1
61       */
62      default FragmentAndParameters render(RenderingContext renderingContext) {
63          // the default implementation ensures compatibility with prior releases. When the
64          // deprecated renderWithTableAlias method is removed, this function can become purely abstract.
65          // Also remove the method tableAliasCalculator() from RenderingContext.
66          return FragmentAndParameters.fromFragment(renderWithTableAlias(renderingContext.tableAliasCalculator()));
67      }
68  
69      /**
70       * Returns the name of the item aliased with a table name if appropriate.
71       * For example, "a.foo".  This is appropriate for where clauses and order by clauses.
72       *
73       * @param tableAliasCalculator the table alias calculator for the current renderer
74       * @return the item name with the table alias applied
75       * @deprecated Please replace this method by overriding the more general "render" method
76       */
77      @Deprecated
78      default String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
79          throw new DynamicSqlException(Messages.getString("ERROR.36"));  //$NON-NLS-1$
80      }
81  
82      /**
83       * Utility method to make it easier to build column lists for methods that require an
84       * array rather than the varargs method.
85       *
86       * @param columns list of BasicColumn
87       * @return an array of BasicColumn
88       */
89      static BasicColumn[] columnList(BasicColumn... columns) {
90          return columns;
91      }
92  }