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.datatype;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.ITable;
028:
029: import java.sql.*;
030:
031: /**
032: * @author Manuel Laflamme
033: * @version $Revision: 554 $
034: * @since Feb 19, 2002
035: */
036: public class TimeDataType extends AbstractDataType {
037:
038: /**
039: * Logger for this class
040: */
041: private static final Logger logger = LoggerFactory
042: .getLogger(TimeDataType.class);
043:
044: TimeDataType() {
045: super ("TIME", Types.TIME, Time.class, false);
046: }
047:
048: ////////////////////////////////////////////////////////////////////////////
049: // DataType class
050:
051: public Object typeCast(Object value) throws TypeCastException {
052: logger.debug("typeCast(value=" + value + ") - start");
053:
054: if (value == null || value == ITable.NO_VALUE) {
055: return null;
056: }
057:
058: if (value instanceof java.sql.Time) {
059: return value;
060: }
061:
062: if (value instanceof java.util.Date) {
063: java.util.Date date = (java.util.Date) value;
064: return new java.sql.Time(date.getTime());
065: }
066:
067: if (value instanceof Long) {
068: Long date = (Long) value;
069: return new java.sql.Time(date.longValue());
070: }
071:
072: if (value instanceof String) {
073: try {
074: return java.sql.Time.valueOf((String) value);
075: } catch (IllegalArgumentException e) {
076: logger.error("typeCast()", e);
077:
078: throw new TypeCastException(value, this , e);
079: }
080: }
081:
082: throw new TypeCastException(value, this );
083: }
084:
085: public boolean isDateTime() {
086: logger.debug("isDateTime() - start");
087:
088: return true;
089: }
090:
091: public Object getSqlValue(int column, ResultSet resultSet)
092: throws SQLException, TypeCastException {
093: logger.debug("getSqlValue(column=" + column + ", resultSet="
094: + resultSet + ") - start");
095:
096: Time value = resultSet.getTime(column);
097: if (value == null || resultSet.wasNull()) {
098: return null;
099: }
100: return value;
101: }
102:
103: public void setSqlValue(Object value, int column,
104: PreparedStatement statement) throws SQLException,
105: TypeCastException {
106: logger.debug("setSqlValue(value=" + value + ", column="
107: + column + ", statement=" + statement + ") - start");
108:
109: statement.setTime(column, (java.sql.Time) typeCast(value));
110: }
111: }
|