-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNullFunction.cs
More file actions
89 lines (74 loc) · 3.1 KB
/
NullFunction.cs
File metadata and controls
89 lines (74 loc) · 3.1 KB
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
using Queries.Core.Attributes;
using Queries.Core.Parts.Columns;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Queries.Core.Parts.Functions;
/// <summary>
/// "ISNULL" function
/// </summary>
[Function]
public class NullFunction : IAliasable<NullFunction>, IColumn, IEquatable<NullFunction>
{
/// <summary>
/// Column onto which the function must be applied.
/// </summary>
public IColumn Column { get; }
/// <summary>
/// Value to use as replacement when <see cref="Column"/>'s value is <see langword="null" />
/// </summary>
public IColumn DefaultValue { get; }
/// <summary>
/// Additionnal default values that can be used as fallback when <see cref="DefaultValue"/> returns <see langword="null" />.
/// </summary>
public IEnumerable<IColumn> AdditionalDefaultValues { get; }
/// <summary>
/// Builds a new <see cref="NullFunction"/> instance.
/// </summary>
/// <param name="column">The column to apply the function onto.</param>
/// <param name="defaultValue">The default value value to use if <paramref name="column"/>'s value is <see langword="null" />.</param>
/// <param name="additionalDefaultValues"></param>
/// <exception cref="ArgumentNullException"> if either <paramref name="column"/> or <paramref name="defaultValue"/> is <see langword="null" /></exception>
public NullFunction(IColumn column, IColumn defaultValue, params IColumn[] additionalDefaultValues)
{
Column = column ?? throw new ArgumentNullException(nameof(column));
DefaultValue = defaultValue ?? throw new ArgumentNullException(nameof(defaultValue));
AdditionalDefaultValues = additionalDefaultValues ?? Enumerable.Empty<IColumn>();
}
private string _alias;
///<inheritdoc/>
public string Alias => _alias;
/// <summary>
/// Sets the alias of the result of the function
/// </summary>
/// <param name="alias">the new alias</param>
/// <returns>The current instance</returns>
public NullFunction As(string alias)
{
_alias = alias;
return this;
}
///<inheritdoc/>
public override bool Equals(object obj) => Equals(obj as NullFunction);
///<inheritdoc/>
public bool Equals(NullFunction other) => (Column, DefaultValue).Equals((other?.Column, other?.DefaultValue));
///<inheritdoc/>
public override int GetHashCode() => (Column, DefaultValue).GetHashCode();
/// <summary>
/// Performs a deep copy of the current instance.
/// </summary>
/// <returns><see cref="NullFunction"/></returns>
public IColumn Clone() => new NullFunction(Column.Clone(), DefaultValue, AdditionalDefaultValues?.ToArray());
///<inheritdoc/>
public override string ToString()
{
IDictionary<string, object> props = new Dictionary<string, object>
{
["Function"] = nameof(NullFunction),
[nameof(Column)] = Column,
[nameof(DefaultValue)] = DefaultValue,
[nameof(AdditionalDefaultValues)] = AdditionalDefaultValues.Where(val => val != null)
};
return props.Jsonify();
}
}