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.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  import java.util.TreeSet;
25  
26  import org.mybatis.generator.api.IntrospectedColumn;
27  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
28  import org.mybatis.generator.api.dom.java.JavaVisibility;
29  import org.mybatis.generator.api.dom.java.Method;
30  import org.mybatis.generator.api.dom.java.Parameter;
31  import org.mybatis.generator.api.dom.java.TopLevelClass;
32  
33  /**
34   * 
35   * @author Jeff Butler
36   * 
37   */
38  public class ProviderUpdateByExampleWithoutBLOBsMethodGenerator extends
39          AbstractJavaProviderMethodGenerator {
40  
41  	public ProviderUpdateByExampleWithoutBLOBsMethodGenerator(boolean useLegacyBuilder) {
42          super(useLegacyBuilder);
43      }
44  
45      @Override
46      public void addClassElements(TopLevelClass topLevelClass) {
47          Set<String> staticImports = new TreeSet<String>();
48          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
49  
50          if (useLegacyBuilder) {
51          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN"); //$NON-NLS-1$
52          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.UPDATE"); //$NON-NLS-1$
53          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET"); //$NON-NLS-1$
54          	staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL"); //$NON-NLS-1$
55          } else {
56          	importedTypes.add(NEW_BUILDER_IMPORT);
57          }
58  
59          importedTypes.add(new FullyQualifiedJavaType("java.util.Map")); //$NON-NLS-1$
60          
61          Method method = new Method(getMethodName());
62          method.setReturnType(FullyQualifiedJavaType.getStringInstance());
63          method.setVisibility(JavaVisibility.PUBLIC);
64          method.addParameter(new Parameter(new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
65                  "parameter")); //$NON-NLS-1$
66          
67          context.getCommentGenerator().addGeneralMethodComment(method,
68                  introspectedTable);
69  
70          if (useLegacyBuilder) {
71          	method.addBodyLine("BEGIN();"); //$NON-NLS-1$
72          } else {
73          	method.addBodyLine("SQL sql = new SQL();"); //$NON-NLS-1$
74          }
75          
76          method.addBodyLine(String.format("%sUPDATE(\"%s\");", //$NON-NLS-1$
77                  builderPrefix,
78          		escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
79          method.addBodyLine(""); //$NON-NLS-1$
80          
81          for (IntrospectedColumn introspectedColumn : getColumns()) {
82              StringBuilder sb = new StringBuilder();
83              sb.append(getParameterClause(introspectedColumn));
84              sb.insert(2, "record."); //$NON-NLS-1$
85              
86              method.addBodyLine(String.format("%sSET(\"%s = %s\");", //$NON-NLS-1$
87                      builderPrefix,
88              		escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)),
89                      sb.toString()));
90          }
91          
92          method.addBodyLine(""); //$NON-NLS-1$
93          
94          FullyQualifiedJavaType example =
95              new FullyQualifiedJavaType(introspectedTable.getExampleType());
96          importedTypes.add(example);
97          method.addBodyLine(String.format("%s example = (%s) parameter.get(\"example\");", //$NON-NLS-1$
98                  example.getShortName(), example.getShortName()));
99          
100         if (useLegacyBuilder) {
101         	method.addBodyLine("applyWhere(example, true);"); //$NON-NLS-1$
102         	method.addBodyLine("return SQL();"); //$NON-NLS-1$
103         } else {
104         	method.addBodyLine("applyWhere(sql, example, true);"); //$NON-NLS-1$
105         	method.addBodyLine("return sql.toString();"); //$NON-NLS-1$
106         }
107         
108         if (callPlugins(method, topLevelClass)) {
109             topLevelClass.addStaticImports(staticImports);
110             topLevelClass.addImportedTypes(importedTypes);
111             topLevelClass.addMethod(method);
112         }
113     }
114     
115     public String getMethodName() {
116         return introspectedTable.getUpdateByExampleStatementId();        
117     }
118     
119     public List<IntrospectedColumn> getColumns() {
120         return introspectedTable.getNonBLOBColumns();
121     }
122     
123     public boolean callPlugins(Method method, TopLevelClass topLevelClass) {
124         return context.getPlugins().providerUpdateByExampleWithoutBLOBsMethodGenerated(method, topLevelClass,
125                 introspectedTable);
126     }
127 }