tidb query 源码
tidb query 代码
文件路径:/util/dbutil/query.go
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbutil
import (
"database/sql"
"github.com/pingcap/errors"
)
// ScanRowsToInterfaces scans rows to interface array.
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
}
// ColumnData saves column's data.
type ColumnData struct {
Data []byte
IsNull bool
}
// ScanRow scans rows into a map.
func ScanRow(rows *sql.Rows) (map[string]*ColumnData, error) {
cols, err := rows.Columns()
if err != nil {
return 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, errors.Trace(err)
}
result := make(map[string]*ColumnData)
for i := range colVals {
data := &ColumnData{
Data: colVals[i],
IsNull: colVals[i] == nil,
}
result[cols[i]] = data
}
return result, nil
}
相关信息
相关文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦