Ejemplo n.º 1
0
        protected static ProcessResult CreateResult(ActionContext context, IQueryable query, int?takeCount, IQueryable inlineCountQuery)
        {
            int?max = context.MaxResultCount ?? context.Service?.MaxResultCount;

            if (max > 0)
            {
                var count = takeCount ?? Queryable.Count((dynamic)query);
                if (count > max)
                {
                    throw new Exception($"Maximum allowed read count exceeded");
                }
            }

            var service = context.Service;

            if (service == null)
            {
                return(CreateResult(context, Enumerable.ToList((dynamic)query), inlineCountQuery));
            }

            var beforeArgs = new BeforeQueryEventArgs(context, query);

            service.OnBeforeQueryExecute(beforeArgs);
            query = beforeArgs.Query;

            var result    = Enumerable.ToList((dynamic)query);
            var afterArgs = new AfterQueryEventArgs(context, query, result);

            service.OnAfterQueryExecute(afterArgs);
            result = afterArgs.Result;

            return(CreateResult(context, result, inlineCountQuery));
        }
Ejemplo n.º 2
0
 void ILinquestService.OnBeforeQueryExecute(BeforeQueryEventArgs args)
 => BeforeQueryExecute?.Invoke(this, args);
Ejemplo n.º 3
0
        public virtual ProcessResult HandleContent(IQueryable query, ActionContext context)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var service = context.Service;

            if (service != null)
            {
                var args = new BeforeQueryEventArgs(context, query);
                service.OnBeforeHandleQuery(args);
                query = args.Query;
            }

            var parameters = context.Parameters;

            if (parameters == null || !parameters.Any())
            {
                return(CreateResult(context, query, null, null));
            }

            var        inlineCountEnabled = false;
            int?       takeCount          = null;
            IQueryable inlineCountQuery   = null;

            foreach (var prm in parameters)
            {
                switch (prm.Name.ToLowerInvariant())
                {
                case "$inlinecount":
                    inlineCountEnabled = prm.Value != "false" || prm.Value == "allpages";
                    break;

                case "$filter":
                case "$where":
                    inlineCountQuery = null;
                    query            = Where(query, prm.Value);
                    break;

                case "$orderby":
                    query = OrderBy(query, prm.Value);
                    break;

                case "$orderbydescending":
                    query = OrderbyDescending(query, prm.Value);
                    break;

                case "$thenby":
                    query = ThenBy(query, prm.Value);
                    break;

                case "$thenbydescending":
                    query = ThenByDescending(query, prm.Value);
                    break;

                case "$select":
                    query = Select(query, prm.Value);
                    break;

                case "$skip":
                    if (inlineCountEnabled && inlineCountQuery == null)
                    {
                        inlineCountQuery = query;
                    }
                    query = Skip(query, Convert.ToInt32(prm.Value));
                    break;

                case "$top":
                case "$take":
                    if (inlineCountEnabled && inlineCountQuery == null)
                    {
                        inlineCountQuery = query;
                    }
                    var take = Convert.ToInt32(prm.Value);
                    query     = Take(query, take);
                    takeCount = take;
                    break;

                case "$groupby":
                    inlineCountQuery = null;
                    var keyValue = prm.Value.Split(';');
                    if (keyValue.Length > 2)
                    {
                        throw new Exception("Invalid groupBy expression");
                    }

                    var keySelector   = keyValue[0];
                    var valueSelector = keyValue.Length == 2 ? keyValue[1] : null;
                    query = GroupBy(query, keySelector, valueSelector);
                    break;

                case "$distinct":
                    inlineCountQuery = null;
                    query            = Distinct(query);
                    break;

                case "$reverse":
                    query = Reverse(query);
                    break;

                case "$selectmany":
                    inlineCountQuery = null;
                    takeCount        = null;
                    query            = SelectMany(query, prm.Value);
                    break;

                case "$skipwhile":
                    inlineCountQuery = null;
                    query            = SkipWhile(query, prm.Value);
                    break;

                case "$takewhile":
                    inlineCountQuery = null;
                    query            = TakeWhile(query, prm.Value);
                    break;

                case "$aggregate":
                    var funcSeed = prm.Value.Split(';');
                    if (funcSeed.Length > 2)
                    {
                        throw new Exception("Invalid aggregate expression");
                    }

                    var func = funcSeed[0];
                    var seed = funcSeed.Length == 2 ? funcSeed[1] : null;
                    return(CreateResult(context, Aggregate(query, func, seed), inlineCountQuery));

                case "$all":
                    return(CreateResult(context, All(query, prm.Value), inlineCountQuery));

                case "$any":
                    return(CreateResult(context, Any(query, prm.Value), inlineCountQuery));

                case "$average":
                case "$avg":
                    return(CreateResult(context, Avg(query, prm.Value), inlineCountQuery));

                case "$max":
                    return(CreateResult(context, Max(query, prm.Value), inlineCountQuery));

                case "$min":
                    return(CreateResult(context, Min(query, prm.Value), inlineCountQuery));

                case "$sum":
                    return(CreateResult(context, Sum(query, prm.Value), inlineCountQuery));

                case "$count":
                    return(CreateResult(context, Count(query, prm.Value), inlineCountQuery));

                case "$first":
                    return(CreateResult(context, First(query, prm.Value), inlineCountQuery));

                case "$firstordefault":
                    return(CreateResult(context, FirstOrDefault(query, prm.Value), inlineCountQuery));

                case "$single":
                    return(CreateResult(context, Single(query, prm.Value), inlineCountQuery));

                case "$singleordefault":
                    return(CreateResult(context, SingleOrDefault(query, prm.Value), inlineCountQuery));

                case "$last":
                    return(CreateResult(context, Last(query, prm.Value), inlineCountQuery));

                case "$lastordefault":
                    return(CreateResult(context, LastOrDefault(query, prm.Value), inlineCountQuery));

                case "$elementat":
                    return(CreateResult(context, ElementAt(query, Convert.ToInt32(prm.Value)), inlineCountQuery));

                case "$elementatordefault":
                    return(CreateResult(context, ElementAtOrDefault(query, Convert.ToInt32(prm.Value)), inlineCountQuery));

                default:
                    throw new Exception($"Unknown query parameter {prm.Value}");
                }
            }

            return(CreateResult(context, query, takeCount, inlineCountQuery));
        }
Ejemplo n.º 4
0
 void ILinquestService.OnBeforeHandleQuery(BeforeQueryEventArgs args)
 => BeforeHandleQuery?.Invoke(this, args);