public IAggregateFunctionExecutor GetAggregateFunctionExecutor(ExecuteAggregateFunctionStage executeAggregateFunctionStage)
        {
            var t        = typeof(DefaultAggregateFunctionExecutor <,>).MakeGenericType(executeAggregateFunctionStage.InType, executeAggregateFunctionStage.OutType);
            var executor = (IAggregateFunctionExecutor)Activator.CreateInstance(t);

            return(executor);
        }
Exemplo n.º 2
0
        public async ValueTask <IQueryable> Visit(ExecuteAggregateFunctionStage executeAggregateFunctionStage)
        {
            Debug.Assert(queryable != null);
            var aggregateFunctionExecutor = _aggregateFunctionExecutorFactory.GetAggregateFunctionExecutor(executeAggregateFunctionStage);
            var selectResult = await aggregateFunctionExecutor.Execute(queryable, executeAggregateFunctionStage);

            columns = selectResult.Columns;
            return(selectResult.Queryable);
        }
        private OutType HandleSumFunction(IQueryable <Entity> queryable, ExecuteAggregateFunctionStage executeAggregateFunctionStage)
        {
            Debug.Assert(executeAggregateFunctionStage.Parameters.Count == 1);
            var parameter = executeAggregateFunctionStage.Parameters[0];

            if (typeof(OutType) == typeof(decimal?))
            {
                return(queryable.Sum(GetParameterLambda <decimal?>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(decimal))
            {
                return(queryable.Sum(GetParameterLambda <decimal>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(double?))
            {
                return(queryable.Sum(GetParameterLambda <double?>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(double))
            {
                return(queryable.Sum(GetParameterLambda <double>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(float?))
            {
                return(queryable.Sum(GetParameterLambda <float?>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(float))
            {
                return(queryable.Sum(GetParameterLambda <float>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(int?))
            {
                return(queryable.Sum(GetParameterLambda <int?>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(int))
            {
                return(queryable.Sum(GetParameterLambda <int>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(long?))
            {
                return(queryable.Sum(GetParameterLambda <long?>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(long))
            {
                return(queryable.Sum(GetParameterLambda <long>(executeAggregateFunctionStage.ParameterExpression, parameter)).Cast <OutType>());
            }
            if (typeof(OutType) == typeof(short))
            {
                return(((short)queryable.Sum(GetParameterLambda <int>(executeAggregateFunctionStage.ParameterExpression, parameter))).Cast <OutType>());
            }

            throw new SqlErrorException($"Type ${typeof(OutType).Name} is not supported for sum operations");
        }
        public override ValueTask <OutType> ExecuteAggregateFunction(
            IQueryable <Entity> queryable,
            ExecuteAggregateFunctionStage executeAggregateFunctionStage)
        {
            switch (executeAggregateFunctionStage.FunctionName)
            {
            case "count":
                return(new ValueTask <OutType>(queryable.LongCount().Cast <OutType>()));

            case "sum":
                return(new ValueTask <OutType>(HandleSumFunction(queryable, executeAggregateFunctionStage)));

            default:
                throw new NotImplementedException($"The function {executeAggregateFunctionStage.FunctionName} is not yet implemented.");
            }
        }
        public async ValueTask <SelectResult> Execute(IQueryable queryable, ExecuteAggregateFunctionStage executeAggregateFunctionStage)
        {
            var result = await ExecuteAggregateFunction((IQueryable <Entity>) queryable, executeAggregateFunctionStage);

            var outType        = typeof(OutType);
            var anonType       = AnonType.GetAnonType(outType);
            var getDelegate    = AnonTypeUtils.GetDelegates(anonType)[0];
            var columnMetadata = new ColumnMetadata(executeAggregateFunctionStage.ColumnName, result.GetType(), getDelegate);

            List <AnonType <OutType> > output = new List <AnonType <OutType> >()
            {
                new AnonType <OutType>()
                {
                    P0 = result
                }
            };

            return(new SelectResult(output.AsQueryable(), ImmutableList.Create(columnMetadata)));
        }
 public abstract ValueTask <OutType> ExecuteAggregateFunction(IQueryable <Entity> queryable, ExecuteAggregateFunctionStage executeAggregateFunctionStage);