Skip to content

Commit

Permalink
Using SqlServerCondition.SupportsFunctions2022 for LTRIM and RTRIM. H…
Browse files Browse the repository at this point in the history
…andling compatibility level to low.
  • Loading branch information
abdielcs committed May 16, 2024
1 parent 4448f35 commit e1a73e7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore.SqlServer/Properties/SqlServerStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,7 @@
<data name="TransientExceptionDetected" xml:space="preserve">
<value>An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call.</value>
</data>
<data name="TrimStartTrimEndWithArgsCompatibilityLevelTooLow" xml:space="preserve">
<value>EF Core's SQL Server compatibility level is set to {compatibilityLevel}; compatibility level 160 (SQL Server 2022) is the minimum for functions LTRIM and RTRIM with the optional characters positional argument.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Internal;
using ExpressionExtensions = Microsoft.EntityFrameworkCore.Query.ExpressionExtensions;

namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
Expand Down Expand Up @@ -205,7 +206,7 @@ public SqlServerStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactor
}

if (TrimStartMethodInfoWithCharArg.Equals(method)
&& _sqlServerSingletonOptions.CompatibilityLevel >= 160)
&& CheckTrimStartTrimEndWithArgsCompatibilityLevel())
{
return _sqlExpressionFactory.Function(
"LTRIM",
Expand All @@ -217,7 +218,7 @@ public SqlServerStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactor
}

if (TrimStartMethodInfoWithCharArrayArg.Equals(method)
&& _sqlServerSingletonOptions.CompatibilityLevel >= 160)
&& CheckTrimStartTrimEndWithArgsCompatibilityLevel())
{
var firstArgumentValue = (arguments[0] as SqlConstantExpression)?.Value as char[];
var firstArgument = _sqlExpressionFactory.Constant(new string(firstArgumentValue), instance.TypeMapping);
Expand Down Expand Up @@ -246,7 +247,7 @@ public SqlServerStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactor


if (TrimEndMethodInfoWithCharArg.Equals(method)
&& _sqlServerSingletonOptions.CompatibilityLevel >= 160)
&& CheckTrimStartTrimEndWithArgsCompatibilityLevel())
{
return _sqlExpressionFactory.Function(
"RTRIM",
Expand All @@ -258,7 +259,7 @@ public SqlServerStringMethodTranslator(ISqlExpressionFactory sqlExpressionFactor
}

if (TrimEndMethodInfoWithCharArrayArg.Equals(method)
&& _sqlServerSingletonOptions.CompatibilityLevel >= 160)
&& CheckTrimStartTrimEndWithArgsCompatibilityLevel())
{
var firstArgumentValue = (arguments[0] as SqlConstantExpression)?.Value as char[];
var firstArgument = _sqlExpressionFactory.Constant(new string(firstArgumentValue), instance.TypeMapping);
Expand Down Expand Up @@ -421,4 +422,16 @@ private SqlExpression TranslateIndexOf(
},
charIndexExpression);
}

private bool CheckTrimStartTrimEndWithArgsCompatibilityLevel()
=> CheckCompatibilityLevel(160, SqlServerStrings.TrimStartTrimEndWithArgsCompatibilityLevelTooLow);

private bool CheckCompatibilityLevel(int compatibilityLevel, Func<object?, string> compatibilityTooLowFunction)
{
if (_sqlServerSingletonOptions.CompatibilityLevel < compatibilityLevel)
{
throw new InvalidOperationException(compatibilityTooLowFunction(compatibilityLevel));
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,7 @@ WHERE LTRIM([c].[ContactTitle]) = N'Owner'
""");
}

[SqlServerCondition(SqlServerCondition.SupportsFunctions2022)]
public override async Task TrimStart_with_char_argument_in_predicate(bool async)
{
await base.TrimStart_with_char_argument_in_predicate(async);
Expand All @@ -2552,6 +2553,7 @@ WHERE LTRIM([c].[ContactTitle], N'O') = N'wner'
""");
}

[SqlServerCondition(SqlServerCondition.SupportsFunctions2022)]
public override async Task TrimStart_with_char_array_argument_in_predicate(bool async)
{
await base.TrimStart_with_char_array_argument_in_predicate(async);
Expand All @@ -2576,6 +2578,7 @@ WHERE RTRIM([c].[ContactTitle]) = N'Owner'
""");
}

[SqlServerCondition(SqlServerCondition.SupportsFunctions2022)]
public override async Task TrimEnd_with_char_argument_in_predicate(bool async)
{
await base.TrimEnd_with_char_argument_in_predicate(async);
Expand All @@ -2588,6 +2591,7 @@ WHERE RTRIM([c].[ContactTitle], N'r') = N'Owne'
""");
}

[SqlServerCondition(SqlServerCondition.SupportsFunctions2022)]
public override async Task TrimEnd_with_char_array_argument_in_predicate(bool async)
{
await base.TrimEnd_with_char_array_argument_in_predicate(async);
Expand Down

0 comments on commit e1a73e7

Please sign in to comment.