/
FunctionExpression.cs
79 lines (68 loc) · 2.6 KB
/
FunctionExpression.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
using System;
using System.Collections.ObjectModel;
using System.Threading;
namespace DevZest.Data.Primitives
{
/// <summary>
/// Represents function call in column expression.
/// </summary>
/// <typeparam name="T">Data type of the expression.</typeparam>
public abstract class FunctionExpression<T> : ColumnExpression<T>
{
/// <summary>
/// Initializes a new instance of <see cref="FunctionExpression{T}"/> class.
/// </summary>
/// <param name="parameters">The parameters of the function call.</param>
protected FunctionExpression(params Column[] parameters)
{
if (parameters == null || parameters.Length == 0)
Parameters = null;
Parameters = new ReadOnlyCollection<Column>(parameters);
}
/// <summary>
/// Gets the parameters of the function call.
/// </summary>
public ReadOnlyCollection<Column> Parameters;
/// <inheritdoc/>
protected sealed override IColumns GetBaseColumns()
{
var result = Columns.Empty;
if (Parameters == null)
return result;
for (int i = 0; i < Parameters.Count; i++)
result = result.Union(Parameters[i].BaseColumns);
return result.Seal();
}
/// <summary>
/// Gets the function key which identifies this function.
/// </summary>
protected abstract FunctionKey FunctionKey { get; }
private DbExpression _dbExpression;
/// <inheritdoc/>
public sealed override DbExpression GetDbExpression()
{
return LazyInitializer.EnsureInitialized(ref _dbExpression, () => CreateDbExpression());
}
private DbExpression CreateDbExpression()
{
if (Parameters == null)
return new DbFunctionExpression(typeof(T), FunctionKey);
var paramList = new DbExpression[Parameters.Count];
for (int i = 0; i < Parameters.Count; i++)
{
var parameter = Parameters[i];
paramList[i] = parameter?.DbExpression;
}
return new DbFunctionExpression(typeof(T), FunctionKey, paramList);
}
/// <inheritdoc/>
protected sealed internal override ColumnExpression PerformTranslateTo(Model model)
{
var parameters = Parameters.TranslateToParams(model);
if (parameters == null)
return this;
else
return (ColumnExpression)Activator.CreateInstance(GetType(), parameters);
}
}
}