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