View Javadoc

1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.dataset.datatype;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import org.dbunit.dataset.ITable;
27  
28  import java.sql.PreparedStatement;
29  import java.sql.ResultSet;
30  import java.sql.SQLException;
31  import java.sql.Types;
32  
33  /**
34   * @author Manuel Laflamme
35   * @author Last changed by: $Author: gommma $
36   * @version $Revision: 1023 $ $Date: 2009-08-03 16:08:12 -0500 (Mon, 03 Aug 2009) $
37   */
38  public class BooleanDataType extends AbstractDataType
39  {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(BooleanDataType.class);
45  
46      BooleanDataType()
47      {
48          this("BOOLEAN", Types.BOOLEAN);
49      }
50  
51      /**
52       * @param name
53       * @param sqlType
54       * @since 2.3
55       */
56      BooleanDataType(String name, int sqlType)
57      {
58          super(name, sqlType, Boolean.class, false);
59      }
60  
61      ////////////////////////////////////////////////////////////////////////////
62      // DataType class
63  
64      public Object typeCast(Object value) throws TypeCastException
65      {
66          logger.debug("typeCast(value={}) - start", value);
67  
68          if (value == null || value == ITable.NO_VALUE)
69          {
70              return null;
71          }
72  
73          if (value instanceof Boolean)
74          {
75              return value;
76          }
77  
78          if (value instanceof Number)
79          {
80              Number number = (Number)value;
81              if (number.intValue() == 0)
82                  return Boolean.FALSE;
83              else
84                  return Boolean.TRUE;
85          }
86  
87          if (value instanceof String)
88          {
89              String string = (String)value;
90  
91              if (string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false"))
92              {
93                  return Boolean.valueOf(string);
94              }
95              else
96              {
97                  return typeCast(DataType.INTEGER.typeCast(string));
98              }
99          }
100 
101         throw new TypeCastException(value, this);
102     }
103 
104 
105     protected int compareNonNulls(Object value1, Object value2) throws TypeCastException
106     {
107         logger.debug("compareNonNulls(value1={}, value2={}) - start", value1, value2);
108         
109         Boolean value1bool = (Boolean)value1;
110         Boolean value2bool = (Boolean)value2;
111         
112         if (value1bool.equals(value2bool))
113         {
114             return 0;
115         }
116 
117         if (value1bool.equals(Boolean.FALSE))
118         {
119             return -1;
120         }
121 
122         return 1;
123     }
124 
125     public Object getSqlValue(int column, ResultSet resultSet)
126             throws SQLException, TypeCastException
127     {
128     	if(logger.isDebugEnabled())
129     		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
130 
131         boolean value = resultSet.getBoolean(column);
132         if (resultSet.wasNull())
133         {
134             return null;
135         }
136         return value ? Boolean.TRUE : Boolean.FALSE;
137     }
138 
139     public void setSqlValue(Object value, int column, PreparedStatement statement)
140             throws SQLException, TypeCastException
141     {
142     	if(logger.isDebugEnabled())
143     		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
144         		new Object[]{value, new Integer(column), statement} );
145 
146     	Boolean castValue = (Boolean)typeCast(value);
147     	if(castValue==null)
148     	{
149     	    statement.setNull(column, Types.BOOLEAN);
150     	}
151     	else
152     	{
153     	    statement.setBoolean(column, castValue.booleanValue());
154     	}
155     }
156 
157 }
158 
159 
160 
161 
162