forked from samus/mongodb-csharp
/
ProjectionBuilder.cs
106 lines (89 loc) · 3.24 KB
/
ProjectionBuilder.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using MongoDB.Linq.Expressions;
using System.Collections.ObjectModel;
namespace MongoDB.Linq.Translators
{
internal class ProjectionBuilder : MongoExpressionVisitor
{
private bool _isMapReduce;
private ParameterExpression _document;
private GroupingKeyDeterminer _determiner;
public ProjectionBuilder()
{
_determiner = new GroupingKeyDeterminer();
}
public LambdaExpression Build(Expression projector, Type documentType, string parameterName, bool isMapReduce)
{
_isMapReduce = isMapReduce;
if (_isMapReduce)
_document = Expression.Parameter(typeof(Document), parameterName);
else
_document = Expression.Parameter(documentType, parameterName);
return Expression.Lambda(Visit(projector), _document);
}
protected override Expression VisitField(FieldExpression field)
{
if (_isMapReduce)
{
var parts = field.Name.Split('.');
bool isGroupingField = _determiner.IsGroupingKey(field);
Expression current;
if (parts.Contains("Key") && isGroupingField)
current = _document;
else
{
current = Expression.Call(
_document,
"Get",
new[] { typeof(Document) },
Expression.Constant("value"));
}
for (int i = 0, n = parts.Length; i < n; i++)
{
var type = i == n - 1 ? field.Type : typeof(Document);
if (parts[i] == "Key" && isGroupingField)
parts[i] = "_id";
current = Expression.Call(
current,
"Get",
new[] { type },
Expression.Constant(parts[i]));
}
return current;
}
else
return Visit(field.Expression);
}
protected override Expression VisitParameter(ParameterExpression p)
{
return _document;
}
private class GroupingKeyDeterminer : MongoExpressionVisitor
{
private bool _isGroupingKey;
public bool IsGroupingKey(Expression exp)
{
_isGroupingKey = false;
Visit(exp);
return _isGroupingKey;
}
protected override Expression Visit(Expression exp)
{
if (exp == null)
return exp;
if (_isGroupingKey)
return exp;
if (exp.Type.IsGenericType && exp.Type.GetGenericTypeDefinition() == typeof(Grouping<,>))
{
_isGroupingKey = true;
return exp;
}
return base.Visit(exp);
}
}
}
}