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.
Support for DateOnly/TimeOnly for SQL Server
Closes dotnet#24507
- Loading branch information
Showing
26 changed files
with
1,878 additions
and
413 deletions.
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
686 changes: 669 additions & 17 deletions
686
src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs
Large diffs are not rendered by default.
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
57 changes: 57 additions & 0 deletions
57
src/EFCore.SqlServer/Query/Internal/SqlServerDateOnlyMemberTranslator.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,57 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.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 SqlServerDateOnlyMemberTranslator : IMemberTranslator | ||
{ | ||
private static readonly Dictionary<string, string> DatePartMapping | ||
= new() | ||
{ | ||
{ nameof(DateTime.Year), "year" }, | ||
{ nameof(DateTime.Month), "month" }, | ||
{ nameof(DateTime.DayOfYear), "dayofyear" }, | ||
{ nameof(DateTime.Day), "day" } | ||
}; | ||
|
||
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 SqlServerDateOnlyMemberTranslator(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 SqlExpression? Translate( | ||
SqlExpression? instance, | ||
MemberInfo member, | ||
Type returnType, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
=> member.DeclaringType == typeof(DateOnly) && DatePartMapping.TryGetValue(member.Name, out var datePart) | ||
? _sqlExpressionFactory.Function( | ||
"DATEPART", | ||
new[] { _sqlExpressionFactory.Fragment(datePart), instance! }, | ||
nullable: true, | ||
argumentsPropagateNullability: new[] { false, true }, | ||
returnType) | ||
: null; | ||
} |
64 changes: 64 additions & 0 deletions
64
src/EFCore.SqlServer/Query/Internal/SqlServerDateOnlyMethodTranslator.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,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.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 SqlServerDateOnlyMethodTranslator : IMethodCallTranslator | ||
{ | ||
private readonly Dictionary<MethodInfo, string> _methodInfoDatePartMapping = new() | ||
{ | ||
{ typeof(DateOnly).GetRuntimeMethod(nameof(DateOnly.AddYears), new[] { typeof(int) })!, "year" }, | ||
{ typeof(DateOnly).GetRuntimeMethod(nameof(DateOnly.AddMonths), new[] { typeof(int) })!, "month" }, | ||
{ typeof(DateOnly).GetRuntimeMethod(nameof(DateOnly.AddDays), new[] { typeof(int) })!, "day" } | ||
}; | ||
|
||
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 SqlServerDateOnlyMethodTranslator(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, | ||
IDiagnosticsLogger<DbLoggerCategory.Query> logger) | ||
{ | ||
if (_methodInfoDatePartMapping.TryGetValue(method, out var datePart) | ||
&& instance != null) | ||
{ | ||
instance = _sqlExpressionFactory.ApplyDefaultTypeMapping(instance); | ||
|
||
return _sqlExpressionFactory.Function( | ||
"DATEADD", | ||
new[] { _sqlExpressionFactory.Fragment(datePart), _sqlExpressionFactory.Convert(arguments[0], typeof(int)), instance }, | ||
nullable: true, | ||
argumentsPropagateNullability: new[] { false, true, true }, | ||
instance.Type, | ||
instance.TypeMapping); | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.