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.api;
17  
18  import java.sql.Types;
19  import java.util.Properties;
20  
21  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
22  import org.mybatis.generator.config.Context;
23  import org.mybatis.generator.internal.util.StringUtility;
24  
25  /**
26   * This class holds information about an introspected column.
27   *
28   * @author Jeff Butler
29   */
30  public class IntrospectedColumn {
31      protected String actualColumnName;
32  
33      protected int jdbcType;
34  
35      /**
36       * The platform specific data type name as reported from DatabaseMetadata.getColumns()
37       */
38      protected String actualTypeName;
39  
40      protected String jdbcTypeName;
41  
42      protected boolean nullable;
43  
44      protected int length;
45  
46      protected int scale;
47  
48      protected boolean identity;
49  
50      protected boolean isSequenceColumn;
51  
52      protected String javaProperty;
53  
54      protected FullyQualifiedJavaType fullyQualifiedJavaType;
55  
56      protected String tableAlias;
57  
58      protected String typeHandler;
59  
60      protected Context context;
61  
62      protected boolean isColumnNameDelimited;
63  
64      protected IntrospectedTable introspectedTable;
65  
66      protected final Properties properties;
67  
68      // any database comment associated with this column. May be null
69      protected String remarks;
70  
71      protected String defaultValue;
72  
73      /**
74       * true if the JDBC driver reports that this column is auto-increment.
75       */
76      protected boolean isAutoIncrement;
77  
78      /**
79       * true if the JDBC driver reports that this column is generated.
80       */
81      protected boolean isGeneratedColumn;
82  
83      /**
84       * True if there is a column override that defines this column as GENERATED ALWAYS.
85       */
86      protected boolean isGeneratedAlways;
87  
88      /**
89       * Constructs a Column definition. This object holds all the information
90       * about a column that is required to generate Java objects and SQL maps;
91       */
92      public IntrospectedColumn() {
93          super();
94          properties = new Properties();
95      }
96  
97      public int getJdbcType() {
98          return jdbcType;
99      }
100 
101     public void setJdbcType(int jdbcType) {
102         this.jdbcType = jdbcType;
103     }
104 
105     public int getLength() {
106         return length;
107     }
108 
109     public void setLength(int length) {
110         this.length = length;
111     }
112 
113     public boolean isNullable() {
114         return nullable;
115     }
116 
117     public void setNullable(boolean nullable) {
118         this.nullable = nullable;
119     }
120 
121     public int getScale() {
122         return scale;
123     }
124 
125     public void setScale(int scale) {
126         this.scale = scale;
127     }
128 
129     /*
130      * This method is primarily used for debugging, so we don't externalize the
131      * strings
132      */
133     @Override
134     public String toString() {
135         return "Actual Column Name: " //$NON-NLS-1$
136                 + actualColumnName
137                 + ", JDBC Type: " //$NON-NLS-1$
138                 + jdbcType
139                 + ", Nullable: " //$NON-NLS-1$
140                 + nullable
141                 + ", Length: " //$NON-NLS-1$
142                 + length
143                 + ", Scale: " //$NON-NLS-1$
144                 + scale
145                 + ", Identity: " //$NON-NLS-1$
146                 + identity;
147     }
148 
149     public void setActualColumnName(String actualColumnName) {
150         this.actualColumnName = actualColumnName;
151         isColumnNameDelimited = StringUtility
152                 .stringContainsSpace(actualColumnName);
153     }
154 
155     public boolean isIdentity() {
156         return identity;
157     }
158 
159     public void setIdentity(boolean identity) {
160         this.identity = identity;
161     }
162 
163     public boolean isBLOBColumn() {
164         String typeName = getJdbcTypeName();
165 
166         return "BINARY".equals(typeName) || "BLOB".equals(typeName) //$NON-NLS-1$ //$NON-NLS-2$
167                 || "CLOB".equals(typeName) || "LONGNVARCHAR".equals(typeName) //$NON-NLS-1$ //$NON-NLS-2$
168                 || "LONGVARBINARY".equals(typeName) || "LONGVARCHAR".equals(typeName) //$NON-NLS-1$ //$NON-NLS-2$
169                 || "NCLOB".equals(typeName) || "VARBINARY".equals(typeName); //$NON-NLS-1$ //$NON-NLS-2$
170     }
171 
172     public boolean isStringColumn() {
173         return fullyQualifiedJavaType.equals(FullyQualifiedJavaType
174                 .getStringInstance());
175     }
176 
177     public boolean isJdbcCharacterColumn() {
178         return jdbcType == Types.CHAR || jdbcType == Types.CLOB
179                 || jdbcType == Types.LONGVARCHAR || jdbcType == Types.VARCHAR
180                 || jdbcType == Types.LONGNVARCHAR || jdbcType == Types.NCHAR
181                 || jdbcType == Types.NCLOB || jdbcType == Types.NVARCHAR;
182     }
183 
184     public String getJavaProperty() {
185         return getJavaProperty(null);
186     }
187 
188     public String getJavaProperty(String prefix) {
189         if (prefix == null) {
190             return javaProperty;
191         }
192 
193         return prefix + javaProperty;
194     }
195 
196     public void setJavaProperty(String javaProperty) {
197         this.javaProperty = javaProperty;
198     }
199 
200     public boolean isJDBCDateColumn() {
201         return fullyQualifiedJavaType.equals(FullyQualifiedJavaType
202                 .getDateInstance())
203                 && "DATE".equalsIgnoreCase(jdbcTypeName); //$NON-NLS-1$
204     }
205 
206     public boolean isJDBCTimeColumn() {
207         return fullyQualifiedJavaType.equals(FullyQualifiedJavaType
208                 .getDateInstance())
209                 && "TIME".equalsIgnoreCase(jdbcTypeName); //$NON-NLS-1$
210     }
211 
212     public String getTypeHandler() {
213         return typeHandler;
214     }
215 
216     public void setTypeHandler(String typeHandler) {
217         this.typeHandler = typeHandler;
218     }
219 
220     public String getActualColumnName() {
221         return actualColumnName;
222     }
223 
224     public void setColumnNameDelimited(boolean isColumnNameDelimited) {
225         this.isColumnNameDelimited = isColumnNameDelimited;
226     }
227 
228     public boolean isColumnNameDelimited() {
229         return isColumnNameDelimited;
230     }
231 
232     public String getJdbcTypeName() {
233         if (jdbcTypeName == null) {
234             return "OTHER"; //$NON-NLS-1$
235         }
236 
237         return jdbcTypeName;
238     }
239 
240     public void setJdbcTypeName(String jdbcTypeName) {
241         this.jdbcTypeName = jdbcTypeName;
242     }
243 
244     public FullyQualifiedJavaType getFullyQualifiedJavaType() {
245         return fullyQualifiedJavaType;
246     }
247 
248     public void setFullyQualifiedJavaType(
249             FullyQualifiedJavaType fullyQualifiedJavaType) {
250         this.fullyQualifiedJavaType = fullyQualifiedJavaType;
251     }
252 
253     public String getTableAlias() {
254         return tableAlias;
255     }
256 
257     public void setTableAlias(String tableAlias) {
258         this.tableAlias = tableAlias;
259     }
260 
261     public Context getContext() {
262         return context;
263     }
264 
265     public void setContext(Context context) {
266         this.context = context;
267     }
268 
269     public IntrospectedTable getIntrospectedTable() {
270         return introspectedTable;
271     }
272 
273     public void setIntrospectedTable(IntrospectedTable introspectedTable) {
274         this.introspectedTable = introspectedTable;
275     }
276 
277     public Properties getProperties() {
278         return properties;
279     }
280 
281     public void setProperties(Properties properties) {
282         this.properties.putAll(properties);
283     }
284 
285     public String getRemarks() {
286         return remarks;
287     }
288 
289     public void setRemarks(String remarks) {
290         this.remarks = remarks;
291     }
292 
293     public String getDefaultValue() {
294         return defaultValue;
295     }
296 
297     public void setDefaultValue(String defaultValue) {
298         this.defaultValue = defaultValue;
299     }
300 
301     public boolean isSequenceColumn() {
302         return isSequenceColumn;
303     }
304 
305     public void setSequenceColumn(boolean isSequenceColumn) {
306         this.isSequenceColumn = isSequenceColumn;
307     }
308 
309     public boolean isAutoIncrement() {
310         return isAutoIncrement;
311     }
312 
313     public void setAutoIncrement(boolean isAutoIncrement) {
314         this.isAutoIncrement = isAutoIncrement;
315     }
316 
317     public boolean isGeneratedColumn() {
318         return isGeneratedColumn;
319     }
320 
321     public void setGeneratedColumn(boolean isGeneratedColumn) {
322         this.isGeneratedColumn = isGeneratedColumn;
323     }
324 
325     public boolean isGeneratedAlways() {
326         return isGeneratedAlways;
327     }
328 
329     public void setGeneratedAlways(boolean isGeneratedAlways) {
330         this.isGeneratedAlways = isGeneratedAlways;
331     }
332 
333     /**
334      * The platform specific type name as reported by the JDBC driver. This value is determined
335      * from the DatabaseMetadata.getColumns() call - specifically ResultSet.getString("TYPE_NAME").
336      * This value is platform dependent.
337      *
338      * @return the platform specific type name as reported by the JDBC driver
339      */
340     public String getActualTypeName() {
341         return actualTypeName;
342     }
343 
344     public void setActualTypeName(String actualTypeName) {
345         this.actualTypeName = actualTypeName;
346     }
347 }