View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.TextElement;
25  import org.mybatis.generator.api.dom.xml.XmlElement;
26  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
27  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
28  
29  public class InsertElementGenerator extends AbstractXmlElementGenerator {
30  
31      private final boolean isSimple;
32  
33      public InsertElementGenerator(boolean isSimple) {
34          super();
35          this.isSimple = isSimple;
36      }
37  
38      @Override
39      public void addElements(XmlElement parentElement) {
40          FullyQualifiedJavaType parameterType;
41          if (isSimple) {
42              parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
43          } else {
44              parameterType = introspectedTable.getRules().calculateAllFieldsClass();
45          }
46  
47          XmlElement answer = buildInitialInsert(introspectedTable.getInsertStatementId(), parameterType);
48  
49          StringBuilder insertClause = new StringBuilder();
50  
51          insertClause.append("insert into "); //$NON-NLS-1$
52          insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
53          insertClause.append(" ("); //$NON-NLS-1$
54  
55          StringBuilder valuesClause = new StringBuilder();
56          valuesClause.append("values ("); //$NON-NLS-1$
57  
58          List<String> valuesClauses = new ArrayList<>();
59          List<IntrospectedColumn> columns =
60                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
61          for (int i = 0; i < columns.size(); i++) {
62              IntrospectedColumn introspectedColumn = columns.get(i);
63  
64              insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
65              valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
66              if (i + 1 < columns.size()) {
67                  insertClause.append(", "); //$NON-NLS-1$
68                  valuesClause.append(", "); //$NON-NLS-1$
69              }
70  
71              if (valuesClause.length() > 80) {
72                  answer.addElement(new TextElement(insertClause.toString()));
73                  insertClause.setLength(0);
74                  OutputUtilities.xmlIndent(insertClause, 1);
75  
76                  valuesClauses.add(valuesClause.toString());
77                  valuesClause.setLength(0);
78                  OutputUtilities.xmlIndent(valuesClause, 1);
79              }
80          }
81  
82          insertClause.append(')');
83          answer.addElement(new TextElement(insertClause.toString()));
84  
85          valuesClause.append(')');
86          valuesClauses.add(valuesClause.toString());
87  
88          for (String clause : valuesClauses) {
89              answer.addElement(new TextElement(clause));
90          }
91  
92          if (context.getPlugins().sqlMapInsertElementGenerated(answer, introspectedTable)) {
93              parentElement.addElement(answer);
94          }
95      }
96  }