Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ public static T Greatest<T>(
this DbFunctions _,
[NotParameterized] params T[] values)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Greatest)));

/// <summary>
/// Returns a value indicating whether a given JSON path exists within the specified JSON.
/// Usually corresponds to the <c>JSON_PATH_EXISTS</c> SQL function.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="json">The JSON value to check.</param>
/// <param name="path">The JSON path to look for.</param>
public static bool JsonExists(
this DbFunctions _,
object json,
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 SqlServerJsonFunctionTranslator(sqlExpressionFactory),
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,39 @@
// 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;

// 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 SqlServerJsonFunctionTranslator(ISqlExpressionFactory sqlExpressionFactory) : IMethodCallTranslator
{
/// <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)
=> method.Name == nameof(RelationalDbFunctionsExtensions.JsonExists)
&& method.DeclaringType == typeof(RelationalDbFunctionsExtensions)
? sqlExpressionFactory.Equal(
sqlExpressionFactory.Function(
"JSON_PATH_EXISTS",
[arguments[1], arguments[2]],
nullable: true,
argumentsPropagateNullability: [true, false],
typeof(int)),
sqlExpressionFactory.Constant(1))
: null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,25 @@ WHERE CAST(DATALENGTH(N'foo') AS int) = 3
}
}

[ConditionalFact, SqlServerCondition(SqlServerCondition.SupportsFunctions2022)]
public virtual async Task JsonExists_with_column()
{
await using var context = CreateContext();

// Note: Address is not valid JSON, so this will return no results,
// but the purpose is to verify the SQL translation is correct
var result = await context.Customers
.Where(c => EF.Functions.JsonExists(c.Address!, "$.city"))
.ToListAsync();

AssertSql(
"""
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE JSON_PATH_EXISTS([c].[Address], N'$.city') = 1
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}