forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
60 lines (52 loc) · 1.36 KB
/
count.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
package query
import (
"database/sql"
"fmt"
"github.com/pkg/errors"
)
// Count returns the number of rows in the given table.
func Count(tx *sql.Tx, table string, where string, args ...interface{}) (int, error) {
stmt := fmt.Sprintf("SELECT COUNT(*) FROM %s", table)
if where != "" {
stmt += fmt.Sprintf(" WHERE %s", where)
}
rows, err := tx.Query(stmt, args...)
if err != nil {
return -1, err
}
defer rows.Close()
// For sanity, make sure we read one and only one row.
if !rows.Next() {
return -1, fmt.Errorf("no rows returned")
}
var count int
err = rows.Scan(&count)
if err != nil {
return -1, fmt.Errorf("failed to scan count column")
}
if rows.Next() {
return -1, fmt.Errorf("more than one row returned")
}
err = rows.Err()
if err != nil {
return -1, err
}
return count, nil
}
// CountAll returns a map associating each table name in the database
// with the total count of its rows.
func CountAll(tx *sql.Tx) (map[string]int, error) {
tables, err := SelectStrings(tx, "SELECT name FROM sqlite_master WHERE type = 'table'")
if err != nil {
return nil, errors.Wrap(err, "Failed to fetch table names")
}
counts := map[string]int{}
for _, table := range tables {
count, err := Count(tx, table, "")
if err != nil {
return nil, errors.Wrapf(err, "Failed to count rows of %s", table)
}
counts[table] = count
}
return counts, nil
}