View Javadoc
1   /*
2    *    Copyright 2006-2023 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.generator.codegen.mybatis3;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  
21  import org.mybatis.generator.api.IntrospectedColumn;
22  
23  public class MyBatis3FormattingUtilities {
24  
25      private MyBatis3FormattingUtilities() {
26          super();
27      }
28  
29      public static String getParameterClause(IntrospectedColumn introspectedColumn) {
30          return getParameterClause(introspectedColumn, null);
31      }
32  
33      public static String getParameterClause(IntrospectedColumn introspectedColumn, String prefix) {
34          StringBuilder sb = new StringBuilder();
35  
36          sb.append("#{"); //$NON-NLS-1$
37          sb.append(introspectedColumn.getJavaProperty(prefix));
38          sb.append(",jdbcType="); //$NON-NLS-1$
39          sb.append(introspectedColumn.getJdbcTypeName());
40  
41          if (stringHasValue(introspectedColumn.getTypeHandler())) {
42              sb.append(",typeHandler="); //$NON-NLS-1$
43              sb.append(introspectedColumn.getTypeHandler());
44          }
45  
46          sb.append('}');
47  
48          return sb.toString();
49      }
50  
51      /**
52       * The phrase to use in a select list. If there is a table alias, the value will be "alias.columnName as
53       * alias_columnName"
54       *
55       * @param introspectedColumn
56       *            the introspected column
57       *
58       * @return the proper phrase
59       */
60      public static String getSelectListPhrase(IntrospectedColumn introspectedColumn) {
61          if (stringHasValue(introspectedColumn.getTableAlias())) {
62              StringBuilder sb = new StringBuilder();
63  
64              sb.append(getAliasedEscapedColumnName(introspectedColumn));
65              sb.append(" as "); //$NON-NLS-1$
66              if (introspectedColumn.isColumnNameDelimited()) {
67                  sb.append(introspectedColumn.getContext().getBeginningDelimiter());
68              }
69              sb.append(introspectedColumn.getTableAlias());
70              sb.append('_');
71              sb.append(introspectedColumn.getActualColumnName());
72              if (introspectedColumn.isColumnNameDelimited()) {
73                  sb.append(introspectedColumn.getContext().getEndingDelimiter());
74              }
75              return sb.toString();
76          } else {
77              return getEscapedColumnName(introspectedColumn);
78          }
79      }
80  
81      public static String getEscapedColumnName(IntrospectedColumn introspectedColumn) {
82          StringBuilder sb = new StringBuilder();
83          sb.append(introspectedColumn.getActualColumnName());
84  
85          if (introspectedColumn.isColumnNameDelimited()) {
86              sb.insert(0, introspectedColumn.getContext().getBeginningDelimiter());
87              sb.append(introspectedColumn.getContext().getEndingDelimiter());
88          }
89  
90          return sb.toString();
91      }
92  
93      public static String getAliasedEscapedColumnName(IntrospectedColumn introspectedColumn) {
94          if (stringHasValue(introspectedColumn.getTableAlias())) {
95              return introspectedColumn.getTableAlias() + '.' + getEscapedColumnName(introspectedColumn);
96          } else {
97              return getEscapedColumnName(introspectedColumn);
98          }
99      }
100 
101     /**
102      * The aliased column name for a select statement generated by the example clauses. This is not appropriate for
103      * selects in SqlMaps because the column is not escaped for MyBatis. If there is a table alias, the value will be
104      * alias.columnName.
105      * <p>
106      * This method is used in the Example classes and the returned value will be in a Java string. So we need to escape
107      * double quotes if they are the delimiters.
108      *
109      * @param introspectedColumn
110      *            the introspected column
111      *
112      * @return the aliased column name
113      */
114     public static String getAliasedActualColumnName(IntrospectedColumn introspectedColumn) {
115         StringBuilder sb = new StringBuilder();
116         if (stringHasValue(introspectedColumn.getTableAlias())) {
117             sb.append(introspectedColumn.getTableAlias());
118             sb.append('.');
119         }
120 
121         if (introspectedColumn.isColumnNameDelimited()) {
122             sb.append(escapeStringForJava(introspectedColumn.getContext().getBeginningDelimiter()));
123         }
124 
125         sb.append(introspectedColumn.getActualColumnName());
126 
127         if (introspectedColumn.isColumnNameDelimited()) {
128             sb.append(escapeStringForJava(introspectedColumn.getContext().getEndingDelimiter()));
129         }
130 
131         return sb.toString();
132     }
133 
134     /**
135      * The renamed column name for a select statement. If there is a table alias, the value will be alias_columnName.
136      * This is appropriate for use in a result map.
137      *
138      * @param introspectedColumn
139      *            the introspected column
140      *
141      * @return the renamed column name
142      */
143     public static String getRenamedColumnNameForResultMap(IntrospectedColumn introspectedColumn) {
144         if (stringHasValue(introspectedColumn.getTableAlias())) {
145             return introspectedColumn.getTableAlias() + '_' + introspectedColumn.getActualColumnName();
146         } else {
147             return introspectedColumn.getActualColumnName();
148         }
149     }
150 }