Skip to content
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

Minor cleanup for queryable function support #20139

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 26 additions & 53 deletions src/EFCore.Relational/Metadata/Internal/DbFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,23 @@ public static IEnumerable<DbFunction> GetDbFunctions([NotNull] IModel model)
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static DbFunction FindDbFunction(
[NotNull] IModel model,
[NotNull] MethodInfo methodInfo)
{
var functions = (SortedDictionary<string, DbFunction>)model[RelationalAnnotationNames.DbFunctions];
if (functions == null
|| !functions.TryGetValue(GetFunctionName(methodInfo, methodInfo.GetParameters()), out var dbFunction))
{
return null;
}

return dbFunction;
}
public static DbFunction FindDbFunction([NotNull] IModel model, [NotNull] MethodInfo methodInfo)
=> model[RelationalAnnotationNames.DbFunctions] is SortedDictionary<string, DbFunction> functions
&& functions.TryGetValue(GetFunctionName(methodInfo, methodInfo.GetParameters()), out var dbFunction)
? dbFunction
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static DbFunction FindDbFunction(
[NotNull] IModel model,
[NotNull] string name)
{
var functions = (SortedDictionary<string, DbFunction>)model[RelationalAnnotationNames.DbFunctions];
return functions == null
|| !functions.TryGetValue(name, out var dbFunction)
? null
: dbFunction;
}
public static DbFunction FindDbFunction([NotNull] IModel model, [NotNull] string name)
=> model[RelationalAnnotationNames.DbFunctions] is SortedDictionary<string, DbFunction> functions
&& functions.TryGetValue(name, out var dbFunction)
? dbFunction
: null;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -208,16 +195,8 @@ public static DbFunction AddDbFunction(
}

private static SortedDictionary<string, DbFunction> GetOrCreateFunctions(IMutableModel model)
{
var functions = (SortedDictionary<string, DbFunction>)model[RelationalAnnotationNames.DbFunctions];
if (functions == null)
{
functions = new SortedDictionary<string, DbFunction>();
model[RelationalAnnotationNames.DbFunctions] = functions;
}

return functions;
}
=> (SortedDictionary<string, DbFunction>)(
model[RelationalAnnotationNames.DbFunctions] ??= new SortedDictionary<string, DbFunction>());

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -229,22 +208,19 @@ public static DbFunction RemoveDbFunction(
[NotNull] IMutableModel model,
[NotNull] MethodInfo methodInfo)
{
var functions = (SortedDictionary<string, DbFunction>)model[RelationalAnnotationNames.DbFunctions];
if (functions == null)
if (model[RelationalAnnotationNames.DbFunctions] is SortedDictionary<string, DbFunction> functions)
{
return null;
}
var name = GetFunctionName(methodInfo, methodInfo.GetParameters());
if (functions.TryGetValue(name, out var function))
{
functions.Remove(name);
function.Builder = null;

var name = GetFunctionName(methodInfo, methodInfo.GetParameters());
if (!functions.TryGetValue(name, out var function))
{
return null;
return function;
}
}

functions.Remove(name);
function.Builder = null;

return function;
return null;
}

/// <summary>
Expand All @@ -257,17 +233,14 @@ public static DbFunction RemoveDbFunction(
[NotNull] IMutableModel model,
[NotNull] string name)
{
var functions = (SortedDictionary<string, DbFunction>)model[RelationalAnnotationNames.DbFunctions];
if (functions == null
|| !functions.TryGetValue(name, out var function))
if (model[RelationalAnnotationNames.DbFunctions] is SortedDictionary<string, DbFunction> functions
&& functions.TryGetValue(name, out var function))
{
return null;
functions.Remove(name);
function.Builder = null;
}

functions.Remove(name);
function.Builder = null;

return function;
return null;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Query/ShapedQueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private ShapedQueryExpression(
public virtual Expression ShaperExpression { get; }

public override Type Type => ResultCardinality == ResultCardinality.Enumerable
? typeof(IQueryable<>).MakeGenericType(ShaperExpression.Type)
? typeof(IQueryable<>).MakeGenericType(ShaperExpression.Type)
: ShaperExpression.Type;

public sealed override ExpressionType NodeType => ExpressionType.Extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static int DuplicateNameTest()
public static MethodInfo MethodHmi = typeof(TestMethods).GetTypeInfo().GetDeclaredMethod(nameof(TestMethods.MethodH));

public static MethodInfo MethodJmi = typeof(TestMethods).GetTypeInfo().GetDeclaredMethod(nameof(TestMethods.MethodJ));

public class TestMethods
{
public static int Foo => 1;
Expand Down