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

Translate to NULLIF #32710

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -56,4 +56,17 @@ public static class RelationalDbFunctionsExtensions
this DbFunctions _,
[NotParameterized] params T[] values)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Greatest)));

/// <summary>
/// Returns <see langword="null"/> if <paramref name="expression"/> equals <paramref name="matchExpression"/>, otherwise returns <paramref name="expression"/>. Usually corresponds to the <c>NULLIF</c> SQL function.
/// </summary>
/// <typeparam name="T">The type</typeparam>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="expression">The expression.</param>
/// <param name="matchExpression">The matching expression.</param>
public static T? NullIf<T>(
this DbFunctions _,
T expression,
T matchExpression)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Collate)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be

Suggested change
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Collate)));
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(NullIf)));

}
52 changes: 52 additions & 0 deletions src/EFCore.Relational/Query/Internal/NullIfTranslator.cs
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace Microsoft.EntityFrameworkCore.Query.Internal;

/// <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 class NullIfTranslator : IMethodCallTranslator
{
private static readonly MethodInfo MethodInfo
= typeof(RelationalDbFunctionsExtensions).GetMethod(nameof(RelationalDbFunctionsExtensions.NullIf))!;

private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <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 NullIfTranslator(ISqlExpressionFactory sqlExpressionFactory)
{
_sqlExpressionFactory = sqlExpressionFactory;
}

/// <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 SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
=> method.IsGenericMethod
&& Equals(method.GetGenericMethodDefinition(), MethodInfo)
? _sqlExpressionFactory.Function(
"NULLIF",
new[] { arguments[1], arguments[2] },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
method.ReturnType)
: null;
}
Expand Up @@ -37,7 +37,8 @@ public RelationalMethodCallTranslatorProvider(RelationalMethodCallTranslatorProv
new GetValueOrDefaultTranslator(sqlExpressionFactory),
new ComparisonTranslator(sqlExpressionFactory),
new ByteArraySequenceEqualTranslator(sqlExpressionFactory),
new RandomTranslator(sqlExpressionFactory)
new RandomTranslator(sqlExpressionFactory),
new NullIfTranslator(sqlExpressionFactory)
});
_sqlExpressionFactory = sqlExpressionFactory;
}
Expand Down
Expand Up @@ -92,6 +92,16 @@ public virtual async Task Greatest_with_parameter_array_is_not_supported(bool as
ss => ss.Set<OrderDetail>().Where(od => EF.Functions.Greatest(arr) == 10251)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task NullIf(bool async)
=> AssertCount(
async,
ss => ss.Set<Customer>(),
ss => ss.Set<Customer>(),
c => EF.Functions.NullIf(c.ContactName, "maria anders") == null,
c => c.ContactName == null);

protected abstract string CaseInsensitiveCollation { get; }
protected abstract string CaseSensitiveCollation { get; }
}