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