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.sqlmap.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.ibatis2.Ibatis2FormattingUtilities;
23  
24  /**
25   * 
26   * @author Jeff Butler
27   * 
28   */
29  public class DeleteByPrimaryKeyElementGenerator extends
30          AbstractXmlElementGenerator {
31  
32      public DeleteByPrimaryKeyElementGenerator() {
33          super();
34      }
35  
36      @Override
37      public void addElements(XmlElement parentElement) {
38          XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$
39  
40          answer.addAttribute(new Attribute(
41                  "id", introspectedTable.getDeleteByPrimaryKeyStatementId())); //$NON-NLS-1$
42          String parameterClass;
43          if (introspectedTable.getRules().generatePrimaryKeyClass()) {
44              parameterClass = introspectedTable.getPrimaryKeyType();
45          } else {
46              parameterClass = introspectedTable.getBaseRecordType();
47          }
48          answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
49                  parameterClass));
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          StringBuilder sb = new StringBuilder();
54          sb.append("delete from "); //$NON-NLS-1$
55          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
56          answer.addElement(new TextElement(sb.toString()));
57  
58          boolean and = false;
59          for (IntrospectedColumn introspectedColumn : introspectedTable
60                  .getPrimaryKeyColumns()) {
61              sb.setLength(0);
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(Ibatis2FormattingUtilities
70                      .getEscapedColumnName(introspectedColumn));
71              sb.append(" = "); //$NON-NLS-1$
72              sb.append(Ibatis2FormattingUtilities
73                      .getParameterClause(introspectedColumn));
74              answer.addElement(new TextElement(sb.toString()));
75          }
76  
77          if (context.getPlugins()
78                  .sqlMapDeleteByPrimaryKeyElementGenerated(answer,
79                          introspectedTable)) {
80              parentElement.addElement(answer);
81          }
82      }
83  }