1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.dataset.datatype;
23
24 import java.sql.Blob;
25 import java.sql.Clob;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29
30 import org.dbunit.dataset.ITable;
31 import org.dbunit.util.Base64;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41 public class StringDataType extends AbstractDataType
42 {
43
44
45
46
47 private static final Logger logger = LoggerFactory.getLogger(StringDataType.class);
48
49 public StringDataType(String name, int sqlType)
50 {
51 super(name, sqlType, String.class, false);
52 }
53
54
55
56
57 public Object typeCast(Object value) throws TypeCastException
58 {
59 logger.debug("typeCast(value={}) - start", value);
60
61 if (value == null || value == ITable.NO_VALUE)
62 {
63 return null;
64 }
65
66 if (value instanceof String)
67 {
68 return value;
69 }
70
71 if (value instanceof java.sql.Date ||
72 value instanceof java.sql.Time ||
73 value instanceof java.sql.Timestamp)
74 {
75 return value.toString();
76 }
77
78 if (value instanceof Boolean)
79 {
80 return value.toString();
81 }
82
83 if (value instanceof Number)
84 {
85 try
86 {
87 return value.toString();
88 }
89 catch (java.lang.NumberFormatException e)
90 {
91 throw new TypeCastException(value, this, e);
92 }
93 }
94
95 if (value instanceof byte[])
96 {
97 return Base64.encodeBytes((byte[])value);
98 }
99
100 if (value instanceof Blob)
101 {
102 try
103 {
104 Blob blob = (Blob)value;
105 byte[] blobValue = blob.getBytes(1, (int)blob.length());
106 return typeCast(blobValue);
107 }
108 catch (SQLException e)
109 {
110 throw new TypeCastException(value, this, e);
111 }
112 }
113
114 if (value instanceof Clob)
115 {
116 try
117 {
118 Clob clobValue = (Clob)value;
119 int length = (int)clobValue.length();
120 if (length > 0)
121 {
122 return clobValue.getSubString(1, length);
123 }
124 return "";
125 }
126 catch (SQLException e)
127 {
128 throw new TypeCastException(value, this, e);
129 }
130 }
131
132 logger.warn("Unknown/unsupported object type '{}' - " +
133 "will invoke toString() as last fallback which " +
134 "might produce undesired results",
135 value.getClass().getName());
136 return value.toString();
137 }
138
139 public Object getSqlValue(int column, ResultSet resultSet)
140 throws SQLException, TypeCastException
141 {
142 if (logger.isDebugEnabled())
143 logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
144
145 String value = resultSet.getString(column);
146 if (value == null || resultSet.wasNull())
147 {
148 return null;
149 }
150 return value;
151 }
152
153 public void setSqlValue(Object value, int column, PreparedStatement statement)
154 throws SQLException, TypeCastException
155 {
156 if (logger.isDebugEnabled())
157 logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
158 new Object[]{value, new Integer(column), statement} );
159
160 statement.setString(column, asString(value));
161 }
162 }
163
164
165
166
167
168
169
170
171