View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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. The class has
27   * utility methods useful for generating iBATIS objects.
28   * 
29   * @author Jeff Butler
30   */
31  public class IntrospectedColumn {
32      protected String actualColumnName;
33  
34      protected int jdbcType;
35  
36      protected String jdbcTypeName;
37  
38      protected boolean nullable;
39  
40      protected int length;
41  
42      protected int scale;
43  
44      protected boolean identity;
45      
46      protected boolean isSequenceColumn;
47  
48      protected String javaProperty;
49  
50      protected FullyQualifiedJavaType fullyQualifiedJavaType;
51  
52      protected String tableAlias;
53  
54      protected String typeHandler;
55  
56      protected Context context;
57  
58      protected boolean isColumnNameDelimited;
59  
60      protected IntrospectedTable introspectedTable;
61  
62      protected Properties properties;
63  
64      // any database comment associated with this column. May be null
65      protected String remarks;
66  
67      protected String defaultValue;
68  
69      /**
70       * Constructs a Column definition. This object holds all the information
71       * about a column that is required to generate Java objects and SQL maps;
72       */
73      public IntrospectedColumn() {
74          super();
75          properties = new Properties();
76      }
77  
78      public int getJdbcType() {
79          return jdbcType;
80      }
81  
82      public void setJdbcType(int jdbcType) {
83          this.jdbcType = jdbcType;
84      }
85  
86      public int getLength() {
87          return length;
88      }
89  
90      public void setLength(int length) {
91          this.length = length;
92      }
93  
94      public boolean isNullable() {
95          return nullable;
96      }
97  
98      public void setNullable(boolean nullable) {
99          this.nullable = nullable;
100     }
101 
102     public int getScale() {
103         return scale;
104     }
105 
106     public void setScale(int scale) {
107         this.scale = scale;
108     }
109 
110     /*
111      * This method is primarily used for debugging, so we don't externalize the
112      * strings
113      */
114     @Override
115     public String toString() {
116         StringBuilder sb = new StringBuilder();
117 
118         sb.append("Actual Column Name: "); //$NON-NLS-1$
119         sb.append(actualColumnName);
120         sb.append(", JDBC Type: "); //$NON-NLS-1$
121         sb.append(jdbcType);
122         sb.append(", Nullable: "); //$NON-NLS-1$
123         sb.append(nullable);
124         sb.append(", Length: "); //$NON-NLS-1$
125         sb.append(length);
126         sb.append(", Scale: "); //$NON-NLS-1$
127         sb.append(scale);
128         sb.append(", Identity: "); //$NON-NLS-1$
129         sb.append(identity);
130 
131         return sb.toString();
132     }
133 
134     public void setActualColumnName(String actualColumnName) {
135         this.actualColumnName = actualColumnName;
136         isColumnNameDelimited = StringUtility
137                 .stringContainsSpace(actualColumnName);
138     }
139 
140     /**
141      * @return Returns the identity.
142      */
143     public boolean isIdentity() {
144         return identity;
145     }
146 
147     /**
148      * @param identity
149      *            The identity to set.
150      */
151     public void setIdentity(boolean identity) {
152         this.identity = identity;
153     }
154 
155     public boolean isBLOBColumn() {
156         String typeName = getJdbcTypeName();
157 
158         return "BINARY".equals(typeName) || "BLOB".equals(typeName) //$NON-NLS-1$ //$NON-NLS-2$
159                 || "CLOB".equals(typeName) || "LONGNVARCHAR".equals(typeName) //$NON-NLS-1$ //$NON-NLS-2$ 
160                 || "LONGVARBINARY".equals(typeName) || "LONGVARCHAR".equals(typeName) //$NON-NLS-1$ //$NON-NLS-2$
161                 || "NCLOB".equals(typeName) || "VARBINARY".equals(typeName); //$NON-NLS-1$ //$NON-NLS-2$ 
162     }
163 
164     public boolean isStringColumn() {
165         return fullyQualifiedJavaType.equals(FullyQualifiedJavaType
166                 .getStringInstance());
167     }
168 
169     public boolean isJdbcCharacterColumn() {
170         return jdbcType == Types.CHAR || jdbcType == Types.CLOB
171                 || jdbcType == Types.LONGVARCHAR || jdbcType == Types.VARCHAR
172                 || jdbcType == Types.LONGNVARCHAR || jdbcType == Types.NCHAR
173                 || jdbcType == Types.NCLOB || jdbcType == Types.NVARCHAR;
174     }
175 
176     public String getJavaProperty() {
177         return getJavaProperty(null);
178     }
179 
180     public String getJavaProperty(String prefix) {
181         if (prefix == null) {
182             return javaProperty;
183         }
184 
185         StringBuilder sb = new StringBuilder();
186         sb.append(prefix);
187         sb.append(javaProperty);
188 
189         return sb.toString();
190     }
191 
192     public void setJavaProperty(String javaProperty) {
193         this.javaProperty = javaProperty;
194     }
195 
196     public boolean isJDBCDateColumn() {
197         return fullyQualifiedJavaType.equals(FullyQualifiedJavaType
198                 .getDateInstance())
199                 && "DATE".equalsIgnoreCase(jdbcTypeName); //$NON-NLS-1$
200     }
201 
202     public boolean isJDBCTimeColumn() {
203         return fullyQualifiedJavaType.equals(FullyQualifiedJavaType
204                 .getDateInstance())
205                 && "TIME".equalsIgnoreCase(jdbcTypeName); //$NON-NLS-1$
206     }
207 
208     public String getTypeHandler() {
209         return typeHandler;
210     }
211 
212     public void setTypeHandler(String typeHandler) {
213         this.typeHandler = typeHandler;
214     }
215 
216     public String getActualColumnName() {
217         return actualColumnName;
218     }
219 
220     public void setColumnNameDelimited(boolean isColumnNameDelimited) {
221         this.isColumnNameDelimited = isColumnNameDelimited;
222     }
223 
224     public boolean isColumnNameDelimited() {
225         return isColumnNameDelimited;
226     }
227 
228     public String getJdbcTypeName() {
229         if (jdbcTypeName == null) {
230             return "OTHER"; //$NON-NLS-1$
231         }
232 
233         return jdbcTypeName;
234     }
235 
236     public void setJdbcTypeName(String jdbcTypeName) {
237         this.jdbcTypeName = jdbcTypeName;
238     }
239 
240     public FullyQualifiedJavaType getFullyQualifiedJavaType() {
241         return fullyQualifiedJavaType;
242     }
243 
244     public void setFullyQualifiedJavaType(
245             FullyQualifiedJavaType fullyQualifiedJavaType) {
246         this.fullyQualifiedJavaType = fullyQualifiedJavaType;
247     }
248 
249     public String getTableAlias() {
250         return tableAlias;
251     }
252 
253     public void setTableAlias(String tableAlias) {
254         this.tableAlias = tableAlias;
255     }
256 
257     public Context getContext() {
258         return context;
259     }
260 
261     public void setContext(Context context) {
262         this.context = context;
263     }
264 
265     public IntrospectedTable getIntrospectedTable() {
266         return introspectedTable;
267     }
268 
269     public void setIntrospectedTable(IntrospectedTable introspectedTable) {
270         this.introspectedTable = introspectedTable;
271     }
272 
273     public Properties getProperties() {
274         return properties;
275     }
276 
277     public void setProperties(Properties properties) {
278         this.properties.putAll(properties);
279     }
280 
281     public String getRemarks() {
282         return remarks;
283     }
284 
285     public void setRemarks(String remarks) {
286         this.remarks = remarks;
287     }
288 
289     public String getDefaultValue() {
290         return defaultValue;
291     }
292 
293     public void setDefaultValue(String defaultValue) {
294         this.defaultValue = defaultValue;
295     }
296 
297     public boolean isSequenceColumn() {
298         return isSequenceColumn;
299     }
300 
301     public void setSequenceColumn(boolean isSequenceColumn) {
302         this.isSequenceColumn = isSequenceColumn;
303     }
304 }