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      private static final Logger logger =
46              LoggerFactory.getLogger(BigIntegerDataType.class);
47  
48      public BigIntegerDataType()
49      {
50          super("BIGINT", Types.BIGINT, BigInteger.class, true);
51      }
52  
53      // //////////////////////////////////////////////////////////////////////////
54      // DataType class
55  
56      @Override
57      public Object typeCast(final Object value) throws TypeCastException
58      {
59          logger.debug("typeCast(value={}) - start", value);
60  
61          if (value == null || value == ITable.NO_VALUE)
62          {
63              return null;
64          }
65  
66          if (value instanceof BigInteger)
67          {
68              return value;
69          } else if (value instanceof BigDecimal)
70          {
71              return ((BigDecimal) value).toBigInteger();
72          } else if (value instanceof Number)
73          {
74              final long l = ((Number) value).longValue();
75              return new BigInteger(String.valueOf(l));
76          }
77  
78          try
79          {
80              final BigDecimal bd = new BigDecimal(value.toString());
81              return bd.toBigInteger();
82          } catch (final java.lang.NumberFormatException e)
83          {
84              throw new TypeCastException(value, this, e);
85          }
86      }
87  
88      @Override
89      public Object getSqlValue(final int column, final ResultSet resultSet)
90              throws SQLException, TypeCastException
91      {
92          logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
93                  resultSet);
94          final BigDecimal rawValue = resultSet.getBigDecimal(column);
95          final BigInteger value =
96                  resultSet.wasNull() ? null : rawValue.toBigInteger();
97          logger.debug("getSqlValue: column={}, value={}", column, value);
98          return value;
99      }
100 
101     @Override
102     public void setSqlValue(final Object value, final int column,
103             final PreparedStatement statement)
104             throws SQLException, TypeCastException
105     {
106         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
107                 value, column, statement);
108 
109         final BigInteger val = (BigInteger) typeCast(value);
110         final BigDecimal valueBigDecimal =
111                 (val == null) ? null : new BigDecimal(val);
112         statement.setBigDecimal(column, valueBigDecimal);
113     }
114 }