|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Reflection; |
| 7 | +using JetBrains.Annotations; |
| 8 | +using Microsoft.EntityFrameworkCore.Diagnostics; |
| 9 | +using Microsoft.EntityFrameworkCore.Query; |
| 10 | +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 11 | +using Microsoft.EntityFrameworkCore.Utilities; |
| 12 | + |
| 13 | +#nullable enable |
| 14 | + |
| 15 | +namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 19 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 20 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 21 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 22 | + /// </summary> |
| 23 | + public class SqlServerIsNumericFunctionTranslator : IMethodCallTranslator |
| 24 | + { |
| 25 | + private readonly ISqlExpressionFactory _sqlExpressionFactory; |
| 26 | + |
| 27 | + private static readonly MethodInfo _methodInfo = typeof(SqlServerDbFunctionsExtensions) |
| 28 | + .GetRequiredRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.IsNumeric), new[] { typeof(DbFunctions), typeof(string) }); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 32 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 33 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 34 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 35 | + /// </summary> |
| 36 | + public SqlServerIsNumericFunctionTranslator([NotNull] ISqlExpressionFactory sqlExpressionFactory) |
| 37 | + => _sqlExpressionFactory = sqlExpressionFactory; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 41 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 42 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 43 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 44 | + /// </summary> |
| 45 | + public virtual SqlExpression? Translate( |
| 46 | + SqlExpression? instance, |
| 47 | + MethodInfo method, |
| 48 | + IReadOnlyList<SqlExpression> arguments, |
| 49 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 50 | + { |
| 51 | + Check.NotNull(method, nameof(method)); |
| 52 | + Check.NotNull(arguments, nameof(arguments)); |
| 53 | + Check.NotNull(logger, nameof(logger)); |
| 54 | + |
| 55 | + return _methodInfo.Equals(method) |
| 56 | + ? _sqlExpressionFactory.Equal( |
| 57 | + _sqlExpressionFactory.Function( |
| 58 | + "ISNUMERIC", |
| 59 | + new[] { arguments[1] }, |
| 60 | + nullable: false, |
| 61 | + argumentsPropagateNullability: new[] { false }, |
| 62 | + typeof(int)), |
| 63 | + _sqlExpressionFactory.Constant(1)) |
| 64 | + : null; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments