-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-5421: Add Mql.Field so EF Core Provider can access shadow prop…
…erties.
- Loading branch information
Showing
8 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...onExpressionTranslators/MethodTranslators/FieldMethodToAggregationExpressionTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq.Expressions; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
{ | ||
internal static class FieldMethodToAggregationExpressionTranslator | ||
{ | ||
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (method.Is(MqlMethod.Field)) | ||
{ | ||
var documentExpression = arguments[0]; | ||
var fieldNameExpression = arguments[1]; | ||
var fieldSerializerExpression = arguments[2]; | ||
|
||
var documentTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, documentExpression); | ||
var fieldName = fieldNameExpression.GetConstantValue<string>(expression); | ||
var fieldSerializer = fieldSerializerExpression.GetConstantValue<IBsonSerializer>(expression); | ||
|
||
var ast = AstExpression.GetField(documentTranslation.Ast, fieldName); | ||
return new AggregationExpression(expression, ast, fieldSerializer); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ressionToFilterTranslators/ToFilterFieldTranslators/FieldMethodToFilterFieldTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq.Expressions; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; | ||
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators | ||
{ | ||
internal static class FieldMethodToFilterFieldTranslator | ||
{ | ||
public static AstFilterField Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (method.Is(MqlMethod.Field)) | ||
{ | ||
var documentExpression = arguments[0]; | ||
var fieldNameExpression = arguments[1]; | ||
var fieldSerializerExpression = arguments[2]; | ||
|
||
var documentField = ExpressionToFilterFieldTranslator.Translate(context, documentExpression); | ||
var fieldName = fieldNameExpression.GetConstantValue<string>(expression); | ||
var fieldSerializer = fieldSerializerExpression.GetConstantValue<IBsonSerializer>(expression); | ||
|
||
return documentField.SubField(fieldName, fieldSerializer); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/MqlFieldTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Linq; | ||
using FluentAssertions; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira | ||
{ | ||
public class MqlFieldTests : Linq3IntegrationTest | ||
{ | ||
[Fact] | ||
public void Select_Mql_Field_should_work_with_BsonDocument() | ||
{ | ||
var collection = GetCollection<BsonDocument>(); | ||
|
||
var queryable = collection.AsQueryable() | ||
.Select(root => Mql.Field(root, "X", Int32Serializer.Instance) + 1); // like root.X except BsonDocument does not have a property called X | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $project : { _v : { $add : ['$X', 1] }, _id : 0 } }"); | ||
|
||
var results = queryable.ToList(); | ||
results.Should().Equal(2, 3); | ||
} | ||
|
||
[Fact] | ||
public void Select_Mql_Field_should_work_with_POCO() | ||
{ | ||
var collection = GetCollection<C>(); | ||
|
||
var queryable = collection.AsQueryable() | ||
.Select(root => Mql.Field(root, "X", Int32Serializer.Instance) + 1); // like root.X except C does not have a property called X | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $project : { _v : { $add : ['$X', 1] }, _id : 0 } }"); | ||
|
||
var results = queryable.ToList(); | ||
results.Should().Equal(2, 3); | ||
} | ||
|
||
[Fact] | ||
public void Where_Mql_Field_should_work_with_BsonDocument() | ||
{ | ||
var collection = GetCollection<BsonDocument>(); | ||
|
||
var queryable = collection.AsQueryable() | ||
.Where(root => Mql.Field(root, "X", Int32Serializer.Instance) == 1); // like root.X except BsonDocument does not have a property called X | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $match : { X : 1 } }"); | ||
|
||
var results = queryable.ToList(); | ||
results.Select(x => x["_id"].AsInt32).Should().Equal(1); | ||
} | ||
|
||
[Fact] | ||
public void Where_Mql_Field_should_work_with_POCO() | ||
{ | ||
var collection = GetCollection<C>(); | ||
|
||
var queryable = collection.AsQueryable() | ||
.Where(root => Mql.Field(root, "X", Int32Serializer.Instance) == 1); // like root.X except C does not have a property called X | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $match : { X : 1 } }"); | ||
|
||
var results = queryable.ToList(); | ||
results.Select(x => x.Id).Should().Equal(1); | ||
} | ||
|
||
private IMongoCollection<TDocument> GetCollection<TDocument>() | ||
{ | ||
var collection = GetCollection<BsonDocument>("test"); | ||
CreateCollection( | ||
collection, | ||
new BsonDocument { { "_id", 1 }, { "X", 1 } }, | ||
new BsonDocument { { "_id", 2 }, { "X", 2 } }); | ||
|
||
var database = collection.Database; | ||
var collectionName = collection.CollectionNamespace.CollectionName; | ||
return database.GetCollection<TDocument>(collectionName); | ||
} | ||
|
||
[BsonIgnoreExtraElements] | ||
private class C | ||
{ | ||
public int Id { get; set; } | ||
} | ||
} | ||
} |