DateTimeOffsetType.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2019, 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.ext.mssql;

  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.time.DateTimeException;
  26. import java.time.OffsetDateTime;
  27. import java.time.format.DateTimeFormatter;
  28. import java.time.format.DateTimeParseException;
  29. import java.time.temporal.TemporalAccessor;

  30. import org.dbunit.dataset.datatype.AbstractDataType;
  31. import org.dbunit.dataset.datatype.TypeCastException;

  32. /**
  33.  * @author Richard DiCroce
  34.  * @since 2.7.0
  35.  */
  36. public class DateTimeOffsetType extends AbstractDataType
  37. {
  38.     public static final int TYPE = -155;

  39.     /** @see https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetimeoffset-transact-sql?view=sql-server-2017 */
  40.     private static final DateTimeFormatter SQL_SERVER_FORMAT =
  41.             DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.n] xxx");

  42.     public DateTimeOffsetType()
  43.     {
  44.         super("datetimeoffset", TYPE, OffsetDateTime.class, false);
  45.     }

  46.     @Override
  47.     public Object typeCast(final Object value) throws TypeCastException
  48.     {
  49.         if (value == null || value instanceof OffsetDateTime)
  50.         {
  51.             return value;
  52.         }

  53.         // if a java.time type, attempt a direct conversion
  54.         // if that fails, there's not enough info to do the conversion, so don't
  55.         // bother trying string parse
  56.         if (value instanceof TemporalAccessor)
  57.         {
  58.             try
  59.             {
  60.                 return OffsetDateTime.from((TemporalAccessor) value);
  61.             } catch (final DateTimeException e)
  62.             {
  63.                 throw new TypeCastException(e);
  64.             }
  65.         }

  66.         final String valueAsString = value.toString();

  67.         // attempt to parse using ISO 8601 format
  68.         DateTimeParseException isoParseException;
  69.         try
  70.         {
  71.             return OffsetDateTime.parse(valueAsString);
  72.         } catch (final DateTimeParseException e)
  73.         {
  74.             isoParseException = e;
  75.         }

  76.         // attempt to parse using SQL Server's ISO-like format
  77.         try
  78.         {
  79.             return OffsetDateTime.parse(valueAsString, SQL_SERVER_FORMAT);
  80.         } catch (final DateTimeParseException e)
  81.         {
  82.             final TypeCastException toThrow = new TypeCastException(
  83.                     "Could not parse value using ISO 8601 or SQL Server's format",
  84.                     e);
  85.             toThrow.addSuppressed(isoParseException);
  86.             throw toThrow;
  87.         }
  88.     }

  89.     @Override
  90.     public Object getSqlValue(final int column, final ResultSet resultSet)
  91.             throws SQLException, TypeCastException
  92.     {
  93.         return resultSet.getObject(column, OffsetDateTime.class);
  94.     }

  95.     @Override
  96.     public void setSqlValue(final Object value, final int column,
  97.             final PreparedStatement statement)
  98.             throws SQLException, TypeCastException
  99.     {
  100.         statement.setObject(column, typeCast(value));
  101.     }

  102.     @Override
  103.     public boolean isDateTime()
  104.     {
  105.         return true;
  106.     }
  107. }