-
Notifications
You must be signed in to change notification settings - Fork 2
/
count_query.go
75 lines (66 loc) · 1.62 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
package gosql
import (
"strings"
)
// CountQuery is a query for counting rows in a table.
type CountQuery struct {
db *DB
count string
table string
joins []string
wheres []*where
whereArgs []interface{}
}
// 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, join)
return cq
}
// Exec executes the query.
func (cq *CountQuery) Exec() (int64, error) {
var count int64
row := cq.db.db.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 ")
q.WriteString(join)
}
for i, where := range cq.wheres {
if i == 0 {
q.WriteString(" where ")
} else {
q.WriteString(where.conjunction)
}
q.WriteString(where.condition)
}
return q.String()
}