View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.dataset;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import org.dbunit.dataset.datatype.DataType;
28  
29  import java.sql.DatabaseMetaData;
30  
31  /**
32   * Represents a table column.
33   *
34   * @author Manuel Laflamme
35   * @author Last changed by: $Author$
36   * @version $Revision$ $Date$
37   * @since 1.0 (Feb 17, 2002)
38   */
39  public class Column
40  {
41  
42      /**
43       * Logger for this class
44       */
45      private static final Logger logger = LoggerFactory.getLogger(Column.class);
46  
47      /**
48       * Indicates that the column might not allow <code>NULL</code> values.
49       */
50      public static final Nullable NO_NULLS = new Nullable("noNulls");
51      /**
52       * Indicates that the column definitely allows <code>NULL</code> values.
53       */
54      public static final Nullable NULLABLE = new Nullable("nullable");
55      /**
56       * Indicates that the nullability of columns is unknown.
57       */
58      public static final Nullable NULLABLE_UNKNOWN = new Nullable("nullableUnknown");
59  
60      private final String _columnName;
61      private final DataType _dataType;
62      private final String _sqlTypeName;
63      private final Nullable _nullable;
64      private final String _defaultValue;
65      private final String _remarks;
66      private final AutoIncrement _autoIncrement;
67  
68      /**
69       * Creates a Column object. This constructor set nullable to true.
70       *
71       * @param columnName the column name
72       * @param dataType the data type
73       */
74      public Column(String columnName, DataType dataType)
75      {
76          this(columnName, dataType, NULLABLE_UNKNOWN);
77      }
78  
79      /**
80       * Creates a Column object.
81       */
82      public Column(String columnName, DataType dataType, Nullable nullable)
83      {
84          this(columnName, dataType, dataType.toString(), nullable, null);
85      }
86  
87      /**
88       * Creates a Column object.
89       */
90      public Column(String columnName, DataType dataType, String sqlTypeName,
91              Nullable nullable)
92      {
93          this(columnName, dataType, sqlTypeName, nullable, null);
94      }
95  
96      /**
97       * Creates a Column object.
98       * @param columnName The name of the column
99       * @param dataType The DbUnit {@link DataType} of the column
100      * @param sqlTypeName The SQL name of the column which comes from the JDBC driver.
101      * See value 'TYPE_NAME' in {@link DatabaseMetaData#getColumns(String, String, String, String)}
102      * @param nullable whether or not the column is nullable
103      * @param defaultValue The default value on the DB for this column. Can be <code>null</code>.
104      */
105     public Column(String columnName, DataType dataType, String sqlTypeName,
106             Nullable nullable, String defaultValue)
107     {
108         this(columnName, dataType, sqlTypeName, nullable, defaultValue, null, null);
109     }
110 
111     /**
112      * Creates a Column object.
113      * @param columnName The name of the column
114      * @param dataType The DbUnit {@link DataType} of the column
115      * @param sqlTypeName The SQL name of the column which comes from the JDBC driver.
116      * See value 'TYPE_NAME' in {@link DatabaseMetaData#getColumns(String, String, String, String)}
117      * @param nullable whether or not the column is nullable
118      * @param defaultValue The default value on the DB for this column. Can be <code>null</code>.
119      * @param remarks The remarks on the DB for this column. Can be <code>null</code>.
120      * @param autoIncrement The auto increment setting for this column. Can be <code>null</code>.
121      */
122     public Column(String columnName, DataType dataType, String sqlTypeName,
123             Nullable nullable, String defaultValue, String remarks, AutoIncrement autoIncrement)
124     {
125         _columnName = columnName;
126         _dataType = dataType;
127         _sqlTypeName = sqlTypeName;
128         _nullable = nullable;
129         _defaultValue = defaultValue;
130         _remarks = remarks;
131         _autoIncrement = autoIncrement;
132     }
133     
134     public Column(ColumnMetaData metadata, DataType dataType) {
135     	_columnName = metadata.getColumnName(); 
136 		_dataType = dataType;
137 		_sqlTypeName = metadata.getSqlTypeName();
138 		_nullable = Column.nullableValue(metadata.getNullable());
139 		_defaultValue = metadata.getColumnDefault();
140 		_remarks = metadata.getRemarks();
141 		_autoIncrement = Column.AutoIncrement.autoIncrementValue(metadata.getAutoincrement());
142     }
143 
144     public boolean hasDefaultValue()
145     {
146         return _defaultValue != null;
147     }
148     
149     public boolean isNotNullable()
150     {
151         return _nullable== Column.NO_NULLS;
152     }
153     
154     /**
155      * Returns this column name.
156      */
157     public String getColumnName()
158     {
159         return _columnName;
160     }
161 
162     /**
163      * Returns this column data type.
164      */
165     public DataType getDataType()
166     {
167         return _dataType;
168     }
169 
170     /**
171      * Returns this column sql data type name.
172      */
173     public String getSqlTypeName()
174     {
175         return _sqlTypeName;
176     }
177 
178     /**
179      * Returns <code>true</code> if this column is nullable.
180      */
181     public Nullable getNullable()
182     {
183         return _nullable;
184     }
185 
186     /**
187      * @return The default value the database uses for this column 
188      * if not specified in the insert column list
189      */
190     public String getDefaultValue()
191     {
192         return _defaultValue;
193     }
194     
195     /**
196      * @return The remarks set on the database for this column
197      * @since 2.4.3
198      */
199     public String getRemarks()
200     {
201         return _remarks;
202     }
203     
204     /**
205      * @return The auto-increment property for this column
206      * @since 2.4.3
207      */
208     public AutoIncrement getAutoIncrement()
209     {
210         return _autoIncrement;
211     }
212     
213     /**
214      * Returns the appropriate Nullable constant according specified JDBC
215      * DatabaseMetaData constant.
216      *
217      * @param nullable one of the following constants
218      * {@link java.sql.DatabaseMetaData#columnNoNulls},
219      * {@link java.sql.DatabaseMetaData#columnNullable},
220      * {@link java.sql.DatabaseMetaData#columnNullableUnknown}
221      */
222     public static Nullable nullableValue(int nullable)
223     {
224         if(logger.isDebugEnabled())
225             logger.debug("nullableValue(nullable={}) - start", String.valueOf(nullable));
226 
227         switch (nullable)
228         {
229             case DatabaseMetaData.columnNoNulls:
230                 return NO_NULLS;
231 
232             case DatabaseMetaData.columnNullable:
233                 return NULLABLE;
234 
235             case DatabaseMetaData.columnNullableUnknown:
236                 return NULLABLE_UNKNOWN;
237 
238             default:
239                 throw new IllegalArgumentException("Unknown constant value "
240                         + nullable);
241         }
242     }
243 
244     /**
245      * Returns the appropriate Nullable constant.
246      *
247      * @param nullable <code>true</code> if null is allowed
248      */
249     public static Nullable nullableValue(boolean nullable)
250     {
251         if(logger.isDebugEnabled())
252             logger.debug("nullableValue(nullable={}) - start", String.valueOf(nullable));
253         
254         return nullable ? NULLABLE : NO_NULLS;
255     }
256     
257 
258     ////////////////////////////////////////////////////////////////////////////
259     // Object class
260 
261     public String toString()
262     {
263         return "(" + _columnName + ", " + _dataType + ", " + _nullable + ")";
264     }
265 
266     public boolean equals(Object o)
267     {
268         logger.debug("equals(o={}) - start", o);
269 
270         if (this == o) return true;
271         if (!(o instanceof Column)) return false;
272 
273         final Column column = (Column)o;
274 
275         if (!_columnName.equals(column._columnName)) return false;
276         if (!_dataType.equals(column._dataType)) return false;
277         if (!_nullable.equals(column._nullable)) return false;
278         if (!_sqlTypeName.equals(column._sqlTypeName)) return false;
279         
280         // Default value is nullable
281         if (_defaultValue==null){
282             if(column._defaultValue!=null)
283                 return false;
284         }
285         else{
286             if(!_defaultValue.equals(column._defaultValue))
287                 return false;
288         }
289 
290         return true;
291     }
292 
293     public int hashCode()
294     {
295         int result;
296         result = _columnName.hashCode();
297         result = 29 * result + _dataType.hashCode();
298         result = 29 * result + _sqlTypeName.hashCode();
299         result = 29 * result + _nullable.hashCode();
300         result = 29 * result + (_defaultValue==null? 0 : _defaultValue.hashCode());
301         return result;
302     }
303 
304     /**
305      * Specifies nullable usage.
306      * 
307 	 * @author Manuel Laflamme
308 	 * @author Last changed by: $Author$
309 	 * @version $Revision$ $Date$
310 	 * @since Feb 17, 2002
311 	 * @see Column
312      */
313     public static class Nullable
314     {
315 
316         private final String _name;
317 
318         private Nullable(String name)
319         {
320             _name = name;
321         }
322 
323         ////////////////////////////////////////////////////////////////////////////
324         // Object class
325 
326         public String toString()
327         {
328             return _name;
329         }
330         
331         public boolean equals(Object other) {
332         	return (other instanceof Nullable && ((Nullable) other).toString() == this.toString());
333         }
334     }
335     
336     
337     /**
338      * Enumeration for valid auto-increment values provided by JDBC driver implementations.
339      * 
340      * @author gommma
341      * @author Last changed by: $Author$
342      * @version $Revision$ $Date$
343      * @since 2.4.3
344      * @see Column
345      */
346     public static class AutoIncrement
347     {
348         public static final AutoIncrement YES = new AutoIncrement("YES");
349         public static final AutoIncrement NO = new AutoIncrement("NO");
350         public static final AutoIncrement UNKNOWN = new AutoIncrement("UNKNOWN");
351         
352         /**
353          * Logger for this class
354          */
355         private static final Logger LOGGER = LoggerFactory.getLogger(AutoIncrement.class);
356 
357         private final String key;
358         private AutoIncrement(String key)
359         {
360             this.key = key;
361         }
362         
363         public String getKey() 
364         {
365             return key;
366         }
367 
368         /**
369          * Searches the enumeration type for the given String provided by the JDBC driver.
370          * <p>
371          * If the parameter <code>autoIncrementValue</code>
372          * <ul>
373          * <li>equalsIgnoreCase &quot;YES&quot; or equals &quot;1&quot; then {@link AutoIncrement#YES} is returned</li>
374          * <li></li>
375          * </ul>
376          * </p>
377          * @param isAutoIncrement The String from the JDBC driver.
378          * @return The enumeration
379          */
380         public static AutoIncrement autoIncrementValue(String isAutoIncrement) 
381         {
382             if(LOGGER.isDebugEnabled())
383                 logger.debug("autoIncrementValue(isAutoIncrement={}) - start", isAutoIncrement);
384             
385             AutoIncrement result = AutoIncrement.UNKNOWN;
386             
387             if(isAutoIncrement != null)
388             {
389                 if(isAutoIncrement.equalsIgnoreCase("YES") || isAutoIncrement.equals("1"))
390                 {
391                     result = AutoIncrement.YES;
392                 }
393                 else if(isAutoIncrement.equalsIgnoreCase("NO") || isAutoIncrement.equals("0"))
394                 {
395                     result = AutoIncrement.NO;
396                 }
397             }
398             return result;
399         }
400 
401 
402         public String toString()
403         {
404             return "autoIncrement=" + key;
405         }
406         
407         public boolean equals(Object other) {
408         	return (other instanceof AutoIncrement && ((AutoIncrement) other).getKey() == this.key);
409         }
410     }
411 
412 }