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