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.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Types;
033:
034: /**
035: * @author Manuel Laflamme
036: * @version $Revision: 554 $
037: * @since Feb 19, 2002
038: */
039: public class DateDataType extends AbstractDataType {
040:
041: /**
042: * Logger for this class
043: */
044: private static final Logger logger = LoggerFactory
045: .getLogger(DateDataType.class);
046:
047: DateDataType() {
048: super ("DATE", Types.DATE, java.sql.Date.class, false);
049: }
050:
051: ////////////////////////////////////////////////////////////////////////////
052: // DataType class
053:
054: public Object typeCast(Object value) throws TypeCastException {
055: logger.debug("typeCast(value=" + value + ") - start");
056:
057: if (value == null || value == ITable.NO_VALUE) {
058: return null;
059: }
060:
061: if (value instanceof java.sql.Date) {
062: return value;
063: }
064:
065: if (value instanceof java.util.Date) {
066: java.util.Date date = (java.util.Date) value;
067: return new java.sql.Date(date.getTime());
068: }
069:
070: if (value instanceof Long) {
071: Long date = (Long) value;
072: return new java.sql.Date(date.longValue());
073: }
074:
075: if (value instanceof String) {
076: String stringValue = (String) value;
077:
078: // Probably a Timestamp, try it just in case!
079: if (stringValue.length() > 10) {
080: try {
081: long time = java.sql.Timestamp.valueOf(stringValue)
082: .getTime();
083: return new java.sql.Date(time);
084: // return java.sql.Date.valueOf(new java.sql.Date(time).toString());
085: } catch (IllegalArgumentException e) {
086: logger.error("typeCast()", e);
087:
088: // Was not a Timestamp, let java.sql.Date handle this value
089: }
090: }
091:
092: try {
093: return java.sql.Date.valueOf(stringValue);
094: } catch (IllegalArgumentException e) {
095: logger.error("typeCast()", e);
096:
097: throw new TypeCastException(value, this , e);
098: }
099: }
100:
101: throw new TypeCastException(value, this );
102: }
103:
104: public boolean isDateTime() {
105: logger.debug("isDateTime() - start");
106:
107: return true;
108: }
109:
110: public Object getSqlValue(int column, ResultSet resultSet)
111: throws SQLException, TypeCastException {
112: logger.debug("getSqlValue(column=" + column + ", resultSet="
113: + resultSet + ") - start");
114:
115: java.sql.Date value = resultSet.getDate(column);
116: if (value == null || resultSet.wasNull()) {
117: return null;
118: }
119: return value;
120: }
121:
122: public void setSqlValue(Object value, int column,
123: PreparedStatement statement) throws SQLException,
124: TypeCastException {
125: logger.debug("setSqlValue(value=" + value + ", column="
126: + column + ", statement=" + statement + ") - start");
127:
128: statement.setDate(column, (java.sql.Date) typeCast(value));
129: }
130: }
|