scan.go
920 字节
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
package starrocks
import (
"database/sql"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"reflect"
)
func ScanRows(rows *sql.Rows) ([][]string, error) {
defer rows.Close()
var results [][]string
cols, err := rows.Columns()
if err != nil {
log.Logger.Error(err.Error())
return nil, err
}
for rows.Next() {
var row = make([]interface{}, 0)
generic := reflect.TypeOf(row).Elem()
for _ = range cols {
row = append(row, reflect.New(generic).Interface())
}
err := rows.Scan(row...)
if err != nil {
log.Logger.Error(err.Error())
return nil, err
}
var rowStrings = make([]string, len(cols))
for i := range row {
rowStrings[i] = utils.AssertString(*(row[i].(*interface{})))
}
results = append(results, rowStrings)
}
return results, nil
}