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  
22  package org.dbunit.dataset.datatype;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import org.dbunit.dataset.ITable;
28  
29  import java.math.BigDecimal;
30  import java.sql.PreparedStatement;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  
34  /**
35   * @author Manuel Laflamme
36   * @version $Revision$
37   */
38  public class DoubleDataType extends AbstractDataType
39  {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(DoubleDataType.class);
45  
46      DoubleDataType(String name, int sqlType)
47      {
48          super(name, sqlType, Double.class, true);
49      }
50  
51      /////////////////////////////////////////////////////////////////////////////
52      //  DataType methods
53  
54      public Object typeCast(Object value) throws TypeCastException
55      {
56          logger.debug("typeCast(value={}) - start", value);
57  
58          if (value == null || value == ITable.NO_VALUE)
59          {
60              return null;
61          }
62  
63          if (value instanceof Number)
64          {
65              return new Double(((Number)value).doubleValue());
66          }
67  
68          try
69          {
70              return typeCast(new BigDecimal(value.toString()));
71          }
72          catch (java.lang.NumberFormatException e)
73          {
74              throw new TypeCastException(value, this, e);
75          }
76      }
77  
78      public Object getSqlValue(int column, ResultSet resultSet)
79              throws SQLException, TypeCastException
80      {
81      	if(logger.isDebugEnabled())
82      		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
83  
84          double value = resultSet.getDouble(column);
85          if (resultSet.wasNull())
86          {
87              return null;
88          }
89          return new Double(value);
90      }
91  
92      public void setSqlValue(Object value, int column, PreparedStatement statement)
93              throws SQLException, TypeCastException
94      {
95      	if(logger.isDebugEnabled())
96      		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
97          		new Object[]{value, new Integer(column), statement} );
98  
99          statement.setDouble(column, ((Number)typeCast(value)).doubleValue());
100     }
101 
102 }
103 
104 
105 
106 
107