-
Couldn't load subscription status.
- Fork 1.3k
CSHARP-5730: Support static String.Compare method #1789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
fb728dc
3a85339
f4e62c6
eea6c74
3852a30
d34f3d3
b429c2e
bc8cb71
0d10969
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* 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.Serializers; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
| { | ||
| internal static class CompareMethodToAggregationExpressionTranslator | ||
| { | ||
| public static TranslatedExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
| { | ||
| var method = expression.Method; | ||
| var arguments = expression.Arguments; | ||
|
|
||
| if (method.Is(StringMethod.StaticCompare)) | ||
| { | ||
| var strAExpression = arguments[0]; | ||
| var strATranslation = ExpressionToAggregationExpressionTranslator.Translate(context, strAExpression); | ||
| var strBExpression = arguments[1]; | ||
| var strBTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, strBExpression); | ||
| var ast = AstExpression.Cmp(strATranslation.Ast, strBTranslation.Ast); | ||
|
||
| return new TranslatedExpression(expression, ast, Int32Serializer.Instance); | ||
| } | ||
|
|
||
| throw new ExpressionNotSupportedException(expression); | ||
| } | ||
| } | ||
| } | ||
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| */ | ||
|
|
||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
|
|
@@ -28,7 +29,8 @@ public static bool CanTranslate(Expression leftExpression) | |
| { | ||
| return | ||
| leftExpression is MethodCallExpression leftMethodCallExpression && | ||
| IComparableMethod.IsCompareToMethod(leftMethodCallExpression.Method); | ||
| leftMethodCallExpression.Method is var method && | ||
| (IComparableMethod.IsCompareToMethod(method) || IsStaticCompareMethod(method)); | ||
| } | ||
|
|
||
| // caller is responsible for ensuring constant is on the right | ||
|
|
@@ -39,13 +41,27 @@ public static AstFilter Translate( | |
| AstComparisonFilterOperator comparisonOperator, | ||
| Expression rightExpression) | ||
| { | ||
| if (leftExpression is MethodCallExpression leftMethodCallExpression && | ||
| IComparableMethod.IsCompareToMethod(leftMethodCallExpression.Method)) | ||
| if (CanTranslate(leftExpression)) | ||
| { | ||
| var fieldExpression = leftMethodCallExpression.Object; | ||
| var leftMethodCallExpression = (MethodCallExpression)leftExpression; | ||
| var method= leftMethodCallExpression.Method; | ||
| var arguments = leftMethodCallExpression.Arguments; | ||
|
|
||
| Expression fieldExpression; | ||
| Expression valueExpression; | ||
| if (method.IsStatic) | ||
| { | ||
| fieldExpression = arguments[0]; | ||
| valueExpression = arguments[1]; | ||
| } | ||
| else | ||
| { | ||
| fieldExpression = leftMethodCallExpression.Object; | ||
| valueExpression = arguments[0]; | ||
| } | ||
|
|
||
| var fieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression); | ||
|
|
||
| var valueExpression = leftMethodCallExpression.Arguments[0]; | ||
| var value = valueExpression.GetConstantValue<object>(containingExpression: expression); | ||
| var serializedValue = SerializationHelper.SerializeValue(fieldTranslation.Serializer, value); | ||
|
|
||
|
|
@@ -58,5 +74,17 @@ public static AstFilter Translate( | |
|
|
||
| throw new ExpressionNotSupportedException(expression); | ||
| } | ||
|
|
||
| private static bool IsStaticCompareMethod(MethodInfo method) | ||
|
||
| { | ||
| return | ||
| method.IsStatic && | ||
| method.IsPublic && | ||
| method.ReturnType == typeof(int) && | ||
| method.GetParameters() is var parameters && | ||
| parameters.Length == 2 && | ||
| parameters[0].ParameterType == method.DeclaringType && | ||
| parameters[1].ParameterType == parameters[0].ParameterType; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* 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.Collections.Generic; | ||
| using System.Linq; | ||
| using MongoDB.Driver.TestHelpers; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; | ||
|
|
||
| public class CSharp5730Tests : LinqIntegrationTest<CSharp5730Tests.ClassFixture> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have tests for |
||
| { | ||
| public CSharp5730Tests(ClassFixture fixture) | ||
| : base(fixture) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Where_String_Compare_greater_than_zero_should_work() | ||
| { | ||
| var collection = Fixture.Collection; | ||
|
|
||
| var queryable = collection.AsQueryable() | ||
| .Where(d => string.Compare(d.Key, "a4e48b55-0519-4ab3-b6b9-7c532fc65b56") > 0); | ||
|
|
||
| var stages = Translate(collection, queryable); | ||
| AssertStages(stages, "{ $match : { Key : { $gt : 'a4e48b55-0519-4ab3-b6b9-7c532fc65b56' } } }"); | ||
|
|
||
| var result = queryable.ToList(); | ||
| result.Select(x => x. Id).Should().Equal(4); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Where_String_CompareTo_greater_than_zero_should_work() | ||
| { | ||
| var collection = Fixture.Collection; | ||
|
|
||
| var queryable = collection.AsQueryable() | ||
| .Where(d => d.Key.CompareTo("a4e48b55-0519-4ab3-b6b9-7c532fc65b56") > 0); | ||
|
|
||
| var stages = Translate(collection, queryable); | ||
| AssertStages(stages, "{ $match : { Key : { $gt : 'a4e48b55-0519-4ab3-b6b9-7c532fc65b56' } } }"); | ||
|
|
||
| var result = queryable.ToList(); | ||
| result.Select(x => x. Id).Should().Equal(4); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Select_String_Compare_should_work() | ||
| { | ||
| var collection = Fixture.Collection; | ||
|
|
||
| var queryable = collection.AsQueryable() | ||
| .Select(d => string.Compare(d.Key, "a4e48b55-0519-4ab3-b6b9-7c532fc65b56") > 0); | ||
|
|
||
| var stages = Translate(collection, queryable); | ||
| AssertStages(stages, "{ $project : { _v : { $gt : [{ $cmp : ['$Key', 'a4e48b55-0519-4ab3-b6b9-7c532fc65b56'] }, 0] }, _id : 0 } }"); | ||
|
|
||
| var results = queryable.ToList(); | ||
| results.Should().Equal(false, false, false, true); | ||
| } | ||
|
|
||
| public class C | ||
| { | ||
| public int Id { get; set; } | ||
| public string Key { get; set; } | ||
| } | ||
|
|
||
| public sealed class ClassFixture : MongoCollectionFixture<C> | ||
| { | ||
| protected override IEnumerable<C> InitialData => | ||
| [ | ||
| new C { Id = 1, Key = "1b2bc240-ec2a-4a17-8790-8407e3bbb847"}, | ||
| new C { Id = 2, Key = "a4e48b55-0519-4ab3-b6b9-7c532fc65b56"}, | ||
| new C { Id = 3, Key = "9ff72c5d-189e-4511-b7ad-3f83489e4ea4"}, | ||
| new C { Id = 4, Key = "d78ca958-abac-46cd-94a7-fbf7a2ba683d"} | ||
| ]; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CompareandCompareTohandling is consolidated into a single class.