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 org.mybatis.generator.api.IntrospectedColumn;
19  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
20  import org.mybatis.generator.api.dom.xml.Attribute;
21  import org.mybatis.generator.api.dom.xml.TextElement;
22  import org.mybatis.generator.api.dom.xml.XmlElement;
23  import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
24  import org.mybatis.generator.config.GeneratedKey;
25  
26  /**
27   * 
28   * @author Jeff Butler
29   * 
30   */
31  public class InsertSelectiveElementGenerator extends
32          AbstractXmlElementGenerator {
33  
34      public InsertSelectiveElementGenerator() {
35          super();
36      }
37  
38      @Override
39      public void addElements(XmlElement parentElement) {
40          XmlElement answer = new XmlElement("insert"); //$NON-NLS-1$
41  
42          answer.addAttribute(new Attribute(
43                  "id", introspectedTable.getInsertSelectiveStatementId())); //$NON-NLS-1$
44  
45          FullyQualifiedJavaType parameterType = introspectedTable.getRules()
46                  .calculateAllFieldsClass();
47  
48          answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
49                  parameterType.getFullyQualifiedName()));
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          GeneratedKey gk = introspectedTable.getGeneratedKey();
54          if (gk != null) {
55              IntrospectedColumn introspectedColumn = introspectedTable
56                  .getColumn(gk.getColumn());
57              // if the column is null, then it's a configuration error. The
58              // warning has already been reported
59              if (introspectedColumn != null) {
60                  if (gk.isJdbcStandard()) {
61                      answer.addAttribute(new Attribute("useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
62                      answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
63                      answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$
64                  } else {
65                      answer.addElement(getSelectKey(introspectedColumn, gk));
66                  }
67              }
68          }
69  
70          StringBuilder sb = new StringBuilder();
71  
72          sb.append("insert into "); //$NON-NLS-1$
73          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
74          answer.addElement(new TextElement(sb.toString()));
75  
76          XmlElement insertTrimElement = new XmlElement("trim"); //$NON-NLS-1$
77          insertTrimElement.addAttribute(new Attribute("prefix", "(")); //$NON-NLS-1$ //$NON-NLS-2$
78          insertTrimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
79          insertTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); //$NON-NLS-1$ //$NON-NLS-2$
80          answer.addElement(insertTrimElement);
81  
82          XmlElement valuesTrimElement = new XmlElement("trim"); //$NON-NLS-1$
83          valuesTrimElement.addAttribute(new Attribute("prefix", "values (")); //$NON-NLS-1$ //$NON-NLS-2$
84          valuesTrimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-1$ //$NON-NLS-2$
85          valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ",")); //$NON-NLS-1$ //$NON-NLS-2$
86          answer.addElement(valuesTrimElement);
87  
88          for (IntrospectedColumn introspectedColumn : introspectedTable
89                  .getAllColumns()) {
90              if (introspectedColumn.isIdentity()) {
91                  // cannot set values on identity fields
92                  continue;
93              }
94  
95              if (introspectedColumn.isSequenceColumn()
96                      || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
97                  // if it is a sequence column, it is not optional
98                  // This is required for MyBatis3 because MyBatis3 parses
99                  // and calculates the SQL before executing the selectKey
100                 
101                 // if it is primitive, we cannot do a null check
102                 sb.setLength(0);
103                 sb.append(MyBatis3FormattingUtilities
104                     .getEscapedColumnName(introspectedColumn));
105                 sb.append(',');
106                 insertTrimElement.addElement(new TextElement(sb.toString()));
107 
108                 sb.setLength(0);
109                 sb.append(MyBatis3FormattingUtilities
110                     .getParameterClause(introspectedColumn));
111                 sb.append(',');
112                 valuesTrimElement.addElement(new TextElement(sb.toString()));
113 
114                 continue;
115             }            
116             
117             XmlElement insertNotNullElement = new XmlElement("if"); //$NON-NLS-1$
118             sb.setLength(0);
119             sb.append(introspectedColumn.getJavaProperty());
120             sb.append(" != null"); //$NON-NLS-1$
121             insertNotNullElement.addAttribute(new Attribute(
122                     "test", sb.toString())); //$NON-NLS-1$
123 
124             sb.setLength(0);
125             sb.append(MyBatis3FormattingUtilities
126                     .getEscapedColumnName(introspectedColumn));
127             sb.append(',');
128             insertNotNullElement.addElement(new TextElement(sb.toString()));
129             insertTrimElement.addElement(insertNotNullElement);
130 
131             XmlElement valuesNotNullElement = new XmlElement("if"); //$NON-NLS-1$
132             sb.setLength(0);
133             sb.append(introspectedColumn.getJavaProperty());
134             sb.append(" != null"); //$NON-NLS-1$
135             valuesNotNullElement.addAttribute(new Attribute(
136                     "test", sb.toString())); //$NON-NLS-1$
137 
138             sb.setLength(0);
139             sb.append(MyBatis3FormattingUtilities
140                     .getParameterClause(introspectedColumn));
141             sb.append(',');
142             valuesNotNullElement.addElement(new TextElement(sb.toString()));
143             valuesTrimElement.addElement(valuesNotNullElement);
144         }
145 
146         if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(
147                 answer, introspectedTable)) {
148             parentElement.addElement(answer);
149         }
150     }
151 }