forked from samus/mongodb-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FieldBinder.cs
181 lines (155 loc) · 6.32 KB
/
FieldBinder.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
173
174
175
176
177
178
179
180
181
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Linq.Expressions;
using MongoDB.Util;
namespace MongoDB.Linq.Translators
{
internal class FieldBinder : ExpressionVisitor
{
private static readonly HashSet<Type> CollectionTypes = new HashSet<Type>()
{
typeof(ICollection), typeof(ICollection<>)
};
private Alias _alias;
private FieldFinder _finder;
public Expression Bind(Expression expression)
{
_alias = new Alias();
_finder = new FieldFinder();
return Visit(expression);
}
protected override Expression Visit(Expression exp)
{
if (exp == null)
return exp;
var fieldName = _finder.Find(exp);
if (fieldName != null)
return new FieldExpression(exp, _alias, fieldName);
return base.Visit(exp);
}
private class FieldFinder : ExpressionVisitor
{
private Stack<string> _fieldParts;
private bool _isBlocked;
public string Find(Expression expression)
{
if (expression.NodeType == ExpressionType.Parameter)
return null;
_fieldParts = new Stack<string>();
_isBlocked = false;
Visit(expression);
var fieldName = string.Join(".", _fieldParts.ToArray());
if (_isBlocked)
fieldName = null;
return fieldName;
}
protected override Expression Visit(Expression exp)
{
if (exp == null)
return null;
switch (exp.NodeType)
{
case ExpressionType.ArrayIndex:
case ExpressionType.Call:
case ExpressionType.MemberAccess:
case ExpressionType.Parameter:
return base.Visit(exp);
default:
_isBlocked = true;
return exp;
}
}
protected override Expression VisitBinary(BinaryExpression b)
{
//this is an ArrayIndex Node
_fieldParts.Push(((int)((ConstantExpression)b.Right).Value).ToString());
Visit(b.Left);
return b;
}
protected override Expression VisitMemberAccess(System.Linq.Expressions.MemberExpression m)
{
var declaringType = m.Member.DeclaringType;
if (!IsNativeToMongo(declaringType) && !IsCollection(declaringType))
{
_fieldParts.Push(m.Member.Name);
Visit(m.Expression);
return m;
}
_isBlocked = true;
return m;
}
protected override Expression VisitMethodCall(MethodCallExpression m)
{
if (m.Method.DeclaringType == typeof(Queryable) || m.Method.DeclaringType == typeof(Enumerable))
{
if (m.Method.Name == "ElementAt" || m.Method.Name == "ElementAtOrDefault")
{
_fieldParts.Push(((int)((ConstantExpression)m.Arguments[1]).Value).ToString());
Visit(m.Arguments[0]);
return m;
}
}
else if (m.Method.DeclaringType == typeof(MongoQueryable))
{
if (m.Method.Name == "Key")
{
_fieldParts.Push((string)((ConstantExpression)m.Arguments[1]).Value);
Visit(m.Arguments[0]);
return m;
}
}
else if (typeof(Document).IsAssignableFrom(m.Method.DeclaringType))
{
if (m.Method.Name == "get_Item") //TODO: does this work for VB?
{
_fieldParts.Push((string)((ConstantExpression)m.Arguments[0]).Value);
Visit(m.Object);
return m;
}
}
else if (typeof(IList<>).IsOpenTypeAssignableFrom(m.Method.DeclaringType) || typeof(IList).IsAssignableFrom(m.Method.DeclaringType))
{
if (m.Method.Name == "get_Item")
{
_fieldParts.Push(((int)((ConstantExpression)m.Arguments[0]).Value).ToString());
Visit(m.Object);
return m;
}
}
_isBlocked = true;
return m;
}
//protected override Expression VisitParameter(ParameterExpression p)
//{
// if (p.Type.IsGenericType && p.Type.GetGenericTypeDefinition() == typeof(IGrouping<,>))
// _isBlocked = true;
// return base.VisitParameter(p);
//}
private static bool IsCollection(Type type)
{
//HACK: this is going to generally subvert custom objects that implement ICollection or ICollection<T>,
//but are not collections
if (type.IsGenericType)
type = type.GetGenericTypeDefinition();
return CollectionTypes.Any(x => x.IsAssignableFrom(type));
}
private static bool IsNativeToMongo(Type type)
{
//TODO: this code exists here and in BsonClassMapDescriptor. Should probably be centralized...
var typeCode = Type.GetTypeCode(type);
if (typeCode != TypeCode.Object)
return true;
if (type == typeof(Guid))
return true;
if (type == typeof(Oid))
return true;
if (type == typeof(byte[]))
return true;
return false;
}
}
}
}