-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConcatFunction.cs
More file actions
104 lines (89 loc) · 3.09 KB
/
ConcatFunction.cs
File metadata and controls
104 lines (89 loc) · 3.09 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#if !SYSTEM_TEXT_JSON
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#endif
using Queries.Core.Attributes;
using Queries.Core.Parts.Columns;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Queries.Core.Parts.Functions;
/// <summary>
/// Concat two or more columns.
/// </summary>
[Function]
#if !SYSTEM_TEXT_JSON
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
#endif
public class ConcatFunction : IAliasable<ConcatFunction>, IColumn, IEquatable<ConcatFunction>
{
/// <summary>
/// List of columns to concat
/// </summary>
public IEnumerable<IColumn> Columns { get; }
/// <summary>
/// Builds a new <see cref="ConcatFunction"/> instance.
/// </summary>
/// <param name="first">First column to concatenate</param>
/// <param name="second">Second column to concatenate</param>
/// <param name="columns">Additional columns to concatenate.</param>
/// <exception cref="ArgumentNullException">if either <paramref name="first"/> or <paramref name="second"/> is <see langword="null" />.</exception>
public ConcatFunction(IColumn first, IColumn second, params IColumn[] columns)
{
if (first == null)
{
throw new ArgumentNullException(nameof(first));
}
if (second == null)
{
throw new ArgumentNullException(nameof(second));
}
IEnumerable<IColumn> localColumns = (columns ?? Enumerable.Empty<IColumn>())
.Where(x => x != null)
.ToArray();
Columns = new[] { first, second }.Union(localColumns);
}
private string _alias;
/// <summary>
/// Alias of the result of the result of the function.
/// </summary>
public string Alias => _alias;
/// <summary>
/// Gives the result of the concat an alias
/// </summary>
/// <param name="alias">The new alias</param>
/// <returns>The current instance</returns>
public ConcatFunction As(string alias)
{
_alias = alias;
return this;
}
///<inheritdoc/>
public override bool Equals(object obj) => Equals(obj as ConcatFunction);
///<inheritdoc/>
public bool Equals(ConcatFunction other) => other is not null
&& Columns.SequenceEqual(other.Columns) && Alias == other.Alias;
///<inheritdoc/>
public override int GetHashCode()
{
int hashCode = -1367283405;
hashCode = (hashCode * -1521134295) + EqualityComparer<IEnumerable<IColumn>>.Default.GetHashCode(Columns);
return (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(Alias);
}
///<inheritdoc/>
public override string ToString()
{
var props = new
{
Type = nameof(ConcatFunction),
Columns,
Alias
};
return props.Jsonify();
}
/// <summary>
/// Performs a deep copy of the current instance.
/// </summary>
/// <returns><see cref="ConcatFunction"/></returns>
public IColumn Clone() => new ConcatFunction(Columns.ElementAt(0), Columns.ElementAt(1), Columns.Skip(2).Select(x => x.Clone()).ToArray());
}