-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Define SqlServer translation - Define Sqllite translation Fixes #31136
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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; | ||
} | ||
} |