-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopNByExamplePlugin.java
218 lines (183 loc) · 11.2 KB
/
TopNByExamplePlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.revengemission.plugins.mybatis;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.List;
/**
* TopN
*/
public class TopNByExamplePlugin extends AbstractXmbgPlugin {
private static final String FIELD_NAME = "topN";
private static final String CLIENT_METHOD_NAME = "topOneByExample";
private static final String CLIENT_METHOD_NAME2 = "topNByExample";
@Override
public boolean validate(List<String> warnings) {
return true;
}
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
Field field = new Field(FIELD_NAME, FullyQualifiedJavaType.getIntInstance());
field.setVisibility(JavaVisibility.PRIVATE);
field.setInitializationString("5");
topLevelClass.addField(field);
char c = FIELD_NAME.charAt(0);
String camelName = Character.toUpperCase(c) + FIELD_NAME.substring(1);
Method method = new Method("set" + camelName);
method.setVisibility(JavaVisibility.PUBLIC);
method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), FIELD_NAME));
method.addBodyLine("this." + FIELD_NAME + "=" + FIELD_NAME + ";");
topLevelClass.addMethod(method);
method = new Method("get" + camelName);
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
method.addBodyLine("return " + FIELD_NAME + ";");
topLevelClass.addMethod(method);
return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
}
@Override
public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
{
Method method = new Method(CLIENT_METHOD_NAME);
method.setAbstract(true);
method.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example"));
method.setReturnType(new FullyQualifiedJavaType(getEntityName(introspectedTable)));
interfaze.addMethod(method);
}
{
Method method = new Method(CLIENT_METHOD_NAME2);
method.setAbstract(true);
method.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example"));
method.setReturnType(new FullyQualifiedJavaType("List<" + getEntityName(introspectedTable) + ">"));
interfaze.addMethod(method);
}
return true;
}
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
XmlElement parentElement = document.getRootElement();
{
XmlElement selectTopOneElement = new XmlElement("select");
selectTopOneElement.addAttribute(new Attribute("id", CLIENT_METHOD_NAME));
selectTopOneElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
selectTopOneElement.addAttribute(new Attribute("resultMap", "ResultMapWithBLOBs"));
} else {
selectTopOneElement.addAttribute(new Attribute("resultMap", "BaseResultMap"));
}
addTopOneLimitStatement(selectTopOneElement, introspectedTable);
addTopOneSqlserverStatement(selectTopOneElement, introspectedTable);
addTopOneDB2Statement(selectTopOneElement, introspectedTable);
parentElement.addElement(selectTopOneElement);
}
{
XmlElement selectTopNElement = new XmlElement("select");
selectTopNElement.addAttribute(new Attribute("id", CLIENT_METHOD_NAME2));
selectTopNElement.addAttribute(new Attribute("parameterType", introspectedTable.getExampleType()));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
selectTopNElement.addAttribute(new Attribute("resultMap", "ResultMapWithBLOBs"));
} else {
selectTopNElement.addAttribute(new Attribute("resultMap", "BaseResultMap"));
}
addTopNLimitStatement(selectTopNElement, introspectedTable);
addTopNSqlserverStatement(selectTopNElement, introspectedTable);
addTopNDB2Statement(selectTopNElement, introspectedTable);
parentElement.addElement(selectTopNElement);
}
return super.sqlMapDocumentGenerated(document, introspectedTable);
}
void addTopOneLimitStatement(XmlElement parentElement, IntrospectedTable introspectedTable) {
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
XmlElement topOneElement = new XmlElement("if");
topOneElement.addAttribute(new Attribute("test", "_databaseId == 'mysql' or _databaseId == 'postgresql' or _databaseId == 'sqlite'"));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" />, <include refid=\"Blob_Column_List\" /> from " + tableName));
} else {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" /> from " + tableName));
}
topOneElement.addElement(new TextElement("<include refid=\"Example_Where_Clause\" />"));
XmlElement orderByElement = new XmlElement("if");
orderByElement.addAttribute(new Attribute("test", "orderByClause != null"));
orderByElement.addElement(new TextElement("order by ${orderByClause}"));
topOneElement.addElement(orderByElement);
topOneElement.addElement(new TextElement("limit 1"));
parentElement.addElement(topOneElement);
}
void addTopNLimitStatement(XmlElement parentElement, IntrospectedTable introspectedTable) {
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
XmlElement topOneElement = new XmlElement("if");
topOneElement.addAttribute(new Attribute("test", "_databaseId == 'mysql' or _databaseId == 'postgresql' or _databaseId == 'sqlite'"));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" />, <include refid=\"Blob_Column_List\" /> from " + tableName));
} else {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" /> from " + tableName));
}
topOneElement.addElement(new TextElement("<include refid=\"Example_Where_Clause\" />"));
XmlElement orderByElement = new XmlElement("if");
orderByElement.addAttribute(new Attribute("test", "orderByClause != null"));
orderByElement.addElement(new TextElement("order by ${orderByClause}"));
topOneElement.addElement(orderByElement);
topOneElement.addElement(new TextElement("limit ${topN}"));
parentElement.addElement(topOneElement);
}
void addTopOneSqlserverStatement(XmlElement parentElement, IntrospectedTable introspectedTable) {
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
XmlElement topOneElement = new XmlElement("if");
topOneElement.addAttribute(new Attribute("test", "_databaseId == 'sqlserver'"));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
topOneElement.addElement(new TextElement("select top 1 <include refid=\"Base_Column_List\" />, <include refid=\"Blob_Column_List\" /> from " + tableName));
} else {
topOneElement.addElement(new TextElement("select top 1 <include refid=\"Base_Column_List\" /> from " + tableName));
}
topOneElement.addElement(new TextElement("<include refid=\"Example_Where_Clause\" />"));
XmlElement orderByElement = new XmlElement("if");
orderByElement.addAttribute(new Attribute("test", "orderByClause != null"));
orderByElement.addElement(new TextElement("order by ${orderByClause}"));
topOneElement.addElement(orderByElement);
parentElement.addElement(topOneElement);
}
void addTopNSqlserverStatement(XmlElement parentElement, IntrospectedTable introspectedTable) {
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
XmlElement topOneElement = new XmlElement("if");
topOneElement.addAttribute(new Attribute("test", "_databaseId == 'sqlserver'"));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
topOneElement.addElement(new TextElement("select top ${topN} <include refid=\"Base_Column_List\" />, <include refid=\"Blob_Column_List\" /> from " + tableName));
} else {
topOneElement.addElement(new TextElement("select top ${topN} <include refid=\"Base_Column_List\" /> from " + tableName));
}
topOneElement.addElement(new TextElement("<include refid=\"Example_Where_Clause\" />"));
XmlElement orderByElement = new XmlElement("if");
orderByElement.addAttribute(new Attribute("test", "orderByClause != null"));
orderByElement.addElement(new TextElement("order by ${orderByClause}"));
topOneElement.addElement(orderByElement);
parentElement.addElement(topOneElement);
}
void addTopOneDB2Statement(XmlElement parentElement, IntrospectedTable introspectedTable) {
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
XmlElement topOneElement = new XmlElement("if");
topOneElement.addAttribute(new Attribute("test", "_databaseId == 'db2'"));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" />, <include refid=\"Blob_Column_List\" /> from " + tableName));
} else {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" /> from " + tableName));
}
topOneElement.addElement(new TextElement("<include refid=\"Example_Where_Clause\" />"));
topOneElement.addElement(new TextElement("fetch first 1 only"));
parentElement.addElement(topOneElement);
}
void addTopNDB2Statement(XmlElement parentElement, IntrospectedTable introspectedTable) {
String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
XmlElement topOneElement = new XmlElement("if");
topOneElement.addAttribute(new Attribute("test", "_databaseId == 'db2'"));
if (!introspectedTable.getBLOBColumns().isEmpty()) {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" />, <include refid=\"Blob_Column_List\" /> from " + tableName));
} else {
topOneElement.addElement(new TextElement("select <include refid=\"Base_Column_List\" /> from " + tableName));
}
topOneElement.addElement(new TextElement("<include refid=\"Example_Where_Clause\" />"));
topOneElement.addElement(new TextElement("fetch first ${topN} only"));
parentElement.addElement(topOneElement);
}
}