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.StringUtility.escapeStringForJava;
19  
20  import java.util.Set;
21  import java.util.TreeSet;
22  
23  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24  import org.mybatis.generator.api.dom.java.JavaVisibility;
25  import org.mybatis.generator.api.dom.java.Method;
26  import org.mybatis.generator.api.dom.java.Parameter;
27  import org.mybatis.generator.api.dom.java.TopLevelClass;
28  
29  /**
30   * 
31   * @author Jeff Butler
32   * 
33   */
34  public class ProviderCountByExampleMethodGenerator extends
35          AbstractJavaProviderMethodGenerator {
36  
37      public ProviderCountByExampleMethodGenerator(boolean useLegacyBuilder) {
38          super(useLegacyBuilder);
39      }
40  
41      @Override
42      public void addClassElements(TopLevelClass topLevelClass) {
43          Set<String> staticImports = new TreeSet<String>();
44          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
45          
46          if (useLegacyBuilder) {
47          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
48          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.FROM"); //$NON-NLS-1$
49          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT"); //$NON-NLS-1$
50          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
51          } else {
52          	importedTypes.add(NEW_BUILDER_IMPORT);
53          }
54          
55          FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
56          importedTypes.add(fqjt);
57  
58          Method method = new Method(
59                  introspectedTable.getCountByExampleStatementId());
60          method.setVisibility(JavaVisibility.PUBLIC);
61          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
62          method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
63          
64          context.getCommentGenerator().addGeneralMethodComment(method,
65                  introspectedTable);
66  
67          if (useLegacyBuilder) {
68          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
69          	method.addBodyLine("SELECT(\"count(*)\");"); //$NON-NLS-1$
70          	method.addBodyLine(String.format("FROM(\"%s\");", //$NON-NLS-1$
71                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
72          	method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
73          	method.addBodyLine("return SQL();"); //$NON-NLS-1$
74          } else {
75          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
76          	method.addBodyLine(String.format("sql.SELECT(\"count(*)\").FROM(\"%s\");", //$NON-NLS-1$
77                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
78          	method.addBodyLine("applyWhere(sql, example, false);"); //$NON-NLS-1$
79          	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
80          }
81          
82          if (context.getPlugins().providerCountByExampleMethodGenerated(method, topLevelClass,
83                  introspectedTable)) {
84              topLevelClass.addStaticImports(staticImports);
85              topLevelClass.addImportedTypes(importedTypes);
86              topLevelClass.addMethod(method);
87          }
88      }
89  }