KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.dbunit.util.Base64;
26
27 import java.sql.*;
28
29 /**
30  * @author Manuel Laflamme
31  * @version $Revision: 1.24 $
32  */

33 public class StringDataType extends AbstractDataType
34 {
35     public StringDataType(String JavaDoc name, int sqlType)
36     {
37         super(name, sqlType, String JavaDoc.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 String JavaDoc)
51         {
52             return value;
53         }
54
55         if (value instanceof java.sql.Date JavaDoc ||
56                 value instanceof java.sql.Time JavaDoc ||
57                 value instanceof java.sql.Timestamp JavaDoc)
58         {
59             return value.toString();
60         }
61
62         if (value instanceof Boolean JavaDoc)
63         {
64             return value.toString();
65         }
66
67         if (value instanceof Number JavaDoc)
68         {
69             try
70             {
71                 return value.toString();
72             }
73             catch (java.lang.NumberFormatException JavaDoc e)
74             {
75                 throw new TypeCastException(value, this, e);
76             }
77         }
78
79         if (value instanceof byte[])
80         {
81             return Base64.encodeBytes((byte[])value);
82         }
83
84         if (value instanceof Blob)
85         {
86             try
87             {
88                 Blob blob = (Blob)value;
89                 byte[] blobValue = blob.getBytes(1, (int)blob.length());
90                 return typeCast(blobValue);
91             }
92             catch (SQLException e)
93             {
94                 throw new TypeCastException(value, this, e);
95             }
96         }
97
98         if (value instanceof Clob)
99         {
100             try
101             {
102                 Clob clobValue = (Clob)value;
103                 int length = (int)clobValue.length();
104                 if (length > 0)
105                 {
106                     return clobValue.getSubString(1, length);
107                 }
108                 return "";
109             }
110             catch (SQLException e)
111             {
112                 throw new TypeCastException(value, this, e);
113             }
114         }
115
116         throw new TypeCastException(value, this);
117     }
118
119     public Object JavaDoc getSqlValue(int column, ResultSet resultSet)
120             throws SQLException, TypeCastException
121     {
122         String JavaDoc value = resultSet.getString(column);
123         if (value == null || resultSet.wasNull())
124         {
125             return null;
126         }
127         return value;
128     }
129
130     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement statement)
131             throws SQLException, TypeCastException
132     {
133         statement.setString(column, asString(value));
134     }
135 }
136
137
138
139
140
141
142
143
144
145
Popular Tags