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 org.mybatis.generator.api.IntrospectedColumn;
19  import org.mybatis.generator.api.dom.xml.Attribute;
20  import org.mybatis.generator.api.dom.xml.TextElement;
21  import org.mybatis.generator.api.dom.xml.XmlElement;
22  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
23  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
24  
25  public class InsertSelectiveElementGenerator extends AbstractXmlElementGenerator {
26  
27      public InsertSelectiveElementGenerator() {
28          super();
29      }
30  
31      @Override
32      public void addElements(XmlElement parentElement) {
33          XmlElement answer = buildInitialInsert(introspectedTable.getInsertSelectiveStatementId(),
34                  introspectedTable.getRules().calculateAllFieldsClass());
35  
36          StringBuilder sb = new StringBuilder();
37  
38          sb.append("insert into "); //$NON-NLS-1$
39          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
40          answer.addElement(new TextElement(sb.toString()));
41  
42          XmlElement insertTrimElement = new XmlElement("trim"); //$NON-NLS-1$
43          insertTrimElement.addAttribute(new Attribute("prefix", "(")); //$NON-NLS-1$ //$NON-NLS-2$
44          insertTrimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
45          insertTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); //$NON-NLS-1$ //$NON-NLS-2$
46          answer.addElement(insertTrimElement);
47  
48          XmlElement valuesTrimElement = new XmlElement("trim"); //$NON-NLS-1$
49          valuesTrimElement.addAttribute(new Attribute("prefix", "values (")); //$NON-NLS-1$ //$NON-NLS-2$
50          valuesTrimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
51          valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); //$NON-NLS-1$ //$NON-NLS-2$
52          answer.addElement(valuesTrimElement);
53  
54          for (IntrospectedColumn introspectedColumn :
55                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
56  
57              if (introspectedColumn.isSequenceColumn()
58                      || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
59                  // if it is a sequence column, it is not optional
60                  // This is required for MyBatis3 because MyBatis3 parses
61                  // and calculates the SQL before executing the selectKey
62  
63                  // if it is primitive, we cannot do a null check
64                  sb.setLength(0);
65                  sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
66                  sb.append(',');
67                  insertTrimElement.addElement(new TextElement(sb.toString()));
68  
69                  sb.setLength(0);
70                  sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
71                  sb.append(',');
72                  valuesTrimElement.addElement(new TextElement(sb.toString()));
73  
74                  continue;
75              }
76  
77              sb.setLength(0);
78              sb.append(introspectedColumn.getJavaProperty());
79              sb.append(" != null"); //$NON-NLS-1$
80              XmlElement insertNotNullElement = new XmlElement("if"); //$NON-NLS-1$
81              insertNotNullElement.addAttribute(new Attribute(
82                      "test", sb.toString())); //$NON-NLS-1$
83  
84              sb.setLength(0);
85              sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
86              sb.append(',');
87              insertNotNullElement.addElement(new TextElement(sb.toString()));
88              insertTrimElement.addElement(insertNotNullElement);
89  
90              sb.setLength(0);
91              sb.append(introspectedColumn.getJavaProperty());
92              sb.append(" != null"); //$NON-NLS-1$
93              XmlElement valuesNotNullElement = new XmlElement("if"); //$NON-NLS-1$
94              valuesNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
95  
96              sb.setLength(0);
97              sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
98              sb.append(',');
99              valuesNotNullElement.addElement(new TextElement(sb.toString()));
100             valuesTrimElement.addElement(valuesNotNullElement);
101         }
102 
103         if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(answer, introspectedTable)) {
104             parentElement.addElement(answer);
105         }
106     }
107 }