DateDataType.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.Types;
  26. import java.time.LocalDateTime;
  27. import java.time.format.DateTimeParseException;

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

  31. /**
  32.  * @author Manuel Laflamme
  33.  * @version $Revision$
  34.  * @since Feb 19, 2002
  35.  */
  36. public class DateDataType extends AbstractDataType
  37. {
  38.     private static final Logger logger =
  39.             LoggerFactory.getLogger(DateDataType.class);

  40.     DateDataType()
  41.     {
  42.         super("DATE", Types.DATE, java.sql.Date.class, false);
  43.     }

  44.     ////////////////////////////////////////////////////////////////////////////
  45.     // DataType class

  46.     @Override
  47.     public Object typeCast(final Object value) throws TypeCastException
  48.     {
  49.         logger.debug("typeCast(value={}) - start", value);

  50.         if (value == null || value == ITable.NO_VALUE)
  51.         {
  52.             return null;
  53.         }

  54.         if (value instanceof java.sql.Date)
  55.         {
  56.             return value;
  57.         }

  58.         if (value instanceof java.util.Date)
  59.         {
  60.             final java.util.Date date = (java.util.Date) value;
  61.             return new java.sql.Date(date.getTime());
  62.         }

  63.         if (value instanceof Long)
  64.         {
  65.             final Long date = (Long) value;
  66.             return new java.sql.Date(date.longValue());
  67.         }

  68.         if (value instanceof String)
  69.         {
  70.             final String stringValue = (String) value;

  71.             if (isExtendedSyntax(stringValue))
  72.             {
  73.                 // Relative date.
  74.                 try
  75.                 {
  76.                     final LocalDateTime datetime =
  77.                             RELATIVE_DATE_TIME_PARSER.parse(stringValue);
  78.                     return java.sql.Date.valueOf(datetime.toLocalDate());
  79.                 } catch (IllegalArgumentException | DateTimeParseException e)
  80.                 {
  81.                     throw new TypeCastException(value, this, e);
  82.                 }
  83.             }

  84.             // Probably a Timestamp, try it just in case!
  85.             if (stringValue.length() > 10)
  86.             {
  87.                 try
  88.                 {
  89.                     final long time =
  90.                             java.sql.Timestamp.valueOf(stringValue).getTime();
  91.                     return new java.sql.Date(time);
  92.                     // return java.sql.Date.valueOf(new
  93.                     // java.sql.Date(time).toString());
  94.                 } catch (final IllegalArgumentException e)
  95.                 {
  96.                     // Was not a Timestamp, let java.sql.Date handle this value
  97.                 }
  98.             }

  99.             try
  100.             {
  101.                 return java.sql.Date.valueOf(stringValue);
  102.             } catch (final IllegalArgumentException e)
  103.             {
  104.                 throw new TypeCastException(value, this, e);
  105.             }
  106.         }

  107.         throw new TypeCastException(value, this);
  108.     }

  109.     @Override
  110.     public boolean isDateTime()
  111.     {
  112.         logger.debug("isDateTime() - start");

  113.         return true;
  114.     }

  115.     @Override
  116.     public Object getSqlValue(final int column, final ResultSet resultSet)
  117.             throws SQLException, TypeCastException
  118.     {
  119.         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
  120.                 resultSet);
  121.         final java.sql.Date rawValue = resultSet.getDate(column);
  122.         final java.sql.Date value = resultSet.wasNull() ? null : rawValue;
  123.         logger.debug("getSqlValue: column={}, value={}", column, value);
  124.         return value;
  125.     }

  126.     @Override
  127.     public void setSqlValue(final Object value, final int column,
  128.             final PreparedStatement statement)
  129.             throws SQLException, TypeCastException
  130.     {
  131.         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
  132.                 value, column, statement);

  133.         statement.setDate(column, (java.sql.Date) typeCast(value));
  134.     }
  135. }