-
Notifications
You must be signed in to change notification settings - Fork 1
/
rows.go
56 lines (48 loc) · 1.04 KB
/
rows.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 main
import (
"database/sql"
)
func getRows(rows *sql.Rows, nulls bool, max int) ([]map[string]interface{}, bool, error) {
columns, err := rows.Columns()
if err != nil {
return nil, false, err
}
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
result := make([]map[string]interface{}, 0)
for max != 0 {
more := rows.Next()
if !more {
return result, false, rows.Err()
}
for i := range columns {
valuePtrs[i] = &values[i]
}
err := rows.Scan(valuePtrs...)
if err != nil {
return nil, false, err
}
obj := make(map[string]interface{})
for i, col := range columns {
val := values[i]
if nulls || val != nil {
var v interface{}
b, ok := val.([]byte)
if ok {
v = string(b)
} else {
v = val
}
obj[col] = v
}
}
result = append(result, obj)
max--
}
return result, true, nil
}
func getAllRows(rows *sql.Rows, null bool) ([]map[string]interface{}, error) {
data, _, err := getRows(rows, null, -1)
return data, err
}