001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.datatype.DataType;
028:
029: import java.sql.DatabaseMetaData;
030:
031: /**
032: * Represents a table column.
033: *
034: * @author Manuel Laflamme
035: * @version $Revision: 554 $
036: * @since Feb 17, 2002
037: */
038: public class Column {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger logger = LoggerFactory
044: .getLogger(Column.class);
045:
046: /**
047: * Indicates that the column might not allow <code>NULL</code> values.
048: */
049: public static final Nullable NO_NULLS = new Nullable("noNulls");
050: /**
051: * Indicates that the column definitely allows <code>NULL</code> values.
052: */
053: public static final Nullable NULLABLE = new Nullable("nullable");
054: /**
055: * Indicates that the nullability of columns is unknown.
056: */
057: public static final Nullable NULLABLE_UNKNOWN = new Nullable(
058: "nullableUnknown");
059:
060: private final String _columnName;
061: private final DataType _dataType;
062: private final String _sqlTypeName;
063: private final Nullable _nullable;
064:
065: /**
066: * Creates a Column object. This contructor set nullable to true.
067: *
068: * @param columnName the column name
069: * @param dataType the data type
070: */
071: public Column(String columnName, DataType dataType) {
072: _columnName = columnName;
073: _dataType = dataType;
074: _nullable = NULLABLE_UNKNOWN;
075: _sqlTypeName = dataType.toString();
076: }
077:
078: /**
079: * Creates a Column object.
080: */
081: public Column(String columnName, DataType dataType,
082: Nullable nullable) {
083: _columnName = columnName;
084: _dataType = dataType;
085: _sqlTypeName = dataType.toString();
086: _nullable = nullable;
087: }
088:
089: /**
090: * Creates a Column object.
091: */
092: public Column(String columnName, DataType dataType,
093: String sqlTypeName, Nullable nullable) {
094: _columnName = columnName;
095: _dataType = dataType;
096: _sqlTypeName = sqlTypeName;
097: _nullable = nullable;
098: }
099:
100: /**
101: * Returns this column name.
102: */
103: public String getColumnName() {
104: logger.debug("getColumnName() - start");
105:
106: return _columnName;
107: }
108:
109: /**
110: * Returns this column data type.
111: */
112: public DataType getDataType() {
113: logger.debug("getDataType() - start");
114:
115: return _dataType;
116: }
117:
118: /**
119: * Returns this column sql data type name.
120: */
121: public String getSqlTypeName() {
122: logger.debug("getSqlTypeName() - start");
123:
124: return _sqlTypeName;
125: }
126:
127: /**
128: * Returns <code>true</code> if this column is nullable.
129: */
130: public Nullable getNullable() {
131: logger.debug("getNullable() - start");
132:
133: return _nullable;
134: }
135:
136: /**
137: * Returns the appropriate Nullable constant according specified JDBC
138: * DatabaseMetaData constant.
139: *
140: * @param nullable one of the following constants
141: * {@link java.sql.DatabaseMetaData#columnNoNulls},
142: * {@link java.sql.DatabaseMetaData#columnNullable},
143: * {@link java.sql.DatabaseMetaData#columnNullableUnknown}
144: */
145: public static Nullable nullableValue(int nullable) {
146: logger
147: .debug("nullableValue(nullable=" + nullable
148: + ") - start");
149:
150: switch (nullable) {
151: case DatabaseMetaData.columnNoNulls:
152: return NO_NULLS;
153:
154: case DatabaseMetaData.columnNullable:
155: return NULLABLE;
156:
157: case DatabaseMetaData.columnNullableUnknown:
158: return NULLABLE_UNKNOWN;
159:
160: default:
161: throw new IllegalArgumentException(
162: "Unknown constant value " + nullable);
163: }
164: }
165:
166: /**
167: * Returns the appropriate Nullable constant.
168: *
169: * @param nullable <code>true</code> if null is allowed
170: */
171: public static Nullable nullableValue(boolean nullable) {
172: logger
173: .debug("nullableValue(nullable=" + nullable
174: + ") - start");
175:
176: return nullable ? NULLABLE : NO_NULLS;
177: }
178:
179: ////////////////////////////////////////////////////////////////////////////
180: // Object class
181:
182: public String toString() {
183: logger.debug("toString() - start");
184:
185: return "(" + _columnName + ", " + _dataType + ", " + _nullable
186: + ")";
187: // return _columnName;
188: }
189:
190: public boolean equals(Object o) {
191: logger.debug("equals(o=" + o + ") - start");
192:
193: if (this == o)
194: return true;
195: if (!(o instanceof Column))
196: return false;
197:
198: final Column column = (Column) o;
199:
200: if (!_columnName.equals(column._columnName))
201: return false;
202: if (!_dataType.equals(column._dataType))
203: return false;
204: if (!_nullable.equals(column._nullable))
205: return false;
206: if (!_sqlTypeName.equals(column._sqlTypeName))
207: return false;
208:
209: return true;
210: }
211:
212: public int hashCode() {
213: logger.debug("hashCode() - start");
214:
215: int result;
216: result = _columnName.hashCode();
217: result = 29 * result + _dataType.hashCode();
218: result = 29 * result + _sqlTypeName.hashCode();
219: result = 29 * result + _nullable.hashCode();
220: return result;
221: }
222:
223: public static class Nullable {
224:
225: /**
226: * Logger for this class
227: */
228: private static final Logger logger = LoggerFactory
229: .getLogger(Nullable.class);
230:
231: private final String _name;
232:
233: private Nullable(String name) {
234: _name = name;
235: }
236:
237: ////////////////////////////////////////////////////////////////////////////
238: // Object class
239:
240: public String toString() {
241: logger.debug("toString() - start");
242:
243: return _name;
244: }
245: }
246:
247: }
|