View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2009, 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.math.BigInteger;
26  import java.sql.PreparedStatement;
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  import java.sql.Types;
30  
31  import org.dbunit.dataset.ITable;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Inserts and reads {@link BigInteger} values into/from a database.
37   * 
38   * @author gommma (gommma AT users.sourceforge.net)
39   * @author Last changed by: $Author$
40   * @version $Revision$ $Date$
41   * @since 2.4.6
42   */
43  public class BigIntegerDataType extends AbstractDataType
44  {
45  
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger = LoggerFactory
50              .getLogger(BigIntegerDataType.class);
51  
52      public BigIntegerDataType()
53      {
54          super("BIGINT", Types.BIGINT, BigInteger.class, true);
55      }
56  
57      // //////////////////////////////////////////////////////////////////////////
58      // DataType class
59  
60      @Override
61      public Object typeCast(Object value) throws TypeCastException
62      {
63          logger.debug("typeCast(value={}) - start", value);
64  
65          if (value == null || value == ITable.NO_VALUE)
66          {
67              return null;
68          }
69  
70          if (value instanceof BigInteger)
71          {
72              return value;
73          } else if (value instanceof BigDecimal)
74          {
75              return ((BigDecimal) value).toBigInteger();
76          } else if (value instanceof Number)
77          {
78              long l = ((Number) value).longValue();
79              return new BigInteger(String.valueOf(l));
80          }
81  
82          try
83          {
84              BigDecimal bd = new BigDecimal(value.toString());
85              return bd.toBigInteger();
86          } catch (java.lang.NumberFormatException e)
87          {
88              throw new TypeCastException(value, this, e);
89          }
90      }
91  
92      @Override
93      public Object getSqlValue(int column, ResultSet resultSet)
94              throws SQLException, TypeCastException
95      {
96          if (logger.isDebugEnabled())
97          {
98              logger.debug("getSqlValue(column={}, resultSet={}) - start",
99                      new Integer(column), resultSet);
100         }
101 
102         BigDecimal value = resultSet.getBigDecimal(column);
103         if (resultSet.wasNull())
104         {
105             return null;
106         }
107         return value.toBigInteger();
108     }
109 
110     @Override
111     public void setSqlValue(Object value, int column,
112             PreparedStatement statement) throws SQLException, TypeCastException
113     {
114         if (logger.isDebugEnabled())
115         {
116             logger.debug(
117                     "setSqlValue(value={}, column={}, statement={}) - start",
118                     new Object[] {value, new Integer(column), statement});
119         }
120 
121         BigInteger val = (BigInteger) typeCast(value);
122         BigDecimal valueBigDecimal = (val == null) ? null : new BigDecimal(val);
123         statement.setBigDecimal(column, valueBigDecimal);
124     }
125 }