View Javadoc
1   /**
2    *    Copyright 2006-2015 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.xmlmapper.elements;
17  
18  import org.mybatis.generator.api.IntrospectedColumn;
19  import org.mybatis.generator.api.dom.xml.Attribute;
20  import org.mybatis.generator.api.dom.xml.TextElement;
21  import org.mybatis.generator.api.dom.xml.XmlElement;
22  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
23  
24  /**
25   * 
26   * @author Jeff Butler
27   * 
28   */
29  public class DeleteByPrimaryKeyElementGenerator extends
30          AbstractXmlElementGenerator {
31  
32      private boolean isSimple;
33      
34      public DeleteByPrimaryKeyElementGenerator(boolean isSimple) {
35          super();
36          this.isSimple = isSimple;
37      }
38  
39      @Override
40      public void addElements(XmlElement parentElement) {
41          XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$
42  
43          answer.addAttribute(new Attribute(
44                  "id", introspectedTable.getDeleteByPrimaryKeyStatementId())); //$NON-NLS-1$
45          String parameterClass;
46          if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
47              parameterClass = introspectedTable.getPrimaryKeyType();
48          } else {
49              // PK fields are in the base class. If more than on PK
50              // field, then they are coming in a map.
51              if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
52                  parameterClass = "map"; //$NON-NLS-1$
53              } else {
54                  parameterClass = introspectedTable.getPrimaryKeyColumns()
55                          .get(0).getFullyQualifiedJavaType().toString();
56              }
57          }
58          answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
59                  parameterClass));
60  
61          context.getCommentGenerator().addComment(answer);
62  
63          StringBuilder sb = new StringBuilder();
64          sb.append("delete from "); //$NON-NLS-1$
65          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
66          answer.addElement(new TextElement(sb.toString()));
67  
68          boolean and = false;
69          for (IntrospectedColumn introspectedColumn : introspectedTable
70                  .getPrimaryKeyColumns()) {
71              sb.setLength(0);
72              if (and) {
73                  sb.append("  and "); //$NON-NLS-1$
74              } else {
75                  sb.append("where "); //$NON-NLS-1$
76                  and = true;
77              }
78  
79              sb.append(MyBatis3FormattingUtilities
80                      .getEscapedColumnName(introspectedColumn));
81              sb.append(" = "); //$NON-NLS-1$
82              sb.append(MyBatis3FormattingUtilities
83                      .getParameterClause(introspectedColumn));
84              answer.addElement(new TextElement(sb.toString()));
85          }
86  
87          if (context.getPlugins()
88                  .sqlMapDeleteByPrimaryKeyElementGenerated(answer,
89                          introspectedTable)) {
90              parentElement.addElement(answer);
91          }
92      }
93  }