1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.internal.rules;
17
18 import org.mybatis.generator.api.IntrospectedTable;
19 import org.mybatis.generator.api.IntrospectedTable.TargetRuntime;
20 import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
21 import org.mybatis.generator.codegen.mybatis3.ListUtilities;
22 import org.mybatis.generator.config.PropertyRegistry;
23 import org.mybatis.generator.config.TableConfiguration;
24 import org.mybatis.generator.internal.util.StringUtility;
25
26
27
28
29
30
31
32
33 public abstract class BaseRules implements Rules {
34
35 protected final TableConfiguration tableConfiguration;
36
37 protected final IntrospectedTable introspectedTable;
38
39 protected final boolean isModelOnly;
40
41 protected BaseRules(IntrospectedTable introspectedTable) {
42 super();
43 this.introspectedTable = introspectedTable;
44 this.tableConfiguration = introspectedTable.getTableConfiguration();
45 String modelOnly = tableConfiguration.getProperty(PropertyRegistry.TABLE_MODEL_ONLY);
46 isModelOnly = StringUtility.isTrue(modelOnly);
47 }
48
49
50
51
52
53
54
55
56 @Override
57 public boolean generateInsert() {
58 if (isModelOnly) {
59 return false;
60 }
61
62 return tableConfiguration.isInsertStatementEnabled();
63 }
64
65
66
67
68
69
70
71
72 @Override
73 public boolean generateInsertSelective() {
74 return generateInsert();
75 }
76
77
78
79
80
81
82
83
84
85 @Override
86 public FullyQualifiedJavaType calculateAllFieldsClass() {
87
88 String answer;
89
90 if (generateRecordWithBLOBsClass()) {
91 answer = introspectedTable.getRecordWithBLOBsType();
92 } else if (generateBaseRecordClass()) {
93 answer = introspectedTable.getBaseRecordType();
94 } else {
95 answer = introspectedTable.getPrimaryKeyType();
96 }
97
98 return new FullyQualifiedJavaType(answer);
99 }
100
101
102
103
104
105
106
107
108
109 @Override
110 public boolean generateUpdateByPrimaryKeyWithoutBLOBs() {
111 if (isModelOnly) {
112 return false;
113 }
114
115 if (ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getBaseColumns()).isEmpty()) {
116 return false;
117 }
118
119 return tableConfiguration.isUpdateByPrimaryKeyStatementEnabled()
120 && introspectedTable.hasPrimaryKeyColumns()
121 && introspectedTable.hasBaseColumns();
122 }
123
124
125
126
127
128
129
130
131
132 @Override
133 public boolean generateUpdateByPrimaryKeyWithBLOBs() {
134 if (isModelOnly) {
135 return false;
136 }
137
138 if (ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns()).isEmpty()) {
139 return false;
140 }
141
142 return tableConfiguration.isUpdateByPrimaryKeyStatementEnabled()
143 && introspectedTable.hasPrimaryKeyColumns()
144 && introspectedTable.hasBLOBColumns();
145 }
146
147
148
149
150
151
152
153
154
155 @Override
156 public boolean generateUpdateByPrimaryKeySelective() {
157 if (isModelOnly) {
158 return false;
159 }
160
161 if (ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns()).isEmpty()) {
162 return false;
163 }
164
165 return tableConfiguration.isUpdateByPrimaryKeyStatementEnabled()
166 && introspectedTable.hasPrimaryKeyColumns()
167 && (introspectedTable.hasBLOBColumns() || introspectedTable
168 .hasBaseColumns());
169 }
170
171
172
173
174
175
176
177
178
179 @Override
180 public boolean generateDeleteByPrimaryKey() {
181 if (isModelOnly) {
182 return false;
183 }
184
185 return tableConfiguration.isDeleteByPrimaryKeyStatementEnabled()
186 && introspectedTable.hasPrimaryKeyColumns();
187 }
188
189
190
191
192
193
194
195
196 @Override
197 public boolean generateDeleteByExample() {
198 if (isModelOnly) {
199 return false;
200 }
201
202 return tableConfiguration.isDeleteByExampleStatementEnabled();
203 }
204
205
206
207
208
209
210
211 @Override
212 public boolean generateBaseResultMap() {
213 if (isModelOnly) {
214 return true;
215 }
216
217 return tableConfiguration.isSelectByExampleStatementEnabled()
218 || tableConfiguration.isSelectByPrimaryKeyStatementEnabled();
219 }
220
221
222
223
224
225
226
227
228 @Override
229 public boolean generateResultMapWithBLOBs() {
230 boolean rc;
231
232 if (introspectedTable.hasBLOBColumns()) {
233 if (isModelOnly) {
234 rc = true;
235 } else {
236 rc = tableConfiguration.isSelectByExampleStatementEnabled()
237 || tableConfiguration.isSelectByPrimaryKeyStatementEnabled();
238 }
239 } else {
240 rc = false;
241 }
242
243 return rc;
244 }
245
246
247
248
249
250
251
252
253
254 @Override
255 public boolean generateSQLExampleWhereClause() {
256 if (isModelOnly) {
257 return false;
258 }
259
260 return tableConfiguration.isSelectByExampleStatementEnabled()
261 || tableConfiguration.isDeleteByExampleStatementEnabled()
262 || tableConfiguration.isCountByExampleStatementEnabled();
263 }
264
265
266
267
268
269
270
271
272
273
274 @Override
275 public boolean generateMyBatis3UpdateByExampleWhereClause() {
276 if (isModelOnly) {
277 return false;
278 }
279
280 return introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3
281 && tableConfiguration.isUpdateByExampleStatementEnabled();
282 }
283
284
285
286
287
288
289
290
291
292 @Override
293 public boolean generateSelectByPrimaryKey() {
294 if (isModelOnly) {
295 return false;
296 }
297
298 return tableConfiguration.isSelectByPrimaryKeyStatementEnabled()
299 && introspectedTable.hasPrimaryKeyColumns()
300 && (introspectedTable.hasBaseColumns() || introspectedTable
301 .hasBLOBColumns());
302 }
303
304
305
306
307
308
309
310
311 @Override
312 public boolean generateSelectByExampleWithoutBLOBs() {
313 if (isModelOnly) {
314 return false;
315 }
316
317 return tableConfiguration.isSelectByExampleStatementEnabled();
318 }
319
320
321
322
323
324
325
326
327
328 @Override
329 public boolean generateSelectByExampleWithBLOBs() {
330 if (isModelOnly) {
331 return false;
332 }
333
334 return tableConfiguration.isSelectByExampleStatementEnabled()
335 && introspectedTable.hasBLOBColumns();
336 }
337
338
339
340
341
342
343
344
345 @Override
346 public boolean generateExampleClass() {
347 if (introspectedTable.getContext().getSqlMapGeneratorConfiguration() == null
348 && introspectedTable.getContext().getJavaClientGeneratorConfiguration() == null) {
349
350 return false;
351 }
352
353 if (isModelOnly) {
354 return false;
355 }
356
357 return tableConfiguration.isSelectByExampleStatementEnabled()
358 || tableConfiguration.isDeleteByExampleStatementEnabled()
359 || tableConfiguration.isCountByExampleStatementEnabled()
360 || tableConfiguration.isUpdateByExampleStatementEnabled();
361 }
362
363 @Override
364 public boolean generateCountByExample() {
365 if (isModelOnly) {
366 return false;
367 }
368
369 return tableConfiguration.isCountByExampleStatementEnabled();
370 }
371
372 @Override
373 public boolean generateUpdateByExampleSelective() {
374 if (isModelOnly) {
375 return false;
376 }
377
378 return tableConfiguration.isUpdateByExampleStatementEnabled();
379 }
380
381 @Override
382 public boolean generateUpdateByExampleWithoutBLOBs() {
383 if (isModelOnly) {
384 return false;
385 }
386
387 return tableConfiguration.isUpdateByExampleStatementEnabled()
388 && (introspectedTable.hasPrimaryKeyColumns() || introspectedTable
389 .hasBaseColumns());
390 }
391
392 @Override
393 public boolean generateUpdateByExampleWithBLOBs() {
394 if (isModelOnly) {
395 return false;
396 }
397
398 return tableConfiguration.isUpdateByExampleStatementEnabled()
399 && introspectedTable.hasBLOBColumns();
400 }
401
402 @Override
403 public IntrospectedTable getIntrospectedTable() {
404 return introspectedTable;
405 }
406
407 @Override
408 public boolean generateBaseColumnList() {
409 if (isModelOnly) {
410 return false;
411 }
412
413 return generateSelectByPrimaryKey()
414 || generateSelectByExampleWithoutBLOBs();
415 }
416
417 @Override
418 public boolean generateBlobColumnList() {
419 if (isModelOnly) {
420 return false;
421 }
422
423 return introspectedTable.hasBLOBColumns()
424 && (tableConfiguration.isSelectByExampleStatementEnabled() || tableConfiguration
425 .isSelectByPrimaryKeyStatementEnabled());
426 }
427
428 @Override
429 public boolean generateJavaClient() {
430 return !isModelOnly;
431 }
432 }