1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.config;
17
18 import static org.mybatis.generator.internal.util.EqualsUtil.areEqual;
19 import static org.mybatis.generator.internal.util.HashCodeUtil.hash;
20 import static org.mybatis.generator.internal.util.HashCodeUtil.SEED;
21 import static org.mybatis.generator.internal.util.messages.Messages.getString;
22 import static org.mybatis.generator.internal.util.StringUtility.composeFullyQualifiedTableName;
23 import static org.mybatis.generator.internal.util.StringUtility.isTrue;
24 import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.mybatis.generator.api.dom.xml.Attribute;
32 import org.mybatis.generator.api.dom.xml.XmlElement;
33
34
35
36
37
38
39 public class TableConfiguration extends PropertyHolder {
40
41
42 private boolean insertStatementEnabled;
43
44
45 private boolean selectByPrimaryKeyStatementEnabled;
46
47
48 private boolean selectByExampleStatementEnabled;
49
50
51 private boolean updateByPrimaryKeyStatementEnabled;
52
53
54 private boolean deleteByPrimaryKeyStatementEnabled;
55
56
57 private boolean deleteByExampleStatementEnabled;
58
59
60 private boolean countByExampleStatementEnabled;
61
62
63 private boolean updateByExampleStatementEnabled;
64
65
66 private List<ColumnOverride> columnOverrides;
67
68
69 private Map<IgnoredColumn, Boolean> ignoredColumns;
70
71
72 private GeneratedKey generatedKey;
73
74
75 private String selectByPrimaryKeyQueryId;
76
77
78 private String selectByExampleQueryId;
79
80
81 private String catalog;
82
83
84 private String schema;
85
86
87 private String tableName;
88
89
90 private String domainObjectName;
91
92
93 private String alias;
94
95
96 private ModelType modelType;
97
98
99 private boolean wildcardEscapingEnabled;
100
101
102 private String configuredModelType;
103
104
105 private boolean delimitIdentifiers;
106
107
108 private ColumnRenamingRule columnRenamingRule;
109
110
111 private boolean isAllColumnDelimitingEnabled;
112
113
114
115
116
117
118
119 public TableConfiguration(Context context) {
120 super();
121
122 this.modelType = context.getDefaultModelType();
123
124 columnOverrides = new ArrayList<ColumnOverride>();
125 ignoredColumns = new HashMap<IgnoredColumn, Boolean>();
126
127 insertStatementEnabled = true;
128 selectByPrimaryKeyStatementEnabled = true;
129 selectByExampleStatementEnabled = true;
130 updateByPrimaryKeyStatementEnabled = true;
131 deleteByPrimaryKeyStatementEnabled = true;
132 deleteByExampleStatementEnabled = true;
133 countByExampleStatementEnabled = true;
134 updateByExampleStatementEnabled = true;
135 }
136
137
138
139
140
141
142 public boolean isDeleteByPrimaryKeyStatementEnabled() {
143 return deleteByPrimaryKeyStatementEnabled;
144 }
145
146
147
148
149
150
151
152 public void setDeleteByPrimaryKeyStatementEnabled(
153 boolean deleteByPrimaryKeyStatementEnabled) {
154 this.deleteByPrimaryKeyStatementEnabled = deleteByPrimaryKeyStatementEnabled;
155 }
156
157
158
159
160
161
162 public boolean isInsertStatementEnabled() {
163 return insertStatementEnabled;
164 }
165
166
167
168
169
170
171
172 public void setInsertStatementEnabled(boolean insertStatementEnabled) {
173 this.insertStatementEnabled = insertStatementEnabled;
174 }
175
176
177
178
179
180
181 public boolean isSelectByPrimaryKeyStatementEnabled() {
182 return selectByPrimaryKeyStatementEnabled;
183 }
184
185
186
187
188
189
190
191 public void setSelectByPrimaryKeyStatementEnabled(
192 boolean selectByPrimaryKeyStatementEnabled) {
193 this.selectByPrimaryKeyStatementEnabled = selectByPrimaryKeyStatementEnabled;
194 }
195
196
197
198
199
200
201 public boolean isUpdateByPrimaryKeyStatementEnabled() {
202 return updateByPrimaryKeyStatementEnabled;
203 }
204
205
206
207
208
209
210
211 public void setUpdateByPrimaryKeyStatementEnabled(
212 boolean updateByPrimaryKeyStatementEnabled) {
213 this.updateByPrimaryKeyStatementEnabled = updateByPrimaryKeyStatementEnabled;
214 }
215
216
217
218
219
220
221
222
223 public boolean isColumnIgnored(String columnName) {
224 for (Map.Entry<IgnoredColumn, Boolean> entry : ignoredColumns
225 .entrySet()) {
226 IgnoredColumn ic = entry.getKey();
227 if (ic.isColumnNameDelimited()) {
228 if (columnName.equals(ic.getColumnName())) {
229 entry.setValue(Boolean.TRUE);
230 return true;
231 }
232 } else {
233 if (columnName.equalsIgnoreCase(ic.getColumnName())) {
234 entry.setValue(Boolean.TRUE);
235 return true;
236 }
237 }
238 }
239
240 return false;
241 }
242
243
244
245
246
247
248
249 public void addIgnoredColumn(IgnoredColumn ignoredColumn) {
250 ignoredColumns.put(ignoredColumn, Boolean.FALSE);
251 }
252
253
254
255
256
257
258
259 public void addColumnOverride(ColumnOverride columnOverride) {
260 columnOverrides.add(columnOverride);
261 }
262
263
264
265
266 @Override
267 public boolean equals(Object obj) {
268 if (this == obj) {
269 return true;
270 }
271
272 if (!(obj instanceof TableConfiguration)) {
273 return false;
274 }
275
276 TableConfiguration other = (TableConfiguration) obj;
277
278 return areEqual(this.catalog, other.catalog)
279 && areEqual(this.schema, other.schema)
280 && areEqual(this.tableName, other.tableName);
281 }
282
283
284
285
286 @Override
287 public int hashCode() {
288 int result = SEED;
289 result = hash(result, catalog);
290 result = hash(result, schema);
291 result = hash(result, tableName);
292
293 return result;
294 }
295
296
297
298
299
300
301 public boolean isSelectByExampleStatementEnabled() {
302 return selectByExampleStatementEnabled;
303 }
304
305
306
307
308
309
310
311 public void setSelectByExampleStatementEnabled(
312 boolean selectByExampleStatementEnabled) {
313 this.selectByExampleStatementEnabled = selectByExampleStatementEnabled;
314 }
315
316
317
318
319
320
321
322
323 public ColumnOverride getColumnOverride(String columnName) {
324 for (ColumnOverride co : columnOverrides) {
325 if (co.isColumnNameDelimited()) {
326 if (columnName.equals(co.getColumnName())) {
327 return co;
328 }
329 } else {
330 if (columnName.equalsIgnoreCase(co.getColumnName())) {
331 return co;
332 }
333 }
334 }
335
336 return null;
337 }
338
339
340
341
342
343
344 public GeneratedKey getGeneratedKey() {
345 return generatedKey;
346 }
347
348
349
350
351
352
353 public String getSelectByExampleQueryId() {
354 return selectByExampleQueryId;
355 }
356
357
358
359
360
361
362
363 public void setSelectByExampleQueryId(String selectByExampleQueryId) {
364 this.selectByExampleQueryId = selectByExampleQueryId;
365 }
366
367
368
369
370
371
372 public String getSelectByPrimaryKeyQueryId() {
373 return selectByPrimaryKeyQueryId;
374 }
375
376
377
378
379
380
381
382 public void setSelectByPrimaryKeyQueryId(String selectByPrimaryKeyQueryId) {
383 this.selectByPrimaryKeyQueryId = selectByPrimaryKeyQueryId;
384 }
385
386
387
388
389
390
391 public boolean isDeleteByExampleStatementEnabled() {
392 return deleteByExampleStatementEnabled;
393 }
394
395
396
397
398
399
400
401 public void setDeleteByExampleStatementEnabled(
402 boolean deleteByExampleStatementEnabled) {
403 this.deleteByExampleStatementEnabled = deleteByExampleStatementEnabled;
404 }
405
406
407
408
409
410
411 public boolean areAnyStatementsEnabled() {
412 return selectByExampleStatementEnabled
413 || selectByPrimaryKeyStatementEnabled || insertStatementEnabled
414 || updateByPrimaryKeyStatementEnabled
415 || deleteByExampleStatementEnabled
416 || deleteByPrimaryKeyStatementEnabled
417 || countByExampleStatementEnabled
418 || updateByExampleStatementEnabled;
419 }
420
421
422
423
424
425
426
427 public void setGeneratedKey(GeneratedKey generatedKey) {
428 this.generatedKey = generatedKey;
429 }
430
431
432
433
434
435
436 public String getAlias() {
437 return alias;
438 }
439
440
441
442
443
444
445
446 public void setAlias(String alias) {
447 this.alias = alias;
448 }
449
450
451
452
453
454
455 public String getCatalog() {
456 return catalog;
457 }
458
459
460
461
462
463
464
465 public void setCatalog(String catalog) {
466 this.catalog = catalog;
467 }
468
469
470
471
472
473
474 public String getDomainObjectName() {
475 return domainObjectName;
476 }
477
478
479
480
481
482
483
484 public void setDomainObjectName(String domainObjectName) {
485 this.domainObjectName = domainObjectName;
486 }
487
488
489
490
491
492
493 public String getSchema() {
494 return schema;
495 }
496
497
498
499
500
501
502
503 public void setSchema(String schema) {
504 this.schema = schema;
505 }
506
507
508
509
510
511
512 public String getTableName() {
513 return tableName;
514 }
515
516
517
518
519
520
521
522 public void setTableName(String tableName) {
523 this.tableName = tableName;
524 }
525
526
527
528
529
530
531 public List<ColumnOverride> getColumnOverrides() {
532 return columnOverrides;
533 }
534
535
536
537
538
539
540
541
542
543 public List<String> getIgnoredColumnsInError() {
544 List<String> answer = new ArrayList<String>();
545
546 for (Map.Entry<IgnoredColumn, Boolean> entry : ignoredColumns
547 .entrySet()) {
548 if (Boolean.FALSE.equals(entry.getValue())) {
549 answer.add(entry.getKey().getColumnName());
550 }
551 }
552
553 return answer;
554 }
555
556
557
558
559
560
561 public ModelType getModelType() {
562 return modelType;
563 }
564
565
566
567
568
569
570
571 public void setConfiguredModelType(String configuredModelType) {
572 this.configuredModelType = configuredModelType;
573 this.modelType = ModelType.getModelType(configuredModelType);
574 }
575
576
577
578
579
580
581 public boolean isWildcardEscapingEnabled() {
582 return wildcardEscapingEnabled;
583 }
584
585
586
587
588
589
590
591 public void setWildcardEscapingEnabled(boolean wildcardEscapingEnabled) {
592 this.wildcardEscapingEnabled = wildcardEscapingEnabled;
593 }
594
595
596
597
598
599
600 public XmlElement toXmlElement() {
601 XmlElement xmlElement = new XmlElement("table");
602 xmlElement.addAttribute(new Attribute("tableName", tableName));
603
604 if (stringHasValue(catalog)) {
605 xmlElement.addAttribute(new Attribute("catalog", catalog));
606 }
607
608 if (stringHasValue(schema)) {
609 xmlElement.addAttribute(new Attribute("schema", schema));
610 }
611
612 if (stringHasValue(alias)) {
613 xmlElement.addAttribute(new Attribute("alias", alias));
614 }
615
616 if (stringHasValue(domainObjectName)) {
617 xmlElement.addAttribute(new Attribute(
618 "domainObjectName", domainObjectName));
619 }
620
621 if (!insertStatementEnabled) {
622 xmlElement.addAttribute(new Attribute("enableInsert", "false"));
623 }
624
625 if (!selectByPrimaryKeyStatementEnabled) {
626 xmlElement.addAttribute(new Attribute(
627 "enableSelectByPrimaryKey", "false"));
628 }
629
630 if (!selectByExampleStatementEnabled) {
631 xmlElement.addAttribute(new Attribute(
632 "enableSelectByExample", "false"));
633 }
634
635 if (!updateByPrimaryKeyStatementEnabled) {
636 xmlElement.addAttribute(new Attribute(
637 "enableUpdateByPrimaryKey", "false"));
638 }
639
640 if (!deleteByPrimaryKeyStatementEnabled) {
641 xmlElement.addAttribute(new Attribute(
642 "enableDeleteByPrimaryKey", "false"));
643 }
644
645 if (!deleteByExampleStatementEnabled) {
646 xmlElement.addAttribute(new Attribute(
647 "enableDeleteByExample", "false"));
648 }
649
650 if (!countByExampleStatementEnabled) {
651 xmlElement.addAttribute(new Attribute(
652 "enableCountByExample", "false"));
653 }
654
655 if (!updateByExampleStatementEnabled) {
656 xmlElement.addAttribute(new Attribute(
657 "enableUpdateByExample", "false"));
658 }
659
660 if (stringHasValue(selectByPrimaryKeyQueryId)) {
661 xmlElement.addAttribute(new Attribute(
662 "selectByPrimaryKeyQueryId", selectByPrimaryKeyQueryId));
663 }
664
665 if (stringHasValue(selectByExampleQueryId)) {
666 xmlElement.addAttribute(new Attribute(
667 "selectByExampleQueryId", selectByExampleQueryId));
668 }
669
670 if (configuredModelType != null) {
671 xmlElement.addAttribute(new Attribute(
672 "modelType", configuredModelType));
673 }
674
675 if (wildcardEscapingEnabled) {
676 xmlElement.addAttribute(new Attribute("escapeWildcards", "true"));
677 }
678
679 if (isAllColumnDelimitingEnabled) {
680 xmlElement.addAttribute(new Attribute("delimitAllColumns", "true"));
681 }
682
683 if (delimitIdentifiers) {
684 xmlElement
685 .addAttribute(new Attribute("delimitIdentifiers", "true"));
686 }
687
688 addPropertyXmlElements(xmlElement);
689
690 if (generatedKey != null) {
691 xmlElement.addElement(generatedKey.toXmlElement());
692 }
693
694 if (columnRenamingRule != null) {
695 xmlElement.addElement(columnRenamingRule.toXmlElement());
696 }
697
698 if (ignoredColumns.size() > 0) {
699 for (IgnoredColumn ignoredColumn : ignoredColumns.keySet()) {
700 xmlElement.addElement(ignoredColumn.toXmlElement());
701 }
702 }
703
704 if (columnOverrides.size() > 0) {
705 for (ColumnOverride columnOverride : columnOverrides) {
706 xmlElement.addElement(columnOverride.toXmlElement());
707 }
708 }
709
710 return xmlElement;
711 }
712
713
714
715
716 @Override
717 public String toString() {
718 return composeFullyQualifiedTableName(catalog, schema,
719 tableName, '.');
720 }
721
722
723
724
725
726
727 public boolean isDelimitIdentifiers() {
728 return delimitIdentifiers;
729 }
730
731
732
733
734
735
736
737 public void setDelimitIdentifiers(boolean delimitIdentifiers) {
738 this.delimitIdentifiers = delimitIdentifiers;
739 }
740
741
742
743
744
745
746 public boolean isCountByExampleStatementEnabled() {
747 return countByExampleStatementEnabled;
748 }
749
750
751
752
753
754
755
756 public void setCountByExampleStatementEnabled(
757 boolean countByExampleStatementEnabled) {
758 this.countByExampleStatementEnabled = countByExampleStatementEnabled;
759 }
760
761
762
763
764
765
766 public boolean isUpdateByExampleStatementEnabled() {
767 return updateByExampleStatementEnabled;
768 }
769
770
771
772
773
774
775
776 public void setUpdateByExampleStatementEnabled(
777 boolean updateByExampleStatementEnabled) {
778 this.updateByExampleStatementEnabled = updateByExampleStatementEnabled;
779 }
780
781
782
783
784
785
786
787
788
789 public void validate(List<String> errors, int listPosition) {
790 if (!stringHasValue(tableName)) {
791 errors.add(getString(
792 "ValidationError.6", Integer.toString(listPosition)));
793 }
794
795 String fqTableName = composeFullyQualifiedTableName(
796 catalog, schema, tableName, '.');
797
798 if (generatedKey != null) {
799 generatedKey.validate(errors, fqTableName);
800 }
801
802
803
804 if (isTrue(getProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES))
805 && selectByExampleStatementEnabled
806 && selectByPrimaryKeyStatementEnabled) {
807 boolean queryId1Set = stringHasValue(selectByExampleQueryId);
808 boolean queryId2Set = stringHasValue(selectByPrimaryKeyQueryId);
809
810 if (queryId1Set != queryId2Set) {
811 errors.add(getString("ValidationError.13",
812 fqTableName));
813 }
814 }
815
816 if (columnRenamingRule != null) {
817 columnRenamingRule.validate(errors, fqTableName);
818 }
819
820 for (ColumnOverride columnOverride : columnOverrides) {
821 columnOverride.validate(errors, fqTableName);
822 }
823
824 for (IgnoredColumn ignoredColumn : ignoredColumns.keySet()) {
825 ignoredColumn.validate(errors, fqTableName);
826 }
827 }
828
829
830
831
832
833
834 public ColumnRenamingRule getColumnRenamingRule() {
835 return columnRenamingRule;
836 }
837
838
839
840
841
842
843
844 public void setColumnRenamingRule(ColumnRenamingRule columnRenamingRule) {
845 this.columnRenamingRule = columnRenamingRule;
846 }
847
848
849
850
851
852
853 public boolean isAllColumnDelimitingEnabled() {
854 return isAllColumnDelimitingEnabled;
855 }
856
857
858
859
860
861
862
863 public void setAllColumnDelimitingEnabled(
864 boolean isAllColumnDelimitingEnabled) {
865 this.isAllColumnDelimitingEnabled = isAllColumnDelimitingEnabled;
866 }
867 }