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