-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryElement.cs
More file actions
57 lines (50 loc) · 1.61 KB
/
QueryElement.cs
File metadata and controls
57 lines (50 loc) · 1.61 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
using System;
using System.Collections.Generic;
using System.Linq;
using FizzCode.DbTools.DataDefinition.Base;
namespace FizzCode.DbTools.QueryBuilder;
public abstract class QueryElement
{
public SqlTableOrView Table { get; set; }
public List<QueryColumn> QueryColumns { get; set; }
protected QueryElement(SqlTableOrView sqlTable, params QueryColumn[] columns)
{
Table = sqlTable;
QueryColumns = columns.ToList();
}
protected QueryElement(SqlTableOrView sqlTable, string? alias, params QueryColumn[] columns)
: this(sqlTable, columns)
{
if ((alias == null && Table.GetAlias() is null)
|| (alias != null && Table.GetAlias() != alias))
{
_ = sqlTable switch
{
SqlTable table => Table = table.Alias(alias),
SqlView view => Table = view.AliasView(alias),
_ => throw new ArgumentException("Unknown SqlTableOrView Type.")
};
}
}
public List<QueryColumn>? GetColumns()
{
if (QueryColumns.Count == 1 && QueryColumns[0] is None)
return null;
if (QueryColumns.Count == 0)
{
if (Table is SqlTable table)
{
return table.Columns.Select(c => (QueryColumn)c).ToList();
}
else if (Table is SqlView view)
{
return view.Columns.Select(c => (QueryColumn)c).ToList();
}
else
{
throw new ArgumentException("Unknown SqlTableOrView Type.");
}
}
return QueryColumns;
}
}