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: */
038: public class BooleanDataType extends AbstractDataType {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger logger = LoggerFactory
044: .getLogger(BooleanDataType.class);
045:
046: BooleanDataType() {
047: super ("BIT", Types.BIT, Boolean.class, false);
048: }
049:
050: ////////////////////////////////////////////////////////////////////////////
051: // DataType class
052:
053: public Object typeCast(Object value) throws TypeCastException {
054: logger.debug("typeCast(value=" + value + ") - start");
055:
056: if (value == null || value == ITable.NO_VALUE) {
057: return null;
058: }
059:
060: if (value instanceof Boolean) {
061: return value;
062: }
063:
064: if (value instanceof Number) {
065: Number number = (Number) value;
066: if (number.intValue() == 0)
067: return Boolean.FALSE;
068: else
069: return Boolean.TRUE;
070: }
071:
072: if (value instanceof String) {
073: String string = (String) value;
074:
075: if (string.equalsIgnoreCase("true")
076: || string.equalsIgnoreCase("false")) {
077: return Boolean.valueOf(string);
078: } else {
079: return typeCast(DataType.INTEGER.typeCast(string));
080: }
081: }
082:
083: throw new TypeCastException(value, this );
084: }
085:
086: public int compare(Object o1, Object o2) throws TypeCastException {
087: logger.debug("compare(o1=" + o1 + ", o2=" + o2 + ") - start");
088:
089: Boolean value1 = (Boolean) typeCast(o1);
090: Boolean value2 = (Boolean) typeCast(o2);
091:
092: if (value1 == null && value2 == null) {
093: return 0;
094: }
095:
096: if (value1 == null && value2 != null) {
097: return -1;
098: }
099:
100: if (value1 != null && value2 == null) {
101: return 1;
102: }
103:
104: if (value1.equals(value2)) {
105: return 0;
106: }
107:
108: if (value1.equals(Boolean.FALSE)) {
109: return -1;
110: }
111:
112: return 1;
113: }
114:
115: public Object getSqlValue(int column, ResultSet resultSet)
116: throws SQLException, TypeCastException {
117: logger.debug("getSqlValue(column=" + column + ", resultSet="
118: + resultSet + ") - start");
119:
120: boolean value = resultSet.getBoolean(column);
121: if (resultSet.wasNull()) {
122: return null;
123: }
124: return value ? Boolean.TRUE : Boolean.FALSE;
125: }
126:
127: public void setSqlValue(Object value, int column,
128: PreparedStatement statement) throws SQLException,
129: TypeCastException {
130: logger.debug("setSqlValue(value=" + value + ", column="
131: + column + ", statement=" + statement + ") - start");
132:
133: statement.setBoolean(column, ((Boolean) typeCast(value))
134: .booleanValue());
135: }
136:
137: }
|