TimeDataType.java

  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. package org.dbunit.dataset.datatype;

  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Time;
  26. import java.sql.Types;
  27. import java.time.LocalDateTime;
  28. import java.time.format.DateTimeParseException;

  29. import org.dbunit.dataset.ITable;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;

  32. /**
  33.  * Data type that maps a SQL {@link Types#TIME} object to a java object.
  34.  *
  35.  * @author Manuel Laflamme
  36.  * @author Last changed by: $Author$
  37.  * @version $Revision$ $Date$
  38.  * @since 1.0 (Feb 19, 2002)
  39.  */
  40. public class TimeDataType extends AbstractDataType
  41. {
  42.     private static final Logger logger =
  43.             LoggerFactory.getLogger(TimeDataType.class);

  44.     TimeDataType()
  45.     {
  46.         super("TIME", Types.TIME, Time.class, false);
  47.     }

  48.     ////////////////////////////////////////////////////////////////////////////
  49.     // DataType class

  50.     @Override
  51.     public Object typeCast(final Object value) throws TypeCastException
  52.     {
  53.         logger.debug("typeCast(value={}) - start", value);

  54.         if (value == null || value == ITable.NO_VALUE)
  55.         {
  56.             return null;
  57.         }

  58.         if (value instanceof java.sql.Time)
  59.         {
  60.             return value;
  61.         }

  62.         if (value instanceof java.util.Date)
  63.         {
  64.             final java.util.Date date = (java.util.Date) value;
  65.             return new java.sql.Time(date.getTime());
  66.         }

  67.         if (value instanceof Long)
  68.         {
  69.             final Long date = (Long) value;
  70.             return new java.sql.Time(date);
  71.         }

  72.         if (value instanceof String)
  73.         {
  74.             final String stringValue = (String) value;

  75.             if (isExtendedSyntax(stringValue))
  76.             {
  77.                 // Relative date.
  78.                 try
  79.                 {
  80.                     final LocalDateTime datetime =
  81.                             RELATIVE_DATE_TIME_PARSER.parse(stringValue);
  82.                     return java.sql.Time.valueOf(datetime.toLocalTime());
  83.                 } catch (IllegalArgumentException | DateTimeParseException e)
  84.                 {
  85.                     throw new TypeCastException(value, this, e);
  86.                 }
  87.             }

  88.             try
  89.             {
  90.                 return java.sql.Time.valueOf(stringValue);
  91.             } catch (final IllegalArgumentException e)
  92.             {
  93.                 throw new TypeCastException(value, this, e);
  94.             }
  95.         }

  96.         throw new TypeCastException(value, this);
  97.     }

  98.     @Override
  99.     public boolean isDateTime()
  100.     {
  101.         logger.debug("isDateTime() - start");

  102.         return true;
  103.     }

  104.     @Override
  105.     public Object getSqlValue(final int column, final ResultSet resultSet)
  106.             throws SQLException, TypeCastException
  107.     {
  108.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
  109.                 resultSet);
  110.         final Time rawValue = resultSet.getTime(column);
  111.         final Time value = resultSet.wasNull() ? null : rawValue;
  112.         logger.debug("getSqlValue: column={}, value={}", column, value);
  113.         return value;
  114.     }

  115.     @Override
  116.     public void setSqlValue(final Object value, final int column,
  117.             final PreparedStatement statement)
  118.             throws SQLException, TypeCastException
  119.     {
  120.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  121.                 value, column, statement);

  122.         statement.setTime(column, (java.sql.Time) typeCast(value));
  123.     }
  124. }