View Javadoc
1   /*
2    *    Copyright 2006-2026 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.stringContainsSpace;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  import static org.mybatis.generator.internal.util.messages.Messages.getString;
21  
22  import java.util.List;
23  import java.util.Objects;
24  import java.util.Optional;
25  
26  import org.jspecify.annotations.Nullable;
27  
28  public class ColumnOverride extends PropertyHolder {
29      private final String columnName;
30      private final @Nullable String javaProperty;
31      private final @Nullable String jdbcType;
32      private final @Nullable String javaType;
33      private final @Nullable String typeHandler;
34      private final boolean isColumnNameDelimited;
35  
36      /**
37       * If true, the column is a GENERATED ALWAYS column which means
38       * that it should not be used in insert or update statements.
39       */
40      private final boolean isGeneratedAlways;
41  
42      protected ColumnOverride(Builder builder) {
43          super(builder);
44          columnName = Objects.requireNonNull(builder.columnName);
45          javaProperty = builder.javaProperty;
46          jdbcType = builder.jdbcType;
47          javaType = builder.javaType;
48          typeHandler = builder.typeHandler;
49          isGeneratedAlways = builder.isGeneratedAlways;
50          isColumnNameDelimited = Objects.requireNonNullElseGet(builder.isColumnNameDelimited,
51                  () -> stringContainsSpace(columnName));
52      }
53  
54      public String getColumnName() {
55          return columnName;
56      }
57  
58      public Optional<String> getJavaProperty() {
59          return Optional.ofNullable(javaProperty);
60      }
61  
62      public Optional<String> getJavaType() {
63          return Optional.ofNullable(javaType);
64      }
65  
66      public Optional<String> getJdbcType() {
67          return Optional.ofNullable(jdbcType);
68      }
69  
70      public Optional<String> getTypeHandler() {
71          return Optional.ofNullable(typeHandler);
72      }
73  
74      public boolean isColumnNameDelimited() {
75          return isColumnNameDelimited;
76      }
77  
78      public boolean isGeneratedAlways() {
79          return isGeneratedAlways;
80      }
81  
82      public void validate(List<String> errors, String tableName) {
83          if (!stringHasValue(columnName)) {
84              errors.add(getString("ValidationError.22", tableName)); //$NON-NLS-1$
85          }
86      }
87  
88      public static class Builder extends AbstractBuilder<Builder> {
89          private @Nullable String columnName;
90          private @Nullable String javaProperty;
91          private @Nullable String jdbcType;
92          private @Nullable String javaType;
93          private @Nullable String typeHandler;
94          private @Nullable Boolean isColumnNameDelimited;
95          private boolean isGeneratedAlways;
96  
97          public Builder withColumnName(@Nullable String columnName) {
98              this.columnName = columnName;
99              return this;
100         }
101 
102         public Builder withJavaProperty(@Nullable String javaProperty) {
103             this.javaProperty = javaProperty;
104             return this;
105         }
106 
107         public Builder withJdbcType(@Nullable String jdbcType) {
108             this.jdbcType = jdbcType;
109             return this;
110         }
111 
112         public Builder withJavaType(@Nullable String javaType) {
113             this.javaType = javaType;
114             return this;
115         }
116 
117         public Builder withTypeHandler(@Nullable String typeHandler) {
118             this.typeHandler = typeHandler;
119             return this;
120         }
121 
122         public Builder withColumnNameDelimited(@Nullable Boolean isColumnNameDelimited) {
123             this.isColumnNameDelimited = isColumnNameDelimited;
124             return this;
125         }
126 
127         public Builder withGeneratedAlways(boolean isGeneratedAlways) {
128             this.isGeneratedAlways = isGeneratedAlways;
129             return this;
130         }
131 
132         public ColumnOverride build() {
133             return new ColumnOverride(this);
134         }
135 
136         @Override
137         protected Builder getThis() {
138             return this;
139         }
140     }
141 }