-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #328
- Loading branch information
Showing
3 changed files
with
144 additions
and
1 deletion.
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
68 changes: 68 additions & 0 deletions
68
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlTimeSpanMemberTranslator.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,68 @@ | ||
using System; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Utilities; | ||
|
||
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal | ||
{ | ||
public class NpgsqlTimeSpanMemberTranslator : IMemberTranslator | ||
{ | ||
readonly ISqlExpressionFactory _sqlExpressionFactory; | ||
|
||
public NpgsqlTimeSpanMemberTranslator([NotNull] ISqlExpressionFactory sqlExpressionFactory) | ||
=> _sqlExpressionFactory = sqlExpressionFactory; | ||
|
||
static readonly bool[][] TrueArrays = | ||
{ | ||
Array.Empty<bool>(), | ||
new[] { true } | ||
}; | ||
|
||
static readonly bool[] FalseTrueArray = { false, true }; | ||
|
||
public virtual SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType) | ||
{ | ||
Check.NotNull(member, nameof(member)); | ||
Check.NotNull(returnType, nameof(returnType)); | ||
|
||
if (member.DeclaringType == typeof(TimeSpan)) | ||
{ | ||
return member.Name switch | ||
{ | ||
nameof(TimeSpan.Days) => Floor(DatePart("day", instance)), | ||
nameof(TimeSpan.Hours) => Floor(DatePart("hour", instance)), | ||
nameof(TimeSpan.Minutes) => Floor(DatePart("minute", instance)), | ||
nameof(TimeSpan.Seconds) => Floor(DatePart("second", instance)), | ||
nameof(TimeSpan.Milliseconds) => _sqlExpressionFactory.Modulo( | ||
Floor(DatePart("millisecond", instance)), | ||
_sqlExpressionFactory.Constant(1000)), | ||
_ => null | ||
}; | ||
} | ||
|
||
return null; | ||
|
||
SqlExpression Floor(SqlExpression value) | ||
=> _sqlExpressionFactory.Convert( | ||
_sqlExpressionFactory.Function( | ||
"floor", | ||
new[] { value }, | ||
nullable: true, | ||
argumentsPropagateNullability: TrueArrays[1], | ||
typeof(double)), | ||
typeof(int)); | ||
|
||
SqlFunctionExpression DatePart(string part, SqlExpression value) | ||
=> _sqlExpressionFactory.Function("date_part", new[] | ||
{ | ||
_sqlExpressionFactory.Constant(part), | ||
value | ||
}, | ||
nullable: true, | ||
argumentsPropagateNullability: FalseTrueArray, | ||
returnType); | ||
} | ||
} | ||
} |
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