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.DeleteByPrimaryKeyMethodGenerator;
30  
31  /**
32   * 
33   * @author Jeff Butler
34   */
35  public class AnnotatedDeleteByPrimaryKeyMethodGenerator extends
36          DeleteByPrimaryKeyMethodGenerator {
37  
38      public AnnotatedDeleteByPrimaryKeyMethodGenerator(boolean isSimple) {
39          super(isSimple);
40      }
41  
42      @Override
43      public void addMapperAnnotations(Interface interfaze, Method method) {
44          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Delete")); //$NON-NLS-1$
45          
46          method.addAnnotation("@Delete({"); //$NON-NLS-1$
47          
48          StringBuilder sb = new StringBuilder();
49          javaIndent(sb, 1);
50          sb.append("\"delete from " ); //$NON-NLS-1$
51          sb.append(escapeStringForJava(
52                  introspectedTable.getFullyQualifiedTableNameAtRuntime()));
53          sb.append("\","); //$NON-NLS-1$
54          method.addAnnotation(sb.toString());
55          
56          boolean and = false;
57          Iterator<IntrospectedColumn> iter = introspectedTable.getPrimaryKeyColumns().iterator();
58          while (iter.hasNext()) {
59              IntrospectedColumn introspectedColumn = iter.next();
60              sb.setLength(0);
61              javaIndent(sb, 1);
62              if (and) {
63                  sb.append("  \"and "); //$NON-NLS-1$
64              } else {
65                  sb.append("\"where "); //$NON-NLS-1$
66                  and = true;
67              }
68  
69              sb.append(escapeStringForJava(
70                      getEscapedColumnName(introspectedColumn)));
71              sb.append(" = "); //$NON-NLS-1$
72              sb.append(getParameterClause(introspectedColumn));
73              sb.append('\"');
74              if (iter.hasNext()) {
75                  sb.append(',');
76              }
77              
78              method.addAnnotation(sb.toString());
79          }
80          
81          method.addAnnotation("})"); //$NON-NLS-1$
82      }
83  }