-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
101 lines (85 loc) · 1.38 KB
/
aggregate.go
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
package orm
// 后面可以每次支持新的操作符就加一个
const (
avg = "AVG"
sum = "SUM"
max = "MAX"
min = "MIN"
count = "COUNT"
)
type Aggregate struct {
table TableReference
fn string
arg string
alias string
}
func (a Aggregate) expr() {}
//func (a Aggregate) selectable() {}
func (a Aggregate) selectedAlias() string {
return a.alias
}
func (a Aggregate) fieldName() string {
return a.arg
}
func (a Aggregate) target() TableReference {
return a.table
}
func (a Aggregate) As(alias string) Aggregate {
return Aggregate{
fn: a.fn,
arg: a.arg,
alias: alias,
}
}
// EQ 例如 C("id").Eq(12)
func (a Aggregate) EQ(arg any) Predicate {
return Predicate{
left: a,
op: opEQ,
right: exprOf(arg),
}
}
func (a Aggregate) LT(arg any) Predicate {
return Predicate{
left: a,
op: opLT,
right: exprOf(arg),
}
}
func (a Aggregate) GT(arg any) Predicate {
return Predicate{
left: a,
op: opGT,
right: exprOf(arg),
}
}
func Avg(col string) Aggregate {
return Aggregate{
arg: col,
fn: avg,
}
}
func Min(col string) Aggregate {
return Aggregate{
arg: col,
fn: min,
}
}
func Max(col string) Aggregate {
return Aggregate{
arg: col,
fn: max,
}
}
func Count(col string) Aggregate {
return Aggregate{
arg: col,
fn: count,
}
}
func Sum(col string) Aggregate {
return Aggregate{
arg: col,
fn: sum,
}
}