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 ProviderDeleteByExampleMethodGenerator extends
35          AbstractJavaProviderMethodGenerator {
36  
37      public ProviderDeleteByExampleMethodGenerator(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.DELETE_FROM"); //$NON-NLS-1$
49          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
50          } else {
51          	importedTypes.add(NEW_BUILDER_IMPORT);
52          }
53          
54          FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
55          importedTypes.add(fqjt);
56  
57          Method method = new Method(
58                  introspectedTable.getDeleteByExampleStatementId());
59          method.setVisibility(JavaVisibility.PUBLIC);
60          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
61          method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
62          
63          context.getCommentGenerator().addGeneralMethodComment(method,
64                  introspectedTable);
65  
66          if (useLegacyBuilder) {
67          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
68          	method.addBodyLine(String.format("DELETE_FROM(\"%s\");", //$NON-NLS-1$
69                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
70          	method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
71          	method.addBodyLine("return SQL();"); //$NON-NLS-1$
72          } else {
73          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
74          	method.addBodyLine(String.format("sql.DELETE_FROM(\"%s\");", //$NON-NLS-1$
75                  escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
76          	method.addBodyLine("applyWhere(sql, example, false);"); //$NON-NLS-1$
77          	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
78          }
79          
80          if (context.getPlugins().providerDeleteByExampleMethodGenerated(method, topLevelClass,
81                  introspectedTable)) {
82              topLevelClass.addStaticImports(staticImports);
83              topLevelClass.addImportedTypes(importedTypes);
84              topLevelClass.addMethod(method);
85          }
86      }
87  }