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 TimestampDataType extends AbstractDataType {
037:
038: /**
039: * Logger for this class
040: */
041: private static final Logger logger = LoggerFactory
042: .getLogger(TimestampDataType.class);
043:
044: TimestampDataType() {
045: super ("TIMESTAMP", Types.TIMESTAMP, Timestamp.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.Timestamp) {
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.Timestamp(date.getTime());
065: }
066:
067: if (value instanceof Long) {
068: Long date = (Long) value;
069: return new java.sql.Timestamp(date.longValue());
070: }
071:
072: if (value instanceof String) {
073: String stringValue = (String) value;
074:
075: // Probably a java.sql.Date, try it just in case!
076: if (stringValue.length() == 10) {
077: try {
078: long time = java.sql.Date.valueOf(stringValue)
079: .getTime();
080: return new java.sql.Timestamp(time);
081: } catch (IllegalArgumentException e) {
082: logger.error("typeCast()", e);
083:
084: // Was not a java.sql.Date, let Timestamp handle this value
085: }
086: }
087:
088: try {
089: return java.sql.Timestamp.valueOf(stringValue);
090: } catch (IllegalArgumentException e) {
091: logger.error("typeCast()", e);
092:
093: throw new TypeCastException(value, this , e);
094: }
095: }
096:
097: throw new TypeCastException(value, this );
098: }
099:
100: public boolean isDateTime() {
101: logger.debug("isDateTime() - start");
102:
103: return true;
104: }
105:
106: public Object getSqlValue(int column, ResultSet resultSet)
107: throws SQLException, TypeCastException {
108: logger.debug("getSqlValue(column=" + column + ", resultSet="
109: + resultSet + ") - start");
110:
111: Timestamp value = resultSet.getTimestamp(column);
112: if (value == null || resultSet.wasNull()) {
113: return null;
114: }
115: return value;
116: }
117:
118: public void setSqlValue(Object value, int column,
119: PreparedStatement statement) throws SQLException,
120: TypeCastException {
121: logger.debug("setSqlValue(value=" + value + ", column="
122: + column + ", statement=" + statement + ") - start");
123:
124: statement.setTimestamp(column,
125: (java.sql.Timestamp) typeCast(value));
126: }
127: }
|