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 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.ibatis2.Ibatis2FormattingUtilities;
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("parameterClass", //$NON-NLS-1$
49                  parameterType.getFullyQualifiedName()));
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          GeneratedKey gk = introspectedTable.getGeneratedKey();
54  
55          if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
56              IntrospectedColumn introspectedColumn = introspectedTable
57                      .getColumn(gk.getColumn());
58              // if the column is null, then it's a configuration error. The
59              // warning has already been reported
60              if (introspectedColumn != null) {
61                  // pre-generated key
62                  answer.addElement(getSelectKey(introspectedColumn, gk));
63              }
64          }
65  
66          StringBuilder sb = new StringBuilder();
67  
68          sb.append("insert into "); //$NON-NLS-1$
69          sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
70          answer.addElement(new TextElement(sb.toString()));
71  
72          XmlElement insertElement = new XmlElement("dynamic"); //$NON-NLS-1$
73          insertElement.addAttribute(new Attribute("prepend", "(")); //$NON-NLS-1$ //$NON-NLS-2$
74          answer.addElement(insertElement);
75  
76          answer.addElement(new TextElement("values")); //$NON-NLS-1$
77  
78          XmlElement valuesElement = new XmlElement("dynamic"); //$NON-NLS-1$
79          valuesElement.addAttribute(new Attribute("prepend", "(")); //$NON-NLS-1$ //$NON-NLS-2$
80          answer.addElement(valuesElement);
81  
82          for (IntrospectedColumn introspectedColumn : introspectedTable
83                  .getAllColumns()) {
84              if (introspectedColumn.isIdentity()) {
85                  // cannot set values on identity fields
86                  continue;
87              }
88  
89              XmlElement insertNotNullElement = new XmlElement("isNotNull"); //$NON-NLS-1$
90              insertNotNullElement.addAttribute(new Attribute("prepend", ",")); //$NON-NLS-1$ //$NON-NLS-2$
91              insertNotNullElement.addAttribute(new Attribute(
92                      "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
93              insertNotNullElement.addElement(new TextElement(
94                      Ibatis2FormattingUtilities
95                              .getEscapedColumnName(introspectedColumn)));
96              insertElement.addElement(insertNotNullElement);
97  
98              XmlElement valuesNotNullElement = new XmlElement("isNotNull"); //$NON-NLS-1$
99              valuesNotNullElement.addAttribute(new Attribute("prepend", ",")); //$NON-NLS-1$ //$NON-NLS-2$
100             valuesNotNullElement.addAttribute(new Attribute(
101                     "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
102             valuesNotNullElement.addElement(new TextElement(
103                     Ibatis2FormattingUtilities
104                             .getParameterClause(introspectedColumn)));
105             valuesElement.addElement(valuesNotNullElement);
106         }
107 
108         insertElement.addElement(new TextElement(")")); //$NON-NLS-1$
109         valuesElement.addElement(new TextElement(")")); //$NON-NLS-1$
110 
111         if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
112             IntrospectedColumn introspectedColumn = introspectedTable
113                     .getColumn(gk.getColumn());
114             // if the column is null, then it's a configuration error. The
115             // warning has already been reported
116             if (introspectedColumn != null) {
117                 // pre-generated key
118                 answer.addElement(getSelectKey(introspectedColumn, gk));
119             }
120         }
121 
122         if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(
123                 answer, introspectedTable)) {
124             parentElement.addElement(answer);
125         }
126     }
127 }