Skip to content

Add DateOnly.FromDateTime query mapping for SQL Server #31180

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

Merged
merged 5 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -59,6 +59,13 @@ public SqlServerDateOnlyMethodTranslator(ISqlExpressionFactory sqlExpressionFact
instance.TypeMapping);
}

if (method.DeclaringType == typeof(DateOnly)
&& method.Name == nameof(DateOnly.FromDateTime)
&& arguments.Count == 1)
{
return _sqlExpressionFactory.Convert(arguments[0], typeof(DateOnly));
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.Sqlite.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 SqliteDateOnlyMethodTranslator : IMethodCallTranslator
{
private readonly SqliteSqlExpressionFactory _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 SqliteDateOnlyMethodTranslator(SqliteSqlExpressionFactory 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,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (method.DeclaringType == typeof(DateOnly)
&& method.Name == nameof(DateOnly.FromDateTime)
&& arguments.Count == 1)
{
return _sqlExpressionFactory.Date(method.ReturnType, arguments[0]);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public SqliteMethodCallTranslatorProvider(RelationalMethodCallTranslatorProvider
{
new SqliteByteArrayMethodTranslator(sqlExpressionFactory),
new SqliteCharMethodTranslator(sqlExpressionFactory),
new SqliteDateOnlyMethodTranslator(sqlExpressionFactory),
new SqliteDateTimeMethodTranslator(sqlExpressionFactory),
new SqliteGlobMethodTranslator(sqlExpressionFactory),
new SqliteHexMethodTranslator(sqlExpressionFactory),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,9 @@ public override Task String_Join_over_nullable_column(bool async)
public override Task String_Concat(bool async)
=> AssertTranslationFailed(() => base.String_Concat(async));

public override Task Where_DateOnly_FromDateTime(bool async)
=> AssertTranslationFailed(() => base.Where_DateOnly_FromDateTime(async));

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1917,4 +1917,14 @@ public virtual Task Datetime_subtraction_TotalDays(bool async)
ss => ss.Set<Order>().Where(o => o.OrderDate.HasValue && (o.OrderDate.Value - date).TotalDays > 365),
entryCount: 267);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_DateOnly_FromDateTime(bool async)
=> AssertQuery(
async,
ss => ss.Set<Order>()
.Where(o => o.OrderDate.HasValue && DateOnly.FromDateTime(o.OrderDate.Value) == new DateOnly(1996, 9, 16))
.AsTracking(),
entryCount: 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,18 @@ FROM [Orders] AS [o]
""");
}

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

AssertSql(
"""
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
WHERE [o].[OrderDate] IS NOT NULL AND CAST([o].[OrderDate] AS date) = '1996-09-16'
""");
}

public override async Task Projecting_Math_Truncate_and_ordering_by_it_twice(bool async)
=> await base.Projecting_Math_Truncate_and_ordering_by_it_twice(async);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,18 @@ public override async Task IsNullOrEmpty_negated_in_predicate(bool async)
public override Task Datetime_subtraction_TotalDays(bool async)
=> AssertTranslationFailed(() => base.Datetime_subtraction_TotalDays(async));

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

AssertSql(
"""
SELECT "o"."OrderID", "o"."CustomerID", "o"."EmployeeID", "o"."OrderDate"
FROM "Orders" AS "o"
WHERE "o"."OrderDate" IS NOT NULL AND date("o"."OrderDate") = '1996-09-16'
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}