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 ProviderUpdateByPrimaryKeySelectiveMethodGenerator extends
39          AbstractJavaProviderMethodGenerator {
40  
41      public ProviderUpdateByPrimaryKeySelectiveMethodGenerator(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.UPDATE"); //$NON-NLS-1$
53          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET"); //$NON-NLS-1$
54          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
55          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.WHERE"); //$NON-NLS-1$
56          } else {
57          	importedTypes.add(NEW_BUILDER_IMPORT);
58          }
59  
60          FullyQualifiedJavaType fqjt = introspectedTable.getRules().calculateAllFieldsClass();
61          importedTypes.add(fqjt);
62          
63          Method method = new Method(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId());
64          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
65          method.setVisibility(JavaVisibility.PUBLIC);
66          method.addParameter(new Parameter(fqjt, "record")); //$NON-NLS-1$
67          
68          context.getCommentGenerator().addGeneralMethodComment(method,
69                  introspectedTable);
70  
71          if (useLegacyBuilder) {
72          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
73          } else {
74          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
75          }
76          
77          method.addBodyLine(String.format("%sUPDATE(\"%s\");", //$NON-NLS-1$
78                  builderPrefix,
79          		escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));
80          method.addBodyLine(""); //$NON-NLS-1$
81          
82          for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
83              if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
84                  method.addBodyLine(String.format("if (record.%s() != null) {", //$NON-NLS-1$
85                      getGetterMethodName(introspectedColumn.getJavaProperty(),
86                              introspectedColumn.getFullyQualifiedJavaType())));
87              }
88  
89              method.addBodyLine(String.format("%sSET(\"%s = %s\");", //$NON-NLS-1$
90              		builderPrefix,
91              		escapeStringForJava(getEscapedColumnName(introspectedColumn)),
92                      getParameterClause(introspectedColumn)));
93                  
94              if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
95                  method.addBodyLine("}"); //$NON-NLS-1$
96              }
97  
98              method.addBodyLine(""); //$NON-NLS-1$
99          }
100 
101         for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
102             method.addBodyLine(String.format("%sWHERE(\"%s = %s\");", //$NON-NLS-1$
103             		builderPrefix,
104             		escapeStringForJava(getEscapedColumnName(introspectedColumn)),
105                     getParameterClause(introspectedColumn)));
106         }
107         
108         method.addBodyLine(""); //$NON-NLS-1$
109         
110         if (useLegacyBuilder) {
111         	method.addBodyLine("return SQL();"); //$NON-NLS-1$
112         } else {
113         	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
114         }
115 
116         if (context.getPlugins().providerUpdateByPrimaryKeySelectiveMethodGenerated(method, topLevelClass,
117                 introspectedTable)) {
118             topLevelClass.addStaticImports(staticImports);
119             topLevelClass.addImportedTypes(importedTypes);
120             topLevelClass.addMethod(method);
121         }
122     }
123 }