BigIntegerDataType.java

  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. package org.dbunit.dataset.datatype;

  22. import java.math.BigDecimal;
  23. import java.math.BigInteger;
  24. import java.sql.PreparedStatement;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.sql.Types;

  28. import org.dbunit.dataset.ITable;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * Inserts and reads {@link BigInteger} values into/from a database.
  33.  *
  34.  * @author gommma (gommma AT users.sourceforge.net)
  35.  * @author Last changed by: $Author$
  36.  * @version $Revision$ $Date$
  37.  * @since 2.4.6
  38.  */
  39. public class BigIntegerDataType extends AbstractDataType
  40. {
  41.     private static final Logger logger =
  42.             LoggerFactory.getLogger(BigIntegerDataType.class);

  43.     public BigIntegerDataType()
  44.     {
  45.         super("BIGINT", Types.BIGINT, BigInteger.class, true);
  46.     }

  47.     // //////////////////////////////////////////////////////////////////////////
  48.     // DataType class

  49.     @Override
  50.     public Object typeCast(final Object value) throws TypeCastException
  51.     {
  52.         logger.debug("typeCast(value={}) - start", value);

  53.         if (value == null || value == ITable.NO_VALUE)
  54.         {
  55.             return null;
  56.         }

  57.         if (value instanceof BigInteger)
  58.         {
  59.             return value;
  60.         } else if (value instanceof BigDecimal)
  61.         {
  62.             return ((BigDecimal) value).toBigInteger();
  63.         } else if (value instanceof Number)
  64.         {
  65.             final long l = ((Number) value).longValue();
  66.             return new BigInteger(String.valueOf(l));
  67.         }

  68.         try
  69.         {
  70.             final BigDecimal bd = new BigDecimal(value.toString());
  71.             return bd.toBigInteger();
  72.         } catch (final java.lang.NumberFormatException e)
  73.         {
  74.             throw new TypeCastException(value, this, e);
  75.         }
  76.     }

  77.     @Override
  78.     public Object getSqlValue(final int column, final ResultSet resultSet)
  79.             throws SQLException, TypeCastException
  80.     {
  81.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
  82.                 resultSet);
  83.         final BigDecimal rawValue = resultSet.getBigDecimal(column);
  84.         final BigInteger value =
  85.                 resultSet.wasNull() ? null : rawValue.toBigInteger();
  86.         logger.debug("getSqlValue: column={}, value={}", column, value);
  87.         return value;
  88.     }

  89.     @Override
  90.     public void setSqlValue(final Object value, final int column,
  91.             final PreparedStatement statement)
  92.             throws SQLException, TypeCastException
  93.     {
  94.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  95.                 value, column, statement);

  96.         final BigInteger val = (BigInteger) typeCast(value);
  97.         final BigDecimal valueBigDecimal =
  98.                 (val == null) ? null : new BigDecimal(val);
  99.         statement.setBigDecimal(column, valueBigDecimal);
  100.     }
  101. }