-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Implement EF.Functions.JsonExists translation for SQL Server (#31136) #37470
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
Closed
kimjaejung96
wants to merge
2
commits into
dotnet:main
from
kimjaejung96:users/kimjaejung96/implement-json-exists-31136
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/EFCore.SqlServer/Query/Internal/Translators/SqlServerJsonFunctionTranslator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // 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 : IMethodCallTranslator | ||
| { | ||
| private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
|
||
| private static readonly MethodInfo JsonExistsMethodInfo = typeof(RelationalDbFunctionsExtensions) | ||
| .GetRuntimeMethod(nameof(RelationalDbFunctionsExtensions.JsonExists), [typeof(DbFunctions), typeof(object), typeof(string)])!; | ||
|
|
||
| /// <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 SqlServerJsonFunctionTranslator(ISqlExpressionFactory sqlExpressionFactory) | ||
kimjaejung96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| => _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)) | ||
kimjaejung96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
kimjaejung96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return _sqlExpressionFactory.Equal( | ||
| _sqlExpressionFactory.Function( | ||
| "JSON_PATH_EXISTS", | ||
| [arguments[1], arguments[2]], | ||
| nullable: true, | ||
| argumentsPropagateNullability: [true, false], | ||
| typeof(int)), | ||
| _sqlExpressionFactory.Constant(1)); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // ReSharper disable ArgumentsStyleStringLiteral | ||
| // ReSharper disable InconsistentNaming | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| public class RelationalDbFunctionsTest | ||
kimjaejung96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| [ConditionalFact] | ||
| public void Collate_on_client_throws() | ||
| => Assert.Equal( | ||
| CoreStrings.FunctionOnClient(nameof(RelationalDbFunctionsExtensions.Collate)), | ||
| Assert.Throws<InvalidOperationException>(() => EF.Functions.Collate("abc", "Latin1_General_CI_AS")).Message); | ||
|
|
||
| [ConditionalFact] | ||
| public void Least_on_client_throws() | ||
| => Assert.Equal( | ||
| CoreStrings.FunctionOnClient(nameof(RelationalDbFunctionsExtensions.Least)), | ||
| Assert.Throws<InvalidOperationException>(() => EF.Functions.Least(1, 2, 3)).Message); | ||
|
|
||
| [ConditionalFact] | ||
| public void Greatest_on_client_throws() | ||
| => Assert.Equal( | ||
| CoreStrings.FunctionOnClient(nameof(RelationalDbFunctionsExtensions.Greatest)), | ||
| Assert.Throws<InvalidOperationException>(() => EF.Functions.Greatest(1, 2, 3)).Message); | ||
|
|
||
| [ConditionalFact] | ||
| public void JsonExists_on_client_throws() | ||
| => Assert.Equal( | ||
| CoreStrings.FunctionOnClient(nameof(RelationalDbFunctionsExtensions.JsonExists)), | ||
| Assert.Throws<InvalidOperationException>(() => EF.Functions.JsonExists("{\"key\": 1}", "$.key")).Message); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.