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.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.ListUtilities;
23  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
24  
25  public class UpdateByPrimaryKeySelectiveElementGenerator extends AbstractXmlElementGenerator {
26  
27      public UpdateByPrimaryKeySelectiveElementGenerator() {
28          super();
29      }
30  
31      @Override
32      public void addElements(XmlElement parentElement) {
33          XmlElement answer = new XmlElement("update"); //$NON-NLS-1$
34  
35          answer.addAttribute(new Attribute(
36                  "id", introspectedTable.getUpdateByPrimaryKeySelectiveStatementId())); //$NON-NLS-1$
37  
38          String parameterType;
39  
40          if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
41              parameterType = introspectedTable.getRecordWithBLOBsType();
42          } else {
43              parameterType = introspectedTable.getBaseRecordType();
44          }
45  
46          answer.addAttribute(new Attribute("parameterType", parameterType)); //$NON-NLS-1$
47  
48          context.getCommentGenerator().addComment(answer);
49  
50          StringBuilder sb = new StringBuilder();
51  
52          sb.append("update "); //$NON-NLS-1$
53          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
54          answer.addElement(new TextElement(sb.toString()));
55  
56          XmlElement dynamicElement = new XmlElement("set"); //$NON-NLS-1$
57          answer.addElement(dynamicElement);
58  
59          for (IntrospectedColumn introspectedColumn :
60                  ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())) {
61              sb.setLength(0);
62              sb.append(introspectedColumn.getJavaProperty());
63              sb.append(" != null"); //$NON-NLS-1$
64              XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
65              isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
66              dynamicElement.addElement(isNotNullElement);
67  
68              sb.setLength(0);
69              sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
70              sb.append(" = "); //$NON-NLS-1$
71              sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
72              sb.append(',');
73  
74              isNotNullElement.addElement(new TextElement(sb.toString()));
75          }
76  
77          buildPrimaryKeyWhereClause().forEach(answer::addElement);
78  
79          if (context.getPlugins().sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer, introspectedTable)) {
80              parentElement.addElement(answer);
81          }
82      }
83  }