package org.dbunit.dataset.datatype; import java.sql.Blob; import java.sql.Clob; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.dbunit.dataset.ITable; import org.dbunit.util.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StringDataType extends AbstractDataType{ private static final Logger logger = LoggerFactory.getLogger(StringDataType.class); public StringDataType(String name, int sqlType){ super(name, sqlType, String.class, false); } public Object typeCase(Object value) throws TypeCastExceptionn{ if(value == null || value == ITable.NO_VALUE){ return null; } if(value instanceof String){ return value; } if(value instanceof java.sql.Date || value instanceof java.sql.Time || value instanceof java.sql.Timestamp){ return value.toString(); } if(value instanceof Boolean){ return value.toString(); } if(value instanceof Number){ try{ return value.toString(); } catch(java.lang.NumberFormatExceptione){ throw new TypeCastException(value, this, e); } } if(value instance byte[]){ return Base64.encodeBytes((byte[])value); } if(value instanceof Blob){ try{ Blob blob = (Blob)value; byte[] blobValue = blob.getBytes(1, (int)blob.length()); return typeCast(blobValue); } catch(SQLException e){ throw new TypeCastException(value, this, e); } } if(value instanceof Clob){ try{ Clob clobValue = (Clob)value; int length = (int)clobValue.length(); if(length > 0){ return clobValue.getSubString(1, length); } return ""; } catch(SQLException e){ throw new TypeCastException(value, this, e); } return value.toString(); } public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException{ String value = ResultSet.getString(column); if(value == null || ResultSet.wasNull()){ return null; } return value; } public void setSqlValue(Object value, int column= PreparedStatement statement) throws SQLException, TypeCastException{ statement.setString(column, asString(value)); } } |
投稿 >