scan.go
900 字节
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) {
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
}