-
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
[6.0] Code literal support for DateOnly/TimeOnly #26159
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
using System.Reflection; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Design.Internal; | ||
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal; | ||
using Microsoft.EntityFrameworkCore.TestUtilities; | ||
using Xunit; | ||
|
@@ -275,12 +276,52 @@ public override void String_literal_generated_correctly() | |
Test_GenerateSqlLiteral_helper(GetMapping("varchar(max)"), "Text", "'Text'"); | ||
} | ||
|
||
[ConditionalFact] | ||
public virtual void DateOnly_code_literal_generated_correctly() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: we should probably improve type mapping testing, see #26158 |
||
{ | ||
var typeMapping = new DateOnlyTypeMapping("date", DbType.Date); | ||
|
||
Test_GenerateCodeLiteral_helper(typeMapping, new DateOnly(2020, 3, 5), "new DateOnly(2020, 3, 5)"); | ||
} | ||
|
||
[ConditionalFact] | ||
public virtual void TimeOnly_code_literal_generated_correctly() | ||
{ | ||
var typeMapping = new TimeOnlyTypeMapping("time", DbType.Time); | ||
|
||
Test_GenerateCodeLiteral_helper(typeMapping, new TimeOnly(12, 30, 10), "new TimeOnly(12, 30, 10)"); | ||
Test_GenerateCodeLiteral_helper(typeMapping, new TimeOnly(12, 30, 10, 500), "new TimeOnly(12, 30, 10, 500)"); | ||
|
||
Test_GenerateCodeLiteral_helper( | ||
typeMapping, | ||
new TimeOnly(12, 30, 10).Add(TimeSpan.FromTicks(10)), | ||
"new TimeOnly(12, 30, 10).Add(TimeSpan.FromTicks(10))"); | ||
Test_GenerateCodeLiteral_helper( | ||
typeMapping, | ||
new TimeOnly(12, 30, 10, 500).Add(TimeSpan.FromTicks(10)), | ||
"new TimeOnly(12, 30, 10, 500).Add(TimeSpan.FromTicks(10))"); | ||
} | ||
|
||
public static RelationalTypeMapping GetMapping(string type) | ||
=> new SqlServerTypeMappingSource( | ||
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(), | ||
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>()) | ||
.FindMapping(type); | ||
|
||
protected virtual void Test_GenerateCodeLiteral_helper( | ||
RelationalTypeMapping typeMapping, | ||
object value, | ||
string expectedCode) | ||
{ | ||
var typeMappingSource = new SqlServerTypeMappingSource( | ||
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(), | ||
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>()); | ||
|
||
var csharpHelper = new CSharpHelper(typeMappingSource); | ||
|
||
Assert.Equal(expectedCode, csharpHelper.UnknownLiteral(value)); | ||
} | ||
|
||
private class FakeType : Type | ||
{ | ||
public FakeType(string fullName) | ||
|
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.
It doesn't seem sustainable that we add something here whenever any provider supports any new type. In this case, I can see that ultimately it does belong here since its essentially a new primitive, even though it's not supported by SQL Server yet. So, I just want to clarify that this kind of change isn't needed and that providers can do whatever is necessary without needing a change in this file.
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.
Agreed - I do think this makes sense for built-in BCL types, but type mappings can also override GenerateCodeLiteral. We can consider moving some of the things from here to the mappings.