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.UpdateByPrimaryKeyWithoutBLOBsMethodGenerator;
30  
31  /**
32   * 
33   * @author Jeff Butler
34   */
35  public class AnnotatedUpdateByPrimaryKeyWithoutBLOBsMethodGenerator extends
36      UpdateByPrimaryKeyWithoutBLOBsMethodGenerator {
37  
38      private boolean isSimple;
39      
40      public AnnotatedUpdateByPrimaryKeyWithoutBLOBsMethodGenerator(boolean isSimple) {
41          super();
42          this.isSimple = isSimple;
43      }
44  
45      @Override
46      public void addMapperAnnotations(Interface interfaze, Method method) {
47          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Update")); //$NON-NLS-1$
48          
49          method.addAnnotation("@Update({"); //$NON-NLS-1$
50  
51          StringBuilder sb = new StringBuilder();
52          javaIndent(sb, 1);
53          sb.append("\"update "); //$NON-NLS-1$
54          sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
55          sb.append("\","); //$NON-NLS-1$
56          method.addAnnotation(sb.toString());
57  
58          // set up for first column
59          sb.setLength(0);
60          javaIndent(sb, 1);
61          sb.append("\"set "); //$NON-NLS-1$
62  
63          Iterator<IntrospectedColumn> iter;
64          if (isSimple) {
65              iter = introspectedTable.getNonPrimaryKeyColumns().iterator();
66          } else {
67              iter = introspectedTable.getBaseColumns().iterator();
68          }
69          while (iter.hasNext()) {
70              IntrospectedColumn introspectedColumn = iter.next();
71  
72              sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
73              sb.append(" = "); //$NON-NLS-1$
74              sb.append(getParameterClause(introspectedColumn));
75  
76              if (iter.hasNext()) {
77                  sb.append(',');
78              }
79  
80              sb.append("\","); //$NON-NLS-1$
81              method.addAnnotation(sb.toString());
82  
83              // set up for the next column
84              if (iter.hasNext()) {
85                  sb.setLength(0);
86                  javaIndent(sb, 1);
87                  sb.append("  \""); //$NON-NLS-1$
88              }
89          }
90  
91          boolean and = false;
92          iter = introspectedTable.getPrimaryKeyColumns().iterator();
93          while (iter.hasNext()) {
94              IntrospectedColumn introspectedColumn = iter.next();
95              sb.setLength(0);
96              javaIndent(sb, 1);
97              if (and) {
98                  sb.append("  \"and "); //$NON-NLS-1$
99              } else {
100                 sb.append("\"where "); //$NON-NLS-1$
101                 and = true;
102             }
103 
104             sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
105             sb.append(" = "); //$NON-NLS-1$
106             sb.append(getParameterClause(introspectedColumn));
107             sb.append('\"');
108             if (iter.hasNext()) {
109                 sb.append(',');
110             }
111             method.addAnnotation(sb.toString());
112         }
113         
114         method.addAnnotation("})"); //$NON-NLS-1$
115     }
116 }