예제 #1
0
        public static object Avg(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            int         count;
            StorageType type;

            return(Aggregates.Mean(data, node, context.GlobalContext, nameof(Avg), out count, out type));
        }
예제 #2
0
        static object Sum(IEnumerable data
                          , ExpressionNode node
                          , object context
                          , string exp
                          , out int count
                          , out StorageType type)
        {
            count = 0;
            type  = StorageType.Empty;

            //if (0 == view.Data.Count)
            //    return 0;

            object value = 0;

            foreach (object value2 in Aggregates.NotNullValue(data, node, context))
            {
                count++;

                if (type == StorageType.Empty)
                {
                    type = DataStorageHelper.GetStorageType(value2.GetType());
                    if (count == 1 && type == StorageType.String)
                    {
                        value = 0m;
                    }

                    if (!(type == StorageType.SqlDecimal ||
                          type == StorageType.SqlMoney ||
                          type == StorageType.Decimal ||
                          type == StorageType.Single))
                    {
                        if (ExpressionNode.IsUnsignedSql(type))
                        {
                            type = StorageType.UInt64;
                        }
                        if (ExpressionNode.IsInteger(type))
                        {
                            type = StorageType.Int64;
                        }
                        else if (ExpressionNode.IsIntegerSql(type))
                        {
                            type = StorageType.SqlInt64;
                        }
                        else if (ExpressionNode.IsFloat(type))
                        {
                            type = StorageType.Double;
                        }
                        else if (ExpressionNode.IsFloatSql(type))
                        {
                            type = StorageType.SqlDouble;
                        }
                    }
                }

                value = Add(value, value2, type);
            }
            return(value);
        }
예제 #3
0
        public static object First(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            foreach (object value in Aggregates.NotNullValue(data, node, context.GlobalContext))
            {
                return(value);
            }

            return(null);
        }
예제 #4
0
        public static object Last(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            object obj1 = (object)null;

            foreach (object obj2 in Aggregates.NotNullValue(data, node, context.GlobalContext))
            {
                obj1 = obj2;
            }
            return(obj1);
        }
예제 #5
0
 public static object First(FunctionContext context, IEnumerable data, ExpressionNode node)
 {
     using (IEnumerator <object> enumerator = Aggregates.NotNullValue(data, node, context.GlobalContext).GetEnumerator())
     {
         if (enumerator.MoveNext())
         {
             return(enumerator.Current);
         }
     }
     return((object)null);
 }
예제 #6
0
        public static object Last(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            object value = null;

            foreach (object value1 in Aggregates.NotNullValue(data, node, context.GlobalContext))
            {
                value = value1;
            }

            return(value);
        }
예제 #7
0
        private static Decimal DecimalSqrt(Decimal x, Decimal?guess = null)
        {
            Decimal valueOrDefault = guess.GetValueOrDefault(x / new Decimal(2));
            Decimal num1           = x / valueOrDefault;
            Decimal num2           = (valueOrDefault + num1) / new Decimal(2);

            if (num2 == valueOrDefault)
            {
                return(num2);
            }
            return(Aggregates.DecimalSqrt(x, new Decimal?(num2)));
        }
예제 #8
0
        public static object Sum(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            int         count;
            StorageType type;

            return(Aggregates.Sum(data
                                  , node
                                  , context.GlobalContext
                                  , "Sum"
                                  , out count
                                  , out type));
        }
예제 #9
0
        private static object Sum(
            IEnumerable data,
            ExpressionNode node,
            object context,
            string exp,
            out int count,
            out StorageType type)
        {
            count = 0;
            type  = StorageType.Empty;
            object obj1 = (object)0;

            foreach (object obj2 in Aggregates.NotNullValue(data, node, context))
            {
                ++count;
                if (type == StorageType.Empty)
                {
                    type = DataStorageHelper.GetStorageType(obj2.GetType());
                    if (count == 1 && type == StorageType.String)
                    {
                        obj1 = (object)new Decimal(0);
                    }
                    if (type != StorageType.SqlDecimal && type != StorageType.SqlMoney && (type != StorageType.Decimal && type != StorageType.Single))
                    {
                        if (ExpressionNode.IsUnsignedSql(type))
                        {
                            type = StorageType.UInt64;
                        }
                        if (ExpressionNode.IsInteger(type))
                        {
                            type = StorageType.Int64;
                        }
                        else if (ExpressionNode.IsIntegerSql(type))
                        {
                            type = StorageType.SqlInt64;
                        }
                        else if (ExpressionNode.IsFloat(type))
                        {
                            type = StorageType.Double;
                        }
                        else if (ExpressionNode.IsFloatSql(type))
                        {
                            type = StorageType.SqlDouble;
                        }
                    }
                }
                obj1 = Aggregates.Add(obj1, obj2, type);
            }
            return(obj1);
        }
예제 #10
0
        public static object Min(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            object obj1 = (object)null;

            foreach (object obj2 in Aggregates.NotNullValue(data, node, context.GlobalContext))
            {
                if (obj1 == null)
                {
                    obj1 = obj2;
                }
                else if (0 < Aggregates.Compare(obj1, obj2, context, nameof(Min)))
                {
                    obj1 = obj2;
                }
            }
            return(obj1);
        }
예제 #11
0
        public static object Min(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            object value = null;

            foreach (object value2 in Aggregates.NotNullValue(data, node, context.GlobalContext))
            {
                if (null == value)
                {
                    value = value2;
                }
                else if (0 < Aggregates.Compare(value, value2, context, "Min"))
                {
                    value = value2;
                }
            }

            return(value);
        }
예제 #12
0
        public static object StDev(FunctionContext context, IEnumerable data, ExpressionNode node)
        {
            IList <Decimal> numList = (IList <Decimal>) new List <Decimal>();
            Decimal         num1    = new Decimal(0);

            foreach (object obj in Aggregates.NotNullValue(data, node, (object)context))
            {
                StorageType storageType = DataStorageHelper.GetStorageType(obj.GetType());
                if (ExpressionNode.IsNumeric(storageType))
                {
                    Decimal num2 = Convert.ToDecimal(obj);
                    num1 += num2;
                    numList.Add(num2);
                }
                else
                {
                    if (storageType != StorageType.String && storageType != StorageType.SqlString)
                    {
                        throw new NotSupportedException("Standard deviation is not supported for type " + storageType.ToString());
                    }
                    Decimal result;
                    if (Decimal.TryParse(obj.ToString(), out result))
                    {
                        num1 += result;
                        numList.Add(result);
                    }
                }
            }
            if (numList.Count == 0)
            {
                return((object)0);
            }
            Decimal num3 = num1 / (Decimal)numList.Count;
            Decimal num4 = new Decimal(0);

            foreach (Decimal num2 in (IEnumerable <Decimal>)numList)
            {
                Decimal num5 = num2 - num3;
                Decimal num6 = num5 * num5;
                num4 += num6;
            }
            return((object)Aggregates.DecimalSqrt(num4 / (Decimal)numList.Count, new Decimal?()));
        }
예제 #13
0
        private static object Mean(
            IEnumerable data,
            ExpressionNode node,
            object context,
            string exp,
            out int count,
            out StorageType type)
        {
            count = 0;
            type  = StorageType.Empty;
            object obj = Aggregates.Sum(data, node, context, exp, out count, out type);

            if (count == 0)
            {
                return((object)0);
            }
            switch (type)
            {
            case StorageType.Char:
                return((object)((int)(char)obj / count));

            case StorageType.SByte:
            case StorageType.Byte:
            case StorageType.Int16:
            case StorageType.UInt16:
            case StorageType.Int32:
            case StorageType.UInt32:
                return((object)(Convert.ToDouble(obj) / (double)count));

            case StorageType.Int64:
            case StorageType.UInt64:
                return((object)(Convert.ToDecimal(obj) / (Decimal)count));

            case StorageType.Single:
                return((object)(float)((double)(float)obj / (double)count));

            case StorageType.Double:
                return((object)((double)obj / (double)count));

            case StorageType.Decimal:
            case StorageType.String:
                return((object)((Decimal)obj / (Decimal)count));

            case StorageType.TimeSpan:
                return((object)new TimeSpan(((TimeSpan)obj).Ticks / (long)count));

            case StorageType.SqlByte:
                return((object)((SqlDouble)((SqlByte)obj) / (SqlDouble)((double)count)));

            case StorageType.SqlDecimal:
                return((object)((SqlDouble)((SqlDecimal)obj) / (SqlDouble)((double)count)));

            case StorageType.SqlDouble:
                return((object)((SqlDouble)obj / (SqlDouble)((double)count)));

            case StorageType.SqlInt16:
                return((object)((SqlDouble)((SqlInt16)obj) / (SqlDouble)((double)(short)count)));

            case StorageType.SqlInt32:
                return((object)((SqlDouble)((SqlInt32)obj) / (SqlDouble)((double)count)));

            case StorageType.SqlInt64:
                return((object)((SqlDouble)((SqlInt64)obj) / (SqlDouble)((double)count)));

            case StorageType.SqlMoney:
                return((object)((SqlMoney)obj / (SqlMoney)((long)count)));

            case StorageType.SqlSingle:
                return((object)((SqlSingle)obj / (SqlSingle)((float)count)));

            default:
                throw InvalidExpressionException.TypeMismatch(exp);
            }
        }