protected override object CastObjectTo(object value, DataType destType) { string str = value.ToString(); var sqlType = destType.SqlType; switch (sqlType) { case (SqlType.Bit): case (SqlType.Boolean): return(String.Compare(str, "true", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(str, "1", StringComparison.OrdinalIgnoreCase) == 0); case (SqlType.TinyInt): // fall through case (SqlType.SmallInt): // fall through case (SqlType.Integer): return((Number)ToBigNumber(str).ToInt32()); case (SqlType.BigInt): return((Number)ToBigNumber(str).ToInt64()); case (SqlType.Float): return(Number.Parse(Convert.ToString(ToBigNumber(str).ToDouble()))); case (SqlType.Real): return(ToBigNumber(str)); case (SqlType.Double): return(Number.Parse(Convert.ToString(ToBigNumber(str).ToDouble()))); case (SqlType.Numeric): // fall through case (SqlType.Decimal): return(ToBigNumber(str)); case (SqlType.Char): return(new StringObject(CastUtil.PaddedString(str, ((StringType)destType).MaxSize))); case (SqlType.VarChar): case (SqlType.LongVarChar): case (SqlType.String): return(new StringObject(str)); case (SqlType.Date): return(ToDate(str)); case (SqlType.Time): return(ToTime(str)); case (SqlType.TimeStamp): return(ToTimeStamp(str)); case (SqlType.Blob): case (SqlType.Binary): case (SqlType.VarBinary): case (SqlType.LongVarBinary): return(new BinaryObject(Encoding.Unicode.GetBytes(str))); case (SqlType.Null): return(null); case (SqlType.Clob): // TODO: have a context where to get a new CLOB return(new StringObject(str)); default: throw new InvalidCastException(); } }
protected override object CastObjectTo(object value, DataType destType) { var n = (Number)value; var sqlType = destType.SqlType; switch (sqlType) { case (SqlType.Bit): case (SqlType.Boolean): return(n.ToBoolean()); case (SqlType.TinyInt): case (SqlType.SmallInt): case (SqlType.Integer): return((Number)n.ToInt32()); case (SqlType.BigInt): return((Number)n.ToInt64()); case (SqlType.Float): case (SqlType.Real): case (SqlType.Double): double d = n.ToDouble(); NumberState state = NumberState.None; if (Double.IsNaN(d)) { state = NumberState.NotANumber; } else if (Double.IsPositiveInfinity(d)) { state = NumberState.PositiveInfinity; } else if (Double.IsNegativeInfinity(d)) { state = NumberState.NegativeInfinity; } return(new Number(state, new BigDecimal(d))); case (SqlType.Numeric): // fall through case (SqlType.Decimal): return(Number.Parse(n.ToString())); case (SqlType.Char): return(new StringObject(CastUtil.PaddedString(n.ToString(), ((StringType)destType).MaxSize))); case (SqlType.VarChar): case (SqlType.LongVarChar): return(new StringObject(n.ToString())); case (SqlType.Date): case (SqlType.Time): case (SqlType.TimeStamp): return(ToDate(n.ToInt64())); case (SqlType.Blob): // fall through case (SqlType.Binary): // fall through case (SqlType.VarBinary): // fall through case (SqlType.LongVarBinary): return(new BinaryObject(n.ToByteArray())); case (SqlType.Null): return(null); default: throw new InvalidCastException(); } }