From 94648a3835f5938efc47c2f59e7dae4dccc345d0 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 6 May 2022 11:21:43 +0200 Subject: [PATCH] Clean up NodaTime evaluatable filter (#2354) --- ...daTimeEvaluatableExpressionFilterPlugin.cs | 33 +++++++++++++++---- .../NpgsqlEvaluatableExpressionFilter.cs | 12 ------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeEvaluatableExpressionFilterPlugin.cs b/src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeEvaluatableExpressionFilterPlugin.cs index 0339035bd..9c5648dc7 100644 --- a/src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeEvaluatableExpressionFilterPlugin.cs +++ b/src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeEvaluatableExpressionFilterPlugin.cs @@ -9,11 +9,30 @@ public class NpgsqlNodaTimeEvaluatableExpressionFilterPlugin : IEvaluatableExpre typeof(SystemClock).GetMember(nameof(SystemClock.Instance)).FirstOrDefault()!; public virtual bool IsEvaluatableExpression(Expression expression) - => !( - expression is MethodCallExpression methodExpression - && methodExpression.Method == GetCurrentInstantMethod - || - expression is MemberExpression memberExpression - && memberExpression.Member == SystemClockInstanceMember - ); + { + switch (expression) + { + case MethodCallExpression methodCallExpression when methodCallExpression.Method == GetCurrentInstantMethod: + return false; + + case MemberExpression memberExpression: + if (memberExpression.Member == SystemClockInstanceMember) + { + return false; + } + + // We support translating certain NodaTime patterns which accept a time zone as a parameter, + // e.g. Instant.InZone(timezone), as long as the timezone is expressed as an access on DateTimeZoneProviders.Tzdb. + // Prevent this from being evaluated locally and so parameterized, so we can access the member access on + // DateTimeZoneProviders and extract the constant (see NpgsqlNodaTimeMethodCallTranslatorPlugin) + if (memberExpression.Member.DeclaringType == typeof(DateTimeZoneProviders)) + { + return false; + } + + break; + } + + return true; + } } \ No newline at end of file diff --git a/src/EFCore.PG/Query/Internal/NpgsqlEvaluatableExpressionFilter.cs b/src/EFCore.PG/Query/Internal/NpgsqlEvaluatableExpressionFilter.cs index 9e1c69663..ad6433f13 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlEvaluatableExpressionFilter.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlEvaluatableExpressionFilter.cs @@ -34,18 +34,6 @@ public override bool IsEvaluatableExpression(Expression expression, IModel model return false; } - break; - - case MemberExpression memberExpression: - // We support translating certain NodaTime patterns which accept a time zone as a parameter, - // e.g. Instant.InZone(timezone), as long as the timezone is expressed as an access on DateTimeZoneProviders.Tzdb. - // Prevent this from being evaluated locally and so parameterized, so we can access the access tree on - // DateTimeZoneProviders and extract the constant (see NpgsqlNodaTimeMethodCallTranslatorPlugin) - if (memberExpression.Member.DeclaringType?.FullName == "NodaTime.DateTimeZoneProviders") - { - return false; - } - break; }