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.StringUtility.escapeStringForJava;
21  
22  import java.util.List;
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 ProviderUpdateByExampleWithoutBLOBsMethodGenerator extends AbstractJavaProviderMethodGenerator {
34  
35      @Override
36      public void addClassElements(TopLevelClass topLevelClass) {
37          Method method = new Method(getMethodName());
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          context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
45  
46          method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
47  
48          method.addBodyLine(String.format("sql.UPDATE(\"%s\");", //$NON-NLS-1$
49                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
50          method.addBodyLine(""); //$NON-NLS-1$
51  
52          for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(getColumns())) {
53              StringBuilder sb = new StringBuilder();
54              sb.append(getParameterClause(introspectedColumn));
55              sb.insert(2, "row."); //$NON-NLS-1$
56  
57              method.addBodyLine(String.format("sql.SET(\"%s = %s\");", //$NON-NLS-1$
58                      escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)), sb));
59          }
60  
61          method.addBodyLine(""); //$NON-NLS-1$
62  
63          Set<FullyQualifiedJavaType> importedTypes = initializeImportedTypes("java.util.Map"); //$NON-NLS-1$
64          FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType());
65          importedTypes.add(example);
66          method.addBodyLine(String.format("%s example = (%s) parameter.get(\"example\");", //$NON-NLS-1$
67                  example.getShortName(), example.getShortName()));
68  
69          method.addBodyLine("applyWhere(sql, example, true);"); //$NON-NLS-1$
70          method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
71  
72          if (callPlugins(method, topLevelClass)) {
73              topLevelClass.addImportedTypes(importedTypes);
74              topLevelClass.addMethod(method);
75          }
76      }
77  
78      public String getMethodName() {
79          return introspectedTable.getUpdateByExampleStatementId();
80      }
81  
82      public List<IntrospectedColumn> getColumns() {
83          return introspectedTable.getNonBLOBColumns();
84      }
85  
86      public boolean callPlugins(Method method, TopLevelClass topLevelClass) {
87          return context.getPlugins()
88                  .providerUpdateByExampleWithoutBLOBsMethodGenerated(method, topLevelClass, introspectedTable);
89      }
90  }