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 java.math.BigDecimal;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  import java.sql.Types;
29  
30  import org.dbunit.dataset.ITable;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * @author Manuel Laflamme
36   * @version $Revision$
37   */
38  public class LongDataType extends AbstractDataType
39  {
40      private static final Logger logger =
41              LoggerFactory.getLogger(LongDataType.class);
42  
43      LongDataType()
44      {
45          super("BIGINT", Types.BIGINT, Long.class, true);
46      }
47  
48      ////////////////////////////////////////////////////////////////////////////
49      // DataType class
50  
51      @Override
52      public Object typeCast(final Object value) throws TypeCastException
53      {
54          logger.debug("typeCast(value={}) - start", value);
55  
56          if (value == null || value == ITable.NO_VALUE)
57          {
58              return null;
59          }
60  
61          if (value instanceof Number)
62          {
63              return ((Number) value).longValue();
64          }
65  
66          try
67          {
68              return typeCast(new BigDecimal(value.toString()));
69          } catch (final java.lang.NumberFormatException e)
70          {
71              throw new TypeCastException(value, this, e);
72          }
73      }
74  
75      @Override
76      public Object getSqlValue(final int column, final ResultSet resultSet)
77              throws SQLException, TypeCastException
78      {
79          logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
80                  resultSet);
81          final long rawValue = resultSet.getLong(column);
82          final Long value = resultSet.wasNull() ? null : rawValue;
83          logger.debug("getSqlValue: column={}, value={}", column, value);
84          return value;
85      }
86  
87      @Override
88      public void setSqlValue(final Object value, final int column,
89              final PreparedStatement statement)
90              throws SQLException, TypeCastException
91      {
92          logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
93                  value, column, statement);
94  
95          statement.setLong(column, ((Number) typeCast(value)).longValue());
96      }
97  }