-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add EF.Functions.JsonExists(expression, path) DbFunction #35389
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
Draft
mseada94
wants to merge
9
commits into
dotnet:main
Choose a base branch
from
mseada94:feature/json-exists
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
48692ba
Add JsonExists DbFunction
mseada94 ab9e0a8
replace IFF function with CASE expression
mseada94 0c5474c
Merge branch 'dotnet:main' into feature/json-exists
mseada94 d55b43e
Add JsonExists DbFunction
mseada94 17ab035
configurer sql server 160 Compatibility Level
mseada94 522b0e4
fix nullable sql server expression return type
mseada94 1e0aa36
update test assertion
mseada94 0dcce48
update tests
mseada94 e186ed7
fix sqllite translation and null propagation issue
mseada94 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
64 changes: 64 additions & 0 deletions
64
src/EFCore.SqlServer/Query/Internal/Translators/SqlServerJsonFunctionsTranslator.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,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; | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
src/EFCore.Sqlite.Core/Query/Internal/Translators/SqliteJsonFunctionsTranslator.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,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", | ||
[ | ||
_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; | ||
} | ||
} |
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.