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 boolean hasDefaultValue()
135     {
136         return _defaultValue != null;
137     }
138     
139     public boolean isNotNullable()
140     {
141         return _nullable== Column.NO_NULLS;
142     }
143     
144     /**
145      * Returns this column name.
146      */
147     public String getColumnName()
148     {
149         return _columnName;
150     }
151 
152     /**
153      * Returns this column data type.
154      */
155     public DataType getDataType()
156     {
157         return _dataType;
158     }
159 
160     /**
161      * Returns this column sql data type name.
162      */
163     public String getSqlTypeName()
164     {
165         return _sqlTypeName;
166     }
167 
168     /**
169      * Returns <code>true</code> if this column is nullable.
170      */
171     public Nullable getNullable()
172     {
173         return _nullable;
174     }
175 
176     /**
177      * @return The default value the database uses for this column 
178      * if not specified in the insert column list
179      */
180     public String getDefaultValue()
181     {
182         return _defaultValue;
183     }
184     
185     /**
186      * @return The remarks set on the database for this column
187      * @since 2.4.3
188      */
189     public String getRemarks()
190     {
191         return _remarks;
192     }
193     
194     /**
195      * @return The auto-increment property for this column
196      * @since 2.4.3
197      */
198     public AutoIncrement getAutoIncrement()
199     {
200         return _autoIncrement;
201     }
202     
203     /**
204      * Returns the appropriate Nullable constant according specified JDBC
205      * DatabaseMetaData constant.
206      *
207      * @param nullable one of the following constants
208      * {@link java.sql.DatabaseMetaData#columnNoNulls},
209      * {@link java.sql.DatabaseMetaData#columnNullable},
210      * {@link java.sql.DatabaseMetaData#columnNullableUnknown}
211      */
212     public static Nullable nullableValue(int nullable)
213     {
214         if(logger.isDebugEnabled())
215             logger.debug("nullableValue(nullable={}) - start", String.valueOf(nullable));
216 
217         switch (nullable)
218         {
219             case DatabaseMetaData.columnNoNulls:
220                 return NO_NULLS;
221 
222             case DatabaseMetaData.columnNullable:
223                 return NULLABLE;
224 
225             case DatabaseMetaData.columnNullableUnknown:
226                 return NULLABLE_UNKNOWN;
227 
228             default:
229                 throw new IllegalArgumentException("Unknown constant value "
230                         + nullable);
231         }
232     }
233 
234     /**
235      * Returns the appropriate Nullable constant.
236      *
237      * @param nullable <code>true</code> if null is allowed
238      */
239     public static Nullable nullableValue(boolean nullable)
240     {
241         if(logger.isDebugEnabled())
242             logger.debug("nullableValue(nullable={}) - start", String.valueOf(nullable));
243         
244         return nullable ? NULLABLE : NO_NULLS;
245     }
246     
247 
248     ////////////////////////////////////////////////////////////////////////////
249     // Object class
250 
251     public String toString()
252     {
253         return "(" + _columnName + ", " + _dataType + ", " + _nullable + ")";
254     }
255 
256     public boolean equals(Object o)
257     {
258         logger.debug("equals(o={}) - start", o);
259 
260         if (this == o) return true;
261         if (!(o instanceof Column)) return false;
262 
263         final Column column = (Column)o;
264 
265         if (!_columnName.equals(column._columnName)) return false;
266         if (!_dataType.equals(column._dataType)) return false;
267         if (!_nullable.equals(column._nullable)) return false;
268         if (!_sqlTypeName.equals(column._sqlTypeName)) return false;
269         
270         // Default value is nullable
271         if (_defaultValue==null){
272             if(column._defaultValue!=null)
273                 return false;
274         }
275         else{
276             if(!_defaultValue.equals(column._defaultValue))
277                 return false;
278         }
279 
280         return true;
281     }
282 
283     public int hashCode()
284     {
285         int result;
286         result = _columnName.hashCode();
287         result = 29 * result + _dataType.hashCode();
288         result = 29 * result + _sqlTypeName.hashCode();
289         result = 29 * result + _nullable.hashCode();
290         result = 29 * result + (_defaultValue==null? 0 : _defaultValue.hashCode());
291         return result;
292     }
293 
294     /**
295      * Specifies nullable usage.
296      * 
297 	 * @author Manuel Laflamme
298 	 * @author Last changed by: $Author$
299 	 * @version $Revision$ $Date$
300 	 * @since Feb 17, 2002
301 	 * @see Column
302      */
303     public static class Nullable
304     {
305 
306         private final String _name;
307 
308         private Nullable(String name)
309         {
310             _name = name;
311         }
312 
313         ////////////////////////////////////////////////////////////////////////////
314         // Object class
315 
316         public String toString()
317         {
318             return _name;
319         }
320     }
321     
322     
323     /**
324      * Enumeration for valid auto-increment values provided by JDBC driver implementations.
325      * 
326      * @author gommma
327      * @author Last changed by: $Author$
328      * @version $Revision$ $Date$
329      * @since 2.4.3
330      * @see Column
331      */
332     public static class AutoIncrement
333     {
334         public static final AutoIncrement YES = new AutoIncrement("YES");
335         public static final AutoIncrement NO = new AutoIncrement("NO");
336         public static final AutoIncrement UNKNOWN = new AutoIncrement("UNKNOWN");
337         
338         /**
339          * Logger for this class
340          */
341         private static final Logger LOGGER = LoggerFactory.getLogger(AutoIncrement.class);
342 
343         private final String key;
344         private AutoIncrement(String key)
345         {
346             this.key = key;
347         }
348         
349         public String getKey() 
350         {
351             return key;
352         }
353 
354         /**
355          * Searches the enumeration type for the given String provided by the JDBC driver.
356          * <p>
357          * If the parameter <code>autoIncrementValue</code>
358          * <ul>
359          * <li>equalsIgnoreCase &quot;YES&quot; or equals &quot;1&quot; then {@link AutoIncrement#YES} is returned</li>
360          * <li></li>
361          * </ul>
362          * </p>
363          * @param isAutoIncrement The String from the JDBC driver.
364          * @return The enumeration
365          */
366         public static AutoIncrement autoIncrementValue(String isAutoIncrement) 
367         {
368             if(LOGGER.isDebugEnabled())
369                 logger.debug("autoIncrementValue(isAutoIncrement={}) - start", isAutoIncrement);
370             
371             AutoIncrement result = AutoIncrement.UNKNOWN;
372             
373             if(isAutoIncrement != null)
374             {
375                 if(isAutoIncrement.equalsIgnoreCase("YES") || isAutoIncrement.equals("1"))
376                 {
377                     result = AutoIncrement.YES;
378                 }
379                 else if(isAutoIncrement.equalsIgnoreCase("NO") || isAutoIncrement.equals("0"))
380                 {
381                     result = AutoIncrement.NO;
382                 }
383             }
384             return result;
385         }
386 
387 
388         public String toString()
389         {
390             return "autoIncrement=" + key;
391         }
392     }
393 
394 }