forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
56 lines (45 loc) · 1.17 KB
/
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
package dbutil
import (
"database/sql"
"github.com/pingcap/errors"
)
// ScanRowsToInterfaces scans rows to interface arrary.
func ScanRowsToInterfaces(rows *sql.Rows) ([][]interface{}, error) {
var rowsData [][]interface{}
cols, err := rows.Columns()
if err != nil {
return nil, errors.Trace(err)
}
for rows.Next() {
colVals := make([]interface{}, len(cols))
err = rows.Scan(colVals...)
if err != nil {
return nil, errors.Trace(err)
}
rowsData = append(rowsData, colVals)
}
return rowsData, nil
}
// ScanRow scans rows into a map, and another map specify the value is null or not.
func ScanRow(rows *sql.Rows) (map[string][]byte, map[string]bool, error) {
cols, err := rows.Columns()
if err != nil {
return nil, nil, errors.Trace(err)
}
colVals := make([][]byte, len(cols))
colValsI := make([]interface{}, len(colVals))
for i := range colValsI {
colValsI[i] = &colVals[i]
}
err = rows.Scan(colValsI...)
if err != nil {
return nil, nil, errors.Trace(err)
}
result := make(map[string][]byte)
null := make(map[string]bool)
for i := range colVals {
result[cols[i]] = colVals[i]
null[cols[i]] = (colVals[i] == nil)
}
return result, null, nil
}