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 UpdateByExampleWithBLOBsElementGenerator extends
33          AbstractXmlElementGenerator {
34  
35      public UpdateByExampleWithBLOBsElementGenerator() {
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("id", //$NON-NLS-1$
44                  introspectedTable.getUpdateByExampleWithBLOBsStatementId()));
45  
46          answer.addAttribute(new Attribute("parameterType", "map")); //$NON-NLS-1$ //$NON-NLS-2$
47          context.getCommentGenerator().addComment(answer);
48  
49          StringBuilder sb = new StringBuilder();
50          sb.append("update "); //$NON-NLS-1$
51          sb.append(introspectedTable
52                  .getAliasedFullyQualifiedTableNameAtRuntime());
53          answer.addElement(new TextElement(sb.toString()));
54  
55          // set up for first column
56          sb.setLength(0);
57          sb.append("set "); //$NON-NLS-1$
58  
59          Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns()
60                  .iterator();
61          while (iter.hasNext()) {
62              IntrospectedColumn introspectedColumn = iter.next();
63  
64              sb.append(MyBatis3FormattingUtilities
65                      .getAliasedEscapedColumnName(introspectedColumn));
66              sb.append(" = "); //$NON-NLS-1$
67              sb.append(MyBatis3FormattingUtilities.getParameterClause(
68                      introspectedColumn, "record.")); //$NON-NLS-1$
69  
70              if (iter.hasNext()) {
71                  sb.append(',');
72              }
73  
74              answer.addElement(new TextElement(sb.toString()));
75  
76              // set up for the next column
77              if (iter.hasNext()) {
78                  sb.setLength(0);
79                  OutputUtilities.xmlIndent(sb, 1);
80              }
81          }
82  
83          answer.addElement(getUpdateByExampleIncludeElement());
84  
85          if (context.getPlugins()
86                  .sqlMapUpdateByExampleWithBLOBsElementGenerated(answer,
87                          introspectedTable)) {
88              parentElement.addElement(answer);
89          }
90      }
91  }