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