1 Star 1 Fork 0

bigbase/pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
scan.go 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
package struct_filter
import (
"database/sql"
"reflect"
"strconv"
"time"
"github.com/go-pg/pg/types"
)
var timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
var durationType = reflect.TypeOf((*time.Duration)(nil)).Elem()
var nullBoolType = reflect.TypeOf((*sql.NullBool)(nil)).Elem()
type ScanFunc func(v reflect.Value, values []string) error
func scanner(typ reflect.Type) ScanFunc {
switch typ {
case timeType:
return scanTime
case durationType:
return scanDuration
case nullBoolType:
return scanNullBool
}
switch typ.Kind() {
case reflect.Bool:
return scanBool
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return scanInt64
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return scanUint64
case reflect.Float32:
return scanFloat32
case reflect.Float64:
return scanFloat64
case reflect.String:
return scanString
}
return nil
}
func arrayScanner(typ reflect.Type) ScanFunc {
switch typ.Elem().Kind() {
case reflect.Int:
return scanIntSlice
case reflect.Int32:
return scanInt32Slice
case reflect.Int64:
return scanInt64Slice
case reflect.String:
return scanStringSlice
}
return nil
}
func scanBool(v reflect.Value, values []string) error {
f, err := strconv.ParseBool(values[0])
if err != nil {
return err
}
v.SetBool(f)
return nil
}
func scanInt64(v reflect.Value, values []string) error {
n, err := strconv.ParseInt(values[0], 10, 64)
if err != nil {
return err
}
v.SetInt(n)
return nil
}
func scanUint64(v reflect.Value, values []string) error {
n, err := strconv.ParseUint(values[0], 10, 64)
if err != nil {
return err
}
v.SetUint(n)
return nil
}
func scanFloat32(v reflect.Value, values []string) error {
return scanFloat(v, values, 32)
}
func scanFloat64(v reflect.Value, values []string) error {
return scanFloat(v, values, 64)
}
func scanFloat(v reflect.Value, values []string, bits int) error {
n, err := strconv.ParseFloat(values[0], bits)
if err != nil {
return err
}
v.SetFloat(n)
return nil
}
func scanString(v reflect.Value, values []string) error {
v.SetString(values[0])
return nil
}
func scanTime(v reflect.Value, values []string) error {
tm, err := ParseTime(values[0])
if err != nil {
return err
}
v.Set(reflect.ValueOf(tm))
return nil
}
func ParseTime(s string) (time.Time, error) {
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return time.Unix(n, 0), nil
}
return types.ParseTimeString(s)
}
func scanDuration(v reflect.Value, values []string) error {
dur, err := time.ParseDuration(values[0])
if err != nil {
return err
}
v.SetInt(int64(dur))
return nil
}
func scanNullBool(v reflect.Value, values []string) error {
value := sql.NullBool{}
s := values[0]
if s == "" {
v.Set(reflect.ValueOf(value))
return nil
}
f, err := strconv.ParseBool(values[0])
if err != nil {
return err
}
value.Valid = true
value.Bool = f
v.Set(reflect.ValueOf(value))
return nil
}
func scanIntSlice(v reflect.Value, values []string) error {
nn := make([]int, 0, len(values))
for _, s := range values {
n, err := strconv.Atoi(s)
if err != nil {
return err
}
nn = append(nn, n)
}
v.Set(reflect.ValueOf(nn))
return nil
}
func scanInt32Slice(v reflect.Value, values []string) error {
nn := make([]int32, 0, len(values))
for _, s := range values {
n, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return err
}
nn = append(nn, int32(n))
}
v.Set(reflect.ValueOf(nn))
return nil
}
func scanInt64Slice(v reflect.Value, values []string) error {
nn := make([]int64, 0, len(values))
for _, s := range values {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
nn = append(nn, n)
}
v.Set(reflect.ValueOf(nn))
return nil
}
func scanStringSlice(v reflect.Value, values []string) error {
v.Set(reflect.ValueOf(values))
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v7.1.0

搜索帮助