1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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");
51 staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT");
52 staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT");
53 staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.FROM");
54 staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY");
55 staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
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"));
67
68 context.getCommentGenerator().addGeneralMethodComment(method,
69 introspectedTable);
70
71 if (useLegacyBuilder) {
72 method.addBodyLine("BEGIN();");
73 } else {
74 method.addBodyLine("SQL sql = new SQL();");
75 }
76
77 boolean distinctCheck = true;
78 for (IntrospectedColumn introspectedColumn : getColumns()) {
79 if (distinctCheck) {
80 method.addBodyLine("if (example != null && example.isDistinct()) {");
81 method.addBodyLine(String.format("%sSELECT_DISTINCT(\"%s\");",
82 builderPrefix,
83 escapeStringForJava(getSelectListPhrase(introspectedColumn))));
84 method.addBodyLine("} else {");
85 method.addBodyLine(String.format("%sSELECT(\"%s\");",
86 builderPrefix,
87 escapeStringForJava(getSelectListPhrase(introspectedColumn))));
88 method.addBodyLine("}");
89 } else {
90 method.addBodyLine(String.format("%sSELECT(\"%s\");",
91 builderPrefix,
92 escapeStringForJava(getSelectListPhrase(introspectedColumn))));
93 }
94
95 distinctCheck = false;
96 }
97
98 method.addBodyLine(String.format("%sFROM(\"%s\");",
99 builderPrefix,
100 escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
101 if (useLegacyBuilder) {
102 method.addBodyLine("applyWhere(example, false);");
103 } else {
104 method.addBodyLine("applyWhere(sql, example, false);");
105 }
106
107 method.addBodyLine("");
108 method.addBodyLine("if (example != null && example.getOrderByClause() != null) {");
109 method.addBodyLine(String.format("%sORDER_BY(example.getOrderByClause());", builderPrefix));
110 method.addBodyLine("}");
111
112 method.addBodyLine("");
113 if (useLegacyBuilder) {
114 method.addBodyLine("return SQL();");
115 } else {
116 method.addBodyLine("return sql.toString();");
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 }