/
_Boolean.cs
338 lines (292 loc) · 11.9 KB
/
_Boolean.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
using DevZest.Data.Primitives;
using System;
namespace DevZest.Data
{
/// <summary>
/// Represents a nullable <see cref="Boolean"/> column.
/// </summary>
public sealed class _Boolean : Column<bool?>, IColumn<DbReader, Boolean?>
{
/// <inheritdoc/>
protected sealed override Column<bool?> CreateParam(bool? value)
{
return Param(value, this);
}
/// <inheritdoc/>
public override bool AreEqual(bool? x, bool? y)
{
return x == y;
}
/// <inheritdoc/>
protected internal sealed override Column<bool?> CreateConst(bool? value)
{
return Const(value);
}
/// <inheritdoc/>
protected internal override JsonValue SerializeValue(bool? value)
{
return value.HasValue ? (value.GetValueOrDefault() ? JsonValue.True : JsonValue.False) : JsonValue.Null;
}
/// <inheritdoc/>
protected internal override bool? DeserializeValue(JsonValue value)
{
if (value.Type == JsonValueType.Null)
return null;
else if (value.Type == JsonValueType.True)
return true;
else if (value.Type == JsonValueType.False)
return false;
throw new FormatException(DiagnosticMessages.BooleanColumn_CannotDeserialize);
}
/// <summary>Gets the value of this column from <see cref="DbReader"/>'s current row.</summary>
/// <param name="reader">The <see cref="DbReader"/> object.</param>
/// <returns>The value of this column from <see cref="DbReader"/>'s current row.</returns>
public bool? this[DbReader reader]
{
get
{
VerifyDbReader(reader);
return GetValue(reader);
}
}
private bool? GetValue(DbReader reader)
{
return reader.GetBoolean(Ordinal);
}
void IColumn<DbReader>.Read(DbReader reader, DataRow dataRow)
{
this[dataRow] = GetValue(reader);
}
/// <inheritdoc/>
protected internal override bool IsNull(bool? 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 _Boolean Param(bool? x, _Boolean sourceColumn = null)
{
return new ParamExpression<bool?>(x, sourceColumn).MakeColumn<_Boolean>();
}
/// <summary>Column of <see langword="null"/> constant value.</summary>
public static readonly _Boolean Null = new ConstantExpression<bool?>(null).MakeColumn<_Boolean>();
/// <summary>Column of <see langword="true"/> constant value.</summary>
public static readonly _Boolean True = new ConstantExpression<bool?>(true).MakeColumn<_Boolean>();
/// <summary>Column of <see langword="false"/> constant value.</summary>
public static readonly _Boolean False = new ConstantExpression<bool?>(false).MakeColumn<_Boolean>();
/// <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 _Boolean Const(bool? x)
{
return x.HasValue ? (x.GetValueOrDefault() ? True : False) : Null;
}
/// <summary>Implicitly converts the supplied value to a column of parameter expression.</summary>
/// <param name="x">The value of the parameter expression.</param>
/// <returns>The column of parameter expression.</returns>
public static implicit operator _Boolean(bool? x)
{
return Param(x);
}
private sealed class NotExpression : UnaryExpression<bool?>
{
public NotExpression(Column<bool?> operand)
: base(operand)
{
}
protected override DbUnaryExpressionKind ExpressionKind
{
get { return DbUnaryExpressionKind.Not; }
}
protected override bool? EvalCore(bool? x)
{
return !x;
}
}
/// <summary>Performs a NOT operation on a <see cref="_Boolean" />.</summary>
/// <returns>The result <see cref="_Boolean" /> expression.</returns>
/// <param name="x">The original <see cref="_Boolean" /> on which the NOT operation will be performed. </param>
public static _Boolean operator !(_Boolean x)
{
x.VerifyNotNull(nameof(x));
return new NotExpression(x).MakeColumn<_Boolean>();
}
private sealed class AndExpression : BinaryExpression<bool?>
{
public AndExpression(Column<bool?> left, Column<bool?> right)
: base(left, right)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.And; }
}
protected override bool? EvalCore(bool? x, bool? y)
{
return And(x, y);
}
}
// 3 Valued Logic implimentation, may need to be moved into a service provider in the future.
private static bool? And(bool? x, bool? y)
{
if (x == false || y == false)
return false;
if (x == true || y == true)
return true;
return null;
}
/// <summary>Computes the logical AND operation of two specified <see cref="_Boolean" /> objects.</summary>
/// <returns>The result of the logical AND operation.</returns>
/// <param name="x">A <see cref="_Boolean" /> object.</param>
/// <param name="y">A <see cref="_Boolean" /> object.</param>
public static _Boolean operator &(_Boolean x, _Boolean y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new AndExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class OrExpression : BinaryExpression<bool?>
{
public OrExpression(Column<bool?> left, Column<bool?> right)
: base(left, right)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.Or; }
}
protected override bool? EvalCore(bool? x, bool? y)
{
return Or(x, y);
}
}
// 3 Valued Logic implimentation, may need to be moved into a service provider in the future.
private static bool? Or(bool? x, bool? y)
{
if (x == true || y == true)
return true;
if (x == false && y == false)
return false;
return null;
}
/// <summary>Computes the logical OR operation of two specified <see cref="_Boolean" /> objects.</summary>
/// <returns>The result of the logical OR operation.</returns>
/// <param name="x">A <see cref="_Boolean" /> object.</param>
/// <param name="y">A <see cref="_Boolean" /> object.</param>
public static _Boolean operator |(_Boolean x, _Boolean y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new OrExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class FromStringExpression : CastExpression<String, Boolean?>
{
public FromStringExpression(Column<string> x)
: base(x)
{
}
protected override Boolean? Cast(String value)
{
if (value == null)
return null;
return Boolean.Parse(value);
}
}
/// <summary>Converts the supplied <see cref="_String" /> to <see cref="_Boolean" />.</summary>
/// <returns>A <see cref="_Boolean" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_String" /> object. </param>
public static explicit operator _Boolean(_String x)
{
x.VerifyNotNull(nameof(x));
return new FromStringExpression(x).MakeColumn<_Boolean>();
}
private sealed class CastToStringExpression : CastExpression<Boolean?, String>
{
public CastToStringExpression(Column<Boolean?> x)
: base(x)
{
}
protected override String Cast(Boolean? value)
{
return value.HasValue ? value.GetValueOrDefault().ToString() : null;
}
}
/// <inheritdoc/>
public override _String CastToString()
{
return new CastToStringExpression(this).MakeColumn<_String>();
}
/// <summary>Converts the supplied <see cref="_Boolean" /> to <see cref="_String" />.</summary>
/// <returns>A <see cref="_String" /> expression which contains the result.</returns>
/// <param name="x">A <see cref="_Boolean" /> object. </param>
public static explicit operator _String(_Boolean x)
{
x.VerifyNotNull(nameof(x));
return x.CastToString();
}
private sealed class EqualExpression : BinaryExpression<bool?, bool?>
{
public EqualExpression(Column<bool?> x, Column<bool?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.Equal; }
}
protected override bool? EvalCore(bool? x, bool? y)
{
return x.EqualsTo(y);
}
}
/// <summary>Performs a logical comparison of the two <see cref="_Boolean" /> parameters for equality.</summary>
/// <returns>The result <see cref="_Boolean" /> expression.</returns>
/// <param name="x">A <see cref="_Boolean" /> object. </param>
/// <param name="y">A <see cref="_Boolean" /> object. </param>
public static _Boolean operator ==(_Boolean x, _Boolean y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new EqualExpression(x, y).MakeColumn<_Boolean>();
}
private sealed class NotEqualExpression : BinaryExpression<bool?, bool?>
{
public NotEqualExpression(Column<bool?> x, Column<bool?> y)
: base(x, y)
{
}
protected override BinaryExpressionKind Kind
{
get { return BinaryExpressionKind.NotEqual; }
}
protected override bool? EvalCore(bool? x, bool? y)
{
return !x.EqualsTo(y);
}
}
/// <summary>Performs a logical comparison of the two <see cref="_Boolean" /> parameters for non-equality.</summary>
/// <returns>The result <see cref="_Boolean" /> expression.</returns>
/// <param name="x">A <see cref="_Boolean" /> object. </param>
/// <param name="y">A <see cref="_Boolean" /> object. </param>
public static _Boolean operator !=(_Boolean x, _Boolean y)
{
x.VerifyNotNull(nameof(x));
y.VerifyNotNull(nameof(y));
return new NotEqualExpression(x, y).MakeColumn<_Boolean>();
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
// override to eliminate compile warning
return base.Equals(obj);
}
/// <inheritdoc/>
public override int GetHashCode()
{
// override to eliminate compile warning
return base.GetHashCode();
}
}
}