1 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 33 public class StringDataType extends AbstractDataType 34 { 35 public StringDataType(String name, int sqlType) 36 { 37 super(name, sqlType, String .class, false); 38 } 39 40 43 public Object typeCast(Object value) throws TypeCastException 44 { 45 if (value == null || value == ITable.NO_VALUE) 46 { 47 return null; 48 } 49 50 if (value instanceof String ) 51 { 52 return value; 53 } 54 55 if (value instanceof java.sql.Date || 56 value instanceof java.sql.Time || 57 value instanceof java.sql.Timestamp ) 58 { 59 return value.toString(); 60 } 61 62 if (value instanceof Boolean ) 63 { 64 return value.toString(); 65 } 66 67 if (value instanceof Number ) 68 { 69 try 70 { 71 return value.toString(); 72 } 73 catch (java.lang.NumberFormatException 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 getSqlValue(int column, ResultSet resultSet) 120 throws SQLException, TypeCastException 121 { 122 String 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 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 |