View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.getAliasedEscapedColumnName;
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  
25  import org.mybatis.generator.api.IntrospectedColumn;
26  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
27  import org.mybatis.generator.api.dom.java.JavaVisibility;
28  import org.mybatis.generator.api.dom.java.Method;
29  import org.mybatis.generator.api.dom.java.Parameter;
30  import org.mybatis.generator.api.dom.java.TopLevelClass;
31  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
32  
33  public class ProviderUpdateByExampleSelectiveMethodGenerator extends AbstractJavaProviderMethodGenerator {
34  
35      @Override
36      public void addClassElements(TopLevelClass topLevelClass) {
37          Method method = new Method(introspectedTable.getUpdateByExampleSelectiveStatementId());
38          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
39          method.setVisibility(JavaVisibility.PUBLIC);
40          method.addParameter(new Parameter(
41                  new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
42                  "parameter")); //$NON-NLS-1$
43  
44          Set<FullyQualifiedJavaType> importedTypes = initializeImportedTypes("java.util.Map"); //$NON-NLS-1$
45  
46          FullyQualifiedJavaType recordClass = introspectedTable.getRules().calculateAllFieldsClass();
47          importedTypes.add(recordClass);
48          method.addBodyLine(String.format("%s row = (%s) parameter.get(\"row\");", //$NON-NLS-1$
49                  recordClass.getShortName(), recordClass.getShortName()));
50  
51          FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType());
52          importedTypes.add(example);
53          method.addBodyLine(String.format("%s example = (%s) parameter.get(\"example\");", //$NON-NLS-1$
54                  example.getShortName(), example.getShortName()));
55  
56          context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
57  
58          method.addBodyLine(""); //$NON-NLS-1$
59  
60          method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
61  
62          method.addBodyLine(String.format("sql.UPDATE(\"%s\");", //$NON-NLS-1$
63                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
64          method.addBodyLine(""); //$NON-NLS-1$
65  
66          for (IntrospectedColumn introspectedColumn :
67                  ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
68              if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
69                  method.addBodyLine(String.format("if (row.%s() != null) {", //$NON-NLS-1$
70                          getGetterMethodName(introspectedColumn.getJavaProperty(),
71                                  introspectedColumn.getFullyQualifiedJavaType())));
72              }
73  
74              StringBuilder sb = new StringBuilder();
75              sb.append(getParameterClause(introspectedColumn));
76              sb.insert(2, "row."); //$NON-NLS-1$
77  
78              method.addBodyLine(String.format("sql.SET(\"%s = %s\");", //$NON-NLS-1$
79                      escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)),
80                      sb.toString()));
81  
82              if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
83                  method.addBodyLine("}"); //$NON-NLS-1$
84              }
85  
86              method.addBodyLine(""); //$NON-NLS-1$
87          }
88  
89          method.addBodyLine("applyWhere(sql, example, true);"); //$NON-NLS-1$
90          method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
91  
92          if (context.getPlugins()
93                  .providerUpdateByExampleSelectiveMethodGenerated(method, topLevelClass, introspectedTable)) {
94              topLevelClass.addImportedTypes(importedTypes);
95              topLevelClass.addMethod(method);
96          }
97      }
98  }