1 21 22 package org.dbunit.dataset.datatype; 23 24 import org.dbunit.dataset.ITable; 25 26 import java.math.BigDecimal ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 31 36 public class NumberDataType extends AbstractDataType 37 { 38 private static final Number TRUE = new BigDecimal (1); 39 private static final Number FALSE = new BigDecimal (0); 40 41 NumberDataType(String name, int sqlType) 42 { 43 super(name, sqlType, BigDecimal .class, true); 44 } 45 46 49 public Object typeCast(Object value) throws TypeCastException 50 { 51 if (value == null || value == ITable.NO_VALUE) 52 { 53 return null; 54 } 55 56 if (value instanceof BigDecimal ) 57 { 58 return value; 59 } 60 61 if (value instanceof Boolean ) 62 { 63 return ((Boolean )value).booleanValue() ? TRUE : FALSE; 64 } 65 66 try 67 { 68 return new BigDecimal (value.toString()); 69 } 70 catch (java.lang.NumberFormatException e) 71 { 72 throw new TypeCastException(value, this, e); 73 } 74 } 75 76 public Object getSqlValue(int column, ResultSet resultSet) 77 throws SQLException , TypeCastException 78 { 79 BigDecimal value = resultSet.getBigDecimal(column); 80 if (value == null || resultSet.wasNull()) 81 { 82 return null; 83 } 84 return value; 85 } 86 87 public void setSqlValue(Object value, int column, PreparedStatement statement) 88 throws SQLException , TypeCastException 89 { 90 statement.setBigDecimal(column, (BigDecimal )typeCast(value)); 91 } 92 } 93 94 95 96 97 98 99 | Popular Tags |