-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuery.cs
More file actions
495 lines (440 loc) · 26.4 KB
/
Query.cs
File metadata and controls
495 lines (440 loc) · 26.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using System;
using System.Collections.Generic;
using System.Linq;
using FizzCode.DbTools.DataDefinition.Base;
using FizzCode.DbTools.QueryBuilder.Interfaces;
namespace FizzCode.DbTools.QueryBuilder;
public class Query : QueryElement, IQuery
{
public Query(SqlTable sqlTable, string? alias, QueryColumnAliasStrategy queryColumnAliasStrategy, params QueryColumn[] columns)
: base(sqlTable, alias, columns)
{
QueryElements.Add(this);
QueryColumnAliasStrategy = queryColumnAliasStrategy;
}
public Query(SqlTable sqlTable, params QueryColumn[] columns)
: this(sqlTable, null, QueryColumnAliasStrategy.PrefixTableNameIfNeeded, columns)
{
}
public Query(SqlTable sqlTable, QueryColumnAliasStrategy queryColumnAliasStrategy, params QueryColumn[] columns)
: this(sqlTable, null, queryColumnAliasStrategy, columns)
{
}
public QueryColumnAliasStrategy QueryColumnAliasStrategy { get; set; }
public List<JoinBase> Joins { get; } = [];
public List<Query> Unions { get; } = [];
public List<QueryElement> QueryElements { get; } = [];
public List<Filter> Filters { get; } = [];
public Query Union(Query query)
{
Unions.Add(query);
return this;
}
public Query Join(JoinBase join)
{
Joins.Add(join);
QueryElements.Add(join);
return this;
}
/// <summary>
/// Left join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the join condition, from the Foreign Key of the <paramref name="table"/> to the Primary Key of the main table of the Query. For this method, the <paramref name="table"/> has to be one and only one Foreign Key to the Primary Key.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoin(SqlTable table, params QueryColumn[] columns)
{
return LeftJoin(table, null, null, columns);
}
/// <summary>
/// Left join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the Foreign Key of the <paramref name="table"/> to the Primary Key of the main table of the Query.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoinAlias(SqlTable table, string alias, params QueryColumn[] columns)
{
return LeftJoinAlias(table, alias, null, null, columns);
}
/// <summary>
/// Left join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> and the <paramref name="columnTarget"/>.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columnTarget">The column of the other table to join. This might be a column of the main table of the Query, or a column of the table of another QueryElement.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoinAlias(SqlTable table, string? alias, QueryColumn? columnSource, QueryColumn? columnTarget, params QueryColumn[] columns)
{
return Join(new Join(table, alias, columnSource, columnTarget, JoinType.Left, columns));
}
/// <summary>
/// Left join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> to the Primary Key of the main table of the Query..
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoinAlias(SqlTable table, string alias, QueryColumn columnSource, params QueryColumn[] columns)
{
return LeftJoinAlias(table, alias, columnSource, null, columns);
}
/// <summary>
/// Left join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> and the <paramref name="columnTarget"/>.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columnTarget">The column of the other table to join. This might be a column of the main table of the Query, or a column of the table of another QueryElement.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoin(SqlTable table, QueryColumn? columnSource, QueryColumn? columnTarget, params QueryColumn[] columns)
{
return LeftJoinAlias(table, null, columnSource, columnTarget, columns);
}
/// <summary>
/// Left join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> to the Primary Key of the main table of the Query..
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoinTo(SqlTable table, QueryColumn columnSource, params QueryColumn[] columns)
{
return LeftJoin(table, columnSource, null, columns);
}
/// <summary>
/// Left join the <paramref name="table"/> with an <paramref name="alias"/>, providing the join condition.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="on">The join condition.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoinAliasOn(SqlTable table, string? alias, Expression on, params QueryColumn[] columns)
{
return Join(new JoinOn(table, alias, on, JoinType.Left, columns));
}
/// <summary>
/// Left join the <paramref name="table"/>, providing the join condition.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="on">The join condition.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query LeftJoinOn(SqlTable table, Expression on, params QueryColumn[] columns)
{
return LeftJoinAliasOn(table, null, on, columns);
}
/// <summary>
/// Right join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the join condition, from the Foreign Key of the <paramref name="table"/> to the Primary Key of the main table of the Query. For this method, the <paramref name="table"/> has to be one and only one Foreign Key to the Primary Key.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoin(SqlTable table, params QueryColumn[] columns)
{
return RightJoin(table, null, null, columns);
}
/// <summary>
/// Right join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the Foreign Key of the <paramref name="table"/> to the Primary Key of the main table of the Query.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoinAlias(SqlTable table, string alias, params QueryColumn[] columns)
{
return RightJoinAlias(table, alias, null, null, columns);
}
/// <summary>
/// Right join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> and the <paramref name="columnTarget"/>.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columnTarget">The column of the other table to join. This might be a column of the main table of the Query, or a column of the table of another QueryElement.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoinAlias(SqlTable table, string? alias, QueryColumn? columnSource, QueryColumn? columnTarget, params QueryColumn[] columns)
{
return Join(new Join(table, alias, columnSource, columnTarget, JoinType.Right, columns));
}
/// <summary>
/// Right join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> to the Primary Key of the main table of the Query..
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoinAlias(SqlTable table, string alias, QueryColumn columnSource, params QueryColumn[] columns)
{
return RightJoinAlias(table, alias, columnSource, null, columns);
}
/// <summary>
/// Right join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> and the <paramref name="columnTarget"/>.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columnTarget">The column of the other table to join. This might be a column of the main table of the Query, or a column of the table of another QueryElement.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoin(SqlTable table, QueryColumn? columnSource, QueryColumn? columnTarget, params QueryColumn[] columns)
{
return RightJoinAlias(table, null, columnSource, columnTarget, columns);
}
/// <summary>
/// Right join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> to the Primary Key of the main table of the Query..
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoinTo(SqlTable table, QueryColumn columnSource, params QueryColumn[] columns)
{
return RightJoin(table, columnSource, null, columns);
}
/// <summary>
/// Right join the <paramref name="table"/> with an <paramref name="alias"/>, providing the join condition.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="on">The join condition.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoinAliasOn(SqlTable table, string? alias, Expression on, params QueryColumn[] columns)
{
return Join(new JoinOn(table, alias, on, JoinType.Right, columns));
}
/// <summary>
/// Right join the <paramref name="table"/>, providing the join condition.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="on">The join condition.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query RightJoinOn(SqlTable table, Expression on, params QueryColumn[] columns)
{
return RightJoinAliasOn(table, null, on, columns);
}
/// <summary>
/// Inner join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the join condition, from the Foreign Key of the <paramref name="table"/> to the Primary Key of the main table of the Query. For this method, the <paramref name="table"/> has to be one and only one Foreign Key to the Primary Key.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.)</param>
/// <returns>The Query.</returns>
public Query InnerJoin(SqlTable table, params QueryColumn[] columns)
{
return InnerJoin(table, null, null, columns);
}
/// <summary>
/// Inner join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the Foreign Key of the <paramref name="table"/> to the Primary Key of the main table of the Query.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoinAlias(SqlTable table, string alias, params QueryColumn[] columns)
{
return InnerJoinAlias(table, alias, null, null, columns);
}
/// <summary>
/// Inner join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> and the <paramref name="columnTarget"/>.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columnTarget">The column of the other table to join. This might be a column of the main table of the Query, or a column of the table of another QueryElement.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoinAlias(SqlTable table, string? alias, QueryColumn? columnSource, QueryColumn? columnTarget, params QueryColumn[] columns)
{
return Join(new Join(table, alias, columnSource, columnTarget, JoinType.Inner, columns));
}
/// <summary>
/// Inner join the <paramref name="table"/> with an <paramref name="alias"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> to the Primary Key of the main table of the Query..
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoinAlias(SqlTable table, string alias, QueryColumn columnSource, params QueryColumn[] columns)
{
return InnerJoinAlias(table, alias, columnSource, null, columns);
}
/// <summary>
/// Inner join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> and the <paramref name="columnTarget"/>.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columnTarget">The column of the other table to join. This might be a column of the main table of the Query, or a column of the table of another QueryElement.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoin(SqlTable table, QueryColumn? columnSource, QueryColumn? columnTarget, params QueryColumn[] columns)
{
return InnerJoinAlias(table, null, columnSource, columnTarget, columns);
}
/// <summary>
/// Inner join the <paramref name="table"/>.
/// The <see cref="QueryBuilder"/> will automatically build the equality join condition, from the <paramref name="columnSource"/> to the Primary Key of the main table of the Query..
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="columnSource">The column of the joined table to join on.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoinTo(SqlTable table, QueryColumn columnSource, params QueryColumn[] columns)
{
return InnerJoin(table, columnSource, null, columns);
}
/// <summary>
/// Inner join the <paramref name="table"/> with an <paramref name="alias"/>, providing the join condition.
/// If the table is referenced in other parts of the query, use table aliasing.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="alias">The alias for the table.</param>
/// <param name="on">The join condition.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoinAliasOn(SqlTable table, string? alias, Expression on, params QueryColumn[] columns)
{
return Join(new JoinOn(table, alias, on, JoinType.Inner, columns));
}
/// <summary>
/// Inner join the <paramref name="table"/>, providing the join condition.
/// </summary>
/// <param name="table">The table to join.</param>
/// <param name="on">The join condition.</param>
/// <param name="columns">The columns to include in the query. Provide any number of <see cref="QueryColumn"/>s, or <see cref="SqlColumn"/>s (which will be impicitly cast as a QueryColumn.</param>
/// <returns>The Query.</returns>
public Query InnerJoinOn(SqlTable table, Expression on, params QueryColumn[] columns)
{
return InnerJoinAliasOn(table, null, on, columns);
}
//join subquery
public Query InnerJoinOn(Query query, Expression on, params QueryColumn[] columns)
{
return InnerJoinAliasOn(query, null, on, columns);
}
public Query InnerJoinAliasOn(Query query, string? alias, Expression on, params QueryColumn[] columns)
{
return Join(new JoinSubQueryOn(query, alias, on, JoinType.Inner, columns));
}
public string? WhereExpression { get; set; }
public Query Where(params object[] expressionParts)
{
return Where(Expression.GetExpression(expressionParts, QueryElements));
}
public Query Where(string whereExpression)
{
WhereExpression = whereExpression;
return this;
}
public List<QueryColumn> GroupByColumns { get; } = [];
public Query GroupBy(params QueryColumn[] columns)
{
GroupByColumns.AddRange(columns);
return this;
}
public Query AddColumn(string alias, params object[] expressionParts)
{
var qc = new QueryColumn
{
Value = Expression.GetExpression(expressionParts, QueryElements),
As = alias
};
QueryColumns.Add(qc);
return this;
}
public Query AddCase(string alias, object[] whenExpression, object[] thenExpression, object[] elseExpression)
{
var qc = new QueryColumn
{
Value = $"CASE WHEN {GetExpression(whenExpression)} THEN {GetExpression(thenExpression)} ELSE {GetExpression(elseExpression)} END",
As = alias
};
QueryColumns.Add(qc);
return this;
}
public bool IsDisctinct { get; set; }
public Query Disctinct(bool isDistinct = true)
{
IsDisctinct = isDistinct;
return this;
}
private object GetExpression(object[] expressionParts)
{
return Expression.GetExpression(expressionParts, QueryElements);
}
public Query FilterBetween(QueryColumn column)
{
var tableOrView = QueryElements.First(qe => qe.Table.GetAlias() == column.Alias).Table;
SqlParameter parameter;
if (tableOrView is SqlTable table)
{
var sqlColumn = table.Columns[column.Value];
parameter = new SqlParameter(sqlColumn.Table.DatabaseDefinition!)
{
Name = column.Value
};
sqlColumn.Types.CopyTo(parameter.Types);
} else if (tableOrView is SqlView view)
{
var sqlColumn = view.Columns[column.Value];
parameter = new SqlParameter(sqlColumn.View.DatabaseDefinition!)
{
Name = column.Value
};
sqlColumn.Types.CopyTo(parameter.Types);
} else
{
throw new ArgumentException("Unknown SqlTableOrView Type.");
}
var filter = new Filter()
{
Column = column,
Table = tableOrView,
Parameter = parameter,
Type = FilterType.Between
};
Filters.Add(filter);
return this;
}
}