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 java.util.Iterator;
19  
20  import org.mybatis.generator.api.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.OutputUtilities;
22  import org.mybatis.generator.api.dom.xml.Attribute;
23  import org.mybatis.generator.api.dom.xml.TextElement;
24  import org.mybatis.generator.api.dom.xml.XmlElement;
25  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
26  
27  /**
28   * 
29   * @author Jeff Butler
30   * 
31   */
32  public class UpdateByPrimaryKeyWithoutBLOBsElementGenerator extends
33          AbstractXmlElementGenerator {
34  
35      private boolean isSimple;
36      
37      public UpdateByPrimaryKeyWithoutBLOBsElementGenerator(boolean isSimple) {
38          super();
39          this.isSimple = isSimple;
40      }
41  
42      @Override
43      public void addElements(XmlElement parentElement) {
44          XmlElement answer = new XmlElement("update"); //$NON-NLS-1$
45  
46          answer.addAttribute(new Attribute(
47                  "id", introspectedTable.getUpdateByPrimaryKeyStatementId())); //$NON-NLS-1$
48          answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
49                  introspectedTable.getBaseRecordType()));
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          StringBuilder sb = new StringBuilder();
54          sb.append("update "); //$NON-NLS-1$
55          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
56          answer.addElement(new TextElement(sb.toString()));
57  
58          // set up for first column
59          sb.setLength(0);
60          sb.append("set "); //$NON-NLS-1$
61  
62          Iterator<IntrospectedColumn> iter;
63          if (isSimple) {
64              iter = introspectedTable.getNonPrimaryKeyColumns().iterator();
65          } else {
66              iter = introspectedTable.getBaseColumns().iterator();
67          }
68          while (iter.hasNext()) {
69              IntrospectedColumn introspectedColumn = iter.next();
70  
71              sb.append(MyBatis3FormattingUtilities
72                      .getEscapedColumnName(introspectedColumn));
73              sb.append(" = "); //$NON-NLS-1$
74              sb.append(MyBatis3FormattingUtilities
75                      .getParameterClause(introspectedColumn));
76  
77              if (iter.hasNext()) {
78                  sb.append(',');
79              }
80  
81              answer.addElement(new TextElement(sb.toString()));
82  
83              // set up for the next column
84              if (iter.hasNext()) {
85                  sb.setLength(0);
86                  OutputUtilities.xmlIndent(sb, 1);
87              }
88          }
89  
90          boolean and = false;
91          for (IntrospectedColumn introspectedColumn : introspectedTable
92                  .getPrimaryKeyColumns()) {
93              sb.setLength(0);
94              if (and) {
95                  sb.append("  and "); //$NON-NLS-1$
96              } else {
97                  sb.append("where "); //$NON-NLS-1$
98                  and = true;
99              }
100 
101             sb.append(MyBatis3FormattingUtilities
102                     .getEscapedColumnName(introspectedColumn));
103             sb.append(" = "); //$NON-NLS-1$
104             sb.append(MyBatis3FormattingUtilities
105                     .getParameterClause(introspectedColumn));
106             answer.addElement(new TextElement(sb.toString()));
107         }
108 
109         if (context.getPlugins()
110                 .sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(answer,
111                         introspectedTable)) {
112             parentElement.addElement(answer);
113         }
114     }
115 }