-
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.
Closes #8813
- Loading branch information
Showing
14 changed files
with
260 additions
and
15 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/EFCore.Relational/Extensions/RelationalDbFunctionsExtensions.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,34 @@ | ||
// 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 JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public static class RelationalDbFunctionsExtensions | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Explicitly specifies a collation to be used in a LINQ query. Can be used to generate fragments such as | ||
/// <code>WHERE customer.name COLLATE 'de_DE' = 'John Doe'</code>. | ||
/// </para> | ||
/// <para> | ||
/// The available collations and their names vary across databases, consult your database's documentation for more | ||
/// information. | ||
/// </para> | ||
/// </summary> | ||
/// <typeparam name="TProperty"> The type of the operand on which the collation is being specified. </typeparam> | ||
/// <param name="_"> The DbFunctions instance. </param> | ||
/// <param name="operand"> The operand to which to apply the collation. </param> | ||
/// <param name="collation"> The name of the collation. </param> | ||
public static TProperty Collate<TProperty>( | ||
[NotNull] this DbFunctions _, | ||
[NotNull] TProperty operand, | ||
[NotNull] [NotParameterized] string collation) | ||
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Collate))); | ||
} | ||
} |
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,36 @@ | ||
// 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.Collections.Generic; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
public class CollateTranslator : IMethodCallTranslator | ||
{ | ||
private static readonly MethodInfo _methodInfo | ||
= typeof(RelationalDbFunctionsExtensions).GetMethod(nameof(RelationalDbFunctionsExtensions.Collate)); | ||
|
||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public CollateTranslator([NotNull] ISqlExpressionFactory sqlExpressionFactory) | ||
=> _sqlExpressionFactory = sqlExpressionFactory; | ||
|
||
public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments) | ||
{ | ||
Check.NotNull(method, nameof(method)); | ||
Check.NotNull(arguments, nameof(arguments)); | ||
|
||
return method.IsGenericMethod | ||
&& Equals(method.GetGenericMethodDefinition(), _methodInfo) | ||
&& arguments[2] is SqlConstantExpression constantExpression | ||
&& constantExpression.Value is string collation | ||
? new CollateExpression(arguments[1], collation) | ||
: 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
68 changes: 68 additions & 0 deletions
68
src/EFCore.Relational/Query/SqlExpressions/CollateExpression.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,68 @@ | ||
// 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.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions | ||
{ | ||
public class CollateExpression : SqlExpression | ||
{ | ||
public CollateExpression( | ||
[NotNull] SqlExpression operand, | ||
[NotNull] string collation) | ||
: base(operand.Type, operand.TypeMapping) | ||
{ | ||
Check.NotNull(operand, nameof(operand)); | ||
Check.NotEmpty(collation, nameof(collation)); | ||
|
||
Operand = operand; | ||
Collation = collation; | ||
} | ||
|
||
public virtual SqlExpression Operand { get; } | ||
public virtual string Collation { get; } | ||
|
||
protected override Expression VisitChildren(ExpressionVisitor visitor) | ||
{ | ||
Check.NotNull(visitor, nameof(visitor)); | ||
|
||
return Update((SqlExpression)visitor.Visit(Operand)); | ||
} | ||
|
||
public virtual CollateExpression Update([NotNull] SqlExpression operand) | ||
{ | ||
Check.NotNull(operand, nameof(operand)); | ||
|
||
return operand != Operand | ||
? new CollateExpression(operand, Collation) | ||
: this; | ||
} | ||
|
||
public override void Print(ExpressionPrinter expressionPrinter) | ||
{ | ||
Check.NotNull(expressionPrinter, nameof(expressionPrinter)); | ||
|
||
expressionPrinter.Visit(Operand); | ||
expressionPrinter | ||
.Append(" COLLATE ") | ||
.Append(Collation); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
=> obj != null | ||
&& (ReferenceEquals(this, obj) | ||
|| obj is CollateExpression collateExpression | ||
&& Equals(collateExpression)); | ||
|
||
private bool Equals(CollateExpression collateExpression) | ||
=> base.Equals(collateExpression) | ||
&& Operand.Equals(collateExpression.Operand) | ||
&& Collation.Equals(collateExpression.Collation, StringComparison.Ordinal); | ||
|
||
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Operand, Collation); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...FCore.Relational.Specification.Tests/Query/RelationalNorthwindDbFunctionsQueryTestBase.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,43 @@ | ||
// 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.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.TestModels.Northwind; | ||
using Microsoft.EntityFrameworkCore.TestUtilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query | ||
{ | ||
public abstract class RelationalNorthwindDbFunctionsQueryTestBase<TFixture> : NorthwindDbFunctionsQueryTestBase<TFixture> | ||
where TFixture : NorthwindQueryRelationalFixture<NoopModelCustomizer>, new() | ||
{ | ||
public RelationalNorthwindDbFunctionsQueryTestBase(TFixture fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public virtual Task Collate_case_insensitive(bool async) | ||
=> AssertCount( | ||
async, | ||
ss => ss.Set<Customer>(), | ||
ss => ss.Set<Customer>(), | ||
c => EF.Functions.Collate(c.ContactName, CaseInsensitiveCollation) == "maria anders", | ||
c => c.ContactName.Equals("maria anders", StringComparison.OrdinalIgnoreCase)); | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public virtual Task Collate_case_sensitive(bool async) | ||
=> AssertCount( | ||
async, | ||
ss => ss.Set<Customer>(), | ||
ss => ss.Set<Customer>(), | ||
c => EF.Functions.Collate(c.ContactName, CaseSensitiveCollation) == "maria anders", | ||
c => c.ContactName.Equals("maria anders", StringComparison.Ordinal)); | ||
|
||
protected abstract string CaseInsensitiveCollation { get; } | ||
protected abstract string CaseSensitiveCollation { get; } | ||
} | ||
} |
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.