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.Timestamp;
28  import java.sql.Types;
29  import java.text.DateFormat;
30  import java.text.ParseException;
31  import java.text.SimpleDateFormat;
32  import java.util.Date;
33  
34  import org.dbunit.dataset.ITable;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * @author Manuel Laflamme
40   * @author Last changed by: $Author: jbhurst $
41   * @version $Revision: 1131 $ $Date: 2009-12-27 20:22:13 +0100 (dom, 27 dic 2009) $
42   * @since 1.0 (Feb 19, 2002)
43   */
44  public class TimestampDataType extends AbstractDataType
45  {
46  
47      /**
48       * Logger for this class
49       */
50      private static final Logger logger = LoggerFactory.getLogger(TimestampDataType.class);
51  
52      TimestampDataType()
53      {
54          super("TIMESTAMP", Types.TIMESTAMP, Timestamp.class, false);
55      }
56  
57      ////////////////////////////////////////////////////////////////////////////
58      // DataType class
59  
60      public Object typeCast(Object value) throws TypeCastException
61      {
62          logger.debug("typeCast(value={}) - start", value);
63  
64          if (value == null || value == ITable.NO_VALUE)
65          {
66              return null;
67          }
68  
69          if (value instanceof java.sql.Timestamp)
70          {
71              return value;
72          }
73  
74          if (value instanceof java.util.Date)
75          {
76              java.util.Date date = (java.util.Date)value;
77              return new java.sql.Timestamp(date.getTime());
78          }
79  
80          if (value instanceof Long)
81          {
82              Long date = (Long)value;
83              return new java.sql.Timestamp(date.longValue());
84          }
85  
86          if (value instanceof String)
87          {
88              String stringValue = (String)value;
89  
90              String[] patterns = {
91              		"yyyy-MM-dd HH:mm:ss.SSS Z",
92              		"yyyy-MM-dd HH:mm:ss.SSS",
93              		"yyyy-MM-dd HH:mm:ss Z",
94              		"yyyy-MM-dd HH:mm:ss",
95              		"yyyy-MM-dd HH:mm Z",
96              		"yyyy-MM-dd HH:mm",
97              		"yyyy-MM-dd Z",
98              		"yyyy-MM-dd"
99              };
100 
101             for (int i = 0; i < patterns.length; ++i) {
102             	String p = patterns[i];
103 	            try
104 	            {
105 	            	DateFormat df = new SimpleDateFormat(p);
106 	            	Date date = df.parse(stringValue);
107 	            	return new java.sql.Timestamp(date.getTime());
108 	            }
109 	            catch (ParseException e)
110 	            {
111 	            	if (i < patterns.length) continue;
112 	            	throw new TypeCastException(value, this, e);
113 	            }
114             }
115         }
116 
117         throw new TypeCastException(value, this);
118     }
119 
120     public boolean isDateTime()
121     {
122         logger.debug("isDateTime() - start");
123 
124         return true;
125     }
126 
127     public Object getSqlValue(int column, ResultSet resultSet)
128             throws SQLException, TypeCastException
129     {
130     	if(logger.isDebugEnabled())
131     		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
132 
133         Timestamp value = resultSet.getTimestamp(column);
134         if (value == null || resultSet.wasNull())
135         {
136             return null;
137         }
138         return value;
139     }
140 
141     public void setSqlValue(Object value, int column, PreparedStatement statement)
142             throws SQLException, TypeCastException
143     {
144     	if(logger.isDebugEnabled())
145     		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
146         		new Object[]{value, new Integer(column), statement} );
147 
148         statement.setTimestamp(column, (java.sql.Timestamp)typeCast(value));
149     }
150 }
151 
152 
153 
154 
155