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.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 UpdateByPrimaryKeySelectiveElementGenerator extends
30          AbstractXmlElementGenerator {
31  
32      public UpdateByPrimaryKeySelectiveElementGenerator() {
33          super();
34      }
35  
36      @Override
37      public void addElements(XmlElement parentElement) {
38          XmlElement answer = new XmlElement("update"); //$NON-NLS-1$
39  
40          answer
41                  .addAttribute(new Attribute(
42                          "id", introspectedTable.getUpdateByPrimaryKeySelectiveStatementId())); //$NON-NLS-1$
43  
44          String parameterType;
45  
46          if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
47              parameterType = introspectedTable.getRecordWithBLOBsType();
48          } else {
49              parameterType = introspectedTable.getBaseRecordType();
50          }
51  
52          answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
53                  parameterType));
54  
55          context.getCommentGenerator().addComment(answer);
56  
57          StringBuilder sb = new StringBuilder();
58  
59          sb.append("update "); //$NON-NLS-1$
60          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
61          answer.addElement(new TextElement(sb.toString()));
62  
63          XmlElement dynamicElement = new XmlElement("set"); //$NON-NLS-1$
64          answer.addElement(dynamicElement);
65  
66          for (IntrospectedColumn introspectedColumn : introspectedTable
67                  .getNonPrimaryKeyColumns()) {
68              XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
69              sb.setLength(0);
70              sb.append(introspectedColumn.getJavaProperty());
71              sb.append(" != null"); //$NON-NLS-1$
72              isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
73              dynamicElement.addElement(isNotNullElement);
74  
75              sb.setLength(0);
76              sb.append(MyBatis3FormattingUtilities
77                      .getEscapedColumnName(introspectedColumn));
78              sb.append(" = "); //$NON-NLS-1$
79              sb.append(MyBatis3FormattingUtilities
80                      .getParameterClause(introspectedColumn));
81              sb.append(',');
82  
83              isNotNullElement.addElement(new TextElement(sb.toString()));
84          }
85  
86          boolean and = false;
87          for (IntrospectedColumn introspectedColumn : introspectedTable
88                  .getPrimaryKeyColumns()) {
89              sb.setLength(0);
90              if (and) {
91                  sb.append("  and "); //$NON-NLS-1$
92              } else {
93                  sb.append("where "); //$NON-NLS-1$
94                  and = true;
95              }
96  
97              sb.append(MyBatis3FormattingUtilities
98                      .getEscapedColumnName(introspectedColumn));
99              sb.append(" = "); //$NON-NLS-1$
100             sb.append(MyBatis3FormattingUtilities
101                     .getParameterClause(introspectedColumn));
102             answer.addElement(new TextElement(sb.toString()));
103         }
104 
105         if (context.getPlugins()
106                 .sqlMapUpdateByPrimaryKeySelectiveElementGenerated(answer,
107                         introspectedTable)) {
108             parentElement.addElement(answer);
109         }
110     }
111 }