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.math.BigDecimal;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033:
034: /**
035: * @author Manuel Laflamme
036: * @version $Revision: 554 $
037: * @since Feb 17, 2002
038: */
039: public class NumberDataType extends AbstractDataType {
040:
041: /**
042: * Logger for this class
043: */
044: private static final Logger logger = LoggerFactory
045: .getLogger(NumberDataType.class);
046:
047: private static final Number TRUE = new BigDecimal(1);
048: private static final Number FALSE = new BigDecimal(0);
049:
050: NumberDataType(String name, int sqlType) {
051: super (name, sqlType, BigDecimal.class, true);
052: }
053:
054: ////////////////////////////////////////////////////////////////////////////
055: // DataType class
056:
057: public Object typeCast(Object value) throws TypeCastException {
058: logger.debug("typeCast(value=" + value + ") - start");
059:
060: if (value == null || value == ITable.NO_VALUE) {
061: return null;
062: }
063:
064: if (value instanceof BigDecimal) {
065: return value;
066: }
067:
068: if (value instanceof Boolean) {
069: return ((Boolean) value).booleanValue() ? TRUE : FALSE;
070: }
071:
072: try {
073: return new BigDecimal(value.toString());
074: } catch (java.lang.NumberFormatException e) {
075: logger.error("typeCast()", e);
076:
077: throw new TypeCastException(value, this , e);
078: }
079: }
080:
081: public Object getSqlValue(int column, ResultSet resultSet)
082: throws SQLException, TypeCastException {
083: logger.debug("getSqlValue(column=" + column + ", resultSet="
084: + resultSet + ") - start");
085:
086: BigDecimal value = resultSet.getBigDecimal(column);
087: if (value == null || resultSet.wasNull()) {
088: return null;
089: }
090: return value;
091: }
092:
093: public void setSqlValue(Object value, int column,
094: PreparedStatement statement) throws SQLException,
095: TypeCastException {
096: logger.debug("setSqlValue(value=" + value + ", column="
097: + column + ", statement=" + statement + ") - start");
098:
099: statement.setBigDecimal(column, (BigDecimal) typeCast(value));
100: }
101: }
|