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