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 private static final Logger logger =
44 LoggerFactory.getLogger(StringDataType.class);
45
46 public StringDataType(final String name, final int sqlType)
47 {
48 super(name, sqlType, String.class, false);
49 }
50
51
52
53
54 @Override
55 public Object typeCast(final Object value) throws TypeCastException
56 {
57 logger.debug("typeCast(value={}) - start", value);
58
59 if (value == null || value == ITable.NO_VALUE)
60 {
61 return null;
62 }
63
64 if (value instanceof String)
65 {
66 return value;
67 }
68
69 if (value instanceof java.sql.Date || value instanceof java.sql.Time
70 || value instanceof java.sql.Timestamp
71 || value instanceof java.time.temporal.TemporalAccessor)
72 {
73 return value.toString();
74 }
75
76 if (value instanceof Boolean)
77 {
78 return value.toString();
79 }
80
81 if (value instanceof Number)
82 {
83 try
84 {
85 return value.toString();
86 } catch (final java.lang.NumberFormatException e)
87 {
88 throw new TypeCastException(value, this, e);
89 }
90 }
91
92 if (value instanceof byte[])
93 {
94 return Base64.encodeBytes((byte[]) value);
95 }
96
97 if (value instanceof Blob)
98 {
99 try
100 {
101 final Blob blob = (Blob) value;
102 final byte[] blobValue = blob.getBytes(1, (int) blob.length());
103 return typeCast(blobValue);
104 } catch (final SQLException e)
105 {
106 throw new TypeCastException(value, this, e);
107 }
108 }
109
110 if (value instanceof Clob)
111 {
112 try
113 {
114 final Clob clobValue = (Clob) value;
115 final int length = (int) clobValue.length();
116 if (length > 0)
117 {
118 return clobValue.getSubString(1, length);
119 }
120 return "";
121 } catch (final SQLException e)
122 {
123 throw new TypeCastException(value, this, e);
124 }
125 }
126
127 logger.warn(
128 "Unknown/unsupported object type '{}' - "
129 + "will invoke toString() as last fallback which "
130 + "might produce undesired results",
131 value.getClass().getName());
132 return value.toString();
133 }
134
135 @Override
136 public Object getSqlValue(final int column, final ResultSet resultSet)
137 throws SQLException, TypeCastException
138 {
139 logger.debug("getSqlValue(column={}, resultSet={}) - start", column,
140 resultSet);
141 final String rawValue = resultSet.getString(column);
142 final String value = resultSet.wasNull() ? null : rawValue;
143 logger.debug("getSqlValue: column={}, value={}", column, value);
144 return value;
145 }
146
147 @Override
148 public void setSqlValue(final Object value, final int column,
149 final PreparedStatement statement)
150 throws SQLException, TypeCastException
151 {
152 logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
153 value, column, statement);
154
155 statement.setString(column, asString(value));
156 }
157 }