Skip to content

Commit

Permalink
Add quirk
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Oct 14, 2024
1 parent f477f7e commit 9eaf3fc
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions src/EFCore.Relational/Query/Internal/RelationalCommandCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Concurrent;
using Microsoft.Extensions.Caching.Memory;

Expand Down Expand Up @@ -104,21 +105,34 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)

private readonly struct CommandCacheKey : IEquatable<CommandCacheKey>
{
private static readonly bool UseOldBehavior34201 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue34028", out var enabled34028) && enabled34028;

private readonly Expression _queryExpression;
private readonly Dictionary<string, ParameterInfo> _parameterInfos;

internal CommandCacheKey(Expression queryExpression, IReadOnlyDictionary<string, object?> parameterValues)
// Quirk only
private readonly IReadOnlyDictionary<string, object?>? _parameterValues;

public CommandCacheKey(Expression queryExpression, IReadOnlyDictionary<string, object?> parameterValues)
{
_queryExpression = queryExpression;
_parameterInfos = new Dictionary<string, ParameterInfo>();

foreach (var (key, value) in parameterValues)
if (UseOldBehavior34201)
{
_parameterInfos[key] = new ParameterInfo
_parameterValues = parameterValues;
}
else
{
foreach (var (key, value) in parameterValues)
{
IsNull = value is null,
ObjectArrayLength = value is object[] arr ? arr.Length : null
};
_parameterInfos[key] = new ParameterInfo
{
IsNull = value is null,
ObjectArrayLength = value is object[] arr ? arr.Length : null
};
}
}
}

Expand All @@ -140,11 +154,37 @@ public bool Equals(CommandCacheKey commandCacheKey)

if (_parameterInfos.Count > 0)
{
foreach (var (key, info) in _parameterInfos)
if (UseOldBehavior34201)
{
foreach (var (key, value) in _parameterValues!)
{
if (!_parameterValues.TryGetValue(key, out var otherValue))
{
return false;
}

// ReSharper disable once ArrangeRedundantParentheses
if ((value == null) != (otherValue == null))
{
return false;
}

if (value is IEnumerable
&& value.GetType() == typeof(object[]))
{
// FromSql parameters must have the same number of elements
return ((object[])value).Length == (otherValue as object[])?.Length;
}
}
}
else
{
if (!commandCacheKey._parameterInfos.TryGetValue(key, out var otherInfo) || info != otherInfo)
foreach (var (key, info) in _parameterInfos)
{
return false;
if (!commandCacheKey._parameterInfos.TryGetValue(key, out var otherInfo) || info != otherInfo)
{
return false;
}
}
}
}
Expand Down

0 comments on commit 9eaf3fc

Please sign in to comment.