/
_TimeSpan.cs
359 lines (311 loc) · 13.6 KB
/
_TimeSpan.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using DevZest.Data.Primitives;
using System;
using System.Globalization;
namespace DevZest.Data.SqlServer
{
/// <summary>
/// Represents a nullable <see cref="TimeSpan"/> column.
/// </summary>
public sealed class _TimeSpan : Column<TimeSpan?>, IColumn<SqlReader, TimeSpan?>
{
private sealed class CastToStringExpression : CastExpression<TimeSpan?, String>
{
public CastToStringExpression(Column<TimeSpan?> x)
: base(x)
{
}
protected override string Cast(TimeSpan? value)
{
return value.HasValue ? value.GetValueOrDefault().ToString("c") : null;
}
}
/// <inheritdoc/>
public override _String CastToString()
{
return new CastToStringExpression(this).MakeColumn<_String>();
}
/// <summary>Converts the supplied <see cref="_TimeSpan" /> to <see cref="_String" />.</summary>
/// <returns>A <see cref="_String" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_TimeSpan" /> object. </param>
public static explicit operator _String(_TimeSpan x)
{
x.VerifyNotNull(nameof(x));
return x.CastToString();
}
/// <inheritdoc/>
public override bool AreEqual(TimeSpan? x, TimeSpan? y)
{
return x == y;
}
/// <inheritdoc/>
protected override Column<TimeSpan?> CreateParam(TimeSpan? value)
{
return Param(value, this);
}
/// <inheritdoc/>
protected override Column<TimeSpan?> CreateConst(TimeSpan? value)
{
return Const(value);
}
/// <inheritdoc/>
protected override JsonValue SerializeValue(TimeSpan? value)
{
if (!value.HasValue)
return JsonValue.Null;
var x = value.GetValueOrDefault();
return JsonValue.String(x.ToString("c", CultureInfo.InvariantCulture));
}
/// <inheritdoc/>
protected override TimeSpan? DeserializeValue(JsonValue value)
{
return value.Type == JsonValueType.Null ? null : new TimeSpan?(TimeSpan.Parse(value.Text));
}
/// <summary>Gets the value of this column from <see cref="SqlReader"/>'s current row.</summary>
/// <param name="reader">The <see cref="SqlReader"/> object.</param>
/// <returns>The value of this column from <see cref="SqlReader"/>'s current row.</returns>
public TimeSpan? this[SqlReader reader]
{
get
{
VerifyDbReader(reader);
return GetValue(reader);
}
}
private TimeSpan? GetValue(SqlReader reader)
{
return reader.GetTimeSpan(Ordinal);
}
void IColumn<SqlReader>.Read(SqlReader reader, DataRow dataRow)
{
this[dataRow] = GetValue(reader);
}
/// <inheritdoc/>
protected override bool IsNull(TimeSpan? value)
{
return !value.HasValue;
}
/// <summary>Creates a column of parameter expression.</summary>
/// <param name="x">The value of the parameter expression.</param>
/// <param name="sourceColumn">The value which will be passed to <see cref="DbParamExpression.SourceColumn"/>.</param>
/// <returns>The column of parameter expression.</returns>
public static _TimeSpan Param(TimeSpan? x, _TimeSpan sourceColumn = null)
{
return new ParamExpression<TimeSpan?>(x, sourceColumn).MakeColumn<_TimeSpan>();
}
/// <summary>Creates a column of constant expression.</summary>
/// <param name="x">The value of the constant expression.</param>
/// <returns>The column of constant expression.</returns>
public static _TimeSpan Const(TimeSpan? x)
{
return new ConstantExpression<TimeSpan?>(x).MakeColumn<_TimeSpan>();
}
/// <summary>Converts the supplied nullable DateTime to <see cref="_DateTime" /> expression.</summary>
/// <returns>A new <see cref="_DateTime" /> expression from the provided value.</returns>
/// <param name="x">A nullable DateTime value.</param>
public static implicit operator _TimeSpan(TimeSpan? x)
{
return Param(x);
}
private sealed class FromStringCast : CastExpression<String, TimeSpan?>
{
public FromStringCast(Column<String> x)
: base(x)
{
}
protected override TimeSpan? Cast(String value)
{
if (value == null)
return null;
return TimeSpan.Parse(value, CultureInfo.InvariantCulture);
}
}
/// <summary>Converts the supplied <see cref="_String" /> to <see cref="_TimeSpan" />.</summary>
/// <returns>A <see cref="_TimeSpan" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_String" /> object. </param>
public static explicit operator _TimeSpan(_String x)
{
x.VerifyNotNull(nameof(x));
return new FromStringCast(x).MakeColumn<_TimeSpan>();
}
private sealed class LessThanExpression : BinaryExpression<TimeSpan?, bool?>
{
public LessThanExpression(Column<TimeSpan?> x, Column<TimeSpan?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.LessThan; }
}
protected override bool? EvalCore(TimeSpan? x, TimeSpan? y)
{
if (x == null || y == null)
return null;
else
return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault()) < 0;
}
}
/// <summary>Compares the two <see cref="_TimeSpan" /> parameters to determine whether the first is less than the second.</summary>
/// <returns>A <see cref="_TimeSpan" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_TimeSpan" /> object. </param>
/// <param name="y">A <see cref="_TimeSpan" /> object. </param>
public static _Boolean operator <(_TimeSpan x, _TimeSpan y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new LessThanExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class LessThanOrEqualExpression : BinaryExpression<TimeSpan?, bool?>
{
public LessThanOrEqualExpression(Column<TimeSpan?> x, Column<TimeSpan?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.LessThanOrEqual; }
}
protected override bool? EvalCore(TimeSpan? x, TimeSpan? y)
{
if (x == null || y == null)
return null;
else
return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault()) <= 0;
}
}
/// <summary>Compares the two <see cref="_TimeSpan" /> parameters to determine whether the first is less than or equal the second.</summary>
/// <returns>A <see cref="_Boolean" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_TimeSpan" /> object. </param>
/// <param name="y">A <see cref="_TimeSpan" /> object. </param>
public static _Boolean operator <=(_TimeSpan x, _TimeSpan y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new LessThanOrEqualExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class GreaterThanExpression : BinaryExpression<TimeSpan?, bool?>
{
public GreaterThanExpression(Column<TimeSpan?> x, Column<TimeSpan?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.GreaterThan; }
}
protected override bool? EvalCore(TimeSpan? x, TimeSpan? y)
{
if (x == null || y == null)
return null;
else
return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault()) > 0;
}
}
/// <summary>Compares the two <see cref="_TimeSpan" /> parameters to determine whether the first is greater than the second.</summary>
/// <returns>A <see cref="_Boolean" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_DateTime" /> object. </param>
/// <param name="y">A <see cref="_DateTime" /> object. </param>
public static _Boolean operator >(_TimeSpan x, _TimeSpan y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new GreaterThanExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class GreaterThanOrEqualExpression : BinaryExpression<TimeSpan?, bool?>
{
public GreaterThanOrEqualExpression(Column<TimeSpan?> x, Column<TimeSpan?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.GreaterThanOrEqual; }
}
protected override bool? EvalCore(TimeSpan? x, TimeSpan? y)
{
if (x == null || y == null)
return null;
else
return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault()) >= 0;
}
}
/// <summary>Compares the two <see cref="_TimeSpan" /> parameters to determine whether the first is greater than or equal the second.</summary>
/// <returns>A <see cref="_Boolean" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_TimeSpan" /> object. </param>
/// <param name="y">A <see cref="_TimeSpan" /> object. </param>
public static _Boolean operator >=(_TimeSpan x, _TimeSpan y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new GreaterThanOrEqualExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class EqualExpression : BinaryExpression<TimeSpan?, bool?>
{
public EqualExpression(Column<TimeSpan?> x, Column<TimeSpan?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.Equal; }
}
protected override bool? EvalCore(TimeSpan? x, TimeSpan? y)
{
if (x == null || y == null)
return null;
else
return x.GetValueOrDefault() == y.GetValueOrDefault();
}
}
/// <summary>Performs a logical comparison of the two <see cref="_TimeSpan" /> parameters for equality.</summary>
/// <returns>The result <see cref="_Boolean" /> expression.</returns>
/// <param name="x">A <see cref="_TimeSpan" /> object. </param>
/// <param name="y">A <see cref="_TimeSpan" /> object. </param>
public static _Boolean operator ==(_TimeSpan x, _TimeSpan y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new EqualExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class NotEqualExpression : BinaryExpression<TimeSpan?, bool?>
{
public NotEqualExpression(Column<TimeSpan?> x, Column<TimeSpan?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.NotEqual; }
}
protected override bool? EvalCore(TimeSpan? x, TimeSpan? y)
{
if (x == null || y == null)
return null;
else
return x.GetValueOrDefault() != y.GetValueOrDefault();
}
}
/// <summary>Performs a logical comparison of the two <see cref="_TimeSpan" /> parameters for non-equality.</summary>
/// <returns>The result <see cref="_Boolean" /> expression.</returns>
/// <param name="x">A <see cref="_TimeSpan" /> object. </param>
/// <param name="y">A <see cref="_TimeSpan" /> object. </param>
public static _Boolean operator !=(_TimeSpan x, _TimeSpan y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new NotEqualExpression(x, y).MakeColumn<_Boolean>();
}
/// <exclude />
public override bool Equals(object obj)
{
// override to eliminate compile warning
return base.Equals(obj);
}
/// <exclude />
public override int GetHashCode()
{
// override to eliminate compile warning
return base.GetHashCode();
}
}
}