Skip to content

Commit 767f213

Browse files
committed
refactored SelectQuery into BsonSerializationInfoHelper and PredicateTranslator
1 parent 2ec8fa3 commit 767f213

File tree

10 files changed

+3810
-1459
lines changed

10 files changed

+3810
-1459
lines changed

Driver/Driver.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,15 @@
194194
<Compile Include="Linq\Translators\PartialEvaluator.cs" />
195195
<Compile Include="Linq\Expressions\ExpressionPrettyPrinter.cs" />
196196
<Compile Include="Linq\Expressions\ExpressionVisitor.cs" />
197+
<Compile Include="Linq\Translators\PredicateTranslator.cs" />
197198
<Compile Include="Linq\Translators\Projector.cs" />
198199
<Compile Include="Linq\Translators\SelectQuery.cs" />
199200
<Compile Include="Linq\LinqExtensionMethods.cs" />
200201
<Compile Include="Linq\MongoQueryable.cs" />
201202
<Compile Include="Linq\MongoQueryProvider.cs" />
202203
<Compile Include="Linq\Translators\MongoQueryTranslator.cs" />
203204
<Compile Include="Linq\Translators\TranslatedQuery.cs" />
205+
<Compile Include="Linq\Utils\BsonSerializationInfoHelper.cs" />
204206
<Compile Include="Linq\Utils\TypeHelper.cs" />
205207
<Compile Include="MongoUtils.cs" />
206208
<Compile Include="MongoDefaults.cs" />

Driver/Linq/Expressions/ExpressionNormalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected override Expression VisitBinary(BinaryExpression node)
9797
/// <returns>The UnaryExpression (possibly modified).</returns>
9898
protected override Expression VisitUnary(UnaryExpression node)
9999
{
100-
if (node.NodeType == ExpressionType.Convert)
100+
if (node.NodeType == ExpressionType.Convert || node.NodeType == ExpressionType.ConvertChecked)
101101
{
102102
if (node.Type.IsAssignableFrom(node.Operand.Type))
103103
{

Driver/Linq/MongoQueryProvider.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
using System.Text;
2121
using System.Reflection;
2222

23-
// for a good blog post on implementing LINQ query providers see Matt Warren's blog posts
24-
// see: http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx
25-
2623
namespace MongoDB.Driver.Linq
2724
{
2825
/// <summary>
@@ -146,46 +143,5 @@ public object Execute(Expression expression)
146143
var translatedQuery = MongoQueryTranslator.Translate(this, expression);
147144
return translatedQuery.Execute();
148145
}
149-
150-
// private methods
151-
internal bool CanBeEvaluatedLocally(Expression expression)
152-
{
153-
// any operation on a query can't be done locally
154-
var constantExpression = expression as ConstantExpression;
155-
if (constantExpression != null)
156-
{
157-
var query = constantExpression.Value as IQueryable;
158-
if (query != null && query.Provider == this)
159-
{
160-
return false;
161-
}
162-
}
163-
164-
var methodCallExpression = expression as MethodCallExpression;
165-
if (methodCallExpression != null)
166-
{
167-
var declaringType = methodCallExpression.Method.DeclaringType;
168-
if (declaringType == typeof(Enumerable) || declaringType == typeof(Queryable))
169-
{
170-
return false;
171-
}
172-
if (declaringType == typeof(LinqToMongo))
173-
{
174-
return false;
175-
}
176-
}
177-
178-
if (expression.NodeType == ExpressionType.Convert && expression.Type == typeof(object))
179-
{
180-
return true;
181-
}
182-
183-
if (expression.NodeType == ExpressionType.Parameter || expression.NodeType == ExpressionType.Lambda)
184-
{
185-
return false;
186-
}
187-
188-
return true;
189-
}
190146
}
191147
}

Driver/Linq/Translators/MongoQueryTranslator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static TranslatedQuery Translate(IQueryable query)
4545
/// <returns>A TranslatedQuery.</returns>
4646
public static TranslatedQuery Translate(MongoQueryProvider provider, Expression expression)
4747
{
48-
expression = PartialEvaluator.Evaluate(expression, provider.CanBeEvaluatedLocally);
48+
expression = PartialEvaluator.Evaluate(expression, provider);
4949
expression = ExpressionNormalizer.Normalize(expression);
5050
// assume for now it's a SelectQuery
5151
var documentType = GetDocumentType(expression);

Driver/Linq/Translators/PartialEvaluator.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,64 @@ namespace MongoDB.Driver.Linq
2727
public static class PartialEvaluator
2828
{
2929
/// <summary>
30-
/// Performs evaluation and replacement of independent sub-trees.
30+
/// Performs evaluation and replacement of independent sub-trees
3131
/// </summary>
3232
/// <param name="expression">The root of the expression tree.</param>
33-
/// <param name="fnCanBeEvaluated">A function that decides whether a given expression node can be part of the local function.</param>
3433
/// <returns>A new tree with sub-trees evaluated and replaced.</returns>
35-
public static Expression Evaluate(Expression expression, Func<Expression, bool> fnCanBeEvaluated)
34+
public static Expression Evaluate(Expression expression)
3635
{
37-
return new SubtreeEvaluator(new Nominator(fnCanBeEvaluated).Nominate(expression)).Evaluate(expression);
36+
return Evaluate(expression, null);
3837
}
3938

4039
/// <summary>
41-
/// Performs evaluation and replacement of independent sub-trees
40+
/// Performs evaluation and replacement of independent sub-trees.
4241
/// </summary>
4342
/// <param name="expression">The root of the expression tree.</param>
43+
/// <param name="fnCanBeEvaluated">A function that decides whether a given expression node can be part of the local function.</param>
4444
/// <returns>A new tree with sub-trees evaluated and replaced.</returns>
45-
public static Expression Evaluate(Expression expression)
45+
public static Expression Evaluate(Expression expression, IQueryProvider queryProvider)
4646
{
47-
return Evaluate(expression, PartialEvaluator.CanBeEvaluatedLocally);
47+
return new SubtreeEvaluator(new Nominator(e => CanBeEvaluatedLocally(e, queryProvider)).Nominate(expression)).Evaluate(expression);
4848
}
4949

50-
private static bool CanBeEvaluatedLocally(Expression expression)
50+
private static bool CanBeEvaluatedLocally(Expression expression, IQueryProvider queryProvider)
5151
{
52-
return expression.NodeType != ExpressionType.Parameter;
52+
// any operation on a query can't be done locally
53+
var constantExpression = expression as ConstantExpression;
54+
if (constantExpression != null)
55+
{
56+
var query = constantExpression.Value as IQueryable;
57+
if (query != null && (queryProvider == null || query.Provider == queryProvider))
58+
{
59+
return false;
60+
}
61+
}
62+
63+
var methodCallExpression = expression as MethodCallExpression;
64+
if (methodCallExpression != null)
65+
{
66+
var declaringType = methodCallExpression.Method.DeclaringType;
67+
if (declaringType == typeof(Enumerable) || declaringType == typeof(Queryable))
68+
{
69+
return false;
70+
}
71+
if (declaringType == typeof(LinqToMongo))
72+
{
73+
return false;
74+
}
75+
}
76+
77+
if (expression.NodeType == ExpressionType.Convert && expression.Type == typeof(object))
78+
{
79+
return true;
80+
}
81+
82+
if (expression.NodeType == ExpressionType.Parameter || expression.NodeType == ExpressionType.Lambda)
83+
{
84+
return false;
85+
}
86+
87+
return true;
5388
}
5489

5590
/// <summary>

0 commit comments

Comments
 (0)