1 Star 1 Fork 0

bigbase/pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
scan.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
Vladimir Mihailenco 提交于 2016-09-19 07:49 . Update to use v5.
package types
import (
"database/sql"
"encoding/hex"
"reflect"
"strconv"
"time"
"gopkg.in/pg.v5/internal"
)
func Scan(v interface{}, b []byte) error {
switch v := v.(type) {
case *string:
*v = string(b)
return nil
case *[]byte:
if b == nil {
*v = nil
return nil
}
var err error
*v, err = scanBytes(b)
return err
case *int:
if b == nil {
*v = 0
return nil
}
var err error
*v, err = strconv.Atoi(internal.BytesToString(b))
return err
case *int64:
if b == nil {
*v = 0
return nil
}
var err error
*v, err = strconv.ParseInt(internal.BytesToString(b), 10, 64)
return err
case *time.Time:
if b == nil {
*v = time.Time{}
return nil
}
var err error
*v, err = ParseTime(b)
return err
}
vv := reflect.ValueOf(v)
if !vv.IsValid() {
return internal.Errorf("pg: Scan(nil)")
}
if vv.Kind() != reflect.Ptr {
return internal.Errorf("pg: Scan(non-pointer %T)", v)
}
vv = vv.Elem()
if !vv.IsValid() {
return internal.Errorf("pg: Scan(non-pointer %T)", v)
}
return ScanValue(vv, b)
}
func scanSQLScanner(scanner sql.Scanner, b []byte) error {
if b == nil {
return scanner.Scan(nil)
}
return scanner.Scan(b)
}
func scanBytes(b []byte) ([]byte, error) {
if len(b) < 2 {
return nil, internal.Errorf("pg: can't parse bytes: %q", b)
}
b = b[2:] // Trim off "\\x".
tmp := make([]byte, hex.DecodedLen(len(b)))
_, err := hex.Decode(tmp, b)
return tmp, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v5.1.3

搜索帮助