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