|
| 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 System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 10 | +/// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 11 | +/// any release. You should only use it directly in your code with extreme caution and knowing that |
| 12 | +/// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 13 | +/// </summary> |
| 14 | +public class RegexMethodTranslator : IMethodCallTranslator |
| 15 | +{ |
| 16 | + private static readonly HashSet<MethodInfo> MethodInfoDataLengthMapping |
| 17 | + = new() |
| 18 | + { |
| 19 | + typeof(Regex).GetRuntimeMethod(nameof(Regex.IsMatch), new[] { typeof(string), typeof(string) })!, |
| 20 | + typeof(Regex).GetRuntimeMethod(nameof(Regex.IsMatch), new[] { typeof(ReadOnlySpan<char>), typeof(string) })! |
| 21 | + }; |
| 22 | + |
| 23 | + private readonly ISqlExpressionFactory _sqlExpressionFactory; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 27 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 28 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 29 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 30 | + /// </summary> |
| 31 | + public RegexMethodTranslator(ISqlExpressionFactory sqlExpressionFactory) |
| 32 | + { |
| 33 | + _sqlExpressionFactory = sqlExpressionFactory; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 38 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 39 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 40 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 41 | + /// </summary> |
| 42 | + public virtual SqlExpression? Translate( |
| 43 | + SqlExpression? instance, |
| 44 | + MethodInfo method, |
| 45 | + IReadOnlyList<SqlExpression> arguments, |
| 46 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger) |
| 47 | + { |
| 48 | + if (MethodInfoDataLengthMapping.Contains(method)) |
| 49 | + { |
| 50 | + var typeMapping = arguments.Count == 2 |
| 51 | + ? ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]) |
| 52 | + : ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1], arguments[2]); |
| 53 | + |
| 54 | + var newArguments = arguments.Select(e => _sqlExpressionFactory.ApplyTypeMapping(e, typeMapping)); |
| 55 | + |
| 56 | + return _sqlExpressionFactory.Function( |
| 57 | + "RegexMatch", |
| 58 | + newArguments, |
| 59 | + method.ReturnType, |
| 60 | + typeMapping); |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | +} |
| 66 | + |
0 commit comments