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 java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030:
031: /**
032: * @author Manuel Laflamme
033: * @version $Revision: 554 $
034: * @since Mar 19, 2002
035: */
036: public abstract class AbstractDataType extends DataType {
037:
038: /**
039: * Logger for this class
040: */
041: private static final Logger logger = LoggerFactory
042: .getLogger(AbstractDataType.class);
043:
044: private final String _name;
045: private final int _sqlType;
046: private final Class _classType;
047: private final boolean _isNumber;
048:
049: public AbstractDataType(String name, int sqlType, Class classType,
050: boolean isNumber) {
051: _sqlType = sqlType;
052: _name = name;
053: _classType = classType;
054: _isNumber = isNumber;
055: }
056:
057: ////////////////////////////////////////////////////////////////////////////
058: // DataType class
059:
060: public int compare(Object o1, Object o2) throws TypeCastException {
061: logger.debug("compare(o1=" + o1 + ", o2=" + o2 + ") - start");
062:
063: try {
064: Comparable value1 = (Comparable) typeCast(o1);
065: Comparable value2 = (Comparable) typeCast(o2);
066:
067: if (value1 == null && value2 == null) {
068: return 0;
069: }
070:
071: if (value1 == null && value2 != null) {
072: return -1;
073: }
074:
075: if (value1 != null && value2 == null) {
076: return 1;
077: }
078:
079: return value1.compareTo(value2);
080: } catch (ClassCastException e) {
081: logger.error("compare()", e);
082:
083: throw new TypeCastException(e);
084: }
085: }
086:
087: public int getSqlType() {
088: logger.debug("getSqlType() - start");
089:
090: return _sqlType;
091: }
092:
093: public Class getTypeClass() {
094: logger.debug("getTypeClass() - start");
095:
096: return _classType;
097: }
098:
099: public boolean isNumber() {
100: logger.debug("isNumber() - start");
101:
102: return _isNumber;
103: }
104:
105: public boolean isDateTime() {
106: logger.debug("isDateTime() - start");
107:
108: return false;
109: }
110:
111: public Object getSqlValue(int column, ResultSet resultSet)
112: throws SQLException, TypeCastException {
113: logger.debug("getSqlValue(column=" + column + ", resultSet="
114: + resultSet + ") - start");
115:
116: Object value = resultSet.getObject(column);
117: if (value == null || resultSet.wasNull()) {
118: return null;
119: }
120: return value;
121: }
122:
123: public void setSqlValue(Object value, int column,
124: PreparedStatement statement) throws SQLException,
125: TypeCastException {
126: logger.debug("setSqlValue(value=" + value + ", column="
127: + column + ", statement=" + statement + ") - start");
128:
129: statement.setObject(column, typeCast(value), getSqlType());
130: }
131:
132: ////////////////////////////////////////////////////////////////////////////
133: // Object class
134:
135: public String toString() {
136: logger.debug("toString() - start");
137:
138: return _name;
139: }
140: }
|