-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectQuery.cs
More file actions
329 lines (272 loc) · 10 KB
/
SelectQuery.cs
File metadata and controls
329 lines (272 loc) · 10 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#if !SYSTEM_TEXT_JSON
using Newtonsoft.Json;
#else
using System.Text.Json;
#endif
using Queries.Core.Builders.Fluent;
using Queries.Core.Parts;
using Queries.Core.Parts.Clauses;
using Queries.Core.Parts.Columns;
using Queries.Core.Parts.Joins;
using Queries.Core.Parts.Sorting;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Queries.Core.Builders
{
/// <summary>
/// Models a query that can read data
/// </summary>
public class SelectQuery : SelectQueryBase, ISelectQuery<SelectQuery>, IFromQuery<SelectQuery>, IWhereQuery<SelectQuery>, IJoinQuery<SelectQuery>, IOrderQuery<SelectQuery>, IEquatable<SelectQuery>, IColumn
{
/// <summary>
/// Defines the max number of records to retrieve
/// </summary>
public int? PageIndex { get; private set; }
/// <summary>
/// Gets / Sets the number of records to retrieve
/// </summary>
public int? PageSize { get; private set; }
/// <summary>
/// <see cref="ITable"/>s that will be used
/// </summary>
public IReadOnlyList<ITable> Tables => _tables;
private readonly List<ITable> _tables;
/// <summary>
/// List of <see cref="IUnionQuery{T}"/> that the current query will use.
/// </summary>
public IReadOnlyList<IUnionQuery<SelectQuery>> Unions => _unions;
private readonly List<IUnionQuery<SelectQuery>> _unions;
/// <summary>
/// Builds a new <see cref="SelectQuery"/> instance.
/// </summary>
/// <param name="columns">columns of the query</param>
/// <remarks>
/// This constructor filters out <c>null</c> value from <paramref name="columns"/>.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">the constructor is called with no argument</exception>
public SelectQuery(params IColumn[] columns)
{
if (columns.All(x => x is null))
{
throw new ArgumentOutOfRangeException(nameof(columns), columns, "at least one column must be provided");
}
Columns = columns;
_tables = new List<ITable>();
_unions = new List<IUnionQuery<SelectQuery>>();
}
internal SelectQuery(params string[] columnNames) : this(columnNames.Select(colName => colName.Field()).Cast<IColumn>().ToArray())
{
}
/// <summary>
/// Specified that the query should return <paramref name="pageSize"/> elements at most
/// </summary>
/// <param name="pageIndex">1-based index of the page</param>
/// <param name="pageSize">Maximum number of elements a page should contains</param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException">if either <paramref name="pageIndex"/> is < 1 or <paramref name="pageSize"/> is <c>< 0</c></exception>.
public SelectQuery Paginate(int pageIndex, int pageSize)
{
PageIndex = pageIndex;
PageSize = pageSize;
return this;
}
///<inheritdoc/>
public IFromQuery<SelectQuery> From(params ITable[] tables)
{
_tables.AddRange(tables);
return this;
}
///<inheritdoc/>
public IFromQuery<SelectQuery> From(params string[] tables)
{
foreach (string tableName in tables)
{
_tables.Add(tableName.Table());
}
return this;
}
///<inheritdoc cref="IWhereClause"/>
public IWhereQuery<SelectQuery> Where(IWhereClause clause)
{
WhereCriteria = clause ?? throw new ArgumentNullException(nameof(clause), $"{nameof(clause)} cannot be null");
return this;
}
///<inheritdoc/>
public IWhereQuery<SelectQuery> Where(IColumn column, ClauseOperator @operator, IColumn constraint)
=> Where(new WhereClause(column, @operator, constraint));
///<inheritdoc/>
public IWhereQuery<SelectQuery> Where(IColumn column, ClauseOperator @operator, string constraint)
=> Where(column, @operator, constraint?.Literal());
///<inheritdoc/>
public IJoinQuery<SelectQuery> InnerJoin(Table table, IWhereClause clause)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table), $"{nameof(table)} cannot be null");
}
if (clause is null)
{
throw new ArgumentNullException(nameof(clause), $"{nameof(clause)} cannot be null");
}
Joins.Add(new InnerJoin(table, clause));
return this;
}
///<inheritdoc/>
public IJoinQuery<SelectQuery> LeftOuterJoin(Table table, IWhereClause clause)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table), $"{nameof(table)} cannot be null");
}
if (clause is null)
{
throw new ArgumentNullException(nameof(clause), $"{nameof(clause)} cannot be null");
}
Joins.Add(new LeftOuterJoin(table, clause));
return this;
}
///<inheritdoc/>
public IJoinQuery<SelectQuery> RightOuterJoin(Table table, IWhereClause clause)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table), $"{nameof(table)} cannot be null");
}
if (clause is null)
{
throw new ArgumentNullException(nameof(clause), $"{nameof(clause)} cannot be null");
}
Joins.Add(new RightOuterJoin(table, clause));
return this;
}
///<inheritdoc/>
public SelectQuery Build() => this;
///<inheritdoc/>
IOrderQuery<SelectQuery> IWhereQuery<SelectQuery>.OrderBy(IOrder sort, params IOrder[] sorts)
{
Orders.Add(sort);
foreach (IOrder items in sorts.Where(s => Equals(s, null)))
{
Orders.Add(sort);
}
return this;
}
///<inheritdoc/>
public IUnionQuery<SelectQuery> Union(IUnionQuery<SelectQuery> select)
{
_unions.Add(select);
return this;
}
///<inheritdoc/>
public IOrderQuery<SelectQuery> OrderBy(params IOrder[] sorts)
{
foreach (IOrder sort in sorts)
{
Orders.Add(sort);
}
return this;
}
///<inheritdoc/>
public IOrderQuery<SelectQuery> Having(IHavingClause clause)
{
HavingCriteria = clause;
return this;
}
///<inheritdoc/>
public string Alias { get; private set; }
///<inheritdoc/>
public ITable As(string alias)
{
Alias = alias;
return this;
}
/// <inheritdoc />
public override bool Equals(object obj) => Equals(obj as SelectQuery);
/// <inheritdoc />
public bool Equals(SelectQuery other)
{
bool equals = false;
if (other != null && other.PageSize == PageSize && other.PageIndex == PageIndex && other.Alias == Alias)
{
equals = Columns.SequenceEqual(other.Columns)
&& ((WhereCriteria is null && other.WhereCriteria is null) || Equals(WhereCriteria , other.WhereCriteria))
&& Tables.SequenceEqual(other.Tables)
&& Unions.SequenceEqual(other.Unions)
&& Orders.SequenceEqual(other?.Orders);
}
return equals;
}
///<inheritdoc/>
#if !NETSTANDARD2_1
public override int GetHashCode() => (Alias, Columns, HavingCriteria, Joins, PageIndex, PageSize, Orders, Tables, Unions, WhereCriteria).GetHashCode();
#else
public override int GetHashCode()
{
HashCode hash = new();
hash.Add(Alias);
foreach (IColumn item in Columns)
{
hash.Add(item);
}
hash.Add(HavingCriteria);
foreach (IJoin item in Joins)
{
hash.Add(item);
}
hash.Add(PageIndex);
hash.Add(PageSize);
foreach (IOrder item in Orders)
{
hash.Add(item);
}
foreach (ITable item in Tables)
{
hash.Add(item);
}
foreach (IUnionQuery<SelectQuery> item in Unions)
{
hash.Add(item);
}
foreach (IOrder item in Orders)
{
hash.Add(item);
}
hash.Add(WhereCriteria);
return hash.ToHashCode();
}
#endif
/// <summary>
/// Performs a deep copy of the current instance.
/// </summary>
/// <returns>a <see cref="SelectQuery"/> instance that is a deep copy of the current instance.</returns>
public SelectQuery Clone()
{
SelectQuery query = new(Columns.Select(x => x.Clone()).ToArray())
{
Alias = Alias,
Columns = Columns.Select(x => x.Clone()).ToArray(),
HavingCriteria = HavingCriteria?.Clone(),
WhereCriteria = WhereCriteria?.Clone(),
PageIndex = PageIndex,
PageSize = PageSize
};
query.From(Tables.Select(t => t.Clone()).ToArray());
foreach (IUnionQuery<SelectQuery> item in Unions)
{
query.Union(item.Build().Clone());
}
foreach (IOrder item in Orders)
{
query.OrderBy(item);
}
return query;
}
///<inheritdoc/>
ITable ITable.Clone() => Clone();
///<inheritdoc/>
IColumn IColumn.Clone() => Clone();
///<inheritdoc/>
public override string ToString() => this.Jsonify();
}
}