forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.go
49 lines (39 loc) · 1.28 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
package qb
import (
"fmt"
)
// Avg function generates "avg(%s)" statement for column
func Avg(column ColumnElem) AggregateClause {
return Aggregate("AVG", column)
}
// Count function generates "count(%s)" statement for column
func Count(column ColumnElem) AggregateClause {
return Aggregate("COUNT", column)
}
// Sum function generates "sum(%s)" statement for column
func Sum(column ColumnElem) AggregateClause {
return Aggregate("SUM", column)
}
// Min function generates "min(%s)" statement for column
func Min(column ColumnElem) AggregateClause {
return Aggregate("MIN", column)
}
// Max function generates "max(%s)" statement for column
func Max(column ColumnElem) AggregateClause {
return Aggregate("MAX", column)
}
// Aggregate generates a new aggregate clause given function & column
func Aggregate(fn string, column ColumnElem) AggregateClause {
return AggregateClause{fn, column}
}
// AggregateClause is the base struct for building aggregate functions
type AggregateClause struct {
fn string
column ColumnElem
}
// Build compiles the aggregate clause and returns the sql and bindings
func (c AggregateClause) Build(dialect Dialect) (string, []interface{}) {
bindings := []interface{}{}
sql := fmt.Sprintf("%s(%s)", c.fn, dialect.Escape(c.column.Name))
return sql, bindings
}