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 UpdateByExampleSelectiveElementGenerator extends
26          AbstractXmlElementGenerator {
27  
28      public UpdateByExampleSelectiveElementGenerator() {
29          super();
30      }
31  
32      @Override
33      public void addElements(XmlElement parentElement) {
34          XmlElement answer = new XmlElement("update"); //$NON-NLS-1$
35  
36          answer.addAttribute(new Attribute(
37                  "id", introspectedTable.getUpdateByExampleSelectiveStatementId())); //$NON-NLS-1$
38  
39          answer.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-1$ //$NON-NLS-2$
40  
41          context.getCommentGenerator().addComment(answer);
42  
43          StringBuilder sb = new StringBuilder();
44          sb.append("update "); //$NON-NLS-1$
45          sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
46          answer.addElement(new TextElement(sb.toString()));
47  
48          XmlElement dynamicElement = new XmlElement("set"); //$NON-NLS-1$
49          answer.addElement(dynamicElement);
50  
51          for (IntrospectedColumn introspectedColumn :
52                  ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
53              sb.setLength(0);
54              sb.append(introspectedColumn.getJavaProperty("row.")); //$NON-NLS-1$
55              sb.append(" != null"); //$NON-NLS-1$
56              XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
57              isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
58              dynamicElement.addElement(isNotNullElement);
59  
60              sb.setLength(0);
61              sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
62              sb.append(" = "); //$NON-NLS-1$
63              sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "row.")); //$NON-NLS-1$
64              sb.append(',');
65  
66              isNotNullElement.addElement(new TextElement(sb.toString()));
67          }
68  
69          answer.addElement(getUpdateByExampleIncludeElement());
70  
71          if (context.getPlugins().sqlMapUpdateByExampleSelectiveElementGenerated(answer, introspectedTable)) {
72              parentElement.addElement(answer);
73          }
74      }
75  }