1 21 22 package org.dbunit.dataset.datatype; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Types ; 28 29 30 34 public abstract class DataType 35 { 36 public static final DataType UNKNOWN = new UnknownDataType(); 37 38 public static final DataType CHAR = new StringDataType( 39 "CHAR", Types.CHAR); 40 public static final DataType VARCHAR = new StringDataType( 41 "VARCHAR", Types.VARCHAR); 42 public static final DataType LONGVARCHAR = new StringDataType( 43 "LONGVARCHAR", Types.LONGVARCHAR); 44 public static final DataType CLOB = new ClobDataType(); 45 46 public static final DataType NUMERIC = new NumberDataType( 47 "NUMERIC", Types.NUMERIC); 48 public static final DataType DECIMAL = new NumberDataType( 49 "DECIMAL", Types.DECIMAL); 50 51 public static final DataType BOOLEAN = new BooleanDataType(); 52 53 public static final DataType TINYINT = new IntegerDataType( 54 "TINYINT", Types.TINYINT); 55 public static final DataType SMALLINT = new IntegerDataType( 56 "SMALLINT", Types.SMALLINT); 57 public static final DataType INTEGER = new IntegerDataType( 58 "INTEGER", Types.INTEGER); 59 60 public static final DataType BIGINT = new LongDataType(); 61 62 public static final DataType REAL = new FloatDataType(); 63 64 public static final DataType FLOAT = new DoubleDataType( 65 "FLOAT", Types.FLOAT); 66 public static final DataType DOUBLE = new DoubleDataType( 67 "DOUBLE", Types.DOUBLE); 68 69 public static final DataType DATE = new DateDataType(); 70 public static final DataType TIME = new TimeDataType(); 71 public static final DataType TIMESTAMP = new TimestampDataType(); 72 73 public static final DataType BINARY = new BytesDataType( 74 "BINARY", Types.BINARY); 75 public static final DataType VARBINARY = new BytesDataType( 76 "VARBINARY", Types.VARBINARY); 77 public static final DataType LONGVARBINARY = new BytesDataType( 78 "LONGVARBINARY", Types.LONGVARBINARY); 79 public static final DataType BLOB = new BlobDataType(); 80 81 private static final DataType[] TYPES = { 82 VARCHAR, CHAR, LONGVARCHAR, CLOB, NUMERIC, DECIMAL, BOOLEAN, INTEGER, 83 TINYINT, SMALLINT, BIGINT, REAL, DOUBLE, FLOAT, DATE, TIME, TIMESTAMP, 84 VARBINARY, BINARY, LONGVARBINARY, BLOB, 85 }; 86 87 90 public abstract Object typeCast(Object value) throws TypeCastException; 91 92 101 public abstract int compare(Object o1, Object o2) throws TypeCastException; 102 103 106 public abstract int getSqlType(); 107 108 111 public abstract Class getTypeClass(); 112 113 117 public abstract boolean isNumber(); 118 119 123 public abstract boolean isDateTime(); 124 125 128 public abstract Object getSqlValue(int column, ResultSet resultSet) 129 throws SQLException , TypeCastException; 130 131 134 public abstract void setSqlValue(Object value, int column, 135 PreparedStatement statement) throws SQLException , TypeCastException; 136 137 140 public static String asString(Object value) throws TypeCastException 141 { 142 return (String )DataType.VARCHAR.typeCast(value); 143 } 144 145 150 public static DataType forSqlType(int sqlType) throws DataTypeException 151 { 152 for (int i = 0; i < TYPES.length; i++) 153 { 154 if (sqlType == TYPES[i].getSqlType()) 155 { 156 return TYPES[i]; 157 } 158 } 159 160 return UNKNOWN; 161 } 162 163 169 public static DataType forSqlTypeName(String sqlTypeName) throws DataTypeException 170 { 171 for (int i = 0; i < TYPES.length; i++) 172 { 173 if (sqlTypeName.equals(TYPES[i].toString())) 174 { 175 return TYPES[i]; 176 } 177 } 178 179 return UNKNOWN; 180 } 181 182 187 public static DataType forObject(Object value) 188 { 189 if (value == null) 190 { 191 return UNKNOWN; 192 } 193 194 for (int i = 0; i < TYPES.length; i++) 195 { 196 Class typeClass = TYPES[i].getTypeClass(); 197 if (typeClass.isInstance(value)) 198 { 199 return TYPES[i]; 200 } 201 } 202 203 return UNKNOWN; 204 } 205 } 206 207 208 209 210 211 212 213 214 215 216 | Popular Tags |