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 org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import org.dbunit.dataset.ITable;
28  
29  import java.math.BigDecimal;
30  import java.sql.PreparedStatement;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  
34  /**
35   * @author Manuel Laflamme
36   * @version $Revision$
37   */
38  public class IntegerDataType extends AbstractDataType
39  {
40      private static final Logger logger = LoggerFactory.getLogger(IntegerDataType.class);
41  
42      IntegerDataType(String name, int sqlType)
43      {
44          super(name, sqlType, Integer.class, true);
45      }
46  
47      ////////////////////////////////////////////////////////////////////////////
48      // DataType class
49  
50      public Object typeCast(Object value) throws TypeCastException
51      {
52          logger.debug("typeCast(value={}) - start", value);
53  
54          if (value == null || value == ITable.NO_VALUE)
55          {
56              return null;
57          }
58  
59          if (value instanceof Number)
60          {
61              return new Integer(((Number)value).intValue());
62          }
63  
64          // Treat "false" as 0, "true" as 1
65          if (value instanceof String)
66          {
67              String string = (String) value;
68  
69              if ("false".equalsIgnoreCase(string))
70              {
71                  return new Integer(0);
72              }
73  
74              if ("true".equalsIgnoreCase(string))
75              {
76                  return new Integer(1);
77              }
78          }
79  
80          // Bugfix in release 2.4.6
81          String stringValue = value.toString().trim();
82          if(stringValue.length()<=0)
83          {
84              return null;
85          }
86          
87          try
88          {
89              return typeCast(new BigDecimal(stringValue));
90          }
91          catch (java.lang.NumberFormatException e)
92          {
93              throw new TypeCastException(value, this, e);
94          }
95      }
96  
97      public Object getSqlValue(int column, ResultSet resultSet)
98              throws SQLException, TypeCastException
99      {
100     	if(logger.isDebugEnabled())
101     		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
102 
103         int value = resultSet.getInt(column);
104         if (resultSet.wasNull())
105         {
106             return null;
107         }
108         return new Integer(value);
109     }
110 
111     public void setSqlValue(Object value, int column, PreparedStatement statement)
112             throws SQLException, TypeCastException
113     {
114     	if(logger.isDebugEnabled())
115     		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
116         		new Object[]{value, new Integer(column), statement} );
117 
118         statement.setInt(column, ((Integer)typeCast(value)).intValue());
119     }
120 }
121