KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > TimestampDataType


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 org.dbunit.dataset.ITable;
25
26 import java.sql.*;
27
28 /**
29  * @author Manuel Laflamme
30  * @version $Revision: 1.17 $
31  * @since Feb 19, 2002
32  */

33 public class TimestampDataType extends AbstractDataType
34 {
35     TimestampDataType()
36     {
37         super("TIMESTAMP", Types.TIMESTAMP, Timestamp.class, false);
38     }
39
40     ////////////////////////////////////////////////////////////////////////////
41
// DataType class
42

43     public Object JavaDoc typeCast(Object JavaDoc value) throws TypeCastException
44     {
45         if (value == null || value == ITable.NO_VALUE)
46         {
47             return null;
48         }
49
50         if (value instanceof java.sql.Timestamp JavaDoc)
51         {
52             return value;
53         }
54
55         if (value instanceof java.util.Date JavaDoc)
56         {
57             java.util.Date JavaDoc date = (java.util.Date JavaDoc)value;
58             return new java.sql.Timestamp JavaDoc(date.getTime());
59         }
60
61         if (value instanceof Long JavaDoc)
62         {
63             Long JavaDoc date = (Long JavaDoc)value;
64             return new java.sql.Timestamp JavaDoc(date.longValue());
65         }
66
67         if (value instanceof String JavaDoc)
68         {
69             String JavaDoc stringValue = (String JavaDoc)value;
70
71             // Probably a java.sql.Date, try it just in case!
72
if (stringValue.length() == 10)
73             {
74                 try
75                 {
76                     long time = java.sql.Date.valueOf(stringValue).getTime();
77                     return new java.sql.Timestamp JavaDoc(time);
78                 }
79                 catch (IllegalArgumentException JavaDoc e)
80                 {
81                     // Was not a java.sql.Date, let Timestamp handle this value
82
}
83             }
84
85             try
86             {
87                 return java.sql.Timestamp.valueOf(stringValue);
88             }
89             catch (IllegalArgumentException JavaDoc e)
90             {
91                 throw new TypeCastException(value, this, e);
92             }
93         }
94
95         throw new TypeCastException(value, this);
96     }
97
98     public boolean isDateTime()
99     {
100         return true;
101     }
102
103     public Object JavaDoc getSqlValue(int column, ResultSet resultSet)
104             throws SQLException, TypeCastException
105     {
106         Timestamp value = resultSet.getTimestamp(column);
107         if (value == null || resultSet.wasNull())
108         {
109             return null;
110         }
111         return value;
112     }
113
114     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement statement)
115             throws SQLException, TypeCastException
116     {
117         statement.setTimestamp(column, (java.sql.Timestamp JavaDoc)typeCast(value));
118     }
119 }
120
121
122
123
124
125
Popular Tags