Skip to content

Commit

Permalink
fixed issue with a flattening projection and still maintaining the or…
Browse files Browse the repository at this point in the history
…iginal hierarchy.
  • Loading branch information
craiggwilson committed May 31, 2010
1 parent 5d3fe70 commit eb99839
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 3 deletions.
Expand Up @@ -317,6 +317,20 @@ public void ProjectionWithConstraints()
Assert.AreEqual(new Document("Age", new Document().Merge(Op.GreaterThan(21)).Merge(Op.LessThan(42))), queryObject.Query);
}

[Test]
public void ProjectionWithLocalCreation_ChildobjectShouldNotBeNull()
{
var people = Collection.Linq()
.Select(p => new PersonWrapper(p, p.FirstName));

var queryObject = ((IMongoQueryable)people).GetQueryObject();
Assert.AreEqual(0, queryObject.Fields.Count());
Assert.AreEqual(0, queryObject.NumberToLimit);
Assert.AreEqual(0, queryObject.NumberToSkip);
Assert.AreEqual(0, queryObject.Query.Count);

}

[Test]
public void Regex_IsMatch()
{
Expand Down
8 changes: 8 additions & 0 deletions source/MongoDB/Linq/Expressions/MongoExpressionExtensions.cs
Expand Up @@ -8,6 +8,14 @@ namespace MongoDB.Linq.Expressions
{
internal static class MongoExpressionExtensions
{
public static bool HasSelectAllField(this IEnumerable<FieldDeclaration> fields)
{
if (fields == null)
return true;

return fields.Any(f => f.Name == "*");
}

public static SelectExpression AddField(this SelectExpression select, FieldDeclaration field)
{
List<FieldDeclaration> fields = new List<FieldDeclaration>(select.Fields);
Expand Down
5 changes: 4 additions & 1 deletion source/MongoDB/Linq/MongoQueryProvider.cs
Expand Up @@ -164,9 +164,12 @@ private Expression BuildExecutionPlan(Expression expression)

private ProjectionExpression Translate(Expression expression)
{
var rootQueryable = new RootQueryableFinder().Find(expression);
var elementType = ((IQueryable)((ConstantExpression)rootQueryable).Value).ElementType;

expression = PartialEvaluator.Evaluate(expression, CanBeEvaluatedLocally);

expression = new FieldBinder().Bind(expression);
expression = new FieldBinder().Bind(expression, elementType);
expression = new QueryBinder(this, expression).Bind(expression);
expression = new AggregateRewriter().Rewrite(expression);
expression = new RedundantFieldRemover().Remove(expression);
Expand Down
12 changes: 11 additions & 1 deletion source/MongoDB/Linq/Translators/FieldBinder.cs
Expand Up @@ -17,11 +17,13 @@ internal class FieldBinder : ExpressionVisitor

private Alias _alias;
private FieldFinder _finder;
private Type _elementType;

public Expression Bind(Expression expression)
public Expression Bind(Expression expression, Type elementType)
{
_alias = new Alias();
_finder = new FieldFinder();
_elementType = elementType;
return Visit(expression);
}

Expand All @@ -37,6 +39,14 @@ protected override Expression Visit(Expression exp)
return base.Visit(exp);
}

protected override Expression VisitParameter(ParameterExpression p)
{
if(p.Type == _elementType)
return new FieldExpression(p, _alias, "*");

return base.VisitParameter(p);
}

private class FieldFinder : ExpressionVisitor
{
private Stack<string> _fieldParts;
Expand Down
Expand Up @@ -57,6 +57,8 @@ protected override ReadOnlyCollection<FieldDeclaration> VisitFieldDeclarationLis
for (int i = 0, n = fields.Count; i < n; i++)
{
_currentAggregateName = fields[i].Name;
if (_currentAggregateName == "*")
continue;
Visit(fields[i].Expression);
}

Expand Down
Expand Up @@ -66,6 +66,8 @@ protected override ReadOnlyCollection<FieldDeclaration> VisitFieldDeclarationLis
for (int i = 0, n = fields.Count; i < n; i++)
{
_currentAggregateName = fields[i].Name;
if (_currentAggregateName == "*")
continue;
Visit(fields[i].Expression);
}

Expand Down
Expand Up @@ -76,6 +76,8 @@ protected override ReadOnlyCollection<FieldDeclaration> VisitFieldDeclarationLis
for (int i = 0, n = fields.Count; i < n; i++)
{
_currentAggregateName = fields[i].Name;
if (_currentAggregateName == "*")
continue;
Visit(fields[i].Expression);
}

Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Linq/Translators/MongoQueryObjectBuilder.cs
Expand Up @@ -49,7 +49,7 @@ protected override Expression VisitSelect(SelectExpression select)
_queryObject.ReduceFunction = new MapReduceReduceFunctionBuilder().Build(select.Fields);
_queryObject.FinalizerFunction = new MapReduceFinalizerFunctionBuilder().Build(select.Fields);
}
else if(!_queryAttributes.IsCount)
else if(!_queryAttributes.IsCount && !select.Fields.HasSelectAllField())
{
var fieldGatherer = new FieldGatherer();
foreach (var field in select.Fields)
Expand Down

0 comments on commit eb99839

Please sign in to comment.