|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 5 | +using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal; |
| 6 | + |
| 7 | +// ReSharper disable once CheckNamespace |
| 8 | +namespace Microsoft.EntityFrameworkCore.Sqlite.Query.Internal; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 12 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 13 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 14 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 15 | +/// </summary> |
| 16 | +public class SqliteJsonFunctionsTranslator : IMethodCallTranslator |
| 17 | +{ |
| 18 | + private static readonly MethodInfo JsonExistsMethodInfo = typeof(RelationalDbFunctionsExtensions) |
| 19 | + .GetRuntimeMethod(nameof(RelationalDbFunctionsExtensions.JsonExists), [typeof(DbFunctions), typeof(object), typeof(string)])!; |
| 20 | + |
| 21 | + private readonly ISqlExpressionFactory _sqlExpressionFactory; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 25 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 26 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 27 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 28 | + /// </summary> |
| 29 | + public SqliteJsonFunctionsTranslator(ISqlExpressionFactory sqlExpressionFactory) |
| 30 | + { |
| 31 | + _sqlExpressionFactory = sqlExpressionFactory; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 36 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 37 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 38 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 39 | + /// </summary> |
| 40 | + public virtual SqlExpression? Translate( |
| 41 | + SqlExpression? instance, |
| 42 | + MethodInfo method, |
| 43 | + IReadOnlyList<SqlExpression> arguments, |
| 44 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 45 | + { |
| 46 | + if (JsonExistsMethodInfo.Equals(method) |
| 47 | + && arguments[0].TypeMapping is not SqliteJsonTypeMapping and not StringTypeMapping) |
| 48 | + { |
| 49 | + // IIF(arguments_0 IS NULL, NULL, JSON_TYPE(arguments_0, arguments_1) IS NOT NULL) |
| 50 | + return _sqlExpressionFactory.Function("IFF", |
| 51 | + [ |
| 52 | + _sqlExpressionFactory.IsNull(arguments[0]), |
| 53 | + _sqlExpressionFactory.Fragment("NULL", method.ReturnType), |
| 54 | + _sqlExpressionFactory.IsNotNull( |
| 55 | + _sqlExpressionFactory.Function("JSON_TYPE", |
| 56 | + arguments, |
| 57 | + nullable: true, |
| 58 | + argumentsPropagateNullability: Statics.TrueArrays[2], |
| 59 | + returnType: typeof(string))) |
| 60 | + ], |
| 61 | + nullable: true, |
| 62 | + argumentsPropagateNullability: Statics.TrueArrays[3], |
| 63 | + method.ReturnType); |
| 64 | + } |
| 65 | + |
| 66 | + return null; |
| 67 | + } |
| 68 | +} |
0 commit comments