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.getSelectListPhrase;
19  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
20  
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.mybatis.generator.api.IntrospectedColumn;
25  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
26  import org.mybatis.generator.api.dom.java.JavaVisibility;
27  import org.mybatis.generator.api.dom.java.Method;
28  import org.mybatis.generator.api.dom.java.Parameter;
29  import org.mybatis.generator.api.dom.java.TopLevelClass;
30  
31  public class ProviderSelectByExampleWithoutBLOBsMethodGenerator extends AbstractJavaProviderMethodGenerator {
32  
33      @Override
34      public void addClassElements(TopLevelClass topLevelClass) {
35          FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
36          Set<FullyQualifiedJavaType> importedTypes = initializeImportedTypes(fqjt);
37  
38          Method method = new Method(getMethodName());
39          method.setVisibility(JavaVisibility.PUBLIC);
40          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
41          method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
42  
43          context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
44  
45          method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
46  
47          boolean distinctCheck = true;
48          for (IntrospectedColumn introspectedColumn : getColumns()) {
49              if (distinctCheck) {
50                  method.addBodyLine("if (example != null && example.isDistinct()) {"); //$NON-NLS-1$
51                  method.addBodyLine(String.format("sql.SELECT_DISTINCT(\"%s\");", //$NON-NLS-1$
52                          escapeStringForJava(getSelectListPhrase(introspectedColumn))));
53                  method.addBodyLine("} else {"); //$NON-NLS-1$
54                  method.addBodyLine(String.format("sql.SELECT(\"%s\");", //$NON-NLS-1$
55                          escapeStringForJava(getSelectListPhrase(introspectedColumn))));
56                  method.addBodyLine("}"); //$NON-NLS-1$
57              } else {
58                  method.addBodyLine(String.format("sql.SELECT(\"%s\");", //$NON-NLS-1$
59                          escapeStringForJava(getSelectListPhrase(introspectedColumn))));
60              }
61  
62              distinctCheck = false;
63          }
64  
65          method.addBodyLine(String.format("sql.FROM(\"%s\");", //$NON-NLS-1$
66                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
67          method.addBodyLine("applyWhere(sql, example, false);"); //$NON-NLS-1$
68  
69          method.addBodyLine(""); //$NON-NLS-1$
70          method.addBodyLine("if (example != null && example.getOrderByClause() != null) {"); //$NON-NLS-1$
71          method.addBodyLine("sql.ORDER_BY(example.getOrderByClause());"); //$NON-NLS-1$
72          method.addBodyLine("}"); //$NON-NLS-1$
73  
74          method.addBodyLine(""); //$NON-NLS-1$
75          method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
76  
77          if (callPlugins(method, topLevelClass)) {
78              topLevelClass.addImportedTypes(importedTypes);
79              topLevelClass.addMethod(method);
80          }
81      }
82  
83      public List<IntrospectedColumn> getColumns() {
84          return introspectedTable.getNonBLOBColumns();
85      }
86  
87      public String getMethodName() {
88          return introspectedTable.getSelectByExampleStatementId();
89      }
90  
91      public boolean callPlugins(Method method, TopLevelClass topLevelClass) {
92          return context.getPlugins()
93                  .providerSelectByExampleWithoutBLOBsMethodGenerated(method, topLevelClass, introspectedTable);
94      }
95  }