-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathQuery.Select.cs
More file actions
141 lines (113 loc) · 3.71 KB
/
Query.Select.cs
File metadata and controls
141 lines (113 loc) · 3.71 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace SqlKata
{
public partial class Query
{
public Query Select(params string[] columns)
{
return Select(columns.AsEnumerable());
}
public Query Select(IEnumerable<string> columns)
{
Method = "select";
columns = columns
.Select(x => Helper.ExpandExpression(x))
.SelectMany(x => x)
.ToArray();
foreach (var column in columns)
{
AddComponent("select", new Column
{
Name = column
});
}
return this;
}
/// <summary>
/// Add a new "raw" select expression to the query.
/// </summary>
/// <returns></returns>
public Query SelectRaw(string sql, params object[] bindings)
{
Method = "select";
AddComponent("select", new RawColumn
{
Expression = sql,
Bindings = bindings,
});
return this;
}
public Query Select(Query query, string alias)
{
Method = "select";
query = query.Clone();
AddComponent("select", new QueryColumn
{
Query = query.As(alias),
});
return this;
}
public Query Select(Func<Query, Query> callback, string alias)
{
return Select(callback.Invoke(NewChild()), alias);
}
public Query SelectAggregate(string aggregate, string column, Query filter = null)
{
Method = "select";
AddComponent("select", new AggregatedColumn
{
Column = new Column { Name = column },
Aggregate = aggregate,
Filter = filter,
});
return this;
}
public Query SelectAggregate(string aggregate, string column, Func<Query, Query> filter)
{
if (filter == null)
{
return SelectAggregate(aggregate, column);
}
return SelectAggregate(aggregate, column, filter.Invoke(NewChild()));
}
public Query SelectSum(string column, Func<Query, Query> filter = null)
{
return SelectAggregate("sum", column, filter);
}
public Query SelectCount(string column, Func<Query, Query> filter = null)
{
return SelectAggregate("count", column, filter);
}
public Query SelectAvg(string column, Func<Query, Query> filter = null)
{
return SelectAggregate("avg", column, filter);
}
public Query SelectMin(string column, Func<Query, Query> filter = null)
{
return SelectAggregate("min", column, filter);
}
public Query SelectMax(string column, Func<Query, Query> filter = null)
{
return SelectAggregate("max", column, filter);
}
public Query Select<T>() where T : class {
var properties = typeof(T).GetProperties();
var columns = new List<string>();
foreach (var property in properties) {
if (property.GetSetMethod() == null) {
continue;
}
var name = property.Name;
var attribute = property.GetCustomAttribute<ColumnAttribute>();
if (attribute != null) {
name = $"{attribute.Name} as {name}";
}
columns.Add(name);
}
return Select(columns);
}
}
}