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   * @since Feb 17, 2002
38   */
39  public class NumberDataType extends AbstractDataType
40  {
41  
42      /**
43       * Logger for this class
44       */
45      private static final Logger logger = LoggerFactory.getLogger(NumberDataType.class);
46  
47      private static final Number TRUE = new BigDecimal((double)1);
48      private static final Number FALSE = new BigDecimal((double)0);
49  
50      NumberDataType(String name, int sqlType)
51      {
52          super(name, sqlType, BigDecimal.class, true);
53      }
54  
55      ////////////////////////////////////////////////////////////////////////////
56      // DataType class
57  
58      public Object typeCast(Object value) throws TypeCastException
59      {
60          logger.debug("typeCast(value={}) - start", value);
61  
62          if (value == null || value == ITable.NO_VALUE)
63          {
64              return null;
65          }
66  
67          if (value instanceof BigDecimal)
68          {
69              return value;
70          }
71  
72          if (value instanceof Boolean)
73          {
74              return ((Boolean)value).booleanValue() ? TRUE : FALSE;
75          }
76  
77          try
78          {
79              return new BigDecimal(value.toString());
80          }
81          catch (java.lang.NumberFormatException e)
82          {
83              throw new TypeCastException(value, this, e);
84          }
85      }
86  
87      public Object getSqlValue(int column, ResultSet resultSet)
88              throws SQLException, TypeCastException
89      {
90      	if(logger.isDebugEnabled())
91      		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
92  
93          BigDecimal value = resultSet.getBigDecimal(column);
94          if (value == null || resultSet.wasNull())
95          {
96              return null;
97          }
98          return value;
99      }
100 
101     public void setSqlValue(Object value, int column, PreparedStatement statement)
102             throws SQLException, TypeCastException
103     {
104     	if(logger.isDebugEnabled())
105     		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
106     				new Object[]{value, new Integer(column), statement} );
107 
108         statement.setBigDecimal(column, (BigDecimal)typeCast(value));
109     }
110 }
111 
112 
113 
114 
115 
116