예제 #1
0
        internal static object ConvertToCSharpVal(UTF8Buffer srcVal, SFDataType srcType, Type destType)
        {
            if (srcVal == null)
            {
                return(DBNull.Value);
            }

            try
            {
                // The most common conversions are checked first for maximum performance
                if (destType == typeof(Int64))
                {
                    return(FastParser.FastParseInt64(srcVal.Buffer, srcVal.offset, srcVal.length));
                }
                else if (destType == typeof(Int32))
                {
                    return(FastParser.FastParseInt32(srcVal.Buffer, srcVal.offset, srcVal.length));
                }
                else if (destType == typeof(decimal))
                {
                    return(FastParser.FastParseDecimal(srcVal.Buffer, srcVal.offset, srcVal.length));
                }
                else if (destType == typeof(string))
                {
                    return(srcVal.ToString());
                }
                else if (destType == typeof(DateTime))
                {
                    return(ConvertToDateTime(srcVal, srcType));
                }
                else if (destType == typeof(DateTimeOffset))
                {
                    return(ConvertToDateTimeOffset(srcVal, srcType));
                }
                else if (destType == typeof(Boolean))
                {
                    var val = srcVal.Buffer[srcVal.offset];
                    return(val == '1' || val == 't' || val == 'T');
                }
                else if (destType == typeof(byte[]))
                {
                    return(srcType == SFDataType.BINARY ?
                           HexToBytes(srcVal.ToString()) : srcVal.GetBytes());
                }
                else if (destType == typeof(Int16))
                {
                    // Use checked keyword to make sure we generate an OverflowException if conversion fails
                    int result = FastParser.FastParseInt32(srcVal.Buffer, srcVal.offset, srcVal.length);
                    return(checked ((Int16)result));
                }
                else if (destType == typeof(byte))
                {
                    // Use checked keyword to make sure we generate an OverflowException if conversion fails
                    int result = FastParser.FastParseInt32(srcVal.Buffer, srcVal.offset, srcVal.length);
                    return(checked ((byte)result));
                }
                else if (destType == typeof(double))
                {
                    return(Convert.ToDouble(srcVal.ToString(), CultureInfo.InvariantCulture));
                }
                else if (destType == typeof(float))
                {
                    return(Convert.ToSingle(srcVal.ToString(), CultureInfo.InvariantCulture));
                }
                else if (destType == typeof(Guid))
                {
                    return(new Guid(srcVal.ToString()));
                }
                else if (destType == typeof(char[]))
                {
                    byte[] data = srcType == SFDataType.BINARY ?
                                  HexToBytes(srcVal.ToString()) : srcVal.GetBytes();
                    return(Encoding.UTF8.GetString(data).ToCharArray());
                }
                else
                {
                    throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Invalid destination type.");
                }
            }
            catch (OverflowException)
            {
                throw new OverflowException($"Error converting '{srcVal} to {destType.Name}'. Use GetString() to handle very large values");
            }
        }
예제 #2
0
 // Define an extension method that can safely be called even on null objects
 // Calling ToString() on a null object causes an exception
 public static string SafeToString(this UTF8Buffer v)
 {
     return(v == null ? null : v.ToString());
 }