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.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  import java.util.TreeSet;
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  
32  /**
33   * 
34   * @author Jeff Butler
35   * 
36   */
37  public class ProviderSelectByExampleWithoutBLOBsMethodGenerator extends
38          AbstractJavaProviderMethodGenerator {
39  
40      public ProviderSelectByExampleWithoutBLOBsMethodGenerator(boolean useLegacyBuilder) {
41          super(useLegacyBuilder);
42      }
43  
44      @Override
45      public void addClassElements(TopLevelClass topLevelClass) {
46          Set<String> staticImports = new TreeSet<String>();
47          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
48          
49          if (useLegacyBuilder) {
50          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
51          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT"); //$NON-NLS-1$
52          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT"); //$NON-NLS-1$
53          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.FROM"); //$NON-NLS-1$
54          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY"); //$NON-NLS-1$
55          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
56          } else {
57          	importedTypes.add(NEW_BUILDER_IMPORT);
58          }
59          
60          FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
61          importedTypes.add(fqjt);
62  
63          Method method = new Method(getMethodName());
64          method.setVisibility(JavaVisibility.PUBLIC);
65          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
66          method.addParameter(new Parameter(fqjt, "example")); //$NON-NLS-1$
67          
68          context.getCommentGenerator().addGeneralMethodComment(method,
69                  introspectedTable);
70          
71          if (useLegacyBuilder) {
72          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
73          } else {
74          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
75          }
76  
77          boolean distinctCheck = true;
78          for (IntrospectedColumn introspectedColumn : getColumns()) {
79              if (distinctCheck) {
80                  method.addBodyLine("if (example != null && example.isDistinct()) {"); //$NON-NLS-1$
81                  method.addBodyLine(String.format("%sSELECT_DISTINCT(\"%s\");", //$NON-NLS-1$
82                  	builderPrefix,
83                      escapeStringForJava(getSelectListPhrase(introspectedColumn))));
84                  method.addBodyLine("} else {"); //$NON-NLS-1$
85                  method.addBodyLine(String.format("%sSELECT(\"%s\");", //$NON-NLS-1$
86                  	builderPrefix,
87                      escapeStringForJava(getSelectListPhrase(introspectedColumn))));
88                  method.addBodyLine("}"); //$NON-NLS-1$
89              } else {
90                  method.addBodyLine(String.format("%sSELECT(\"%s\");", //$NON-NLS-1$
91                  	builderPrefix,
92                      escapeStringForJava(getSelectListPhrase(introspectedColumn))));
93              }
94              
95              distinctCheck = false;
96          }
97  
98          method.addBodyLine(String.format("%sFROM(\"%s\");", //$NON-NLS-1$
99          		builderPrefix,
100                 escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
101         if (useLegacyBuilder) {
102         	method.addBodyLine("applyWhere(example, false);"); //$NON-NLS-1$
103         } else {
104         	method.addBodyLine("applyWhere(sql, example, false);"); //$NON-NLS-1$
105         }
106         
107         method.addBodyLine(""); //$NON-NLS-1$
108         method.addBodyLine("if (example != null && example.getOrderByClause() != null) {"); //$NON-NLS-1$
109         method.addBodyLine(String.format("%sORDER_BY(example.getOrderByClause());", builderPrefix)); //$NON-NLS-1$
110         method.addBodyLine("}"); //$NON-NLS-1$
111         
112         method.addBodyLine(""); //$NON-NLS-1$
113         if (useLegacyBuilder) {
114         	method.addBodyLine("return SQL();"); //$NON-NLS-1$
115         } else {
116         	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
117         }
118         
119         if (callPlugins(method, topLevelClass)) {
120             topLevelClass.addStaticImports(staticImports);
121             topLevelClass.addImportedTypes(importedTypes);
122             topLevelClass.addMethod(method);
123         }
124     }
125     
126     public List<IntrospectedColumn> getColumns() {
127         return introspectedTable.getNonBLOBColumns();
128     }
129     
130     public String getMethodName() {
131         return introspectedTable.getSelectByExampleStatementId();        
132     }
133     
134     public boolean callPlugins(Method method, TopLevelClass topLevelClass) {
135         return context.getPlugins().providerSelectByExampleWithoutBLOBsMethodGenerated(method, topLevelClass,
136                 introspectedTable);
137     }
138 }