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 UpdateByExampleWithoutBLOBsElementGenerator extends
33          AbstractXmlElementGenerator {
34  
35      public UpdateByExampleWithoutBLOBsElementGenerator() {
36          super();
37      }
38  
39      @Override
40      public void addElements(XmlElement parentElement) {
41          XmlElement answer = new XmlElement("update"); //$NON-NLS-1$
42  
43          answer.addAttribute(new Attribute(
44                  "id", introspectedTable.getUpdateByExampleStatementId())); //$NON-NLS-1$
45  
46          context.getCommentGenerator().addComment(answer);
47  
48          StringBuilder sb = new StringBuilder();
49          sb.append("update "); //$NON-NLS-1$
50          sb.append(introspectedTable
51                  .getAliasedFullyQualifiedTableNameAtRuntime());
52          answer.addElement(new TextElement(sb.toString()));
53  
54          // set up for first column
55          sb.setLength(0);
56          sb.append("set "); //$NON-NLS-1$
57  
58          Iterator<IntrospectedColumn> iter = introspectedTable
59                  .getNonBLOBColumns().iterator();
60          while (iter.hasNext()) {
61              IntrospectedColumn introspectedColumn = iter.next();
62  
63              sb.append(Ibatis2FormattingUtilities
64                      .getAliasedEscapedColumnName(introspectedColumn));
65              sb.append(" = "); //$NON-NLS-1$
66              sb.append(Ibatis2FormattingUtilities.getParameterClause(
67                      introspectedColumn, "record.")); //$NON-NLS-1$
68  
69              if (iter.hasNext()) {
70                  sb.append(',');
71              }
72  
73              answer.addElement(new TextElement(sb.toString()));
74  
75              // set up for the next column
76              if (iter.hasNext()) {
77                  sb.setLength(0);
78                  OutputUtilities.xmlIndent(sb, 1);
79              }
80          }
81  
82          XmlElement isParameterPresentElement = new XmlElement(
83                  "isParameterPresent"); //$NON-NLS-1$
84          answer.addElement(isParameterPresentElement);
85  
86          XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
87          includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
88                  introspectedTable.getIbatis2SqlMapNamespace()
89                          + "." + introspectedTable.getExampleWhereClauseId())); //$NON-NLS-1$
90          isParameterPresentElement.addElement(includeElement);
91  
92          if (context.getPlugins()
93                  .sqlMapUpdateByExampleWithoutBLOBsElementGenerated(answer,
94                          introspectedTable)) {
95              parentElement.addElement(answer);
96          }
97      }
98  }