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.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.mybatis.generator.api.IntrospectedColumn;
23  import org.mybatis.generator.api.dom.OutputUtilities;
24  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
25  import org.mybatis.generator.api.dom.xml.Attribute;
26  import org.mybatis.generator.api.dom.xml.TextElement;
27  import org.mybatis.generator.api.dom.xml.XmlElement;
28  import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
29  import org.mybatis.generator.config.GeneratedKey;
30  
31  /**
32   * 
33   * @author Jeff Butler
34   * 
35   */
36  public class InsertElementGenerator extends AbstractXmlElementGenerator {
37  
38      public InsertElementGenerator() {
39          super();
40      }
41  
42      @Override
43      public void addElements(XmlElement parentElement) {
44          XmlElement answer = new XmlElement("insert"); //$NON-NLS-1$
45  
46          answer.addAttribute(new Attribute(
47                  "id", introspectedTable.getInsertStatementId())); //$NON-NLS-1$
48  
49          FullyQualifiedJavaType parameterType = introspectedTable.getRules()
50                  .calculateAllFieldsClass();
51  
52          answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
53                  parameterType.getFullyQualifiedName()));
54  
55          context.getCommentGenerator().addComment(answer);
56  
57          GeneratedKey gk = introspectedTable.getGeneratedKey();
58  
59          if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
60              IntrospectedColumn introspectedColumn = introspectedTable
61                      .getColumn(gk.getColumn());
62              // if the column is null, then it's a configuration error. The
63              // warning has already been reported
64              if (introspectedColumn != null) {
65                  // pre-generated key
66                  answer.addElement(getSelectKey(introspectedColumn, gk));
67              }
68          }
69  
70          StringBuilder insertClause = new StringBuilder();
71          StringBuilder valuesClause = new StringBuilder();
72  
73          insertClause.append("insert into "); //$NON-NLS-1$
74          insertClause.append(introspectedTable
75                  .getFullyQualifiedTableNameAtRuntime());
76          insertClause.append(" ("); //$NON-NLS-1$
77  
78          valuesClause.append("values ("); //$NON-NLS-1$
79  
80          List<String> valuesClauses = new ArrayList<String>();
81          Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns()
82                  .iterator();
83          while (iter.hasNext()) {
84              IntrospectedColumn introspectedColumn = iter.next();
85              if (introspectedColumn.isIdentity()) {
86                  // cannot set values on identity fields
87                  continue;
88              }
89  
90              insertClause.append(Ibatis2FormattingUtilities
91                      .getEscapedColumnName(introspectedColumn));
92              valuesClause.append(Ibatis2FormattingUtilities
93                      .getParameterClause(introspectedColumn));
94              if (iter.hasNext()) {
95                  insertClause.append(", "); //$NON-NLS-1$
96                  valuesClause.append(", "); //$NON-NLS-1$
97              }
98  
99              if (valuesClause.length() > 80) {
100                 answer.addElement(new TextElement(insertClause.toString()));
101                 insertClause.setLength(0);
102                 OutputUtilities.xmlIndent(insertClause, 1);
103 
104                 valuesClauses.add(valuesClause.toString());
105                 valuesClause.setLength(0);
106                 OutputUtilities.xmlIndent(valuesClause, 1);
107             }
108         }
109 
110         insertClause.append(')');
111         answer.addElement(new TextElement(insertClause.toString()));
112 
113         valuesClause.append(')');
114         valuesClauses.add(valuesClause.toString());
115 
116         for (String clause : valuesClauses) {
117             answer.addElement(new TextElement(clause));
118         }
119 
120         if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
121             IntrospectedColumn introspectedColumn = introspectedTable
122                     .getColumn(gk.getColumn());
123             // if the column is null, then it's a configuration error. The
124             // warning has already been reported
125             if (introspectedColumn != null) {
126                 // pre-generated key
127                 answer.addElement(getSelectKey(introspectedColumn, gk));
128             }
129         }
130 
131         if (context.getPlugins().sqlMapInsertElementGenerated(answer,
132                 introspectedTable)) {
133             parentElement.addElement(answer);
134         }
135     }
136 }