/
CaseExpression.cs
134 lines (121 loc) · 4.44 KB
/
CaseExpression.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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace DevZest.Data.Primitives
{
/// <summary>Represents a CASE WHEN..ELSE... expression.</summary>
/// <typeparam name="TResult">The data type of the result.</typeparam>
public sealed class CaseExpression<TResult> : ColumnExpression<TResult>
{
internal CaseExpression()
{
_when = new List<_Boolean>();
_then = new List<Column<TResult>>();
}
private CaseExpression(List<_Boolean> when, List<Column<TResult>> then, Column<TResult> elseExpr)
{
_when = when;
_then = then;
_else = elseExpr;
}
private List<_Boolean> _when;
private List<Column<TResult>> _then;
private Column<TResult> _else;
/// <inheritdoc />
protected sealed override IColumns GetBaseColumns()
{
var result = Columns.Empty;
for (int i = 0; i < _when.Count; i++)
result = result.Union(_when[i].BaseColumns);
for (int i = 0; i < _then.Count; i++)
result = result.Union(_then[i].BaseColumns);
result = result.Union(_else.BaseColumns);
return result.Seal();
}
/// <summary>
/// Construct CASE WHEN expression.
/// </summary>
/// <param name="when">The condition.</param>
/// <returns>The result.</returns>
public CaseWhen<TResult> When(_Boolean when)
{
when.VerifyNotNull(nameof(when));
return new CaseWhen<TResult>(this, when);
}
internal CaseExpression<TResult> WhenThen(_Boolean when, Column<TResult> then)
{
Debug.Assert(!object.ReferenceEquals(when, null));
Debug.Assert(then != null);
_when.Add(when);
_then.Add(then);
return this;
}
/// <summary>Builds the ELSE... expression.</summary>
/// <typeparam name="TColumn">The column type of the result.</typeparam>
/// <param name="value">Value of the Else... expression.</param>
/// <returns>The result column expression.</returns>
public TColumn Else<TColumn>(TColumn value)
where TColumn : Column<TResult>, new()
{
_else = value;
return this.MakeColumn<TColumn>();
}
/// <inheritdoc/>
public sealed override TResult this[DataRow dataRow]
{
get
{
for (int i = 0; i < _when.Count; i++)
{
var whenValue = _when[i][dataRow];
if (whenValue == true)
return _then[i][dataRow];
}
return _else[dataRow];
}
}
/// <inheritdoc/>
public override DbExpression GetDbExpression()
{
return new DbCaseExpression(typeof(TResult), null,
_when.Select(x => x.DbExpression),
_then.Select(x => x.DbExpression),
_else.DbExpression);
}
/// <inheritdoc/>
protected override IModels GetScalarSourceModels()
{
var result = Models.Empty;
for (int i = 0; i < _when.Count; i++)
{
result = result.Union(_when[i].ScalarSourceModels);
result = result.Union(_then[i].ScalarSourceModels);
}
result = result.Union(_else.ScalarSourceModels);
return result.Seal();
}
/// <inheritdoc/>
protected override IModels GetAggregateBaseModels()
{
var result = Models.Empty;
for (int i = 0; i < _when.Count; i++)
{
result = result.Union(_when[i].AggregateSourceModels);
result = result.Union(_then[i].AggregateSourceModels);
}
result = result.Union(_else.AggregateSourceModels);
return result;
}
/// <inheritdoc />
protected internal override ColumnExpression PerformTranslateTo(Model model)
{
var when = _when.TranslateToColumns(model);
var then = _then.TranslateToColumns(model);
var elseExpr = _else.TranslateTo(model);
if (when != _when || _then != then || elseExpr != _else)
return new CaseExpression<TResult>(when, then, elseExpr);
else
return this;
}
}
}