KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > Column


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.dbunit.dataset.datatype.DataType;
25
26 import java.sql.DatabaseMetaData JavaDoc;
27
28 /**
29  * Represents a table column.
30  *
31  * @author Manuel Laflamme
32  * @version $Revision: 1.12 $
33  * @since Feb 17, 2002
34  */

35 public class Column
36 {
37     /**
38      * Indicates that the column might not allow <code>NULL</code> values.
39      */

40     public static final Nullable NO_NULLS = new Nullable("noNulls");
41     /**
42      * Indicates that the column definitely allows <code>NULL</code> values.
43      */

44     public static final Nullable NULLABLE = new Nullable("nullable");
45     /**
46      * Indicates that the nullability of columns is unknown.
47      */

48     public static final Nullable NULLABLE_UNKNOWN = new Nullable("nullableUnknown");
49
50     private final String JavaDoc _columnName;
51     private final DataType _dataType;
52     private final String JavaDoc _sqlTypeName;
53     private final Nullable _nullable;
54
55     /**
56      * Creates a Column object. This contructor set nullable to true.
57      *
58      * @param columnName the column name
59      * @param dataType the data type
60      */

61     public Column(String JavaDoc columnName, DataType dataType)
62     {
63         _columnName = columnName;
64         _dataType = dataType;
65         _nullable = NULLABLE_UNKNOWN;
66         _sqlTypeName = dataType.toString();
67     }
68
69     /**
70      * Creates a Column object.
71      */

72     public Column(String JavaDoc columnName, DataType dataType, Nullable nullable)
73     {
74         _columnName = columnName;
75         _dataType = dataType;
76         _sqlTypeName = dataType.toString();
77         _nullable = nullable;
78     }
79
80     /**
81      * Creates a Column object.
82      */

83     public Column(String JavaDoc columnName, DataType dataType, String JavaDoc sqlTypeName,
84             Nullable nullable)
85     {
86         _columnName = columnName;
87         _dataType = dataType;
88         _sqlTypeName = sqlTypeName;
89         _nullable = nullable;
90     }
91
92     /**
93      * Returns this column name.
94      */

95     public String JavaDoc getColumnName()
96     {
97         return _columnName;
98     }
99
100     /**
101      * Returns this column data type.
102      */

103     public DataType getDataType()
104     {
105         return _dataType;
106     }
107
108     /**
109      * Returns this column sql data type name.
110      */

111     public String JavaDoc getSqlTypeName()
112     {
113         return _sqlTypeName;
114     }
115
116     /**
117      * Returns <code>true</code> if this column is nullable.
118      */

119     public Nullable getNullable()
120     {
121         return _nullable;
122     }
123
124     /**
125      * Returns the appropriate Nullable constant according specified JDBC
126      * DatabaseMetaData constant.
127      *
128      * @param nullable one of the following constants
129      * {@link java.sql.DatabaseMetaData#columnNoNulls},
130      * {@link java.sql.DatabaseMetaData#columnNullable},
131      * {@link java.sql.DatabaseMetaData#columnNullableUnknown}
132      */

133     public static Nullable nullableValue(int nullable)
134     {
135         switch (nullable)
136         {
137             case DatabaseMetaData.columnNoNulls:
138                 return NO_NULLS;
139
140             case DatabaseMetaData.columnNullable:
141                 return NULLABLE;
142
143             case DatabaseMetaData.columnNullableUnknown:
144                 return NULLABLE_UNKNOWN;
145
146             default:
147                 throw new IllegalArgumentException JavaDoc("Unknown constant value "
148                         + nullable);
149         }
150     }
151
152     /**
153      * Returns the appropriate Nullable constant.
154      *
155      * @param nullable <code>true</code> if null is allowed
156      */

157     public static Nullable nullableValue(boolean nullable)
158     {
159         return nullable ? NULLABLE : NO_NULLS;
160     }
161
162     ////////////////////////////////////////////////////////////////////////////
163
// Object class
164

165     public String JavaDoc toString()
166     {
167         return "(" + _columnName + ", " + _dataType + ", " + _nullable + ")";
168 // return _columnName;
169
}
170
171     public boolean equals(Object JavaDoc o)
172     {
173         if (this == o) return true;
174         if (!(o instanceof Column)) return false;
175
176         final Column column = (Column)o;
177
178         if (!_columnName.equals(column._columnName)) return false;
179         if (!_dataType.equals(column._dataType)) return false;
180         if (!_nullable.equals(column._nullable)) return false;
181         if (!_sqlTypeName.equals(column._sqlTypeName)) return false;
182
183         return true;
184     }
185
186     public int hashCode()
187     {
188         int result;
189         result = _columnName.hashCode();
190         result = 29 * result + _dataType.hashCode();
191         result = 29 * result + _sqlTypeName.hashCode();
192         result = 29 * result + _nullable.hashCode();
193         return result;
194     }
195
196     public static class Nullable
197     {
198
199         private final String JavaDoc _name;
200
201         private Nullable(String JavaDoc name)
202         {
203             _name = name;
204         }
205
206         ////////////////////////////////////////////////////////////////////////////
207
// Object class
208

209         public String JavaDoc toString()
210         {
211             return _name;
212         }
213     }
214
215 }
216
217
218
219
220
221
222
223
224
Popular Tags