|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Linq.Expressions; |
| 17 | +using System.Reflection; |
| 18 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; |
| 19 | +using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators |
| 24 | +{ |
| 25 | + internal static class CompareComparisonExpressionToFilterTranslator |
| 26 | + { |
| 27 | + public static bool CanTranslate(Expression leftExpression) |
| 28 | + { |
| 29 | + return |
| 30 | + leftExpression is MethodCallExpression leftMethodCallExpression && |
| 31 | + leftMethodCallExpression.Method is var method && |
| 32 | + (IsStaticCompareMethod(method) || IsInstanceCompareToMethod(method)); |
| 33 | + } |
| 34 | + |
| 35 | + // caller is responsible for ensuring constant is on the right |
| 36 | + public static AstFilter Translate( |
| 37 | + TranslationContext context, |
| 38 | + Expression expression, |
| 39 | + Expression leftExpression, |
| 40 | + AstComparisonFilterOperator outerComparisonOperator, |
| 41 | + Expression rightExpression) |
| 42 | + { |
| 43 | + if (CanTranslate(leftExpression)) |
| 44 | + { |
| 45 | + var compareMethodCallExpression = (MethodCallExpression)leftExpression; |
| 46 | + var compareMethod = compareMethodCallExpression.Method; |
| 47 | + var compareArguments = compareMethodCallExpression.Arguments; |
| 48 | + var outerValue = rightExpression.GetConstantValue<int>(containingExpression: expression); |
| 49 | + |
| 50 | + Expression fieldExpression; |
| 51 | + Expression innerValueExpression; |
| 52 | + if (compareMethod.IsStatic) |
| 53 | + { |
| 54 | + fieldExpression = compareArguments[0]; |
| 55 | + innerValueExpression = compareArguments[1]; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + fieldExpression = compareMethodCallExpression.Object; |
| 60 | + innerValueExpression = compareArguments[0]; |
| 61 | + } |
| 62 | + |
| 63 | + var fieldComparisonOperator = (outerComparisonOperator, outerValue) switch |
| 64 | + { |
| 65 | + (AstComparisonFilterOperator.Eq, -1) => AstComparisonFilterOperator.Lt, |
| 66 | + (AstComparisonFilterOperator.Ne, -1) => AstComparisonFilterOperator.Gte, |
| 67 | + (AstComparisonFilterOperator.Gt, -1) => AstComparisonFilterOperator.Gte, |
| 68 | + (AstComparisonFilterOperator.Eq, 0) => AstComparisonFilterOperator.Eq, |
| 69 | + (AstComparisonFilterOperator.Ne, 0) => AstComparisonFilterOperator.Ne, |
| 70 | + (AstComparisonFilterOperator.Lt, 0) => AstComparisonFilterOperator.Lt, |
| 71 | + (AstComparisonFilterOperator.Lte, 0) => AstComparisonFilterOperator.Lte, |
| 72 | + (AstComparisonFilterOperator.Gt, 0) => AstComparisonFilterOperator.Gt, |
| 73 | + (AstComparisonFilterOperator.Gte, 0) => AstComparisonFilterOperator.Gte, |
| 74 | + (AstComparisonFilterOperator.Eq, 1) => AstComparisonFilterOperator.Gt, |
| 75 | + (AstComparisonFilterOperator.Ne, 1) => AstComparisonFilterOperator.Lte, |
| 76 | + (AstComparisonFilterOperator.Lt, 1) => AstComparisonFilterOperator.Lte, |
| 77 | + _ => throw new ExpressionNotSupportedException(expression) |
| 78 | + }; |
| 79 | + |
| 80 | + if (fieldExpression.NodeType == ExpressionType.Constant && innerValueExpression.NodeType != ExpressionType.Constant) |
| 81 | + { |
| 82 | + (fieldExpression, innerValueExpression) = (innerValueExpression, fieldExpression); |
| 83 | + fieldComparisonOperator = fieldComparisonOperator.GetComparisonOperatorForSwappedLeftAndRight(); |
| 84 | + } |
| 85 | + |
| 86 | + var fieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression); |
| 87 | + var value = innerValueExpression.GetConstantValue<object>(containingExpression: expression); |
| 88 | + var serializedValue = SerializationHelper.SerializeValue(fieldTranslation.Serializer, value); |
| 89 | + |
| 90 | + return AstFilter.Compare(fieldTranslation.Ast, fieldComparisonOperator, serializedValue); |
| 91 | + } |
| 92 | + |
| 93 | + throw new ExpressionNotSupportedException(expression); |
| 94 | + } |
| 95 | + |
| 96 | + private static bool IsInstanceCompareToMethod(MethodInfo method) |
| 97 | + { |
| 98 | + return |
| 99 | + method.IsPublic && |
| 100 | + !method.IsStatic && |
| 101 | + method.ReturnType == typeof(int) && |
| 102 | + method.Name == "CompareTo" && |
| 103 | + method.GetParameters() is var parameters && |
| 104 | + parameters.Length == 1 && |
| 105 | + parameters[0].ParameterType is var parameterType && |
| 106 | + (parameterType == method.DeclaringType || parameterType == typeof(object)); |
| 107 | + } |
| 108 | + |
| 109 | + private static bool IsStaticCompareMethod(MethodInfo method) |
| 110 | + { |
| 111 | + return |
| 112 | + method.IsPublic && |
| 113 | + method.IsStatic && |
| 114 | + method.ReturnType == typeof(int) && |
| 115 | + method.Name == "Compare" && |
| 116 | + method.GetParameters() is var parameters && |
| 117 | + parameters.Length == 2 && |
| 118 | + parameters[0].ParameterType == method.DeclaringType && |
| 119 | + parameters[1].ParameterType == method.DeclaringType; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments