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.internal.util.JavaBeansUtil.getGetterMethodName;
19  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
20  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getAliasedEscapedColumnName;
21  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getParameterClause;
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 ProviderUpdateByExampleSelectiveMethodGenerator extends
39          AbstractJavaProviderMethodGenerator {
40  
41  	public ProviderUpdateByExampleSelectiveMethodGenerator(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          } else {
56          	importedTypes.add(NEW_BUILDER_IMPORT);
57          }
58  
59          importedTypes.add(new FullyQualifiedJavaType("java.util.Map")); //$NON-NLS-1$
60          
61          Method method = new Method(introspectedTable.getUpdateByExampleSelectiveStatementId());
62          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
63          method.setVisibility(JavaVisibility.PUBLIC);
64          method.addParameter(new Parameter(new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
65                  "parameter")); //$NON-NLS-1$
66          
67          FullyQualifiedJavaType record =
68              introspectedTable.getRules().calculateAllFieldsClass();
69          importedTypes.add(record);
70          method.addBodyLine(String.format("%s record = (%s) parameter.get(\"record\");", //$NON-NLS-1$
71                  record.getShortName(), record.getShortName()));
72  
73          FullyQualifiedJavaType example =
74              new FullyQualifiedJavaType(introspectedTable.getExampleType());
75          importedTypes.add(example);
76          method.addBodyLine(String.format("%s example = (%s) parameter.get(\"example\");", //$NON-NLS-1$
77                  example.getShortName(), example.getShortName()));
78  
79          context.getCommentGenerator().addGeneralMethodComment(method,
80                  introspectedTable);
81  
82          method.addBodyLine(""); //$NON-NLS-1$
83          
84          if (useLegacyBuilder) {
85          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
86          } else {
87          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
88          }
89          
90          method.addBodyLine(String.format("%sUPDATE(\"%s\");", //$NON-NLS-1$
91          		builderPrefix,
92                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
93          method.addBodyLine(""); //$NON-NLS-1$
94          
95          for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
96              if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
97                  method.addBodyLine(String.format("if (record.%s() != null) {", //$NON-NLS-1$
98                      getGetterMethodName(introspectedColumn.getJavaProperty(),
99                              introspectedColumn.getFullyQualifiedJavaType())));
100             }
101 
102             StringBuilder sb = new StringBuilder();
103             sb.append(getParameterClause(introspectedColumn));
104             sb.insert(2, "record."); //$NON-NLS-1$
105             
106             method.addBodyLine(String.format("%sSET(\"%s = %s\");", //$NON-NLS-1$
107                     builderPrefix,
108             		escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)),
109                     sb.toString()));
110                 
111             if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
112                 method.addBodyLine("}"); //$NON-NLS-1$
113             }
114 
115             method.addBodyLine(""); //$NON-NLS-1$
116         }
117         
118         if (useLegacyBuilder) {
119         	method.addBodyLine("applyWhere(example, true);"); //$NON-NLS-1$
120         	method.addBodyLine("return SQL();"); //$NON-NLS-1$
121         } else {
122         	method.addBodyLine("applyWhere(sql, example, true);"); //$NON-NLS-1$
123         	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
124         }
125         
126         if (context.getPlugins().providerUpdateByExampleSelectiveMethodGenerated(method, topLevelClass,
127                 introspectedTable)) {
128             topLevelClass.addStaticImports(staticImports);
129             topLevelClass.addImportedTypes(importedTypes);
130             topLevelClass.addMethod(method);
131         }
132     }
133 }