View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.dom.xml.Attribute;
19  import org.mybatis.generator.api.dom.xml.TextElement;
20  import org.mybatis.generator.api.dom.xml.XmlElement;
21  
22  public class DeleteByPrimaryKeyElementGenerator extends AbstractXmlElementGenerator {
23  
24      private final boolean isSimple;
25  
26      public DeleteByPrimaryKeyElementGenerator(boolean isSimple) {
27          super();
28          this.isSimple = isSimple;
29      }
30  
31      @Override
32      public void addElements(XmlElement parentElement) {
33          XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$
34  
35          answer.addAttribute(new Attribute("id", introspectedTable.getDeleteByPrimaryKeyStatementId())); //$NON-NLS-1$
36          String parameterClass;
37          if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
38              parameterClass = introspectedTable.getPrimaryKeyType();
39          } else {
40              // PK fields are in the base class. If more than on PK
41              // field, then they are coming in a map.
42              if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
43                  parameterClass = "map"; //$NON-NLS-1$
44              } else {
45                  parameterClass =
46                          introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
47              }
48          }
49          answer.addAttribute(new Attribute("parameterType", parameterClass)); //$NON-NLS-1$
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          String sb = "delete from " + introspectedTable.getFullyQualifiedTableNameAtRuntime(); //$NON-NLS-1$
54          answer.addElement(new TextElement(sb));
55  
56          buildPrimaryKeyWhereClause().forEach(answer::addElement);
57  
58          if (context.getPlugins().sqlMapDeleteByPrimaryKeyElementGenerated(answer,introspectedTable)) {
59              parentElement.addElement(answer);
60          }
61      }
62  }