View Javadoc
1   /*
2    *    Copyright 2006-2025 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.internal.rules;
17  
18  import org.mybatis.generator.api.IntrospectedTable;
19  
20  /**
21   * This class encapsulates all the code generation rules for a table using the
22   * conditional model. In this model we do not generate primary key or record
23   * with BLOBs classes if the class would only hold one field.
24   *
25   * @author Jeff Butler
26   */
27  public class ConditionalModelRules extends BaseRules {
28  
29      public ConditionalModelRules(IntrospectedTable introspectedTable) {
30          super(introspectedTable);
31      }
32  
33      /**
34       * We generate a primary key if there is more than one primary key field.
35       *
36       * @return true if the primary key should be generated
37       */
38      @Override
39      public boolean generatePrimaryKeyClass() {
40          return introspectedTable.getPrimaryKeyColumns().size() > 1;
41      }
42  
43      /**
44       * Generate a base record if there are any base columns, or if there is only
45       * one primary key coulmn (in which case we will not generate a primary key
46       * class), or if there is only one BLOB column (in which case we will not
47       * generate a record with BLOBs class).
48       *
49       * @return true if the class should be generated
50       */
51      @Override
52      public boolean generateBaseRecordClass() {
53          return introspectedTable.hasBaseColumns()
54                  || introspectedTable.getPrimaryKeyColumns().size() == 1
55                  || blobsAreInBaseRecord();
56      }
57  
58      /**
59       * Blobs will be in the base record class if there is only one blob column.
60       *
61       * @return true if there are blobs, but they are in the base record class
62       */
63      private boolean blobsAreInBaseRecord() {
64          return introspectedTable.hasBLOBColumns() && !generateRecordWithBLOBsClass();
65      }
66  
67      /**
68       * We generate a record with BLOBs class if there is more than one BLOB
69       * column. Do not generate a BLOBs class if any other super class would only
70       * contain one field
71       *
72       * @return true if the record with BLOBs class should be generated
73       */
74      @Override
75      public boolean generateRecordWithBLOBsClass() {
76          int otherColumnCount = introspectedTable.getPrimaryKeyColumns().size()
77                  + introspectedTable.getBaseColumns().size();
78  
79          return otherColumnCount > 1
80                  && introspectedTable.getBLOBColumns().size() > 1;
81      }
82  }