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