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.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.sql.Time;
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   * Data type that maps a SQL {@link Types#TIME} object to a java object.
36   * 
37   * @author Manuel Laflamme
38   * @author Last changed by: $Author$
39   * @version $Revision$ $Date$
40   * @since 1.0 (Feb 19, 2002)
41   */
42  public class TimeDataType extends AbstractDataType
43  {
44  
45      /**
46       * Logger for this class
47       */
48      private static final Logger logger = LoggerFactory.getLogger(TimeDataType.class);
49  
50      TimeDataType()
51      {
52          super("TIME", Types.TIME, Time.class, false);
53      }
54  
55      ////////////////////////////////////////////////////////////////////////////
56      // DataType class
57  
58      public Object typeCast(Object value) throws TypeCastException
59      {
60          logger.debug("typeCast(value={}) - start", value);
61  
62          if (value == null || value == ITable.NO_VALUE)
63          {
64              return null;
65          }
66  
67          if (value instanceof java.sql.Time)
68          {
69              return value;
70          }
71  
72          if (value instanceof java.util.Date)
73          {
74              java.util.Date date = (java.util.Date)value;
75              return new java.sql.Time(date.getTime());
76          }
77  
78          if (value instanceof Long)
79          {
80              Long date = (Long)value;
81              return new java.sql.Time(date.longValue());
82          }
83  
84          if (value instanceof String)
85          {
86              try
87              {
88                  return java.sql.Time.valueOf((String)value);
89              }
90              catch (IllegalArgumentException e)
91              {
92                  throw new TypeCastException(value, this, e);
93              }
94          }
95  
96          throw new TypeCastException(value, this);
97      }
98  
99      public boolean isDateTime()
100     {
101         logger.debug("isDateTime() - start");
102 
103         return true;
104     }
105 
106     public Object getSqlValue(int column, ResultSet resultSet)
107             throws SQLException, TypeCastException
108     {
109     	if(logger.isDebugEnabled())
110     		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
111 
112         Time value = resultSet.getTime(column);
113         if (value == null || resultSet.wasNull())
114         {
115             return null;
116         }
117         return value;
118     }
119 
120     public void setSqlValue(Object value, int column, PreparedStatement statement)
121             throws SQLException, TypeCastException
122     {
123     	if(logger.isDebugEnabled())
124     		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
125         		new Object[]{value, new Integer(column), statement} );
126 
127         statement.setTime(column, (java.sql.Time)typeCast(value));
128     }
129 }
130 
131 
132 
133 
134