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.javamapper.elements.annotated;
17  
18  import static org.mybatis.generator.api.dom.OutputUtilities.javaIndent;
19  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getEscapedColumnName;
20  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getParameterClause;
21  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
22  
23  import java.util.Iterator;
24  
25  import org.mybatis.generator.api.IntrospectedColumn;
26  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
27  import org.mybatis.generator.api.dom.java.Interface;
28  import org.mybatis.generator.api.dom.java.Method;
29  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithBLOBsMethodGenerator;
30  
31  /**
32   * 
33   * @author Jeff Butler
34   */
35  public class AnnotatedUpdateByPrimaryKeyWithBLOBsMethodGenerator extends
36      UpdateByPrimaryKeyWithBLOBsMethodGenerator {
37  
38      public AnnotatedUpdateByPrimaryKeyWithBLOBsMethodGenerator() {
39          super();
40      }
41  
42      @Override
43      public void addMapperAnnotations(Interface interfaze, Method method) {
44          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Update")); //$NON-NLS-1$
45          
46          method.addAnnotation("@Update({"); //$NON-NLS-1$
47  
48          StringBuilder sb = new StringBuilder();
49          javaIndent(sb, 1);
50          sb.append("\"update "); //$NON-NLS-1$
51          sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
52          sb.append("\","); //$NON-NLS-1$
53          method.addAnnotation(sb.toString());
54  
55          // set up for first column
56          sb.setLength(0);
57          javaIndent(sb, 1);
58          sb.append("\"set "); //$NON-NLS-1$
59  
60          Iterator<IntrospectedColumn> iter = introspectedTable
61                  .getNonPrimaryKeyColumns().iterator();
62          while (iter.hasNext()) {
63              IntrospectedColumn introspectedColumn = iter.next();
64  
65              sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
66              sb.append(" = "); //$NON-NLS-1$
67              sb.append(getParameterClause(introspectedColumn));
68  
69              if (iter.hasNext()) {
70                  sb.append(',');
71              }
72  
73              sb.append("\","); //$NON-NLS-1$
74              method.addAnnotation(sb.toString());
75  
76              // set up for the next column
77              if (iter.hasNext()) {
78                  sb.setLength(0);
79                  javaIndent(sb, 1);
80                  sb.append("  \""); //$NON-NLS-1$
81              }
82          }
83  
84          boolean and = false;
85          iter = introspectedTable.getPrimaryKeyColumns().iterator();
86          while (iter.hasNext()) {
87              IntrospectedColumn introspectedColumn = iter.next();
88              sb.setLength(0);
89              javaIndent(sb, 1);
90              if (and) {
91                  sb.append("  \"and "); //$NON-NLS-1$
92              } else {
93                  sb.append("\"where "); //$NON-NLS-1$
94                  and = true;
95              }
96  
97              sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
98              sb.append(" = "); //$NON-NLS-1$
99              sb.append(getParameterClause(introspectedColumn));
100             sb.append('\"');
101             if (iter.hasNext()) {
102                 sb.append(',');
103             }
104             method.addAnnotation(sb.toString());
105         }
106         
107         method.addAnnotation("})"); //$NON-NLS-1$
108     }
109 }