-
Notifications
You must be signed in to change notification settings - Fork 2
/
count_query.go
131 lines (117 loc) · 3 KB
/
count_query.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
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
package gosql
import (
"database/sql"
"fmt"
"strings"
)
// QueryRower .
type QueryRower interface {
QueryRow(query string, args ...interface{}) *sql.Row
}
// CountQuery is a query for counting rows in a table.
type CountQuery struct {
db *DB
queryRower QueryRower
count string
table string
joins []string
wheres []*where
whereArgs []interface{}
havings []*having
havingArgs []interface{}
groupBy string
}
// Where specifies which rows will be returned.
func (cq *CountQuery) Where(condition string, args ...interface{}) *CountQuery {
w := &where{
conjunction: " and ",
condition: condition,
}
cq.wheres = append(cq.wheres, w)
cq.whereArgs = append(cq.whereArgs, args...)
return cq
}
// OrWhere specifies which rows will be returned.
func (cq *CountQuery) OrWhere(condition string, args ...interface{}) *CountQuery {
w := &where{
conjunction: " or ",
condition: condition,
}
cq.wheres = append(cq.wheres, w)
cq.whereArgs = append(cq.whereArgs, args...)
return cq
}
// Join joins another table to this query.
func (cq *CountQuery) Join(join string) *CountQuery {
cq.joins = append(cq.joins, fmt.Sprintf(" join %s", join))
return cq
}
// LeftJoin joins another table to this query.
func (cq *CountQuery) LeftJoin(join string) *CountQuery {
cq.joins = append(cq.joins, fmt.Sprintf(" left join %s", join))
return cq
}
// Exec executes the query.
func (cq *CountQuery) Exec() (int64, error) {
var count int64
row := cq.queryRower.QueryRow(cq.String(), cq.whereArgs...)
err := row.Scan(&count)
return count, err
}
// String returns the string representation of CountQuery.
func (cq *CountQuery) String() string {
var q strings.Builder
q.WriteString("select count(")
q.WriteString(cq.count)
q.WriteString(") from ")
q.WriteString(cq.table)
for _, join := range cq.joins {
q.WriteString(join)
}
for i, where := range cq.wheres {
if i == 0 {
q.WriteString(" where ")
} else {
q.WriteString(where.conjunction)
}
q.WriteString(where.condition)
}
if cq.groupBy != "" {
q.WriteString(" group by ")
q.WriteString(cq.groupBy)
}
for i, having := range cq.havings {
if i == 0 {
q.WriteString(" having ")
} else {
q.WriteString(having.conjunction)
}
q.WriteString(having.condition)
}
return q.String()
}
// Having specifies which rows will be returned.
func (cq *CountQuery) Having(condition string, args ...interface{}) *CountQuery {
h := &having{
conjunction: " and ",
condition: condition,
}
cq.havings = append(cq.havings, h)
cq.havingArgs = append(cq.havingArgs, args...)
return cq
}
// OrHaving specifies which rows will be returned.
func (cq *CountQuery) OrHaving(condition string, args ...interface{}) *CountQuery {
h := &having{
conjunction: " or ",
condition: condition,
}
cq.havings = append(cq.havings, h)
cq.havingArgs = append(cq.havingArgs, args...)
return cq
}
// GroupBy specifies how to group the results.
func (cq *CountQuery) GroupBy(bys ...string) *CountQuery {
cq.groupBy = strings.Join(bys, ", ")
return cq
}