View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.config;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.composeFullyQualifiedTableName;
19  import static org.mybatis.generator.internal.util.StringUtility.isTrue;
20  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.Optional;
28  
29  import org.mybatis.generator.internal.util.messages.Messages;
30  
31  public class TableConfiguration extends PropertyHolder {
32  
33      private boolean insertStatementEnabled;
34  
35      private boolean selectByPrimaryKeyStatementEnabled;
36  
37      private boolean selectByExampleStatementEnabled;
38  
39      private boolean updateByPrimaryKeyStatementEnabled;
40  
41      private boolean deleteByPrimaryKeyStatementEnabled;
42  
43      private boolean deleteByExampleStatementEnabled;
44  
45      private boolean countByExampleStatementEnabled;
46  
47      private boolean updateByExampleStatementEnabled;
48  
49      private final List<ColumnOverride> columnOverrides;
50  
51      private final Map<IgnoredColumn, Boolean> ignoredColumns;
52  
53      private GeneratedKey generatedKey;
54  
55      private String selectByPrimaryKeyQueryId;
56  
57      private String selectByExampleQueryId;
58  
59      private String catalog;
60  
61      private String schema;
62  
63      private String tableName;
64  
65      private String domainObjectName;
66  
67      private String alias;
68  
69      private ModelType modelType;
70  
71      private boolean wildcardEscapingEnabled;
72  
73      private boolean delimitIdentifiers;
74  
75      private DomainObjectRenamingRule domainObjectRenamingRule;
76  
77      private ColumnRenamingRule columnRenamingRule;
78  
79      private boolean isAllColumnDelimitingEnabled;
80  
81      private String mapperName;
82      private String sqlProviderName;
83  
84      private final List<IgnoredColumnPattern> ignoredColumnPatterns = new ArrayList<>();
85  
86      public TableConfiguration(Context context) {
87          super();
88  
89          this.modelType = context.getDefaultModelType();
90  
91          columnOverrides = new ArrayList<>();
92          ignoredColumns = new HashMap<>();
93  
94          insertStatementEnabled = true;
95          selectByPrimaryKeyStatementEnabled = true;
96          selectByExampleStatementEnabled = true;
97          updateByPrimaryKeyStatementEnabled = true;
98          deleteByPrimaryKeyStatementEnabled = true;
99          deleteByExampleStatementEnabled = true;
100         countByExampleStatementEnabled = true;
101         updateByExampleStatementEnabled = true;
102     }
103 
104     public boolean isDeleteByPrimaryKeyStatementEnabled() {
105         return deleteByPrimaryKeyStatementEnabled;
106     }
107 
108     public void setDeleteByPrimaryKeyStatementEnabled(
109             boolean deleteByPrimaryKeyStatementEnabled) {
110         this.deleteByPrimaryKeyStatementEnabled = deleteByPrimaryKeyStatementEnabled;
111     }
112 
113     public boolean isInsertStatementEnabled() {
114         return insertStatementEnabled;
115     }
116 
117     public void setInsertStatementEnabled(boolean insertStatementEnabled) {
118         this.insertStatementEnabled = insertStatementEnabled;
119     }
120 
121     public boolean isSelectByPrimaryKeyStatementEnabled() {
122         return selectByPrimaryKeyStatementEnabled;
123     }
124 
125     public void setSelectByPrimaryKeyStatementEnabled(
126             boolean selectByPrimaryKeyStatementEnabled) {
127         this.selectByPrimaryKeyStatementEnabled = selectByPrimaryKeyStatementEnabled;
128     }
129 
130     public boolean isUpdateByPrimaryKeyStatementEnabled() {
131         return updateByPrimaryKeyStatementEnabled;
132     }
133 
134     public void setUpdateByPrimaryKeyStatementEnabled(
135             boolean updateByPrimaryKeyStatementEnabled) {
136         this.updateByPrimaryKeyStatementEnabled = updateByPrimaryKeyStatementEnabled;
137     }
138 
139     public boolean isColumnIgnored(String columnName) {
140         for (Map.Entry<IgnoredColumn, Boolean> entry : ignoredColumns
141                 .entrySet()) {
142             if (entry.getKey().matches(columnName)) {
143                 entry.setValue(Boolean.TRUE);
144                 return true;
145             }
146         }
147 
148         for (IgnoredColumnPattern ignoredColumnPattern : ignoredColumnPatterns) {
149             if (ignoredColumnPattern.matches(columnName)) {
150                 return true;
151             }
152         }
153 
154         return false;
155     }
156 
157     public void addIgnoredColumn(IgnoredColumn ignoredColumn) {
158         ignoredColumns.put(ignoredColumn, Boolean.FALSE);
159     }
160 
161     public void addIgnoredColumnPattern(IgnoredColumnPattern ignoredColumnPattern) {
162         ignoredColumnPatterns.add(ignoredColumnPattern);
163     }
164 
165     public void addColumnOverride(ColumnOverride columnOverride) {
166         columnOverrides.add(columnOverride);
167     }
168 
169     @Override
170     public boolean equals(Object obj) {
171         if (this == obj) {
172             return true;
173         }
174 
175         if (!(obj instanceof TableConfiguration)) {
176             return false;
177         }
178 
179         TableConfiguration other = (TableConfiguration) obj;
180 
181         return Objects.equals(this.catalog, other.catalog)
182                 && Objects.equals(this.schema, other.schema)
183                 && Objects.equals(this.tableName, other.tableName);
184     }
185 
186     @Override
187     public int hashCode() {
188         return Objects.hash(catalog, schema, tableName);
189     }
190 
191     public boolean isSelectByExampleStatementEnabled() {
192         return selectByExampleStatementEnabled;
193     }
194 
195     public void setSelectByExampleStatementEnabled(
196             boolean selectByExampleStatementEnabled) {
197         this.selectByExampleStatementEnabled = selectByExampleStatementEnabled;
198     }
199 
200     /**
201      * May return null if the column has not been overridden.
202      *
203      * @param columnName
204      *            the column name
205      * @return the column override (if any) related to this column
206      */
207     public ColumnOverride getColumnOverride(String columnName) {
208         for (ColumnOverride co : columnOverrides) {
209             if (co.isColumnNameDelimited()) {
210                 if (columnName.equals(co.getColumnName())) {
211                     return co;
212                 }
213             } else {
214                 if (columnName.equalsIgnoreCase(co.getColumnName())) {
215                     return co;
216                 }
217             }
218         }
219 
220         return null;
221     }
222 
223     public Optional<GeneratedKey> getGeneratedKey() {
224         return Optional.ofNullable(generatedKey);
225     }
226 
227     public String getSelectByExampleQueryId() {
228         return selectByExampleQueryId;
229     }
230 
231     public void setSelectByExampleQueryId(String selectByExampleQueryId) {
232         this.selectByExampleQueryId = selectByExampleQueryId;
233     }
234 
235     public String getSelectByPrimaryKeyQueryId() {
236         return selectByPrimaryKeyQueryId;
237     }
238 
239     public void setSelectByPrimaryKeyQueryId(String selectByPrimaryKeyQueryId) {
240         this.selectByPrimaryKeyQueryId = selectByPrimaryKeyQueryId;
241     }
242 
243     public boolean isDeleteByExampleStatementEnabled() {
244         return deleteByExampleStatementEnabled;
245     }
246 
247     public void setDeleteByExampleStatementEnabled(
248             boolean deleteByExampleStatementEnabled) {
249         this.deleteByExampleStatementEnabled = deleteByExampleStatementEnabled;
250     }
251 
252     public boolean areAnyStatementsEnabled() {
253         return selectByExampleStatementEnabled
254                 || selectByPrimaryKeyStatementEnabled || insertStatementEnabled
255                 || updateByPrimaryKeyStatementEnabled
256                 || deleteByExampleStatementEnabled
257                 || deleteByPrimaryKeyStatementEnabled
258                 || countByExampleStatementEnabled
259                 || updateByExampleStatementEnabled;
260     }
261 
262     public void setGeneratedKey(GeneratedKey generatedKey) {
263         this.generatedKey = generatedKey;
264     }
265 
266     public String getAlias() {
267         return alias;
268     }
269 
270     public void setAlias(String alias) {
271         this.alias = alias;
272     }
273 
274     public String getCatalog() {
275         return catalog;
276     }
277 
278     public void setCatalog(String catalog) {
279         this.catalog = catalog;
280     }
281 
282     public String getDomainObjectName() {
283         return domainObjectName;
284     }
285 
286     public void setDomainObjectName(String domainObjectName) {
287         this.domainObjectName = domainObjectName;
288     }
289 
290     public String getSchema() {
291         return schema;
292     }
293 
294     public void setSchema(String schema) {
295         this.schema = schema;
296     }
297 
298     public String getTableName() {
299         return tableName;
300     }
301 
302     public void setTableName(String tableName) {
303         this.tableName = tableName;
304     }
305 
306     public List<ColumnOverride> getColumnOverrides() {
307         return columnOverrides;
308     }
309 
310     /**
311      * Returns a List of Strings. The values are the columns
312      * that were specified to be ignored in the table, but do not exist in the
313      * table.
314      *
315      * @return a List of Strings - the columns that were improperly configured
316      *         as ignored columns
317      */
318     public List<String> getIgnoredColumnsInError() {
319         List<String> answer = new ArrayList<>();
320 
321         for (Map.Entry<IgnoredColumn, Boolean> entry : ignoredColumns
322                 .entrySet()) {
323             if (Boolean.FALSE.equals(entry.getValue())) {
324                 answer.add(entry.getKey().getColumnName());
325             }
326         }
327 
328         return answer;
329     }
330 
331     public ModelType getModelType() {
332         return modelType;
333     }
334 
335     public void setConfiguredModelType(String configuredModelType) {
336         this.modelType = ModelType.getModelType(configuredModelType);
337     }
338 
339     public boolean isWildcardEscapingEnabled() {
340         return wildcardEscapingEnabled;
341     }
342 
343     public void setWildcardEscapingEnabled(boolean wildcardEscapingEnabled) {
344         this.wildcardEscapingEnabled = wildcardEscapingEnabled;
345     }
346 
347     @Override
348     public String toString() {
349         return composeFullyQualifiedTableName(catalog, schema,
350                 tableName, '.');
351     }
352 
353     public boolean isDelimitIdentifiers() {
354         return delimitIdentifiers;
355     }
356 
357     public void setDelimitIdentifiers(boolean delimitIdentifiers) {
358         this.delimitIdentifiers = delimitIdentifiers;
359     }
360 
361     public boolean isCountByExampleStatementEnabled() {
362         return countByExampleStatementEnabled;
363     }
364 
365     public void setCountByExampleStatementEnabled(
366             boolean countByExampleStatementEnabled) {
367         this.countByExampleStatementEnabled = countByExampleStatementEnabled;
368     }
369 
370     public boolean isUpdateByExampleStatementEnabled() {
371         return updateByExampleStatementEnabled;
372     }
373 
374     public void setUpdateByExampleStatementEnabled(
375             boolean updateByExampleStatementEnabled) {
376         this.updateByExampleStatementEnabled = updateByExampleStatementEnabled;
377     }
378 
379     public void validate(List<String> errors, int listPosition) {
380         if (!stringHasValue(tableName)) {
381             errors.add(Messages.getString(
382                     "ValidationError.6", Integer.toString(listPosition))); //$NON-NLS-1$
383         }
384 
385         String fqTableName = composeFullyQualifiedTableName(
386                 catalog, schema, tableName, '.');
387 
388         if (generatedKey != null) {
389             generatedKey.validate(errors, fqTableName);
390         }
391 
392         // when using column indexes, either both or neither query ids
393         // should be set
394         if (isTrue(getProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES))
395                 && selectByExampleStatementEnabled
396                 && selectByPrimaryKeyStatementEnabled) {
397             boolean queryId1Set = stringHasValue(selectByExampleQueryId);
398             boolean queryId2Set = stringHasValue(selectByPrimaryKeyQueryId);
399 
400             if (queryId1Set != queryId2Set) {
401                 errors.add(Messages.getString("ValidationError.13", //$NON-NLS-1$
402                         fqTableName));
403             }
404         }
405 
406         if (domainObjectRenamingRule != null) {
407             domainObjectRenamingRule.validate(errors, fqTableName);
408         }
409 
410         if (columnRenamingRule != null) {
411             columnRenamingRule.validate(errors, fqTableName);
412         }
413 
414         for (ColumnOverride columnOverride : columnOverrides) {
415             columnOverride.validate(errors, fqTableName);
416         }
417 
418         for (IgnoredColumn ignoredColumn : ignoredColumns.keySet()) {
419             ignoredColumn.validate(errors, fqTableName);
420         }
421 
422         for (IgnoredColumnPattern ignoredColumnPattern : ignoredColumnPatterns) {
423             ignoredColumnPattern.validate(errors, fqTableName);
424         }
425     }
426 
427     public DomainObjectRenamingRule getDomainObjectRenamingRule() {
428         return domainObjectRenamingRule;
429     }
430 
431     public void setDomainObjectRenamingRule(DomainObjectRenamingRule domainObjectRenamingRule) {
432         this.domainObjectRenamingRule = domainObjectRenamingRule;
433     }
434 
435     public ColumnRenamingRule getColumnRenamingRule() {
436         return columnRenamingRule;
437     }
438 
439     public void setColumnRenamingRule(ColumnRenamingRule columnRenamingRule) {
440         this.columnRenamingRule = columnRenamingRule;
441     }
442 
443     public boolean isAllColumnDelimitingEnabled() {
444         return isAllColumnDelimitingEnabled;
445     }
446 
447     public void setAllColumnDelimitingEnabled(
448             boolean isAllColumnDelimitingEnabled) {
449         this.isAllColumnDelimitingEnabled = isAllColumnDelimitingEnabled;
450     }
451 
452     public String getMapperName() {
453         return mapperName;
454     }
455 
456     public void setMapperName(String mapperName) {
457         this.mapperName = mapperName;
458     }
459 
460     public String getSqlProviderName() {
461         return sqlProviderName;
462     }
463 
464     public void setSqlProviderName(String sqlProviderName) {
465         this.sqlProviderName = sqlProviderName;
466     }
467 
468     public String getDynamicSqlSupportClassName() {
469         return getProperty(PropertyRegistry.TABLE_DYNAMIC_SQL_SUPPORT_CLASS_NAME);
470     }
471 
472     public String getDynamicSqlTableObjectName() {
473         return getProperty(PropertyRegistry.TABLE_DYNAMIC_SQL_TABLE_OBJECT_NAME);
474     }
475 }