Skip to content
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 DateOnly.ToString / TimeOnly.ToString query mapping for SQL Server #31289

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public SqlServerDateOnlyMethodTranslator(ISqlExpressionFactory sqlExpressionFact
return _sqlExpressionFactory.Convert(arguments[0], typeof(DateOnly));
}


roji marked this conversation as resolved.
Show resolved Hide resolved
if (instance?.Type == typeof(DateOnly)
&& method.Name == nameof(DateOnly.ToString))
{
return _sqlExpressionFactory.Function(
"FORMAT",
new[] { instance, _sqlExpressionFactory.Constant("M/d/yyyy") },
roji marked this conversation as resolved.
Show resolved Hide resolved
nullable: true,
argumentsPropagateNullability: new[] { false, true, true },
returnType: method.ReturnType);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private static readonly Dictionary<Type, string> TypeMapping
{ typeof(decimal), $"varchar({DefaultLength})" },
{ typeof(char), "varchar(1)" },
{ typeof(DateTime), $"varchar({DefaultLength})" },
{ typeof(DateOnly), $"varchar({DefaultLength})" },
roji marked this conversation as resolved.
Show resolved Hide resolved
{ typeof(DateTimeOffset), $"varchar({DefaultLength})" },
{ typeof(TimeSpan), $"varchar({DefaultLength})" },
{ typeof(Guid), "varchar(36)" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public virtual Task ToString_boolean_property_nullable(bool async)
async,
ss => ss.Set<LocustHorde>().Select(lh => lh.Eradicated.ToString()));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_DateOnly_ToString(bool async)
=> AssertQuery(
async,
ss => ss.Set<Mission>().Where(m => m.Date.ToString() == new DateOnly(2020, 1, 1).ToString()).AsTracking(),
entryCount: 1);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Include_multiple_one_to_one_and_one_to_many_self_reference(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4040,6 +4040,18 @@ FROM [Factions] AS [f]
""");
}

public override async Task Where_DateOnly_ToString(bool async)
{
await base.Where_DateOnly_ToString(async);

AssertSql(
"""
SELECT [m].[Id], [m].[BriefingDocument], [m].[BriefingDocumentFileExtension], [m].[CodeName], [m].[Date], [m].[Duration], [m].[Rating], [m].[Time], [m].[Timeline]
FROM [Missions] AS [m]
WHERE FORMAT([m].[Date], N'M/d/yyyy') = N'1/1/2020'
""");
}

public override async Task Correlated_collections_naked_navigation_with_ToList(bool async)
{
await base.Correlated_collections_naked_navigation_with_ToList(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12583,6 +12583,18 @@ FROM [Weapons] AS [w]
""");
}

public override async Task Where_DateOnly_ToString(bool async)
{
await base.Where_DateOnly_ToString(async);

AssertSql(
"""
SELECT [m].[Id], [m].[CodeName], [m].[Date], [m].[Duration], [m].[Rating], [m].[Time], [m].[Timeline]
FROM [Missions] AS [m]
WHERE FORMAT([m].[Date], N'M/d/yyyy') = N'1/1/2020'
""");
}

public override async Task Include_on_derived_entity_with_cast(bool async)
{
await base.Include_on_derived_entity_with_cast(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10834,6 +10834,18 @@ FROM [Weapons] AS [w]
""");
}

public override async Task Where_DateOnly_ToString(bool async)
{
await base.Where_DateOnly_ToString(async);

AssertSql(
"""
SELECT [m].[Id], [m].[CodeName], [m].[Date], [m].[Duration], [m].[Rating], [m].[Time], [m].[Timeline]
FROM [Missions] AS [m]
WHERE FORMAT([m].[Date], N'M/d/yyyy') = N'1/1/2020'
""");
}

public override async Task Include_on_derived_entity_with_cast(bool async)
{
await base.Include_on_derived_entity_with_cast(async);
Expand Down