Skip to content

Commit

Permalink
Clean up NodaTime evaluatable filter (npgsql#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored May 6, 2022
1 parent fed78ec commit 94648a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 0 additions & 12 deletions src/EFCore.PG/Query/Internal/NpgsqlEvaluatableExpressionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 94648a3

Please sign in to comment.