-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathQueryTimeSeriesLogic.cs
142 lines (133 loc) · 5.35 KB
/
QueryTimeSeriesLogic.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
130
131
132
133
134
135
136
137
138
139
140
141
142
using Microsoft.SqlServer.Server;
using Signum.Engine.Linq;
using Signum.Engine.Maps;
namespace Signum.Basics;
public class DateValue : IView
{
[ViewPrimaryKey]
public DateTime Date;
}
public class QueryTimeSeriesLogic
{
[SqlMethod(Name = "GetDatesInRange"), AvoidEagerEvaluation]
public static IQueryable<DateValue> GetDatesInRange(DateTime startDate, DateTime endDate, string incrementType, int step)
{
var mi = (MethodInfo)MethodInfo.GetCurrentMethod()!;
return new Query<DateValue>(DbQueryProvider.Single, Expression.Call(mi,
Expression.Constant(startDate, typeof(DateTime)),
Expression.Constant(endDate, typeof(DateTime)),
Expression.Constant(incrementType, typeof(string)),
Expression.Constant(step, typeof(int))
));
}
public static void Start(SchemaBuilder sb)
{
if (sb.NotDefined(MethodBase.GetCurrentMethod()))
{
Schema.Current.Assets.IncludeUserDefinedFunction("GetDatesInRange", """
(
@startDate DATETIME2,
@endDate DATETIME2,
@incrementType NVARCHAR(20),
@step INT
)
RETURNS @DateRange TABLE
(
Date DATETIME2 PRIMARY KEY
)
AS
BEGIN
DECLARE @currentDate DATETIME2
SET @currentDate = @startDate
IF @incrementType = 'millisecond'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(MILLISECOND, @step, @currentDate)
END
END
ELSE IF @incrementType = 'second'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(SECOND, @step, @currentDate)
END
END
ELSE IF @incrementType = 'minute'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(MINUTE, @step, @currentDate)
END
END
ELSE IF @incrementType = 'hour'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(HOUR, @step, @currentDate)
END
END
ELSE IF @incrementType = 'day'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(DAY, @step, @currentDate)
END
END
ELSE IF @incrementType = 'week'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(WEEK, @step, @currentDate)
END
END
ELSE IF @incrementType = 'month'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(MONTH, @step, @currentDate)
END
END
ELSE IF @incrementType = 'quarter'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(QUARTER, @step, @currentDate)
END
END
ELSE IF @incrementType = 'year'
BEGIN
WHILE @currentDate <= @endDate
BEGIN
INSERT INTO @DateRange (Date)
VALUES (@currentDate)
SET @currentDate = DATEADD(YEAR, @step, @currentDate)
END
END
ELSE
BEGIN
-- Throw exception for invalid incrementType
DECLARE @error INT = CAST('Invalid @incrementType provided.' as int);
END
RETURN
END
""");
}
}
}