-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DATEFROMPARTS function to SqlServer #19400
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f3b8695
Add DATEFROMPARTS function to SqlServer
Marusyk fb0019f
Add tests
Marusyk 98c4c55
Add DATETIME2FROMPARTS function
Marusyk 0355821
Add DATETIMEOFFSETFROMPARTS, SMALLDATETIMEFROMPARTS, TIMEFROMPARTS fu…
Marusyk fa04d44
Combine all from parts methods into single class.
Marusyk 123cab0
Fix tests
Marusyk f60ff10
Fix tests
Marusyk f4880e9
Add tests for DATETIME2FROMPARTS
Marusyk 4951c6d
Add tests and fix return type for functions
Marusyk fb84a50
Fix docs
Marusyk 7a0c202
Add test for TIMEFROMPARTS
Marusyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 0 additions & 44 deletions
44
src/EFCore.SqlServer/Query/Internal/SqlServerDateTimeFromPartsFunctionTranslator.cs
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
src/EFCore.SqlServer/Query/Internal/SqlServerFromPartsFunctionTranslator.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,105 @@ | ||
// 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.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal | ||
{ | ||
public class SqlServerFromPartsFunctionTranslator : IMethodCallTranslator | ||
{ | ||
private readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
private readonly IRelationalTypeMappingSource _typeMappingSource; | ||
|
||
private static readonly MethodInfo _dateFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions) | ||
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int) }); | ||
|
||
private static readonly MethodInfo _dateTimeFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions) | ||
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }); | ||
|
||
private static readonly MethodInfo _dateTime2FromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions) | ||
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTime2FromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }); | ||
|
||
private static readonly MethodInfo _dateTimeOffsetFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions) | ||
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeOffsetFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }); | ||
|
||
private static readonly MethodInfo _smallDateTimeFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions) | ||
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.SmallDateTimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }); | ||
|
||
private static readonly MethodInfo _timeFromPartsMethodInfo = typeof(SqlServerDbFunctionsExtensions) | ||
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.TimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }); | ||
|
||
public SqlServerFromPartsFunctionTranslator( | ||
[NotNull] ISqlExpressionFactory sqlExpressionFactory, | ||
[NotNull] IRelationalTypeMappingSource typeMappingSource) | ||
{ | ||
_sqlExpressionFactory = sqlExpressionFactory; | ||
_typeMappingSource = typeMappingSource; | ||
} | ||
|
||
public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments) | ||
{ | ||
if (_dateFromPartsMethodInfo.Equals(method)) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"DATEFROMPARTS", | ||
arguments.Skip(1), | ||
_dateFromPartsMethodInfo.ReturnType, | ||
_typeMappingSource.FindMapping(typeof(DateTime), "date")); | ||
} | ||
|
||
if (_dateTimeFromPartsMethodInfo.Equals(method)) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"DATETIMEFROMPARTS", | ||
arguments.Skip(1), | ||
_dateTimeFromPartsMethodInfo.ReturnType, | ||
_typeMappingSource.FindMapping(typeof(DateTime), "datetime")); | ||
} | ||
|
||
if (_dateTime2FromPartsMethodInfo.Equals(method)) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"DATETIME2FROMPARTS", | ||
arguments.Skip(1), | ||
_dateTime2FromPartsMethodInfo.ReturnType, | ||
_typeMappingSource.FindMapping(typeof(DateTime), "datetime2")); | ||
} | ||
|
||
if (_dateTimeOffsetFromPartsMethodInfo.Equals(method)) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"DATETIMEOFFSETFROMPARTS", | ||
arguments.Skip(1), | ||
_dateTimeOffsetFromPartsMethodInfo.ReturnType, | ||
_typeMappingSource.FindMapping(typeof(DateTimeOffset), "datetimeoffset")); | ||
} | ||
|
||
if (_smallDateTimeFromPartsMethodInfo.Equals(method)) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"SMALLDATETIMEFROMPARTS", | ||
arguments.Skip(1), | ||
_smallDateTimeFromPartsMethodInfo.ReturnType, | ||
_typeMappingSource.FindMapping(typeof(DateTime), "smalldatetime")); | ||
} | ||
|
||
if (_timeFromPartsMethodInfo.Equals(method)) | ||
{ | ||
return _sqlExpressionFactory.Function( | ||
"TIMEFROMPARTS", | ||
arguments.Skip(1), | ||
_timeFromPartsMethodInfo.ReturnType, | ||
_typeMappingSource.FindMapping(typeof(TimeSpan), "time")); | ||
} | ||
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactor to set function name and return server type using case block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Should I create a new PR for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do it. I may have small clean up tasks in other unrelated PRs too.