View Javadoc
1   /**
2    *    Copyright 2006-2015 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    *       http://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  /**
24   * @author Jeff Butler
25   * 
26   */
27  public class MyBatis3FormattingUtilities {
28      /**
29       * Utility class - no instances
30       */
31      private MyBatis3FormattingUtilities() {
32          super();
33      }
34  
35      public static String getParameterClause(
36              IntrospectedColumn introspectedColumn) {
37          return getParameterClause(introspectedColumn, null);
38      }
39  
40      public static String getParameterClause(
41              IntrospectedColumn introspectedColumn, String prefix) {
42          StringBuilder sb = new StringBuilder();
43  
44          sb.append("#{"); //$NON-NLS-1$
45          sb.append(introspectedColumn.getJavaProperty(prefix));
46          sb.append(",jdbcType="); //$NON-NLS-1$
47          sb.append(introspectedColumn.getJdbcTypeName());
48  
49          if (stringHasValue(introspectedColumn.getTypeHandler())) {
50              sb.append(",typeHandler="); //$NON-NLS-1$
51              sb.append(introspectedColumn.getTypeHandler());
52          }
53  
54          sb.append('}');
55  
56          return sb.toString();
57      }
58  
59      /**
60       * The phrase to use in a select list. If there is a table alias, the value
61       * will be "alias.columnName as alias_columnName"
62       * 
63       * @return the proper phrase
64       */
65      public static String getSelectListPhrase(
66              IntrospectedColumn introspectedColumn) {
67          if (stringHasValue(introspectedColumn.getTableAlias())) {
68              StringBuilder sb = new StringBuilder();
69  
70              sb.append(getAliasedEscapedColumnName(introspectedColumn));
71              sb.append(" as "); //$NON-NLS-1$
72              if (introspectedColumn.isColumnNameDelimited()) {
73                  sb.append(introspectedColumn.getContext()
74                          .getBeginningDelimiter());
75              }
76              sb.append(introspectedColumn.getTableAlias());
77              sb.append('_');
78              sb.append(escapeStringForMyBatis3(introspectedColumn
79                      .getActualColumnName()));
80              if (introspectedColumn.isColumnNameDelimited()) {
81                  sb.append(introspectedColumn.getContext().getEndingDelimiter());
82              }
83              return sb.toString();
84          } else {
85              return getEscapedColumnName(introspectedColumn);
86          }
87      }
88  
89      public static String getEscapedColumnName(
90              IntrospectedColumn introspectedColumn) {
91          StringBuilder sb = new StringBuilder();
92          sb.append(escapeStringForMyBatis3(introspectedColumn
93                  .getActualColumnName()));
94  
95          if (introspectedColumn.isColumnNameDelimited()) {
96              sb.insert(0, introspectedColumn.getContext()
97                      .getBeginningDelimiter());
98              sb.append(introspectedColumn.getContext().getEndingDelimiter());
99          }
100 
101         return sb.toString();
102     }
103 
104     /**
105      * Calculates the string to use in select phrases in SqlMaps.
106      * 
107      * @return the aliased escaped column name
108      */
109     public static String getAliasedEscapedColumnName(
110             IntrospectedColumn introspectedColumn) {
111         if (stringHasValue(introspectedColumn.getTableAlias())) {
112             StringBuilder sb = new StringBuilder();
113 
114             sb.append(introspectedColumn.getTableAlias());
115             sb.append('.');
116             sb.append(getEscapedColumnName(introspectedColumn));
117             return sb.toString();
118         } else {
119             return getEscapedColumnName(introspectedColumn);
120         }
121     }
122 
123     /**
124      * The aliased column name for a select statement generated by the example
125      * clauses. This is not appropriate for selects in SqlMaps because the
126      * column is not escaped for MyBatis. If there is a table alias, the value
127      * will be alias.columnName.
128      * 
129      * This method is used in the Example classes and the returned value will be
130      * in a Java string. So we need to escape double quotes if they are the
131      * delimiters.
132      * 
133      * @return the aliased column name
134      */
135     public static String getAliasedActualColumnName(
136             IntrospectedColumn introspectedColumn) {
137         StringBuilder sb = new StringBuilder();
138         if (stringHasValue(introspectedColumn.getTableAlias())) {
139             sb.append(introspectedColumn.getTableAlias());
140             sb.append('.');
141         }
142 
143         if (introspectedColumn.isColumnNameDelimited()) {
144             sb.append(escapeStringForJava(introspectedColumn
145                     .getContext().getBeginningDelimiter()));
146         }
147 
148         sb.append(introspectedColumn.getActualColumnName());
149 
150         if (introspectedColumn.isColumnNameDelimited()) {
151             sb.append(escapeStringForJava(introspectedColumn
152                     .getContext().getEndingDelimiter()));
153         }
154 
155         return sb.toString();
156     }
157 
158     /**
159      * The renamed column name for a select statement. If there is a table
160      * alias, the value will be alias_columnName. This is appropriate for use in
161      * a result map.
162      * 
163      * @return the renamed column name
164      */
165     public static String getRenamedColumnNameForResultMap(
166             IntrospectedColumn introspectedColumn) {
167         if (stringHasValue(introspectedColumn.getTableAlias())) {
168             StringBuilder sb = new StringBuilder();
169 
170             sb.append(introspectedColumn.getTableAlias());
171             sb.append('_');
172             sb.append(introspectedColumn.getActualColumnName());
173             return sb.toString();
174         } else {
175             return introspectedColumn.getActualColumnName();
176         }
177     }
178 
179     public static String escapeStringForMyBatis3(String s) {
180         // nothing to do for MyBatis3 so far
181         return s;
182     }
183 }