forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translate string Contains, StartsWith, and EndsWith
Summary of changes - Adds initial StringMethodTranslator for Cosmos provider. - Translates Contains, StartsWith, and EndsWith Addresses part of dotnet#16143
- Loading branch information
Showing
2 changed files
with
82 additions
and
1 deletion.
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
81 changes: 81 additions & 0 deletions
81
src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.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,81 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.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 StringMethodTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly MethodInfo _containsMethodInfo | ||
= typeof(string).GetRuntimeMethod(nameof(string.Contains), new[] { typeof(string) }); | ||
|
||
private static readonly MethodInfo _startsWithMethodInfo | ||
= typeof(string).GetRuntimeMethod(nameof(string.StartsWith), new[] { typeof(string) }); | ||
|
||
private static readonly MethodInfo _endsWithMethodInfo | ||
= typeof(string).GetRuntimeMethod(nameof(string.EndsWith), new[] { 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 StringMethodTranslator([NotNull] 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) | ||
{ | ||
Check.NotNull(method, nameof(method)); | ||
Check.NotNull(arguments, nameof(arguments)); | ||
|
||
if (_containsMethodInfo.Equals(method)) | ||
{ | ||
return TranslateSystemFunction("CONTAINS", instance, arguments[0], typeof(bool)); | ||
} | ||
|
||
if (_startsWithMethodInfo.Equals(method)) | ||
{ | ||
return TranslateSystemFunction("STARTSWITH", instance, arguments[0], typeof(bool)); | ||
} | ||
|
||
if (_endsWithMethodInfo.Equals(method)) | ||
{ | ||
return TranslateSystemFunction("ENDSWITH", instance, arguments[0], typeof(bool)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private SqlExpression TranslateSystemFunction(string function, SqlExpression instance, SqlExpression pattern, Type returnType) | ||
{ | ||
Check.NotNull(instance, nameof(instance)); | ||
return _sqlExpressionFactory.Function( | ||
function, | ||
new[] { instance, pattern }, | ||
returnType, | ||
ExpressionExtensions.InferTypeMapping(instance, pattern)); | ||
} | ||
} | ||
} |