-
Notifications
You must be signed in to change notification settings - Fork 1
/
WhereClause.cs
179 lines (157 loc) · 8.2 KB
/
WhereClause.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Queries.Core.Parts.Columns;
using System;
namespace Queries.Core.Parts.Clauses;
/// <summary>
/// a criterion to apply to a <see cref="Column"/>.
/// </summary>
public class WhereClause : IWhereClause, IClause<IColumn>, IEquatable<WhereClause>
{
/// <summary>
/// Unique identifier of the clause
/// </summary>
public Guid UniqueId { get; }
/// <summary>
/// <see cref="IColumn"/> which the current clause will be applied onto
/// </summary>
public IColumn Column { get; }
/// <summary>
/// The <see cref="ClauseOperator"/> of the clause
/// </summary>
public ClauseOperator Operator{ get; }
/// <summary>
/// The constraint of the clause
/// </summary>
public IColumn Constraint { get; set; }
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, IColumn constraint = null)
{
Column = column ?? throw new ArgumentNullException(nameof(column));
UniqueId = Guid.NewGuid();
Operator = @operator;
if (@operator != ClauseOperator.IsNull && @operator != ClauseOperator.IsNotNull)
{
if (@operator == ClauseOperator.In)
{
switch (constraint)
{
case StringValues strings:
Constraint = strings;
break;
case null:
throw new ArgumentNullException(nameof(constraint),
$"{nameof(constraint)} cannot be null when {nameof(@operator)} is {nameof(ClauseOperator)}.{nameof(ClauseOperator.In)}");
default:
break;
}
}
Constraint = constraint;
}
}
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="string"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, string constraint) : this(column, @operator, constraint?.Literal())
{
}
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="DateTime"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, DateTime? constraint) : this(column, @operator, constraint?.Literal())
{
}
#if NET6_0_OR_GREATER
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="DateOnly"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, DateOnly? constraint) : this(column, @operator, constraint?.Literal())
{
}
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="TimeOnly"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, TimeOnly? constraint) : this(column, @operator, constraint?.Literal())
{
}
#endif
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see langword="bool"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, bool? constraint) : this(column, @operator, constraint?.Literal())
{
}
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="long"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, long? constraint) : this(column, @operator, constraint?.Literal())
{
}
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="decimal"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, decimal? constraint) : this(column, @operator, constraint?.Literal())
{
}
/// <summary>
/// Builds a new <see cref="WhereClause"/> instance with a <see cref="float"/> constraint.
/// </summary>
/// <param name="column"><see cref="IColumn"/> where to apply the clause onto</param>
/// <param name="operator"><see cref="ClauseOperator"/> to apply</param>
/// <param name="constraint">constraint to apply to <paramref name="column"/>.</param>
/// <exception cref="ArgumentNullException">if <paramref name="column"/> is <see langword="null" />.</exception>
public WhereClause(IColumn column, ClauseOperator @operator, float? constraint) : this(column, @operator, constraint?.Literal())
{
}
///<inheritdoc/>
public override bool Equals(object obj) => Equals(obj as WhereClause);
///<inheritdoc/>
public bool Equals(IWhereClause other) => Equals(other as WhereClause);
///<inheritdoc/>
public bool Equals(WhereClause other)
=> Column.Equals(other?.Column)
&& Operator == other?.Operator
&& ((Constraint == null && other?.Constraint == null) || Constraint.Equals(other.Constraint));
///<inheritdoc/>
#if !NETSTANDARD2_1
public override int GetHashCode() => (Column, Operator, Constraint).GetHashCode();
#else
public override int GetHashCode() => HashCode.Combine(Column, Operator, Constraint);
#endif
///<inheritdoc/>
public IWhereClause Clone() => new WhereClause(Column.Clone(), Operator, Constraint?.Clone());
///<inheritdoc/>
public override string ToString() => (Column, Operator, Constraint).ToString();
}