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.Types;
28  import java.time.LocalDateTime;
29  import java.time.format.DateTimeParseException;
30  
31  import org.dbunit.dataset.ITable;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * @author Manuel Laflamme
37   * @version $Revision$
38   * @since Feb 19, 2002
39   */
40  public class DateDataType extends AbstractDataType
41  {
42      private static final Logger logger =
43              LoggerFactory.getLogger(DateDataType.class);
44  
45      DateDataType()
46      {
47          super("DATE", Types.DATE, java.sql.Date.class, false);
48      }
49  
50      ////////////////////////////////////////////////////////////////////////////
51      // DataType class
52  
53      @Override
54      public Object typeCast(final Object value) throws TypeCastException
55      {
56          logger.debug("typeCast(value={}) - start", value);
57  
58          if (value == null || value == ITable.NO_VALUE)
59          {
60              return null;
61          }
62  
63          if (value instanceof java.sql.Date)
64          {
65              return value;
66          }
67  
68          if (value instanceof java.util.Date)
69          {
70              final java.util.Date date = (java.util.Date) value;
71              return new java.sql.Date(date.getTime());
72          }
73  
74          if (value instanceof Long)
75          {
76              final Long date = (Long) value;
77              return new java.sql.Date(date.longValue());
78          }
79  
80          if (value instanceof String)
81          {
82              final String stringValue = (String) value;
83  
84              if (isExtendedSyntax(stringValue))
85              {
86                  // Relative date.
87                  try
88                  {
89                      final LocalDateTime datetime =
90                              RELATIVE_DATE_TIME_PARSER.parse(stringValue);
91                      return java.sql.Date.valueOf(datetime.toLocalDate());
92                  } catch (IllegalArgumentException | DateTimeParseException e)
93                  {
94                      throw new TypeCastException(value, this, e);
95                  }
96              }
97  
98              // Probably a Timestamp, try it just in case!
99              if (stringValue.length() > 10)
100             {
101                 try
102                 {
103                     final long time =
104                             java.sql.Timestamp.valueOf(stringValue).getTime();
105                     return new java.sql.Date(time);
106                     // return java.sql.Date.valueOf(new
107                     // java.sql.Date(time).toString());
108                 } catch (final IllegalArgumentException e)
109                 {
110                     // Was not a Timestamp, let java.sql.Date handle this value
111                 }
112             }
113 
114             try
115             {
116                 return java.sql.Date.valueOf(stringValue);
117             } catch (final IllegalArgumentException e)
118             {
119                 throw new TypeCastException(value, this, e);
120             }
121         }
122 
123         throw new TypeCastException(value, this);
124     }
125 
126     @Override
127     public boolean isDateTime()
128     {
129         logger.debug("isDateTime() - start");
130 
131         return true;
132     }
133 
134     @Override
135     public Object getSqlValue(final int column, final ResultSet resultSet)
136             throws SQLException, TypeCastException
137     {
138         logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
139                 resultSet);
140         final java.sql.Date rawValue = resultSet.getDate(column);
141         final java.sql.Date value = resultSet.wasNull() ? null : rawValue;
142         logger.debug("getSqlValue: column={}, value={}", column, value);
143         return value;
144     }
145 
146     @Override
147     public void setSqlValue(final Object value, final int column,
148             final PreparedStatement statement)
149             throws SQLException, TypeCastException
150     {
151         logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
152                 value, column, statement);
153 
154         statement.setDate(column, (java.sql.Date) typeCast(value));
155     }
156 }