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