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

Add EF.Functions.JsonExists(expression, path) DbFunction #35389

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,32 @@ public static T Greatest<T>(
this DbFunctions _,
[NotParameterized] params T[] values)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Greatest)));

/// <summary>
/// Checks whether a specified JSON path exists within a JSON string.
/// Typically corresponds to a database function or SQL expression.
/// </summary>
/// <remarks>
/// <para>
/// This method is translated to a database-specific function or expression.
/// The support for this function depends on the database and provider being used.
/// Refer to your database provider's documentation for detailed support information.
/// </para>
/// <para>
/// For more details, see <see href="https://learn.microsoft.com/en-us/ef/core/providers">EF Core database providers</see>.
/// </para>
/// </remarks>
/// <param name="_">The <see cref="DbFunctions"/> instance.</param>
/// <param name="expression">The JSON string or column containing JSON text.</param>
/// <param name="path">The JSON path to check for existence.</param>
/// <typeparam name="T">The type of the JSON expression.</typeparam>
/// <returns>
/// A nullable boolean value, <see langword="true"/> if the JSON path exists, <see langword="false"/> if not, and <see langword="null"/>
/// when the JSON string is null.
/// </returns>
public static bool? JsonExists<T>(
this DbFunctions _,
T expression,
string path)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(JsonExists)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public SqlServerMethodCallTranslatorProvider(
new SqlServerFullTextSearchFunctionsTranslator(sqlExpressionFactory),
new SqlServerIsDateFunctionTranslator(sqlExpressionFactory),
new SqlServerIsNumericFunctionTranslator(sqlExpressionFactory),
new SqlServerJsonFunctionsTranslator(sqlExpressionFactory, sqlServerSingletonOptions),
new SqlServerMathTranslator(sqlExpressionFactory),
new SqlServerNewGuidTranslator(sqlExpressionFactory),
new SqlServerObjectToStringTranslator(sqlExpressionFactory, typeMappingSource),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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;
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.SqlServer.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 SqlServerJsonFunctionsTranslator : IMethodCallTranslator
{
private static readonly MethodInfo JsonExistsMethodInfo = typeof(RelationalDbFunctionsExtensions)
.GetRuntimeMethod(nameof(RelationalDbFunctionsExtensions.JsonExists), [typeof(DbFunctions), typeof(object), typeof(string)])!;

private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly ISqlServerSingletonOptions _sqlServerSingletonOptions;

/// <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 SqlServerJsonFunctionsTranslator(ISqlExpressionFactory sqlExpressionFactory, ISqlServerSingletonOptions sqlServerSingletonOptions)
{
_sqlExpressionFactory = sqlExpressionFactory;
_sqlServerSingletonOptions = sqlServerSingletonOptions;
}

/// <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 virtual SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (JsonExistsMethodInfo.Equals(method)
&& arguments[0].TypeMapping is SqlServerOwnedJsonTypeMapping or StringTypeMapping
&& _sqlServerSingletonOptions.EngineType == SqlServerEngineType.SqlServer
&& _sqlServerSingletonOptions.SqlServerCompatibilityLevel >= 160)
{
return _sqlExpressionFactory.Function(
"JSON_PATH_EXISTS",
arguments,
nullable: true,
argumentsPropagateNullability: Statics.TrueArrays[2],
method.ReturnType);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public SqliteMethodCallTranslatorProvider(RelationalMethodCallTranslatorProvider
new SqliteDateTimeMethodTranslator(sqlExpressionFactory),
new SqliteGlobMethodTranslator(sqlExpressionFactory),
new SqliteHexMethodTranslator(sqlExpressionFactory),
new SqliteJsonFunctionsTranslator(sqlExpressionFactory),
new SqliteMathTranslator(sqlExpressionFactory),
new SqliteObjectToStringTranslator(sqlExpressionFactory),
new SqliteRandomTranslator(sqlExpressionFactory),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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;
using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Sqlite.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 SqliteJsonFunctionsTranslator : IMethodCallTranslator
{
private static readonly MethodInfo JsonExistsMethodInfo = typeof(RelationalDbFunctionsExtensions)
.GetRuntimeMethod(nameof(RelationalDbFunctionsExtensions.JsonExists), [typeof(DbFunctions), typeof(object), typeof(string)])!;

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 SqliteJsonFunctionsTranslator(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 virtual SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (JsonExistsMethodInfo.Equals(method)
&& arguments[0].TypeMapping is SqliteJsonTypeMapping or StringTypeMapping)
{
// IIF(arguments_0 IS NULL, NULL, JSON_TYPE(arguments_0, arguments_1) IS NOT NULL)
return _sqlExpressionFactory.Function("IFF",
mseada94 marked this conversation as resolved.
Show resolved Hide resolved
[
_sqlExpressionFactory.IsNull(arguments[0]),
_sqlExpressionFactory.Fragment("NULL", method.ReturnType),
_sqlExpressionFactory.IsNotNull(
_sqlExpressionFactory.Function("JSON_TYPE",
arguments,
nullable: true,
argumentsPropagateNullability: Statics.TrueArrays[2],
returnType: typeof(string)))
],
nullable: true,
argumentsPropagateNullability: Statics.TrueArrays[3],
method.ReturnType);
}

return null;
}
}
Loading