dotnet-dynamic-query/PoweredSoft.DynamicQuery/QueryHandler.cs

127 lines
4.6 KiB
C#
Raw Normal View History

2018-10-18 21:52:05 -04:00
using System;
2018-10-17 22:14:21 -04:00
using System.Collections.Generic;
2018-11-15 20:42:30 -05:00
using System.Diagnostics;
2018-10-17 22:14:21 -04:00
using System.Linq;
using System.Reflection;
using System.Text;
2018-10-18 21:52:05 -04:00
using PoweredSoft.DynamicLinq;
2018-10-21 14:15:21 -04:00
using PoweredSoft.DynamicLinq.Fluent;
2018-10-18 21:52:05 -04:00
using PoweredSoft.DynamicQuery.Core;
2018-10-17 22:14:21 -04:00
namespace PoweredSoft.DynamicQuery
{
2018-10-18 21:52:05 -04:00
public class QueryHandler : QueryHandlerBase, IQueryHandler
2018-10-17 22:14:21 -04:00
{
2018-10-18 21:52:05 -04:00
internal MethodInfo ExecuteGeneric = typeof(QueryHandler).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic).First(t => t.Name == "Execute" && t.IsGenericMethod);
internal IQueryExecutionResult ExecuteReflected() => (IQueryExecutionResult)ExecuteGeneric.MakeGenericMethod(QueryableUnderlyingType).Invoke(this, new object[] { });
2018-10-17 22:14:21 -04:00
2018-10-18 21:52:05 -04:00
protected virtual IQueryExecutionResult Execute<T>()
2018-10-17 22:14:21 -04:00
{
CommonBeforeExecute<T>();
2018-10-18 21:52:05 -04:00
return HasGrouping ? ExecuteGrouping<T>() : ExecuteNoGrouping<T>();
2018-10-17 22:14:21 -04:00
}
2018-10-18 21:52:05 -04:00
protected virtual IQueryExecutionResult ExecuteGrouping<T>()
2018-10-17 22:14:21 -04:00
{
var result = new QueryExecutionResult();
2018-10-21 17:06:45 -04:00
// preserve queryable.
var queryableAfterFilters = CurrentQueryable;
result.TotalRecords = queryableAfterFilters.LongCount();
CalculatePageCount(result);
2018-10-19 17:44:13 -04:00
2018-10-21 14:15:21 -04:00
// intercept groups in advance to avoid doing it more than once :)
var finalGroups = Criteria.Groups.Select(g => InterceptGroup<T>(g)).ToList();
// get the aggregates.
2018-10-21 17:37:59 -04:00
var aggregateResults = FetchAggregates<T>(finalGroups);
2018-10-21 14:15:21 -04:00
// sorting.
2018-10-21 15:46:33 -04:00
finalGroups.ForEach(fg => Criteria.Sorts.Insert(0, new Sort(fg.Path, fg.Ascending)));
2018-10-19 17:44:13 -04:00
2018-10-21 15:46:33 -04:00
// apply sorting and paging.
2018-10-21 14:15:21 -04:00
ApplySorting<T>();
ApplyPaging<T>();
2018-10-21 15:46:33 -04:00
// create group & select expression.
CurrentQueryable = CurrentQueryable.GroupBy(QueryableUnderlyingType, gb => finalGroups.ForEach((fg, index) => gb.Path(fg.Path, $"Key_{index}")));
2018-10-21 14:15:21 -04:00
CurrentQueryable = CurrentQueryable.Select(sb =>
{
finalGroups.ForEach((fg, index) => sb.Key($"Key_{index}", $"Key_{index}"));
2018-10-21 14:15:21 -04:00
sb.ToList("Records");
});
2018-10-21 15:46:33 -04:00
// loop through the grouped records.
var groupRecords = CurrentQueryable.ToDynamicClassList();
2018-11-15 20:42:30 -05:00
// now join them into logical collections
result.Data = RecursiveRegroup<T>(groupRecords, aggregateResults, Criteria.Groups.First());
2018-10-21 17:37:59 -04:00
result.Aggregates = CalculateTotalAggregate<T>(queryableAfterFilters);
2018-10-19 17:44:13 -04:00
return result;
2018-10-17 22:14:21 -04:00
}
2018-11-15 20:42:30 -05:00
2018-10-21 17:37:59 -04:00
protected virtual List<IAggregateResult> CalculateTotalAggregate<T>(IQueryable queryableAfterFilters)
2018-10-21 17:06:45 -04:00
{
if (!Criteria.Aggregates.Any())
return null;
IQueryable selectExpression = CreateTotalAggregateSelectExpression<T>(queryableAfterFilters);
2018-11-15 20:42:30 -05:00
var aggregateResult = selectExpression.ToDynamicClassList().FirstOrDefault();
return MaterializeCalculateTotalAggregateResult(aggregateResult);
2018-10-21 17:06:45 -04:00
}
protected virtual List<List<DynamicClass>> FetchAggregates<T>(List<IGroup> finalGroups)
2018-10-21 15:46:33 -04:00
{
2018-10-21 17:37:59 -04:00
if (!Criteria.Aggregates.Any())
return null;
2018-10-21 21:34:53 -04:00
2018-10-21 15:46:33 -04:00
var previousGroups = new List<IGroup>();
2018-10-21 21:34:53 -04:00
var ret = finalGroups.Select(fg =>
2018-10-21 15:46:33 -04:00
{
IQueryable selectExpression = CreateFetchAggregateSelectExpression<T>(fg, previousGroups);
2018-10-21 15:46:33 -04:00
var aggregateResult = selectExpression.ToDynamicClassList();
previousGroups.Add(fg);
return aggregateResult;
}).ToList();
2018-10-21 21:34:53 -04:00
return ret;
2018-10-21 15:46:33 -04:00
}
2018-10-21 14:15:21 -04:00
2018-10-18 21:52:05 -04:00
protected virtual IQueryExecutionResult ExecuteNoGrouping<T>()
2018-10-17 22:14:21 -04:00
{
2018-10-18 21:52:05 -04:00
var result = new QueryExecutionResult();
2018-10-17 22:14:21 -04:00
2018-10-21 17:06:45 -04:00
// after filter queryable
var afterFilterQueryable = CurrentQueryable;
2018-10-18 21:52:05 -04:00
// total records.
2018-10-21 17:06:45 -04:00
result.TotalRecords = afterFilterQueryable.LongCount();
CalculatePageCount(result);
2018-10-17 22:14:21 -04:00
2018-10-18 21:52:05 -04:00
// sorts and paging.
2018-10-21 14:15:21 -04:00
ApplySorting<T>();
ApplyPaging<T>();
// data.
var entities = ((IQueryable<T>)CurrentQueryable).ToList();
var records = InterceptConvertTo<T>(entities);
result.Data = records;
// aggregates.
2018-10-21 17:37:59 -04:00
result.Aggregates = CalculateTotalAggregate<T>(afterFilterQueryable);
2018-10-17 22:14:21 -04:00
2018-10-18 21:52:05 -04:00
return result;
2018-10-17 22:14:21 -04:00
}
2018-10-17 22:14:21 -04:00
2018-10-18 21:52:05 -04:00
public virtual IQueryExecutionResult Execute(IQueryable queryable, IQueryCriteria criteria)
2018-10-17 22:14:21 -04:00
{
Reset(queryable, criteria);
2018-10-18 21:52:05 -04:00
return ExecuteReflected();
2018-10-17 22:14:21 -04:00
}
}
}