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.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      /**
30       * Instantiates a new conditional model rules.
31       *
32       * @param introspectedTable
33       *            the introspected table
34       */
35      public ConditionalModelRules(IntrospectedTable introspectedTable) {
36          super(introspectedTable);
37      }
38  
39      /**
40       * We generate a primary key if there is more than one primary key field.
41       *
42       * @return true if the primary key should be generated
43       */
44      @Override
45      public boolean generatePrimaryKeyClass() {
46          return introspectedTable.getPrimaryKeyColumns().size() > 1;
47      }
48  
49      /**
50       * Generate a base record if there are any base columns, or if there is only
51       * one primary key coulmn (in which case we will not generate a primary key
52       * class), or if there is only one BLOB column (in which case we will not
53       * generate a record with BLOBs class).
54       *
55       * @return true if the class should be generated
56       */
57      @Override
58      public boolean generateBaseRecordClass() {
59          return introspectedTable.hasBaseColumns()
60                  || introspectedTable.getPrimaryKeyColumns().size() == 1
61                  || blobsAreInBaseRecord();
62      }
63  
64      /**
65       * Blobs will be in the base record class if there is only one blob column.
66       *
67       * @return true if there are blobs but they are in the base record class
68       */
69      private boolean blobsAreInBaseRecord() {
70          return introspectedTable.hasBLOBColumns() && !generateRecordWithBLOBsClass();
71      }
72  
73      /**
74       * We generate a record with BLOBs class if there is more than one BLOB
75       * column. Do not generate a BLOBs class if any other super class would only
76       * contain one field
77       *
78       * @return true if the record with BLOBs class should be generated
79       */
80      @Override
81      public boolean generateRecordWithBLOBsClass() {
82          int otherColumnCount = introspectedTable.getPrimaryKeyColumns().size()
83                  + introspectedTable.getBaseColumns().size();
84  
85          return otherColumnCount > 1
86                  && introspectedTable.getBLOBColumns().size() > 1;
87      }
88  }