/
QueryExpressionVisitor.cs
172 lines (149 loc) · 6.79 KB
/
QueryExpressionVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services.Providers;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using DataServiceProvider;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
namespace Mongo.Context.Queryable
{
public class QueryExpressionVisitor : DSPMethodTranslatingVisitor
{
private IQueryable queryableCollection;
private Type collectionType;
public QueryExpressionVisitor(MongoCollection mongoCollection, Type queryDocumentType)
{
var genericMethod = typeof(LinqExtensionMethods).GetMethods()
.Where(x => x.Name == "AsQueryable" && x.GetParameters().Single().ParameterType.IsGenericType)
.Single();
var method = genericMethod.MakeGenericMethod(queryDocumentType);
this.queryableCollection = method.Invoke(null, new object[] { mongoCollection }) as IQueryable;
this.collectionType = queryDocumentType;
}
public override Expression VisitMethodCall(MethodCallExpression m)
{
if (m.Method.Name == "GetValue" && m.Arguments[0].NodeType == ExpressionType.MemberAccess && (m.Arguments[0] as MemberExpression).Expression.Type == typeof(ResourceProperty))
{
var fieldName = (((m.Arguments[0] as MemberExpression).Expression as ConstantExpression).Value as ResourceProperty).Name;
var parameterExpression = Expression.Parameter(this.collectionType, (m.Object as ParameterExpression).Name);
var member = this.collectionType.GetMember(fieldName).First();
return Expression.MakeMemberAccess(parameterExpression, member);
}
else if (m.Method.GetGenericArguments().Any() && m.Method.GetGenericArguments()[0] == typeof(DSPResource))
{
if (m.Method.Name == "OrderBy" || m.Method.Name == "OrderByDescending")
{
var lambda = Visit(ReplaceFieldLambda(m.Arguments[1]));
return Visit(Expression.Call(
ReplaceGenericMethodType(m.Method, (lambda as LambdaExpression).ReturnType),
Visit(m.Arguments[0]),
lambda));
}
else
{
return Visit(Expression.Call(
ReplaceGenericMethodType(m.Method),
new ReadOnlyCollection<Expression>(m.Arguments.Select(x => Visit(x)).ToList())));
}
}
return base.VisitMethodCall(m);
}
public override Expression VisitConstant(ConstantExpression c)
{
if (c.Value != null && c.Value.GetType() == typeof(EnumerableQuery<DSPResource>))
{
return Expression.Constant(this.queryableCollection);
}
else if (c.Type.IsGenericType && c.Type.BaseType == typeof(ValueType) && c.Type.UnderlyingSystemType.Name == "Nullable`1")
{
return Expression.Constant(c.Value);
}
return base.VisitConstant(c);
}
public override Expression VisitLambda(LambdaExpression lambda)
{
if (lambda.Parameters.Count > 0 && lambda.Parameters[0].Type == typeof(DSPResource))
{
return Visit(Expression.Lambda(
Visit(lambda.Body),
ReplaceLambdaParameterType(lambda)));
}
return base.VisitLambda(lambda);
}
public override Expression VisitConditional(ConditionalExpression c)
{
if (ExpressionUtils.IsEqualityWithNullability(c))
{
if (ExpressionUtils.IsConvertWithMethod(c.IfFalse, "Contains"))
{
return Visit((c.IfFalse as UnaryExpression).Operand);
}
else
{
return Visit(c.IfFalse);
}
}
return base.VisitConditional(c);
}
public override Expression VisitBinary(BinaryExpression b)
{
if (b.Left.Type == typeof(Nullable<bool>) && b.Right.Type == typeof(Nullable<bool>) &&
b.NodeType == ExpressionType.Equal && (b.Right as UnaryExpression).NodeType == ExpressionType.Convert)
{
return Visit(ReplaceBinaryComparison(b));
}
return base.VisitBinary(b);
}
public override Expression VisitUnary(UnaryExpression u)
{
if (u.NodeType == ExpressionType.Convert)
{
if (u.Type.IsValueType || u.Type == typeof(string))
{
return Visit(u.Operand);
}
}
return base.VisitUnary(u);
}
private MethodInfo ReplaceGenericMethodType(MethodInfo method, params Type[] genericTypes)
{
var genericArguments = new List<Type>();
genericArguments.Add(this.collectionType);
genericArguments.AddRange(genericTypes);
genericArguments.AddRange(method.GetGenericArguments().Skip(1 + genericTypes.Count()));
return typeof(System.Linq.Queryable).GetMethods()
.Where(x => x.Name == method.Name).First()
.MakeGenericMethod(genericArguments.ToArray());
}
private IEnumerable<ParameterExpression> ReplaceLambdaParameterType(LambdaExpression lambda)
{
var parameterExpressions = new List<ParameterExpression>();
parameterExpressions.Add(Expression.Parameter(this.collectionType, lambda.Parameters[0].Name));
parameterExpressions.AddRange(lambda.Parameters.Skip(1));
return parameterExpressions;
}
private Expression ReplaceFieldLambda(Expression expression)
{
var lambda = (expression as UnaryExpression).Operand as LambdaExpression;
return Expression.Lambda(
(lambda.Body as UnaryExpression).Operand,
lambda.Parameters);
}
private Expression ReplaceBinaryComparison(BinaryExpression b)
{
var consantExpression = (b.Right as UnaryExpression).Operand as ConstantExpression;
if (consantExpression != null && consantExpression.Type == typeof(bool))
{
if ((bool)consantExpression.Value)
return b.Left;
else
return Expression.MakeUnary(ExpressionType.Not, b.Left, b.Type);
}
return b;
}
}
}