-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaggregate.go
More file actions
58 lines (46 loc) · 1.39 KB
/
Copy pathaggregate.go
File metadata and controls
58 lines (46 loc) · 1.39 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
package qrafter
import (
"github.com/SennovE/qrafter/internal/core"
"github.com/SennovE/qrafter/internal/expr"
)
// Aggregate represents a SQL aggregate expression such as COUNT or SUM.
type Aggregate struct {
Expression
}
var _ core.Aggregater = Aggregate{}
func newAggregate(s core.Selecter) Aggregate {
return Aggregate{Expression: newExpression(s)}
}
// Aggregate marks the value as an aggregate expression.
func (a Aggregate) Aggregate() {}
// As returns the aggregate expression with a SQL alias.
func (a Aggregate) As(alias string) Aggregate {
return newAggregate(expr.Alias(a.selecter, alias))
}
// AggregateFunc builds an aggregate expression with the given function name.
func AggregateFunc(name string, args ...any) Aggregate {
return newAggregate(expr.Function(name, asSelecters(args)...))
}
// Count builds a COUNT aggregate expression.
func Count(args ...any) Aggregate {
if len(args) == 0 {
return AggregateFunc("COUNT", Star())
}
return AggregateFunc("COUNT", args...)
}
// Sum builds a SUM aggregate expression.
func Sum(v any) Aggregate {
return AggregateFunc("SUM", v)
}
// Avg builds an AVG aggregate expression.
func Avg(v any) Aggregate {
return AggregateFunc("AVG", v)
}
// Min builds a MIN aggregate expression.
func Min(v any) Aggregate {
return AggregateFunc("MIN", v)
}
// Max builds a MAX aggregate expression.
func Max(v any) Aggregate {
return AggregateFunc("MAX", v)
}