/
SqliteDateTimeAddTranslator.cs
129 lines (118 loc) · 6.32 KB
/
SqliteDateTimeAddTranslator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;
#nullable enable
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 SqliteDateTimeAddTranslator : IMethodCallTranslator
{
private static readonly MethodInfo _addMilliseconds
= typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddMilliseconds), new[] { typeof(double) });
private static readonly MethodInfo _addTicks
= typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddTicks), new[] { typeof(long) });
private readonly Dictionary<MethodInfo, string> _methodInfoToUnitSuffix = new()
{
{ typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddYears), new[] { typeof(int) }), " years" },
{ typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddMonths), new[] { typeof(int) }), " months" },
{ typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddDays), new[] { typeof(double) }), " days" },
{ typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddHours), new[] { typeof(double) }), " hours" },
{ typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddMinutes), new[] { typeof(double) }), " minutes" },
{ typeof(DateTime).GetRequiredRuntimeMethod(nameof(DateTime.AddSeconds), new[] { typeof(double) }), " seconds" }
};
private readonly ISqlExpressionFactory _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 SqliteDateTimeAddTranslator([NotNull] ISqlExpressionFactory 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 virtual SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));
SqlExpression? modifier = null;
if (_addMilliseconds.Equals(method))
{
modifier = _sqlExpressionFactory.Add(
_sqlExpressionFactory.Convert(
_sqlExpressionFactory.Divide(
arguments[0],
_sqlExpressionFactory.Constant(1000.0)),
typeof(string)),
_sqlExpressionFactory.Constant(" seconds"));
}
else if (_addTicks.Equals(method))
{
modifier = _sqlExpressionFactory.Add(
_sqlExpressionFactory.Convert(
_sqlExpressionFactory.Divide(
arguments[0],
_sqlExpressionFactory.Constant((double)TimeSpan.TicksPerDay)),
typeof(string)),
_sqlExpressionFactory.Constant(" seconds"));
}
else if (_methodInfoToUnitSuffix.TryGetValue(method, out var unitSuffix))
{
modifier = _sqlExpressionFactory.Add(
_sqlExpressionFactory.Convert(arguments[0], typeof(string)),
_sqlExpressionFactory.Constant(unitSuffix));
}
if (modifier != null)
{
return _sqlExpressionFactory.Function(
"rtrim",
new SqlExpression[]
{
_sqlExpressionFactory.Function(
"rtrim",
new SqlExpression[]
{
SqliteExpression.Strftime(
_sqlExpressionFactory,
method.ReturnType,
"%Y-%m-%d %H:%M:%f",
instance!,
new[] { modifier }),
_sqlExpressionFactory.Constant("0")
},
nullable: true,
argumentsPropagateNullability: new[] { true, false },
method.ReturnType),
_sqlExpressionFactory.Constant(".")
},
nullable: true,
argumentsPropagateNullability: new[] { true, false },
method.ReturnType);
}
return null;
}
}
}