/
BinaryExpression.cs
99 lines (85 loc) · 3.67 KB
/
BinaryExpression.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
using System;
namespace DevZest.Data.Primitives
{
/// <summary>Represents column expression which contains two column operands.</summary>
/// <typeparam name="T">The data type of the column operands and column expression.</typeparam>
public abstract class BinaryExpression<T> : BinaryExpression<T, T>
{
/// <summary>Initializes a new instance of <see cref="BinaryExpression{T}"/> class.</summary>
/// <param name="left">The left column operand.</param>
/// <param name="right">The right column operand.</param>
protected BinaryExpression(Column<T> left, Column<T> right)
: base(left, right)
{
}
}
/// <summary>Represents column expression which contains two column operands.</summary>
/// <typeparam name="T">The data type of column operands.</typeparam>
/// <typeparam name="TResult">The data type of the column expression.</typeparam>
public abstract class BinaryExpression<T, TResult> : ColumnExpression<TResult>
{
private const string LEFT = nameof(Left);
private const string RIGHT = nameof(Right);
/// <summary>Initializes a new instance of <see cref="BinaryExpression{T, TResult}"/> class.</summary>
/// <param name="left">The left column operand.</param>
/// <param name="right">The right column operand.</param>
protected BinaryExpression(Column<T> left, Column<T> right)
{
left.VerifyNotNull(nameof(left));
right.VerifyNotNull(nameof(right));
Left = left;
Right = right;
}
/// <summary>Gets the left column operand.</summary>
public Column<T> Left { get; private set; }
/// <summary>Gets the right column operand.</summary>
public Column<T> Right { get; private set; }
/// <summary>Gets the kind of this binary expression.</summary>
protected abstract BinaryExpressionKind Kind { get; }
/// <inheritdoc/>
protected sealed override IModels GetScalarSourceModels()
{
return Left.ScalarSourceModels.Union(Right.ScalarSourceModels).Seal();
}
/// <inheritdoc/>
protected sealed override IModels GetAggregateBaseModels()
{
return Left.AggregateSourceModels.Union(Right.AggregateSourceModels).Seal();
}
/// <inheritdoc/>
public sealed override DbExpression GetDbExpression()
{
return new DbBinaryExpression(typeof(TResult), Kind, Left.DbExpression, Right.DbExpression);
}
/// <inheritdoc/>
public sealed override TResult this[DataRow dataRow]
{
get
{
var x = Left[dataRow];
var y = Right[dataRow];
return EvalCore(x, y);
}
}
/// <summary>Evaluates the expression against two operand values.</summary>
/// <param name="x">The left operand value.</param>
/// <param name="y">The right operand value.</param>
/// <returns>The result.</returns>
protected abstract TResult EvalCore(T x, T y);
/// <inheritdoc />
protected sealed override IColumns GetBaseColumns()
{
return Left.BaseColumns.Union(Right.BaseColumns).Seal();
}
/// <inheritdoc />
protected internal sealed override ColumnExpression PerformTranslateTo(Model model)
{
var left = Left.TranslateTo(model);
var right = Right.TranslateTo(model);
if (left == Left && right == Right)
return this;
else
return (ColumnExpression)Activator.CreateInstance(GetType(), left, right);
}
}
}