2018-04-10 21:08:27 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Helpers;
|
2018-04-05 18:54:28 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Parser;
|
2018-04-02 12:47:30 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2018-04-10 21:08:27 -04:00
|
|
|
|
using System.Text;
|
2018-04-02 12:47:30 -04:00
|
|
|
|
|
2018-04-10 21:08:27 -04:00
|
|
|
|
namespace PoweredSoft.DynamicLinq.Resolver
|
2018-04-02 12:47:30 -04:00
|
|
|
|
{
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public class PathExpressionResolver
|
|
|
|
|
{
|
2018-04-10 21:08:27 -04:00
|
|
|
|
public bool NullChecking { get; set; } = false;
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public SelectCollectionHandling CollectionHandling { get; set; } = SelectCollectionHandling.LeaveAsIs;
|
|
|
|
|
public ExpressionParser Parser { get; protected set; }
|
2018-04-05 18:54:28 -04:00
|
|
|
|
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public Expression Result { get; protected set; }
|
2018-04-10 20:00:40 -04:00
|
|
|
|
public Type NullType { get; set; }
|
2018-04-02 12:57:46 -04:00
|
|
|
|
|
|
|
|
|
public PathExpressionResolver(ExpressionParser parser)
|
|
|
|
|
{
|
|
|
|
|
Parser = parser;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-10 21:08:27 -04:00
|
|
|
|
protected Expression CompileGroup(ExpressionParserPieceGroup group, bool nullChecking)
|
2018-04-10 20:00:40 -04:00
|
|
|
|
{
|
|
|
|
|
var expr = group.Parameter as Expression;
|
|
|
|
|
group.Pieces.ForEach(piece =>
|
|
|
|
|
{
|
|
|
|
|
expr = Expression.PropertyOrField(expr, piece.Name);
|
|
|
|
|
});
|
|
|
|
|
return expr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-02 12:57:46 -04:00
|
|
|
|
public void Resolve()
|
|
|
|
|
{
|
2018-04-02 15:09:57 -04:00
|
|
|
|
Result = null;
|
|
|
|
|
|
|
|
|
|
// parse the expression.
|
2018-04-11 23:18:10 -04:00
|
|
|
|
if (!Parser.IsParsed)
|
|
|
|
|
Parser.Parse();
|
2018-04-02 12:57:46 -04:00
|
|
|
|
|
2018-04-05 18:54:28 -04:00
|
|
|
|
// group the piece by common parameters
|
|
|
|
|
var groups = Parser.GroupBySharedParameters();
|
2018-04-04 22:57:40 -04:00
|
|
|
|
|
2018-04-05 20:29:50 -04:00
|
|
|
|
Expression currentExpression = null;
|
2018-04-10 21:08:27 -04:00
|
|
|
|
foreach (var group in groups.Reversed())
|
|
|
|
|
{
|
|
|
|
|
var isLastGroup = groups.IndexOf(group) == groups.Count - 1;
|
|
|
|
|
|
2018-04-05 20:29:50 -04:00
|
|
|
|
if (currentExpression == null)
|
2018-04-05 20:28:07 -04:00
|
|
|
|
{
|
2018-04-10 21:08:27 -04:00
|
|
|
|
var groupExpression = CompileGroup(group, NullChecking);
|
2018-04-05 20:29:50 -04:00
|
|
|
|
var groupExpressionLambda = Expression.Lambda(groupExpression, group.Parameter);
|
2018-04-05 20:28:07 -04:00
|
|
|
|
|
|
|
|
|
if (group.Parent == null)
|
|
|
|
|
{
|
2018-04-05 20:29:50 -04:00
|
|
|
|
currentExpression = groupExpressionLambda;
|
2018-04-10 20:29:43 -04:00
|
|
|
|
continue;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parent = group.Parent;
|
2018-04-10 21:08:27 -04:00
|
|
|
|
var parentExpression = CompileGroup(parent, NullChecking);
|
2018-04-10 20:00:40 -04:00
|
|
|
|
|
|
|
|
|
// check null with where.
|
2018-04-11 23:04:54 -04:00
|
|
|
|
var isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression);
|
2018-04-10 21:08:27 -04:00
|
|
|
|
if (NullChecking != false)
|
2018-04-11 23:04:54 -04:00
|
|
|
|
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany);
|
2018-04-10 20:00:40 -04:00
|
|
|
|
|
|
|
|
|
// the select expression.
|
2018-04-11 23:04:54 -04:00
|
|
|
|
if (isSelectMany)
|
2018-04-11 22:39:54 -04:00
|
|
|
|
{
|
|
|
|
|
var selectType = parent.GroupEnumerableType();
|
|
|
|
|
var selectExpression = Expression.Call(typeof(Enumerable), "SelectMany",
|
|
|
|
|
new Type[] { selectType, groupExpression.Type.GenericTypeArguments.First() },
|
|
|
|
|
parentExpression, groupExpressionLambda);
|
|
|
|
|
currentExpression = selectExpression;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var selectType = parent.GroupEnumerableType();
|
|
|
|
|
var selectExpression = Expression.Call(typeof(Enumerable), "Select",
|
|
|
|
|
new Type[] { selectType, groupExpression.Type },
|
|
|
|
|
parentExpression, groupExpressionLambda);
|
|
|
|
|
currentExpression = selectExpression;
|
|
|
|
|
}
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (group.Parent == null)
|
|
|
|
|
{
|
2018-04-10 21:08:27 -04:00
|
|
|
|
if (NullChecking != false)
|
2018-04-10 20:29:43 -04:00
|
|
|
|
currentExpression = CheckNullOnFirstGroup(group, currentExpression);
|
|
|
|
|
|
2018-04-05 20:29:50 -04:00
|
|
|
|
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
|
2018-04-10 20:29:43 -04:00
|
|
|
|
continue;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parent = group.Parent;
|
2018-04-10 21:08:27 -04:00
|
|
|
|
var parentExpression = CompileGroup(parent, NullChecking);
|
2018-04-05 20:28:07 -04:00
|
|
|
|
var selectType = parent.GroupEnumerableType();
|
2018-04-10 20:08:31 -04:00
|
|
|
|
|
2018-04-11 23:04:54 -04:00
|
|
|
|
bool isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(currentExpression);
|
2018-04-10 21:08:27 -04:00
|
|
|
|
if (NullChecking != false)
|
2018-04-11 23:04:54 -04:00
|
|
|
|
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany);
|
2018-04-10 20:08:31 -04:00
|
|
|
|
|
2018-04-11 23:04:54 -04:00
|
|
|
|
if (isSelectMany)
|
2018-04-11 22:39:54 -04:00
|
|
|
|
{
|
|
|
|
|
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
|
|
|
|
|
currentExpression = Expression.Call(typeof(Enumerable), "SelectMany",
|
|
|
|
|
new Type[] { selectType, currentExpression.Type.GenericTypeArguments.First() },
|
|
|
|
|
parentExpression, currentExpressionLambda);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
|
|
|
|
|
currentExpression = Expression.Call(typeof(Enumerable), "Select",
|
|
|
|
|
new Type[] { selectType, currentExpression.Type },
|
|
|
|
|
parentExpression, currentExpressionLambda);
|
|
|
|
|
}
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
2018-04-10 20:29:43 -04:00
|
|
|
|
}
|
2018-04-10 19:26:44 -04:00
|
|
|
|
|
|
|
|
|
Result = currentExpression;
|
2018-04-05 20:28:07 -04:00
|
|
|
|
}
|
2018-04-10 20:29:43 -04:00
|
|
|
|
|
|
|
|
|
private Expression CheckNullOnFirstGroup(ExpressionParserPieceGroup group, Expression currentExpression)
|
|
|
|
|
{
|
|
|
|
|
var path = string.Join(".", group.Pieces.Select(t => t.Name));
|
|
|
|
|
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
|
2018-04-10 21:08:27 -04:00
|
|
|
|
ConditionOperators.Equal, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
2018-04-10 20:29:43 -04:00
|
|
|
|
parameter: group.Parameter, nullChecking: true);
|
|
|
|
|
|
|
|
|
|
var whereBodyExpression = (whereExpression as LambdaExpression).Body;
|
|
|
|
|
|
2018-04-10 21:08:27 -04:00
|
|
|
|
var nullType = currentExpression.Type;
|
2018-04-10 20:29:43 -04:00
|
|
|
|
Expression ifTrueExpression = null;
|
2018-04-10 21:08:27 -04:00
|
|
|
|
if (QueryableHelpers.IsGenericEnumerable(nullType))
|
2018-04-10 20:29:43 -04:00
|
|
|
|
{
|
2018-04-10 21:08:27 -04:00
|
|
|
|
var listType = typeof(List<>).MakeGenericType(nullType.GenericTypeArguments.First());
|
2018-04-10 20:29:43 -04:00
|
|
|
|
ifTrueExpression = Expression.New(listType);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ifTrueExpression = Expression.Default(NullType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 23:04:54 -04:00
|
|
|
|
private static Expression CheckNullOnEnumerableParent(ExpressionParserPieceGroup group, ExpressionParserPieceGroup parent, Expression parentExpression, bool shouldSkipLast)
|
2018-04-10 20:29:43 -04:00
|
|
|
|
{
|
2018-04-10 21:08:27 -04:00
|
|
|
|
string path = null;
|
2018-04-11 23:04:54 -04:00
|
|
|
|
if (shouldSkipLast)
|
2018-04-10 21:08:27 -04:00
|
|
|
|
path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name));
|
|
|
|
|
else
|
|
|
|
|
path = string.Join(".", group.Pieces.Select(t => t.Name));
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
2018-04-10 20:29:43 -04:00
|
|
|
|
{
|
|
|
|
|
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
|
|
|
|
|
ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
|
|
|
|
parameter: group.Parameter, nullChecking: true);
|
|
|
|
|
|
|
|
|
|
//public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
|
|
|
|
|
parentExpression = Expression.Call(typeof(Enumerable), "Where",
|
|
|
|
|
new Type[] { parent.GroupEnumerableType() },
|
|
|
|
|
parentExpression, whereExpression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parentExpression;
|
|
|
|
|
}
|
2018-04-11 23:18:10 -04:00
|
|
|
|
|
|
|
|
|
public Expression GetResultBodyExpression()
|
|
|
|
|
{
|
|
|
|
|
if (Result == null)
|
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
|
|
return ((LambdaExpression)Result).Body;
|
|
|
|
|
}
|
2018-04-02 12:47:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|