-
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.
Merge branch 'ef-functions' of https://github.com/roji/EntityFramework …
…into roji-ef-functions
- Loading branch information
Showing
14 changed files
with
279 additions
and
7 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/EFCore.Relational.Specification.Tests/RelationalQueryTestBase.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,51 @@ | ||
// 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.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Specification.Tests; | ||
using Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind; | ||
using Microsoft.EntityFrameworkCore.Specification.Tests.TestUtilities.Xunit; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Relational.Specification.Tests | ||
{ | ||
public abstract class RelationalQueryTestBase<TFixture> : QueryTestBase<TFixture> | ||
where TFixture : NorthwindQueryFixtureBase, new() | ||
{ | ||
[ConditionalFact] | ||
public virtual void String_Like_Literal() | ||
{ | ||
using (var context = CreateContext()) | ||
{ | ||
var count = context.Customers.Count(c => EF.Functions.Like(c.ContactName, "%M%")); | ||
Assert.Equal(19, count); | ||
} | ||
} | ||
|
||
[ConditionalFact] | ||
public virtual void String_Like_Identity() | ||
{ | ||
using (var context = CreateContext()) | ||
{ | ||
var count = context.Customers.Count(c => EF.Functions.Like(c.ContactName, c.ContactName)); | ||
Assert.Equal(91, count); | ||
} | ||
} | ||
|
||
[ConditionalFact] | ||
public virtual void String_Like_Literal_With_Escape() | ||
{ | ||
using (var context = CreateContext()) | ||
{ | ||
var count = context.Customers.Count(c => EF.Functions.Like(c.ContactName, "!%", '!')); | ||
Assert.Equal(0, count); | ||
} | ||
} | ||
|
||
protected RelationalQueryTestBase(TFixture fixture) : base(fixture) {} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
src/EFCore.Relational/Query/ExpressionTranslators/Internal/LikeTranslator.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,51 @@ | ||
// 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.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Query.Expressions; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class LikeTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly MethodInfo _methodInfo | ||
= typeof(RelationalDbFunctionsExtensions).GetRuntimeMethod(nameof(RelationalDbFunctionsExtensions.Like), | ||
new[] { typeof(DbFunctions), typeof(string), typeof(string) }); | ||
|
||
private static readonly MethodInfo _methodInfoWithEscape | ||
= typeof(RelationalDbFunctionsExtensions).GetRuntimeMethod(nameof(RelationalDbFunctionsExtensions.Like), | ||
new[] { typeof(DbFunctions), typeof(string), typeof(string), typeof(char) }); | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual Expression Translate(MethodCallExpression methodCallExpression) | ||
{ | ||
Check.NotNull(methodCallExpression, nameof(methodCallExpression)); | ||
|
||
if (Equals(methodCallExpression.Method, _methodInfo)) | ||
{ | ||
return new LikeExpression(methodCallExpression.Arguments[1], methodCallExpression.Arguments[2]); | ||
} | ||
|
||
if (Equals(methodCallExpression.Method, _methodInfoWithEscape)) | ||
{ | ||
return new LikeExpression(methodCallExpression.Arguments[1], methodCallExpression.Arguments[2], methodCallExpression.Arguments[3]); | ||
} | ||
|
||
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using JetBrains.Annotations; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
/// <summary> | ||
/// Provides CLR methods that get translated to database functions when used in LINQ to Entities queries. | ||
/// The methods on this class are accessed via <see cref="EF.Functions"/>. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public static class RelationalDbFunctionsExtensions | ||
{ | ||
/// <summary> | ||
/// Indicates whether the specified input string matches the given pattern by sending an SQL LIKE | ||
/// expression to the database. | ||
/// </summary> | ||
/// <param name="functions">Should always be <see cref="EF.Functions"/>.</param> | ||
/// <param name="input">The string to search for a match.</param> | ||
/// <param name="pattern">The regular expression pattern to match.</param> | ||
/// <returns><string>true</string> if the LIKE expression finds a match; otherwise, <string>false</string>.</returns> | ||
public static bool Like( | ||
[NotNull] this DbFunctions functions, | ||
[NotNull] string input, | ||
[NotNull] string pattern) | ||
=> throw new NotSupportedException(RelationalStrings.DbFunctionsDirectCall); | ||
|
||
/// <summary> | ||
/// Indicates whether the specified input string matches the given pattern by sending an SQL LIKE | ||
/// expression to the database. | ||
/// </summary> | ||
/// <param name="functions">Should always be <see cref="EF.Functions"/>.</param> | ||
/// <param name="input">The string to search for a match.</param> | ||
/// <param name="pattern">The regular expression pattern to match.</param> | ||
/// <param name="escapeChar">Character to use as an escape character in <paramref name="pattern"/>.</param> | ||
/// <returns><string>true</string> if the LIKE expression finds a match; otherwise, <string>false</string>.</returns> | ||
public static bool Like( | ||
[NotNull] this DbFunctions functions, | ||
[NotNull] string input, | ||
[NotNull] string pattern, | ||
char escapeChar) | ||
=> throw new NotSupportedException(RelationalStrings.DbFunctionsDirectCall); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
....SqlServer/Query/ExpressionTranslators/Internal/SqlServerCompositeMethodCallTranslator.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
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,15 @@ | ||
// 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. | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
/// <summary> | ||
/// Provides CLR methods that get translated to database functions when used in LINQ to Entities queries. | ||
/// The methods on this class are accessed via <see cref="EF.Functions"/>. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public class DbFunctions | ||
{ | ||
internal DbFunctions() { } | ||
} | ||
} |
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
Oops, something went wrong.