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.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.config.PropertyRegistry;
22 import org.mybatis.generator.config.TableConfiguration;
23 import org.mybatis.generator.internal.util.StringUtility;
24
25 /**
26 * This class centralizes all the rules related to code generation - including
27 * the methods and objects to create, and certain attributes related to those
28 * objects.
29 *
30 * @author Jeff Butler
31 */
32 public abstract class BaseRules implements Rules {
33
34 /** The table configuration. */
35 protected TableConfiguration tableConfiguration;
36
37 /** The introspected table. */
38 protected IntrospectedTable introspectedTable;
39
40 /** The is model only. */
41 protected final boolean isModelOnly;
42
43 /**
44 * Instantiates a new base rules.
45 *
46 * @param introspectedTable
47 * the introspected table
48 */
49 public BaseRules(IntrospectedTable introspectedTable) {
50 super();
51 this.introspectedTable = introspectedTable;
52 this.tableConfiguration = introspectedTable.getTableConfiguration();
53 String modelOnly = tableConfiguration.getProperty(PropertyRegistry.TABLE_MODEL_ONLY);
54 isModelOnly = StringUtility.isTrue(modelOnly);
55 }
56
57 /**
58 * Implements the rule for generating the insert SQL Map element and DAO
59 * method. If the insert statement is allowed, then generate the element and
60 * method.
61 *
62 * @return true if the element and method should be generated
63 */
64 public boolean generateInsert() {
65 if (isModelOnly) {
66 return false;
67 }
68
69 return tableConfiguration.isInsertStatementEnabled();
70 }
71
72 /**
73 * Implements the rule for generating the insert selective SQL Map element
74 * and DAO method. If the insert statement is allowed, then generate the
75 * element and method.
76 *
77 * @return true if the element and method should be generated
78 */
79 public boolean generateInsertSelective() {
80 if (isModelOnly) {
81 return false;
82 }
83
84 return tableConfiguration.isInsertStatementEnabled();
85 }
86
87 /**
88 * Calculates the class that contains all fields. This class is used as the
89 * insert statement parameter, as well as the returned value from the select
90 * by primary key method. The actual class depends on how the domain model
91 * is generated.
92 *
93 * @return the type of the class that holds all fields
94 */
95 public FullyQualifiedJavaType calculateAllFieldsClass() {
96
97 String answer;
98
99 if (generateRecordWithBLOBsClass()) {
100 answer = introspectedTable.getRecordWithBLOBsType();
101 } else if (generateBaseRecordClass()) {
102 answer = introspectedTable.getBaseRecordType();
103 } else {
104 answer = introspectedTable.getPrimaryKeyType();
105 }
106
107 return new FullyQualifiedJavaType(answer);
108 }
109
110 /**
111 * Implements the rule for generating the update by primary key without
112 * BLOBs SQL Map element and DAO method. If the table has a primary key as
113 * well as other non-BLOB fields, and the updateByPrimaryKey statement is
114 * allowed, then generate the element and method.
115 *
116 * @return true if the element and method should be generated
117 */
118 public boolean generateUpdateByPrimaryKeyWithoutBLOBs() {
119 if (isModelOnly) {
120 return false;
121 }
122
123 boolean rc = tableConfiguration.isUpdateByPrimaryKeyStatementEnabled()
124 && introspectedTable.hasPrimaryKeyColumns()
125 && introspectedTable.hasBaseColumns();
126
127 return rc;
128 }
129
130 /**
131 * Implements the rule for generating the update by primary key with BLOBs
132 * SQL Map element and DAO method. If the table has a primary key as well as
133 * other BLOB fields, and the updateByPrimaryKey statement is allowed, then
134 * generate the element and method.
135 *
136 * @return true if the element and method should be generated
137 */
138 public boolean generateUpdateByPrimaryKeyWithBLOBs() {
139 if (isModelOnly) {
140 return false;
141 }
142
143 boolean rc = tableConfiguration.isUpdateByPrimaryKeyStatementEnabled()
144 && introspectedTable.hasPrimaryKeyColumns()
145 && introspectedTable.hasBLOBColumns();
146
147 return rc;
148 }
149
150 /**
151 * Implements the rule for generating the update by primary key selective
152 * SQL Map element and DAO method. If the table has a primary key as well as
153 * other fields, and the updateByPrimaryKey statement is allowed, then
154 * generate the element and method.
155 *
156 * @return true if the element and method should be generated
157 */
158 public boolean generateUpdateByPrimaryKeySelective() {
159 if (isModelOnly) {
160 return false;
161 }
162
163 boolean rc = tableConfiguration.isUpdateByPrimaryKeyStatementEnabled()
164 && introspectedTable.hasPrimaryKeyColumns()
165 && (introspectedTable.hasBLOBColumns() || introspectedTable
166 .hasBaseColumns());
167
168 return rc;
169 }
170
171 /**
172 * Implements the rule for generating the delete by primary key SQL Map
173 * element and DAO method. If the table has a primary key, and the
174 * deleteByPrimaryKey statement is allowed, then generate the element and
175 * method.
176 *
177 * @return true if the element and method should be generated
178 */
179 public boolean generateDeleteByPrimaryKey() {
180 if (isModelOnly) {
181 return false;
182 }
183
184 boolean rc = tableConfiguration.isDeleteByPrimaryKeyStatementEnabled()
185 && introspectedTable.hasPrimaryKeyColumns();
186
187 return rc;
188 }
189
190 /**
191 * Implements the rule for generating the delete by example SQL Map element
192 * and DAO method. If the deleteByExample statement is allowed, then
193 * generate the element and method.
194 *
195 * @return true if the element and method should be generated
196 */
197 public boolean generateDeleteByExample() {
198 if (isModelOnly) {
199 return false;
200 }
201
202 boolean rc = tableConfiguration.isDeleteByExampleStatementEnabled();
203
204 return rc;
205 }
206
207 /**
208 * Implements the rule for generating the result map without BLOBs. If
209 * either select method is allowed, then generate the result map.
210 *
211 * @return true if the result map should be generated
212 */
213 public boolean generateBaseResultMap() {
214 if (isModelOnly) {
215 return true;
216 }
217
218 boolean rc = tableConfiguration.isSelectByExampleStatementEnabled()
219 || tableConfiguration.isSelectByPrimaryKeyStatementEnabled();
220
221 return rc;
222 }
223
224 /**
225 * Implements the rule for generating the result map with BLOBs. If the
226 * table has BLOB columns, and either select method is allowed, then
227 * generate the result map.
228 *
229 * @return true if the result map should be generated
230 */
231 public boolean generateResultMapWithBLOBs() {
232 boolean rc;
233
234 if (introspectedTable.hasBLOBColumns()) {
235 if (isModelOnly) {
236 rc = true;
237 } else {
238 rc = tableConfiguration.isSelectByExampleStatementEnabled()
239 || tableConfiguration.isSelectByPrimaryKeyStatementEnabled();
240 }
241 } else {
242 rc = false;
243 }
244
245 return rc;
246 }
247
248 /**
249 * Implements the rule for generating the SQL example where clause element.
250 *
251 * In iBATIS2, generate the element if the selectByExample, deleteByExample,
252 * updateByExample, or countByExample statements are allowed.
253 *
254 * In MyBatis3, generate the element if the selectByExample,
255 * deleteByExample, or countByExample statements are allowed.
256 *
257 * @return true if the SQL where clause element should be generated
258 */
259 public boolean generateSQLExampleWhereClause() {
260 if (isModelOnly) {
261 return false;
262 }
263
264 boolean rc = tableConfiguration.isSelectByExampleStatementEnabled()
265 || tableConfiguration.isDeleteByExampleStatementEnabled()
266 || tableConfiguration.isCountByExampleStatementEnabled();
267
268 if (introspectedTable.getTargetRuntime() == TargetRuntime.IBATIS2) {
269 rc |= tableConfiguration.isUpdateByExampleStatementEnabled();
270 }
271
272 return rc;
273 }
274
275 /**
276 * Implements the rule for generating the SQL example where clause element
277 * specifically for use in the update by example methods.
278 *
279 * In iBATIS2, do not generate the element.
280 *
281 * In MyBatis3, generate the element if the updateByExample statements are
282 * allowed.
283 *
284 * @return true if the SQL where clause element should be generated
285 */
286 public boolean generateMyBatis3UpdateByExampleWhereClause() {
287 if (isModelOnly) {
288 return false;
289 }
290
291 return introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3
292 && tableConfiguration.isUpdateByExampleStatementEnabled();
293 }
294
295 /**
296 * Implements the rule for generating the select by primary key SQL Map
297 * element and DAO method. If the table has a primary key as well as other
298 * fields, and the selectByPrimaryKey statement is allowed, then generate
299 * the element and method.
300 *
301 * @return true if the element and method should be generated
302 */
303 public boolean generateSelectByPrimaryKey() {
304 if (isModelOnly) {
305 return false;
306 }
307
308 boolean rc = tableConfiguration.isSelectByPrimaryKeyStatementEnabled()
309 && introspectedTable.hasPrimaryKeyColumns()
310 && (introspectedTable.hasBaseColumns() || introspectedTable
311 .hasBLOBColumns());
312
313 return rc;
314 }
315
316 /**
317 * Implements the rule for generating the select by example without BLOBs
318 * SQL Map element and DAO method. If the selectByExample statement is
319 * allowed, then generate the element and method.
320 *
321 * @return true if the element and method should be generated
322 */
323 public boolean generateSelectByExampleWithoutBLOBs() {
324 if (isModelOnly) {
325 return false;
326 }
327
328 return tableConfiguration.isSelectByExampleStatementEnabled();
329 }
330
331 /**
332 * Implements the rule for generating the select by example with BLOBs SQL
333 * Map element and DAO method. If the table has BLOB fields and the
334 * selectByExample statement is allowed, then generate the element and
335 * method.
336 *
337 * @return true if the element and method should be generated
338 */
339 public boolean generateSelectByExampleWithBLOBs() {
340 if (isModelOnly) {
341 return false;
342 }
343
344 boolean rc = tableConfiguration.isSelectByExampleStatementEnabled()
345 && introspectedTable.hasBLOBColumns();
346
347 return rc;
348 }
349
350 /**
351 * Implements the rule for generating an example class. The class should be
352 * generated if the selectByExample or deleteByExample or countByExample
353 * methods are allowed.
354 *
355 * @return true if the example class should be generated
356 */
357 public boolean generateExampleClass() {
358 if (introspectedTable.getContext().getSqlMapGeneratorConfiguration() == null
359 && introspectedTable.getContext().getJavaClientGeneratorConfiguration() == null) {
360 // this is a model only context - don't generate the example class
361 return false;
362 }
363
364 if (isModelOnly) {
365 return false;
366 }
367
368 boolean rc = tableConfiguration.isSelectByExampleStatementEnabled()
369 || tableConfiguration.isDeleteByExampleStatementEnabled()
370 || tableConfiguration.isCountByExampleStatementEnabled()
371 || tableConfiguration.isUpdateByExampleStatementEnabled();
372
373 return rc;
374 }
375
376 /* (non-Javadoc)
377 * @see org.mybatis.generator.internal.rules.Rules#generateCountByExample()
378 */
379 public boolean generateCountByExample() {
380 if (isModelOnly) {
381 return false;
382 }
383
384 boolean rc = tableConfiguration.isCountByExampleStatementEnabled();
385
386 return rc;
387 }
388
389 /* (non-Javadoc)
390 * @see org.mybatis.generator.internal.rules.Rules#generateUpdateByExampleSelective()
391 */
392 public boolean generateUpdateByExampleSelective() {
393 if (isModelOnly) {
394 return false;
395 }
396
397 boolean rc = tableConfiguration.isUpdateByExampleStatementEnabled();
398
399 return rc;
400 }
401
402 /* (non-Javadoc)
403 * @see org.mybatis.generator.internal.rules.Rules#generateUpdateByExampleWithoutBLOBs()
404 */
405 public boolean generateUpdateByExampleWithoutBLOBs() {
406 if (isModelOnly) {
407 return false;
408 }
409
410 boolean rc = tableConfiguration.isUpdateByExampleStatementEnabled()
411 && (introspectedTable.hasPrimaryKeyColumns() || introspectedTable
412 .hasBaseColumns());
413
414 return rc;
415 }
416
417 /* (non-Javadoc)
418 * @see org.mybatis.generator.internal.rules.Rules#generateUpdateByExampleWithBLOBs()
419 */
420 public boolean generateUpdateByExampleWithBLOBs() {
421 if (isModelOnly) {
422 return false;
423 }
424
425 boolean rc = tableConfiguration.isUpdateByExampleStatementEnabled()
426 && introspectedTable.hasBLOBColumns();
427
428 return rc;
429 }
430
431 /* (non-Javadoc)
432 * @see org.mybatis.generator.internal.rules.Rules#getIntrospectedTable()
433 */
434 public IntrospectedTable getIntrospectedTable() {
435 return introspectedTable;
436 }
437
438 /* (non-Javadoc)
439 * @see org.mybatis.generator.internal.rules.Rules#generateBaseColumnList()
440 */
441 public boolean generateBaseColumnList() {
442 if (isModelOnly) {
443 return false;
444 }
445
446 return generateSelectByPrimaryKey()
447 || generateSelectByExampleWithoutBLOBs();
448 }
449
450 /* (non-Javadoc)
451 * @see org.mybatis.generator.internal.rules.Rules#generateBlobColumnList()
452 */
453 public boolean generateBlobColumnList() {
454 if (isModelOnly) {
455 return false;
456 }
457
458 return introspectedTable.hasBLOBColumns()
459 && (tableConfiguration.isSelectByExampleStatementEnabled() || tableConfiguration
460 .isSelectByPrimaryKeyStatementEnabled());
461 }
462
463 /* (non-Javadoc)
464 * @see org.mybatis.generator.internal.rules.Rules#generateJavaClient()
465 */
466 public boolean generateJavaClient() {
467 return !isModelOnly;
468 }
469 }