Skip to content
Open
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 @@ -26,7 +26,8 @@ public SqliteMemberTranslatorProvider(RelationalMemberTranslatorProviderDependen
[
new SqliteDateTimeMemberTranslator(sqlExpressionFactory),
new SqliteStringLengthTranslator(sqlExpressionFactory),
new SqliteDateOnlyMemberTranslator(sqlExpressionFactory)
new SqliteDateOnlyMemberTranslator(sqlExpressionFactory),
new SqliteTimeOnlyMemberTranslator(sqlExpressionFactory)
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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;

// ReSharper disable once CheckNamespace
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 SqliteTimeOnlyMemberTranslator(SqliteSqlExpressionFactory sqlExpressionFactory) : IMemberTranslator
{
/// <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,
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (member.DeclaringType != typeof(TimeOnly) || instance is null)
{
return null;
}

return member.Name switch
{
nameof(TimeOnly.Hour) => DatePart("%H"),
nameof(TimeOnly.Minute) => DatePart("%M"),
nameof(TimeOnly.Second) => DatePart("%S"),

_ => null
};

SqlExpression DatePart(string datePart)
=> sqlExpressionFactory.Convert(
sqlExpressionFactory.Strftime(
typeof(string),
datePart,
instance),
returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,38 @@ public TimeOnlyTranslationsSqliteTest(BasicTypesQuerySqliteFixture fixture, ITes

public override async Task Hour()
{
// TimeSpan. Issue #18844.
await AssertTranslationFailed(() => base.Hour());
await base.Hour();

AssertSql();
AssertSql(
"""
SELECT "b"."Id", "b"."Bool", "b"."Byte", "b"."ByteArray", "b"."DateOnly", "b"."DateTime", "b"."DateTimeOffset", "b"."Decimal", "b"."Double", "b"."Enum", "b"."FlagsEnum", "b"."Float", "b"."Guid", "b"."Int", "b"."Long", "b"."Short", "b"."String", "b"."TimeOnly", "b"."TimeSpan"
FROM "BasicTypesEntities" AS "b"
WHERE CAST(strftime('%H', "b"."TimeOnly") AS INTEGER) = 15
""");
}

public override async Task Minute()
{
// TimeSpan. Issue #18844.
await AssertTranslationFailed(() => base.Minute());
await base.Minute();

AssertSql();
AssertSql(
"""
SELECT "b"."Id", "b"."Bool", "b"."Byte", "b"."ByteArray", "b"."DateOnly", "b"."DateTime", "b"."DateTimeOffset", "b"."Decimal", "b"."Double", "b"."Enum", "b"."FlagsEnum", "b"."Float", "b"."Guid", "b"."Int", "b"."Long", "b"."Short", "b"."String", "b"."TimeOnly", "b"."TimeSpan"
FROM "BasicTypesEntities" AS "b"
WHERE CAST(strftime('%M', "b"."TimeOnly") AS INTEGER) = 30
""");
}

public override async Task Second()
{
// TimeSpan. Issue #18844.
await AssertTranslationFailed(() => base.Second());
await base.Second();

AssertSql();
AssertSql(
"""
SELECT "b"."Id", "b"."Bool", "b"."Byte", "b"."ByteArray", "b"."DateOnly", "b"."DateTime", "b"."DateTimeOffset", "b"."Decimal", "b"."Double", "b"."Enum", "b"."FlagsEnum", "b"."Float", "b"."Guid", "b"."Int", "b"."Long", "b"."Short", "b"."String", "b"."TimeOnly", "b"."TimeSpan"
FROM "BasicTypesEntities" AS "b"
WHERE CAST(strftime('%S', "b"."TimeOnly") AS INTEGER) = 10
""");
}

public override async Task Millisecond()
Expand Down
Loading