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