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.javamapper.elements.sqlprovider;
17  
18  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getEscapedColumnName;
19  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getParameterClause;
20  import static org.mybatis.generator.internal.util.JavaBeansUtil.getGetterMethodName;
21  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
22  
23  import java.util.Set;
24  import java.util.TreeSet;
25  
26  import org.mybatis.generator.api.IntrospectedColumn;
27  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
28  import org.mybatis.generator.api.dom.java.JavaVisibility;
29  import org.mybatis.generator.api.dom.java.Method;
30  import org.mybatis.generator.api.dom.java.Parameter;
31  import org.mybatis.generator.api.dom.java.TopLevelClass;
32  
33  /**
34   * 
35   * @author Jeff Butler
36   * 
37   */
38  public class ProviderInsertSelectiveMethodGenerator extends
39          AbstractJavaProviderMethodGenerator {
40  
41  	public ProviderInsertSelectiveMethodGenerator(boolean useLegacyBuilder) {
42          super(useLegacyBuilder);
43      }
44  
45      @Override
46      public void addClassElements(TopLevelClass topLevelClass) {
47          Set<String> staticImports = new TreeSet<String>();
48          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
49          
50          if (useLegacyBuilder) {
51          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
52          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO"); //$NON-NLS-1$
53          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
54          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.VALUES"); //$NON-NLS-1$
55          } else {
56          	importedTypes.add(NEW_BUILDER_IMPORT);
57          }
58  
59          FullyQualifiedJavaType fqjt = introspectedTable.getRules()
60              .calculateAllFieldsClass();
61          importedTypes.add(fqjt);
62  
63          Method method = new Method(
64                  introspectedTable.getInsertSelectiveStatementId());
65          method.setVisibility(JavaVisibility.PUBLIC);
66          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
67          method.addParameter(new Parameter(fqjt, "record")); //$NON-NLS-1$
68          
69          context.getCommentGenerator().addGeneralMethodComment(method,
70                  introspectedTable);
71  
72          if (useLegacyBuilder) {
73          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
74          } else {
75          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
76          }
77  
78      	method.addBodyLine(String.format("%sINSERT_INTO(\"%s\");", //$NON-NLS-1$
79                  builderPrefix,
80      			escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));
81      	
82          for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
83              if (introspectedColumn.isIdentity()) {
84                  // cannot set values on identity fields
85                  continue;
86              }
87              
88              method.addBodyLine(""); //$NON-NLS-1$
89              if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
90                      && !introspectedColumn.isSequenceColumn()) {
91                  method.addBodyLine(String.format("if (record.%s() != null) {", //$NON-NLS-1$
92                      getGetterMethodName(introspectedColumn.getJavaProperty(),
93                              introspectedColumn.getFullyQualifiedJavaType())));
94              }
95            	method.addBodyLine(String.format("%sVALUES(\"%s\", \"%s\");", //$NON-NLS-1$
96            			builderPrefix,
97            			escapeStringForJava(getEscapedColumnName(introspectedColumn)),
98                      getParameterClause(introspectedColumn)));
99  
100           	if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()
101                     && !introspectedColumn.isSequenceColumn()) {
102                 method.addBodyLine("}"); //$NON-NLS-1$
103             }
104         }
105         
106         method.addBodyLine(""); //$NON-NLS-1$
107         if (useLegacyBuilder) {
108         	method.addBodyLine("return SQL();"); //$NON-NLS-1$
109         } else {
110         	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
111         }
112         
113         if (context.getPlugins().providerInsertSelectiveMethodGenerated(method, topLevelClass,
114                 introspectedTable)) {
115             topLevelClass.addStaticImports(staticImports);
116             topLevelClass.addImportedTypes(importedTypes);
117             topLevelClass.addMethod(method);
118         }
119     }
120 }