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  import java.time.LocalDateTime;
30  import java.time.format.DateTimeParseException;
31  
32  import org.dbunit.dataset.ITable;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Data type that maps a SQL {@link Types#TIME} object to a java object.
38   *
39   * @author Manuel Laflamme
40   * @author Last changed by: $Author$
41   * @version $Revision$ $Date$
42   * @since 1.0 (Feb 19, 2002)
43   */
44  public class TimeDataType extends AbstractDataType
45  {
46      private static final Logger logger =
47              LoggerFactory.getLogger(TimeDataType.class);
48  
49      TimeDataType()
50      {
51          super("TIME", Types.TIME, Time.class, false);
52      }
53  
54      ////////////////////////////////////////////////////////////////////////////
55      // DataType class
56  
57      @Override
58      public Object typeCast(final 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              final 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              final Long date = (Long) value;
81              return new java.sql.Time(date);
82          }
83  
84          if (value instanceof String)
85          {
86              final String stringValue = (String) value;
87  
88              if (isExtendedSyntax(stringValue))
89              {
90                  // Relative date.
91                  try
92                  {
93                      final LocalDateTime datetime =
94                              RELATIVE_DATE_TIME_PARSER.parse(stringValue);
95                      return java.sql.Time.valueOf(datetime.toLocalTime());
96                  } catch (IllegalArgumentException | DateTimeParseException e)
97                  {
98                      throw new TypeCastException(value, this, e);
99                  }
100             }
101 
102             try
103             {
104                 return java.sql.Time.valueOf(stringValue);
105             } catch (final IllegalArgumentException e)
106             {
107                 throw new TypeCastException(value, this, e);
108             }
109         }
110 
111         throw new TypeCastException(value, this);
112     }
113 
114     @Override
115     public boolean isDateTime()
116     {
117         logger.debug("isDateTime() - start");
118 
119         return true;
120     }
121 
122     @Override
123     public Object getSqlValue(final int column, final ResultSet resultSet)
124             throws SQLException, TypeCastException
125     {
126         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
127                 resultSet);
128         final Time rawValue = resultSet.getTime(column);
129         final Time value = resultSet.wasNull() ? null : rawValue;
130         logger.debug("getSqlValue: column={}, value={}", column, value);
131         return value;
132     }
133 
134     @Override
135     public void setSqlValue(final Object value, final int column,
136             final PreparedStatement statement)
137             throws SQLException, TypeCastException
138     {
139         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
140                 value, column, statement);
141 
142         statement.setTime(column, (java.sql.Time) typeCast(value));
143     }
144 }