public virtual object Floor(object value)
 {
     if (value == null || DBNull.Value == value)
     {
         return((object)null);
     }
     if (!DataStorageHelper.IsNumeric(DataStorageHelper.GetStorageType(value.GetType())))
     {
         return((object)0);
     }
     try
     {
         Decimal result;
         if (Decimal.TryParse(value.ToString(), out result))
         {
             return((object)Math.Floor(result));
         }
         return((object)0);
     }
     catch (Exception ex)
     {
         return((object)0);
     }
 }
 public static int BinarySearch <T>(IList <T> list, T value)
 {
     return(DataStorageHelper.BinarySearch <T>(list, value, (IComparer <T>)Comparer <T> .Default));
 }
 public static bool IsUnknown(object value)
 {
     return(DataStorageHelper.IsObjectNull(value));
 }
        public static StorageType ResultType(
            StorageType left,
            StorageType right,
            bool lc,
            bool rc,
            Operator op)
        {
            if (left == StorageType.Guid && right == StorageType.Guid && Operator.IsRelational(op) || left == StorageType.String && right == StorageType.Guid && Operator.IsRelational(op))
            {
                return(left);
            }
            if (left == StorageType.Guid && right == StorageType.String && Operator.IsRelational(op))
            {
                return(right);
            }
            int precedence1 = (int)DataStorageHelper.GetPrecedence(left);

            if (precedence1 == 0)
            {
                return(StorageType.Empty);
            }
            int precedence2 = (int)DataStorageHelper.GetPrecedence(right);

            if (precedence2 == 0)
            {
                return(StorageType.Empty);
            }
            if (Operator.IsLogical(op))
            {
                return(left == StorageType.Boolean && right == StorageType.Boolean ? StorageType.Boolean : StorageType.Empty);
            }
            if (op == Operator.Plus && (left == StorageType.String || right == StorageType.String))
            {
                return(StorageType.String);
            }
            DataTypePrecedence code           = (DataTypePrecedence)Math.Max(precedence1, precedence2);
            StorageType        precedenceType = DataStorageHelper.GetPrecedenceType(code);

            if (Operator.IsArithmetical(op) && precedenceType != StorageType.String && precedenceType != StorageType.Char && (!DataStorageHelper.IsNumeric(left) || !DataStorageHelper.IsNumeric(right)))
            {
                return(StorageType.Empty);
            }
            if (op == Operator.Multiply && DataStorageHelper.IsInteger(precedenceType))
            {
                return(StorageType.Double);
            }
            if (!DataStorageHelper.IsMixed(left, right))
            {
                return(precedenceType);
            }
            if (lc && !rc)
            {
                return(right);
            }
            if (!lc && rc)
            {
                return(left);
            }
            if (!DataStorageHelper.IsUnsigned(precedenceType))
            {
                return(precedenceType);
            }
            if (code >= DataTypePrecedence.UInt64)
            {
                throw InvalidExpressionException.AmbiguousBinop(op, DataStorageHelper.GetTypeStorage(left), DataStorageHelper.GetTypeStorage(right));
            }
            return(DataStorageHelper.GetPrecedenceType(code + 1));
        }
        public static StorageType ResultSqlType(
            StorageType left,
            StorageType right,
            bool lc,
            bool rc,
            Operator op)
        {
            int precedence1 = (int)DataStorageHelper.GetPrecedence(left);

            if (precedence1 == 0)
            {
                return(StorageType.Empty);
            }
            int precedence2 = (int)DataStorageHelper.GetPrecedence(right);

            if (precedence2 == 0)
            {
                return(StorageType.Empty);
            }
            if (Operator.IsLogical(op))
            {
                if (left != StorageType.Boolean && left != StorageType.SqlBoolean || right != StorageType.Boolean && right != StorageType.SqlBoolean)
                {
                    return(StorageType.Empty);
                }
                return(left == StorageType.Boolean && right == StorageType.Boolean ? StorageType.Boolean : StorageType.SqlBoolean);
            }
            if (op == Operator.Plus)
            {
                if (left == StorageType.SqlString || right == StorageType.SqlString)
                {
                    return(StorageType.SqlString);
                }
                if (left == StorageType.String || right == StorageType.String)
                {
                    return(StorageType.String);
                }
            }
            if (left == StorageType.SqlBinary && right != StorageType.SqlBinary || left != StorageType.SqlBinary && right == StorageType.SqlBinary || (left == StorageType.SqlGuid && right != StorageType.SqlGuid || left != StorageType.SqlGuid && right == StorageType.SqlGuid) || (precedence1 > 19 && precedence2 < 20 || precedence1 < 20 && precedence2 > 19))
            {
                return(StorageType.Empty);
            }
            if (precedence1 > 19)
            {
                if (op == Operator.Plus || op == Operator.Minus)
                {
                    if (left == StorageType.TimeSpan)
                    {
                        return(right);
                    }
                    if (right == StorageType.TimeSpan)
                    {
                        return(left);
                    }
                    return(StorageType.Empty);
                }
                if (!Operator.IsRelational(op))
                {
                    return(StorageType.Empty);
                }
                return(left);
            }
            DataTypePrecedence code = (DataTypePrecedence)Math.Max(precedence1, precedence2);

            DataStorageHelper.GetPrecedenceType(code);
            StorageType type = DataStorageHelper.GetPrecedenceType((DataTypePrecedence)DataStorageHelper.SqlResultType((int)code));

            if (Operator.IsArithmetical(op) && type != StorageType.String && (type != StorageType.Char && type != StorageType.SqlString) && (!DataStorageHelper.IsNumericSql(left) || !DataStorageHelper.IsNumericSql(right)))
            {
                return(StorageType.Empty);
            }
            if (op == Operator.Multiply && DataStorageHelper.IsIntegerSql(type))
            {
                return(StorageType.SqlDouble);
            }
            if (type == StorageType.SqlMoney && left != StorageType.SqlMoney && right != StorageType.SqlMoney)
            {
                type = StorageType.SqlDecimal;
            }
            if (!DataStorageHelper.IsMixedSql(left, right) || !DataStorageHelper.IsUnsignedSql(type))
            {
                return(type);
            }
            if (code >= DataTypePrecedence.UInt64)
            {
                throw InvalidExpressionException.AmbiguousBinop(op, DataStorageHelper.GetTypeStorage(left), DataStorageHelper.GetTypeStorage(right));
            }
            return(DataStorageHelper.GetPrecedenceType(code + 1));
        }
Beispiel #6
0
        public static SqlDouble ConvertToSqlDouble(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlDouble.Null);
            }
            Type dataType = value.GetType();

            switch (DataStorageHelper.GetStorageType(dataType))
            {
            case StorageType.Byte:
                return((double)((byte)value));

            case StorageType.Int16:
                return((double)((short)value));

            case StorageType.UInt16:
                return((double)((ushort)value));

            case StorageType.Int32:
                return((double)((int)value));

            case StorageType.UInt32:
                return((double)((uint)value));

            case StorageType.Int64:
                return((double)((long)value));

            case StorageType.UInt64:
                return((double)((ulong)value));

            case StorageType.Single:
                return((double)((float)value));

            case StorageType.Double:
                return((double)value);

            case StorageType.SqlByte:
                return((SqlByte)value);

            case StorageType.SqlDecimal:
                return((SqlDecimal)value);

            case StorageType.SqlDouble:
                return((SqlDouble)value);

            case StorageType.SqlInt16:
                return((SqlInt16)value);

            case StorageType.SqlInt32:
                return((SqlInt32)value);

            case StorageType.SqlInt64:
                return((SqlInt64)value);

            case StorageType.SqlMoney:
                return((SqlMoney)value);

            case StorageType.SqlSingle:
                return((SqlSingle)value);
            }
            throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlDouble));
        }
Beispiel #7
0
        public static object ChangeTypeForXML(object value, Type type)
        {
            StorageType storageType = DataStorageHelper.GetStorageType(type);
            StorageType type3       = DataStorageHelper.GetStorageType(value.GetType());

            switch (storageType)
            {
            case StorageType.Boolean:
                if ("1" != ((string)value))
                {
                    if ("0" == ((string)value))
                    {
                        return(false);
                    }
                    return(XmlConvert.ToBoolean((string)value));
                }
                return(true);

            case StorageType.Char:
                return(XmlConvert.ToChar((string)value));

            case StorageType.SByte:
                return(XmlConvert.ToSByte((string)value));

            case StorageType.Byte:
                return(XmlConvert.ToByte((string)value));

            case StorageType.Int16:
                return(XmlConvert.ToInt16((string)value));

            case StorageType.UInt16:
                return(XmlConvert.ToUInt16((string)value));

            case StorageType.Int32:
                return(XmlConvert.ToInt32((string)value));

            case StorageType.UInt32:
                return(XmlConvert.ToUInt32((string)value));

            case StorageType.Int64:
                return(XmlConvert.ToInt64((string)value));

            case StorageType.UInt64:
                return(XmlConvert.ToUInt64((string)value));

            case StorageType.Single:
                return(XmlConvert.ToSingle((string)value));

            case StorageType.Double:
                return(XmlConvert.ToDouble((string)value));

            case StorageType.Decimal:
                return(XmlConvert.ToDecimal((string)value));

            case StorageType.DateTime:
                return(XmlConvert.ToDateTime((string)value, XmlDateTimeSerializationMode.RoundtripKind));

            case StorageType.TimeSpan:
            {
                StorageType type2 = type3;
                switch (type2)
                {
                case StorageType.Int32:
                    return(new TimeSpan((long)((int)value)));

                case StorageType.Int64:
                    return(new TimeSpan((long)value));
                }
                if (type2 != StorageType.String)
                {
                    break;
                }
                return(XmlConvert.ToTimeSpan((string)value));
            }

            case StorageType.Guid:
                return(XmlConvert.ToGuid((string)value));

            case StorageType.Uri:
                return(new Uri((string)value));

            case StorageType.SqlBinary:
                return(new SqlBinary(Convert.FromBase64String((string)value)));

            case StorageType.SqlBoolean:
                return(new SqlBoolean(XmlConvert.ToBoolean((string)value)));

            case StorageType.SqlByte:
                return(new SqlByte(XmlConvert.ToByte((string)value)));

            case StorageType.SqlBytes:
                return(new SqlBytes(Convert.FromBase64String((string)value)));

            case StorageType.SqlChars:
                return(new SqlChars(((string)value).ToCharArray()));

            case StorageType.SqlDateTime:
                return(new SqlDateTime(XmlConvert.ToDateTime((string)value, XmlDateTimeSerializationMode.RoundtripKind)));

            case StorageType.SqlDecimal:
                return(SqlDecimal.Parse((string)value));

            case StorageType.SqlDouble:
                return(new SqlDouble(XmlConvert.ToDouble((string)value)));

            case StorageType.SqlGuid:
                return(new SqlGuid(XmlConvert.ToGuid((string)value)));

            case StorageType.SqlInt16:
                return(new SqlInt16(XmlConvert.ToInt16((string)value)));

            case StorageType.SqlInt32:
                return(new SqlInt32(XmlConvert.ToInt32((string)value)));

            case StorageType.SqlInt64:
                return(new SqlInt64(XmlConvert.ToInt64((string)value)));

            case StorageType.SqlMoney:
                return(new SqlMoney(XmlConvert.ToDecimal((string)value)));

            case StorageType.SqlSingle:
                return(new SqlSingle(XmlConvert.ToSingle((string)value)));

            case StorageType.SqlString:
                return(new SqlString((string)value));

            default:
            {
                if ((DBNull.Value == value) || (value == null))
                {
                    return(DBNull.Value);
                }
                switch (type3)
                {
                case StorageType.Boolean:
                    return(XmlConvert.ToString((bool)value));

                case StorageType.Char:
                    return(XmlConvert.ToString((char)value));

                case StorageType.SByte:
                    return(XmlConvert.ToString((sbyte)value));

                case StorageType.Byte:
                    return(XmlConvert.ToString((byte)value));

                case StorageType.Int16:
                    return(XmlConvert.ToString((short)value));

                case StorageType.UInt16:
                    return(XmlConvert.ToString((ushort)value));

                case StorageType.Int32:
                    return(XmlConvert.ToString((int)value));

                case StorageType.UInt32:
                    return(XmlConvert.ToString((uint)value));

                case StorageType.Int64:
                    return(XmlConvert.ToString((long)value));

                case StorageType.UInt64:
                    return(XmlConvert.ToString((ulong)value));

                case StorageType.Single:
                    return(XmlConvert.ToString((float)value));

                case StorageType.Double:
                    return(XmlConvert.ToString((double)value));

                case StorageType.Decimal:
                    return(XmlConvert.ToString((decimal)value));

                case StorageType.DateTime:
                    return(XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind));

                case StorageType.TimeSpan:
                    return(XmlConvert.ToString((TimeSpan)value));

                case StorageType.String:
                    return((string)value);

                case StorageType.Guid:
                    return(XmlConvert.ToString((Guid)value));

                case StorageType.CharArray:
                    return(new string((char[])value));

                case StorageType.SqlBinary:
                {
                    SqlBinary binary = (SqlBinary)value;
                    return(Convert.ToBase64String(binary.Value));
                }

                case StorageType.SqlBoolean:
                {
                    SqlBoolean flag = (SqlBoolean)value;
                    return(XmlConvert.ToString(flag.Value));
                }

                case StorageType.SqlByte:
                {
                    SqlByte num7 = (SqlByte)value;
                    return(XmlConvert.ToString(num7.Value));
                }

                case StorageType.SqlBytes:
                    return(Convert.ToBase64String(((SqlBytes)value).Value));

                case StorageType.SqlChars:
                    return(new string(((SqlChars)value).Value));

                case StorageType.SqlDateTime:
                {
                    SqlDateTime time = (SqlDateTime)value;
                    return(XmlConvert.ToString(time.Value, XmlDateTimeSerializationMode.RoundtripKind));
                }

                case StorageType.SqlDecimal:
                {
                    SqlDecimal num6 = (SqlDecimal)value;
                    return(num6.ToString());
                }

                case StorageType.SqlDouble:
                {
                    SqlDouble num5 = (SqlDouble)value;
                    return(XmlConvert.ToString(num5.Value));
                }

                case StorageType.SqlGuid:
                {
                    SqlGuid guid = (SqlGuid)value;
                    return(XmlConvert.ToString(guid.Value));
                }

                case StorageType.SqlInt16:
                {
                    SqlInt16 num4 = (SqlInt16)value;
                    return(XmlConvert.ToString(num4.Value));
                }

                case StorageType.SqlInt32:
                {
                    SqlInt32 num3 = (SqlInt32)value;
                    return(XmlConvert.ToString(num3.Value));
                }

                case StorageType.SqlInt64:
                {
                    SqlInt64 num2 = (SqlInt64)value;
                    return(XmlConvert.ToString(num2.Value));
                }

                case StorageType.SqlMoney:
                {
                    SqlMoney money = (SqlMoney)value;
                    return(XmlConvert.ToString(money.Value));
                }

                case StorageType.SqlSingle:
                {
                    SqlSingle num = (SqlSingle)value;
                    return(XmlConvert.ToString(num.Value));
                }

                case StorageType.SqlString:
                {
                    SqlString text = (SqlString)value;
                    return(text.Value);
                }
                }
                IConvertible convertible = value as IConvertible;
                if (convertible != null)
                {
                    return(convertible.ToString(CultureInfo.InvariantCulture));
                }
                IFormattable formattable = value as IFormattable;
                if (formattable != null)
                {
                    return(formattable.ToString(null, CultureInfo.InvariantCulture));
                }
                return(value.ToString());
            }
            }
            return((TimeSpan)value);
        }
Beispiel #8
0
        public static object ChangeType2(object value, StorageType stype, Type type, IFormatProvider formatProvider)
        {
            switch (stype)
            {
            case StorageType.SqlBinary:
                return(ConvertToSqlBinary(value));

            case StorageType.SqlBoolean:
                return(ConvertToSqlBoolean(value));

            case StorageType.SqlByte:
                return(ConvertToSqlByte(value));

            case StorageType.SqlBytes:
                return(ConvertToSqlBytes(value));

            case StorageType.SqlChars:
                return(ConvertToSqlChars(value));

            case StorageType.SqlDateTime:
                return(ConvertToSqlDateTime(value));

            case StorageType.SqlDecimal:
                return(ConvertToSqlDecimal(value));

            case StorageType.SqlDouble:
                return(ConvertToSqlDouble(value));

            case StorageType.SqlGuid:
                return(ConvertToSqlGuid(value));

            case StorageType.SqlInt16:
                return(ConvertToSqlInt16(value));

            case StorageType.SqlInt32:
                return(ConvertToSqlInt32(value));

            case StorageType.SqlInt64:
                return(ConvertToSqlInt64(value));

            case StorageType.SqlMoney:
                return(ConvertToSqlMoney(value));

            case StorageType.SqlSingle:
                return(ConvertToSqlSingle(value));

            case StorageType.SqlString:
                return(ConvertToSqlString(value));

            default:
            {
                if ((DBNull.Value == value) || (value == null))
                {
                    return(DBNull.Value);
                }
                Type        dataType    = value.GetType();
                StorageType storageType = DataStorageHelper.GetStorageType(dataType);
                switch (storageType)
                {
                case StorageType.SqlBinary:
                case StorageType.SqlBoolean:
                case StorageType.SqlByte:
                case StorageType.SqlBytes:
                case StorageType.SqlChars:
                case StorageType.SqlDateTime:
                case StorageType.SqlDecimal:
                case StorageType.SqlDouble:
                case StorageType.SqlGuid:
                case StorageType.SqlInt16:
                case StorageType.SqlInt32:
                case StorageType.SqlInt64:
                case StorageType.SqlMoney:
                case StorageType.SqlSingle:
                case StorageType.SqlString:
                    throw InvalidExpressionException.SqlConvertFailed(dataType, type);
                }
                if (StorageType.String != stype)
                {
                    if (StorageType.TimeSpan != stype)
                    {
                        if (StorageType.String == storageType)
                        {
                            switch (stype)
                            {
                            case StorageType.Boolean:
                                if ("1" != ((string)value))
                                {
                                    if ("0" == ((string)value))
                                    {
                                        return(false);
                                    }
                                    goto Label_0547;
                                }
                                return(true);

                            case StorageType.Char:
                                return(((IConvertible)((string)value)).ToChar(formatProvider));

                            case StorageType.SByte:
                                return(((IConvertible)((string)value)).ToSByte(formatProvider));

                            case StorageType.Byte:
                                return(((IConvertible)((string)value)).ToByte(formatProvider));

                            case StorageType.Int16:
                                return(((IConvertible)((string)value)).ToInt16(formatProvider));

                            case StorageType.UInt16:
                                return(((IConvertible)((string)value)).ToUInt16(formatProvider));

                            case StorageType.Int32:
                                return(((IConvertible)((string)value)).ToInt32(formatProvider));

                            case StorageType.UInt32:
                                return(((IConvertible)((string)value)).ToUInt32(formatProvider));

                            case StorageType.Int64:
                                return(((IConvertible)((string)value)).ToInt64(formatProvider));

                            case StorageType.UInt64:
                                return(((IConvertible)((string)value)).ToUInt64(formatProvider));

                            case StorageType.Single:
                                return(((IConvertible)((string)value)).ToSingle(formatProvider));

                            case StorageType.Double:
                                return(((IConvertible)((string)value)).ToDouble(formatProvider));

                            case StorageType.Decimal:
                                return(((IConvertible)((string)value)).ToDecimal(formatProvider));

                            case StorageType.DateTime:
                                return(((IConvertible)((string)value)).ToDateTime(formatProvider));

                            case StorageType.TimeSpan:
                                return(XmlConvert.ToTimeSpan((string)value));

                            case StorageType.String:
                                return((string)value);

                            case StorageType.Guid:
                                return(XmlConvert.ToGuid((string)value));

                            case StorageType.ByteArray:
                            case StorageType.CharArray:
                            case StorageType.Type:
                                goto Label_0547;

                            case StorageType.Uri:
                                return(new Uri((string)value));
                            }
                        }
                        goto Label_0547;
                    }
                    switch (storageType)
                    {
                    case StorageType.Int32:
                        return(new TimeSpan((long)((int)value)));

                    case StorageType.UInt32:
                        goto Label_037A;

                    case StorageType.Int64:
                        return(new TimeSpan((long)value));

                    case StorageType.String:
                        return(XmlConvert.ToTimeSpan((string)value));
                    }
                    goto Label_037A;
                }
                switch (storageType)
                {
                case StorageType.Boolean:
                    return(((bool)value).ToString(formatProvider));

                case StorageType.Char:
                    return(((char)value).ToString(formatProvider));

                case StorageType.SByte:
                    return(((sbyte)value).ToString(formatProvider));

                case StorageType.Byte:
                    return(((byte)value).ToString(formatProvider));

                case StorageType.Int16:
                    return(((short)value).ToString(formatProvider));

                case StorageType.UInt16:
                    return(((ushort)value).ToString(formatProvider));

                case StorageType.Int32:
                    return(((int)value).ToString(formatProvider));

                case StorageType.UInt32:
                    return(((uint)value).ToString(formatProvider));

                case StorageType.Int64:
                    return(((long)value).ToString(formatProvider));

                case StorageType.UInt64:
                    return(((ulong)value).ToString(formatProvider));

                case StorageType.Single:
                    return(((float)value).ToString(formatProvider));

                case StorageType.Double:
                    return(((double)value).ToString(formatProvider));

                case StorageType.Decimal:
                    return(((decimal)value).ToString(formatProvider));

                case StorageType.DateTime:
                    return(((DateTime)value).ToString(formatProvider));

                case StorageType.TimeSpan:
                    return(XmlConvert.ToString((TimeSpan)value));

                case StorageType.String:
                    return((string)value);

                case StorageType.Guid:
                    return(XmlConvert.ToString((Guid)value));

                case StorageType.CharArray:
                    return(new string((char[])value));
                }
                break;
            }
            }
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                return(convertible.ToString(formatProvider));
            }
            IFormattable formattable = value as IFormattable;

            if (formattable != null)
            {
                return(formattable.ToString(null, formatProvider));
            }
            return(value.ToString());

Label_037A:
            return((TimeSpan)value);

Label_0547:
            return(Convert.ChangeType(value, type, formatProvider));
        }
Beispiel #9
0
 public static object ChangeType(object value, Type type, IFormatProvider formatProvider)
 {
     return(ChangeType2(value, DataStorageHelper.GetStorageType(type), type, formatProvider));
 }
Beispiel #10
0
        private static int Compare(object value1, object value2, FunctionContext context, string exp)
        {
            Type        type1        = value1.GetType();
            Type        type2        = value2.GetType();
            StorageType storageType1 = DataStorageHelper.GetStorageType(type1);
            StorageType storageType2 = DataStorageHelper.GetStorageType(type2);
            bool        flag1        = DataStorageHelper.IsSqlType(storageType1);
            bool        flag2        = DataStorageHelper.IsSqlType(storageType2);

            if (flag1 && DataStorageHelper.IsObjectSqlNull(value1) || value1 == DBNull.Value)
            {
                return(-1);
            }
            if (flag2 && DataStorageHelper.IsObjectSqlNull(value2) || value2 == DBNull.Value)
            {
                return(1);
            }
            StorageType resultType = flag1 || flag2?DataStorageHelper.ResultSqlType(storageType1, storageType2, false, false, Operator.LessThen) : DataStorageHelper.ResultType(storageType1, storageType2, false, false, Operator.LessThen);

            if (resultType == StorageType.Empty)
            {
                throw InvalidExpressionException.TypeMismatch(exp);
            }
            return(Operator.BinaryCompare(value1, value2, resultType, Operator.LessOrEqual, new OperatorContext((object)null, context.FormatProvider, (object)null)));
        }
Beispiel #11
0
 public static string EscapeLikeValue(string name)
 {
     return(DataStorageHelper.EscapeLikeValue(name));
 }
Beispiel #12
0
 public static List <SortDescriptor> ParseSortString(string fieldName)
 {
     return(DataStorageHelper.ParseSortString(fieldName));
 }
Beispiel #13
0
 public static int CompareNulls(object xValue, object yValue)
 {
     return(DataStorageHelper.CompareNulls(xValue, yValue));
 }
Beispiel #14
0
        /// <summary>
        ///
        /// </summary>
        public static StorageType ResultSqlType(StorageType left
                                                , StorageType right
                                                , bool lc
                                                , bool rc
                                                , Operator op)
        {
            int leftOpPrecedence = (int)DataStorageHelper.GetPrecedence(left);

            if (leftOpPrecedence == 0)
            {
                return(StorageType.Empty);
            }
            int rightOpPrecedence = (int)DataStorageHelper.GetPrecedence(right);

            if (rightOpPrecedence == 0)
            {
                return(StorageType.Empty);
            }
            if (Operator.IsLogical(op))
            {
                if (((left != StorageType.Boolean) && (left != StorageType.SqlBoolean)) ||
                    ((right != StorageType.Boolean) && (right != StorageType.SqlBoolean)))
                {
                    return(StorageType.Empty);
                }
                if ((left == StorageType.Boolean) && (right == StorageType.Boolean))
                {
                    return(StorageType.Boolean);
                }
                return(StorageType.SqlBoolean);
            }
            if (op == Operator.Plus)
            {
                if ((left == StorageType.SqlString) || (right == StorageType.SqlString))
                {
                    return(StorageType.SqlString);
                }
                if ((left == StorageType.String) || (right == StorageType.String))
                {
                    return(StorageType.String);
                }
            }
            if (((left == StorageType.SqlBinary) && (right != StorageType.SqlBinary)) || ((left != StorageType.SqlBinary) && (right == StorageType.SqlBinary)))
            {
                return(StorageType.Empty);
            }
            if (((left == StorageType.SqlGuid) && (right != StorageType.SqlGuid)) || ((left != StorageType.SqlGuid) && (right == StorageType.SqlGuid)))
            {
                return(StorageType.Empty);
            }
            if ((leftOpPrecedence > 0x13) && (rightOpPrecedence < 20))
            {
                return(StorageType.Empty);
            }
            if ((leftOpPrecedence < 20) && (rightOpPrecedence > 0x13))
            {
                return(StorageType.Empty);
            }
            if (leftOpPrecedence > 0x13)
            {
                if ((op == Operator.Plus) || (op == Operator.Minus))
                {
                    if (left == StorageType.TimeSpan)
                    {
                        return(right);
                    }
                    if (right == StorageType.TimeSpan)
                    {
                        return(left);
                    }
                    return(StorageType.Empty);
                }
                if (!Operator.IsRelational(op))
                {
                    return(StorageType.Empty);
                }
                return(left);
            }
            DataTypePrecedence code           = (DataTypePrecedence)Math.Max(leftOpPrecedence, rightOpPrecedence);
            StorageType        precedenceType = DataStorageHelper.GetPrecedenceType(code);

            precedenceType = DataStorageHelper.GetPrecedenceType((DataTypePrecedence)DataStorageHelper.SqlResultType((int)code));
            if ((Operator.IsArithmetical(op) && (precedenceType != StorageType.String)) && ((precedenceType != StorageType.Char) && (precedenceType != StorageType.SqlString)))
            {
                if (!DataStorageHelper.IsNumericSql(left))
                {
                    return(StorageType.Empty);
                }
                if (!DataStorageHelper.IsNumericSql(right))
                {
                    return(StorageType.Empty);
                }
            }
            if ((op == Operator.Multiply) && DataStorageHelper.IsIntegerSql(precedenceType))
            {
                return(StorageType.SqlDouble);
            }
            if (((precedenceType == StorageType.SqlMoney) && (left != StorageType.SqlMoney)) && (right != StorageType.SqlMoney))
            {
                precedenceType = StorageType.SqlDecimal;
            }
            if (!DataStorageHelper.IsMixedSql(left, right) || !DataStorageHelper.IsUnsignedSql(precedenceType))
            {
                return(precedenceType);
            }
            if (code >= DataTypePrecedence.UInt64)
            {
                throw InvalidExpressionException.AmbiguousBinop(op
                                                                , DataStorageHelper.GetTypeStorage(left)
                                                                , DataStorageHelper.GetTypeStorage(right));
            }
            return(DataStorageHelper.GetPrecedenceType(code + 1));
        }
Beispiel #15
0
        public static object ChangeTypeForXML(object value, Type type)
        {
            StorageType storageType1 = DataStorageHelper.GetStorageType(type);
            StorageType storageType2 = DataStorageHelper.GetStorageType(value.GetType());

            switch (storageType1)
            {
            case StorageType.Boolean:
                if (!("1" != (string)value))
                {
                    return((object)true);
                }
                if ("0" == (string)value)
                {
                    return((object)false);
                }
                return((object)XmlConvert.ToBoolean((string)value));

            case StorageType.Char:
                return((object)XmlConvert.ToChar((string)value));

            case StorageType.SByte:
                return((object)XmlConvert.ToSByte((string)value));

            case StorageType.Byte:
                return((object)XmlConvert.ToByte((string)value));

            case StorageType.Int16:
                return((object)XmlConvert.ToInt16((string)value));

            case StorageType.UInt16:
                return((object)XmlConvert.ToUInt16((string)value));

            case StorageType.Int32:
                return((object)XmlConvert.ToInt32((string)value));

            case StorageType.UInt32:
                return((object)XmlConvert.ToUInt32((string)value));

            case StorageType.Int64:
                return((object)XmlConvert.ToInt64((string)value));

            case StorageType.UInt64:
                return((object)XmlConvert.ToUInt64((string)value));

            case StorageType.Single:
                return((object)XmlConvert.ToSingle((string)value));

            case StorageType.Double:
                return((object)XmlConvert.ToDouble((string)value));

            case StorageType.Decimal:
                return((object)XmlConvert.ToDecimal((string)value));

            case StorageType.DateTime:
                return((object)XmlConvert.ToDateTime((string)value, XmlDateTimeSerializationMode.RoundtripKind));

            case StorageType.TimeSpan:
                StorageType storageType3 = storageType2;
                switch (storageType3)
                {
                case StorageType.Int32:
                    return((object)new TimeSpan((long)(int)value));

                case StorageType.Int64:
                    return((object)new TimeSpan((long)value));

                default:
                    if (storageType3 == StorageType.String)
                    {
                        return((object)XmlConvert.ToTimeSpan((string)value));
                    }
                    return((object)(TimeSpan)value);
                }

            case StorageType.Guid:
                return((object)XmlConvert.ToGuid((string)value));

            case StorageType.Uri:
                return((object)new Uri((string)value));

            case StorageType.SqlBinary:
                return((object)new SqlBinary(Convert.FromBase64String((string)value)));

            case StorageType.SqlBoolean:
                return((object)new SqlBoolean(XmlConvert.ToBoolean((string)value)));

            case StorageType.SqlByte:
                return((object)new SqlByte(XmlConvert.ToByte((string)value)));

            case StorageType.SqlBytes:
                return((object)new SqlBytes(Convert.FromBase64String((string)value)));

            case StorageType.SqlChars:
                return((object)new SqlChars(((string)value).ToCharArray()));

            case StorageType.SqlDateTime:
                return((object)new SqlDateTime(XmlConvert.ToDateTime((string)value, XmlDateTimeSerializationMode.RoundtripKind)));

            case StorageType.SqlDecimal:
                return((object)SqlDecimal.Parse((string)value));

            case StorageType.SqlDouble:
                return((object)new SqlDouble(XmlConvert.ToDouble((string)value)));

            case StorageType.SqlGuid:
                return((object)new SqlGuid(XmlConvert.ToGuid((string)value)));

            case StorageType.SqlInt16:
                return((object)new SqlInt16(XmlConvert.ToInt16((string)value)));

            case StorageType.SqlInt32:
                return((object)new SqlInt32(XmlConvert.ToInt32((string)value)));

            case StorageType.SqlInt64:
                return((object)new SqlInt64(XmlConvert.ToInt64((string)value)));

            case StorageType.SqlMoney:
                return((object)new SqlMoney(XmlConvert.ToDecimal((string)value)));

            case StorageType.SqlSingle:
                return((object)new SqlSingle(XmlConvert.ToSingle((string)value)));

            case StorageType.SqlString:
                return((object)new SqlString((string)value));

            default:
                if (DBNull.Value == value || value == null)
                {
                    return((object)DBNull.Value);
                }
                switch (storageType2)
                {
                case StorageType.Boolean:
                    return((object)XmlConvert.ToString((bool)value));

                case StorageType.Char:
                    return((object)XmlConvert.ToString((char)value));

                case StorageType.SByte:
                    return((object)XmlConvert.ToString((sbyte)value));

                case StorageType.Byte:
                    return((object)XmlConvert.ToString((byte)value));

                case StorageType.Int16:
                    return((object)XmlConvert.ToString((short)value));

                case StorageType.UInt16:
                    return((object)XmlConvert.ToString((ushort)value));

                case StorageType.Int32:
                    return((object)XmlConvert.ToString((int)value));

                case StorageType.UInt32:
                    return((object)XmlConvert.ToString((uint)value));

                case StorageType.Int64:
                    return((object)XmlConvert.ToString((long)value));

                case StorageType.UInt64:
                    return((object)XmlConvert.ToString((ulong)value));

                case StorageType.Single:
                    return((object)XmlConvert.ToString((float)value));

                case StorageType.Double:
                    return((object)XmlConvert.ToString((double)value));

                case StorageType.Decimal:
                    return((object)XmlConvert.ToString((Decimal)value));

                case StorageType.DateTime:
                    return((object)XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind));

                case StorageType.TimeSpan:
                    return((object)XmlConvert.ToString((TimeSpan)value));

                case StorageType.String:
                    return((object)(string)value);

                case StorageType.Guid:
                    return((object)XmlConvert.ToString((Guid)value));

                case StorageType.CharArray:
                    return((object)new string((char[])value));

                case StorageType.SqlBinary:
                    return((object)Convert.ToBase64String(((SqlBinary)value).Value));

                case StorageType.SqlBoolean:
                    return((object)XmlConvert.ToString(((SqlBoolean)value).Value));

                case StorageType.SqlByte:
                    return((object)XmlConvert.ToString(((SqlByte)value).Value));

                case StorageType.SqlBytes:
                    return((object)Convert.ToBase64String(((SqlBytes)value).Value));

                case StorageType.SqlChars:
                    return((object)new string(((SqlChars)value).Value));

                case StorageType.SqlDateTime:
                    return((object)XmlConvert.ToString(((SqlDateTime)value).Value, XmlDateTimeSerializationMode.RoundtripKind));

                case StorageType.SqlDecimal:
                    return((object)((SqlDecimal)value).ToString());

                case StorageType.SqlDouble:
                    return((object)XmlConvert.ToString(((SqlDouble)value).Value));

                case StorageType.SqlGuid:
                    return((object)XmlConvert.ToString(((SqlGuid)value).Value));

                case StorageType.SqlInt16:
                    return((object)XmlConvert.ToString(((SqlInt16)value).Value));

                case StorageType.SqlInt32:
                    return((object)XmlConvert.ToString(((SqlInt32)value).Value));

                case StorageType.SqlInt64:
                    return((object)XmlConvert.ToString(((SqlInt64)value).Value));

                case StorageType.SqlMoney:
                    return((object)XmlConvert.ToString(((SqlMoney)value).Value));

                case StorageType.SqlSingle:
                    return((object)XmlConvert.ToString(((SqlSingle)value).Value));

                case StorageType.SqlString:
                    return((object)((SqlString)value).Value);

                default:
                    IConvertible convertible = value as IConvertible;
                    if (convertible != null)
                    {
                        return((object)convertible.ToString((IFormatProvider)CultureInfo.InvariantCulture));
                    }
                    IFormattable formattable = value as IFormattable;
                    if (formattable != null)
                    {
                        return((object)formattable.ToString((string)null, (IFormatProvider)CultureInfo.InvariantCulture));
                    }
                    return((object)value.ToString());
                }
            }
        }