-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubstringFunction.cs
More file actions
76 lines (66 loc) · 2.47 KB
/
SubstringFunction.cs
File metadata and controls
76 lines (66 loc) · 2.47 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
using System;
using Queries.Core.Parts.Columns;
using Queries.Core.Attributes;
namespace Queries.Core.Parts.Functions;
/// <summary>
/// "SUBSTRING" function.
/// </summary>
[Function]
public class SubstringFunction : IColumn, IAliasable<SubstringFunction>
{
/// <summary>
/// Column the function will be applied to
/// </summary>
public IColumn Column { get; }
/// <summary>
/// Defines where the substring extraction will start
/// </summary>
public int Start { get; }
/// <summary>
/// Defines the length of the extracted substring
/// </summary>
public int? Length { get; }
/// <summary>
/// Builds a new <see cref="SubstringFunction"/> instance.
/// </summary>
/// <param name="column">Column onto which the </param>
/// <param name="start">index of the position where to start the substring</param>
/// <param name="length">positive integer</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException">if either :
/// - <paramref name="start"/> is less than <c>0</c> <see langword="null" />,
/// - <paramref name="length"/> is less than <c>0</c>.
/// </exception>
public SubstringFunction(IColumn column, int start, int? length = null)
{
if (start < 0)
{
throw new ArgumentOutOfRangeException(nameof(start), start, $"{nameof(start)} must be greater or equal to 0");
}
if (length.HasValue && length.Value < 0)
{
throw new ArgumentOutOfRangeException(nameof(length), length, $"{nameof(length)} must be greater or equal to 0");
}
Column = column ?? throw new ArgumentNullException(nameof(column), $"{nameof(column)} cannot be null");
Start = start;
Length = length;
}
///<inheritdoc/>
public string Alias { get; private set; }
/// <summary>
/// Defines an alias for the <see cref="SubstringFunction"/>
/// </summary>
/// <param name="alias">The new alias</param>
/// <returns></returns>
public SubstringFunction As(string alias)
{
// TODO Validate the alias ?
Alias = alias;
return this;
}
/// <summary>
/// Performs a deep copy of the current instance.
/// </summary>
/// <returns><see cref="SubstringFunction"/></returns>
public IColumn Clone() => new SubstringFunction(Column.Clone(), Start, Length);
}