/
CastExpression.cs
66 lines (57 loc) · 2.18 KB
/
CastExpression.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
using System;
namespace DevZest.Data.Primitives
{
/// <summary>Base class for column type cast expression.</summary>
/// <typeparam name="TSource">Data type of source column.</typeparam>
/// <typeparam name="TTarget">Data type of target column.</typeparam>
public abstract class CastExpression<TSource, TTarget> : ColumnExpression<TTarget>
{
/// <summary>Initialize a new instance of <see cref="CastExpression{TSource, TTarget}"/> object.</summary>
/// <param name="operand">The operand to be casted.</param>
protected CastExpression(Column<TSource> operand)
{
operand.VerifyNotNull(nameof(operand));
Operand = operand;
}
/// <inheritdoc />
protected sealed override IColumns GetBaseColumns()
{
return Operand.BaseColumns;
}
/// <summary>Gets the operand to be casted.</summary>
public Column<TSource> Operand { get; private set; }
/// <summary>Casts the provided value.</summary>
/// <param name="value">The value.</param>
/// <returns>The casted result.</returns>
protected abstract TTarget Cast(TSource value);
/// <inheritdoc/>
public sealed override TTarget this[DataRow dataRow]
{
get { return Cast(Operand[dataRow]); }
}
/// <inheritdoc/>
protected sealed override IModels GetScalarSourceModels()
{
return Operand.ParentModel;
}
/// <inheritdoc/>
protected sealed override IModels GetAggregateBaseModels()
{
return Operand.AggregateSourceModels;
}
/// <inheritdoc/>
public sealed override DbExpression GetDbExpression()
{
return new DbCastExpression(Operand.DbExpression, Operand, this.Owner);
}
/// <inheritdoc />
protected internal sealed override ColumnExpression PerformTranslateTo(Model model)
{
var operand = Operand.TranslateTo(model);
if (operand == Operand)
return this;
else
return (ColumnExpression)Activator.CreateInstance(GetType(), operand);
}
}
}