Exemple #1
0
        internal static int Compare(IComparable o1, IComparable o2, bool caseSensitive)
        {
            //TODO: turn this "conversion pipeline" into something nicer

            try {
                if (o1 is string && Numeric.IsNumeric(o2))
                {
                    o1 = (IComparable)Convert.ChangeType(o1, o2.GetType());
                }
                else if (o2 is string && Numeric.IsNumeric(o1))
                {
                    o2 = (IComparable)Convert.ChangeType(o2, o1.GetType());
                }
                else if (o1 is string && o2 is Guid)
                {
                    o2 = o2.ToString();
                }
                else if (o2 is string && o1 is Guid)
                {
                    o1 = o1.ToString();
                }
            } catch (FormatException) {
                throw new EvaluateException(String.Format("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
            }

            if (o1 is string && o2 is string)
            {
                o1 = ((string)o1).TrimEnd(IgnoredTrailingChars);
                o2 = ((string)o2).TrimEnd(IgnoredTrailingChars);
                if (!caseSensitive)
                {
                    o1 = ((string)o1).ToLower();
                    o2 = ((string)o2).ToLower();
                }
            }

            if (o1 is DateTime && o2 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture)
            {
                // DateTime is always CultureInfo.InvariantCulture
                o2 = (IComparable)DateTime.Parse((string)o2, CultureInfo.InvariantCulture);
            }
            else if (o2 is DateTime && o1 is string &&
                     Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture)
            {
                // DateTime is always CultureInfo.InvariantCulture
                o1 = (IComparable)DateTime.Parse((string)o1, CultureInfo.InvariantCulture);
            }
            else if (o2 is DateTime && o1 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture)
            {
                // DateTime is always CultureInfo.InvariantCulture
                o1 = (IComparable)DateTime.Parse((string)o1, CultureInfo.InvariantCulture);
            }

            if (o1.GetType() != o2.GetType())
            {
                o2 = (IComparable)Convert.ChangeType(o2, o1.GetType());
            }

            return(o1.CompareTo(o2));
        }
        private object Unify(object val)
        {
            if (Numeric.IsNumeric(val))
            {
                return(Numeric.Unify((IConvertible)val));
            }

            if (val == null || val == DBNull.Value)
            {
                return(null);
            }

            if (val is bool || val is string || val is DateTime || val is Guid || val is char)
            {
                return(val);
            }

            if (val is Enum)
            {
                return((int)val);
            }

            throw new EvaluateException(String.Format("Cannot handle data type found in column '{0}'.", columnName));
        }