2018-10-18 21:52:05 -04:00
|
|
|
|
using System;
|
2018-10-17 22:14:21 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2018-10-18 21:52:05 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq;
|
|
|
|
|
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
|
|
|
|
{
|
2018-10-18 21:52:05 -04:00
|
|
|
|
ApplyIncludeStrategyInterceptors<T>();
|
|
|
|
|
ApplyBeforeFilterInterceptors<T>();
|
2018-10-17 22:14:21 -04:00
|
|
|
|
ApplyFilters<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
|
|
|
|
{
|
2018-10-18 21:52:05 -04:00
|
|
|
|
throw new NotImplementedException();
|
2018-10-17 22:14: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-18 21:52:05 -04:00
|
|
|
|
// total records.
|
|
|
|
|
result.TotalRecords = CurrentQueryable.LongCount();
|
2018-10-17 22:14:21 -04:00
|
|
|
|
|
2018-10-18 21:52:05 -04:00
|
|
|
|
// sorts and paging.
|
|
|
|
|
ApplyNoGroupingSorts<T>();
|
|
|
|
|
ApplyNoGroupingPaging<T>();
|
|
|
|
|
|
|
|
|
|
// the data.
|
|
|
|
|
result.Data = CurrentQueryable.ToObjectList();
|
2018-10-17 22:14:21 -04:00
|
|
|
|
|
2018-10-18 21:52:05 -04:00
|
|
|
|
// if there is paging.
|
|
|
|
|
if (HasPaging)
|
|
|
|
|
{
|
|
|
|
|
if (result.TotalRecords < Criteria.PageSize)
|
|
|
|
|
result.NumberOfPages = 1;
|
|
|
|
|
else
|
|
|
|
|
result.NumberOfPages = result.TotalRecords / Criteria.PageSize + (result.TotalRecords % Criteria.PageSize != 0 ? 1 : 0);
|
|
|
|
|
}
|
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-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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|