2 Star 1 Fork 1

mosache/YFrame

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
unsafe.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
ヤ沒脩袮兲︶ 提交于 2023-06-19 21:43 . temp
package valuer
import (
"database/sql"
"gitee.com/mosache/YFrame/orm/internal/errs"
"gitee.com/mosache/YFrame/orm/schema"
"reflect"
"unsafe"
)
type unSafeValuer struct {
schema *schema.Schema
entity any // *T
address unsafe.Pointer
}
var _ Creator = NewUnsafeValuer
func NewUnsafeValuer(schema *schema.Schema, entity any) Valuer {
address := reflect.ValueOf(entity).UnsafePointer()
return &unSafeValuer{
schema: schema,
entity: entity,
address: address,
}
}
func (u *unSafeValuer) SetColumn(rows *sql.Rows) error {
var (
cs []string
err error
)
if cs, err = rows.Columns(); err != nil {
return err
}
vals := make([]any, 0)
for _, e := range cs {
if f, ok := u.schema.ColumnMap[e]; ok {
baseAddr := unsafe.Pointer(uintptr(u.address) + f.OffSet)
val := reflect.NewAt(f.Typ, baseAddr)
vals = append(vals, val.Interface())
} else {
return errs.NewErrUnknownColumn(e)
}
}
err = rows.Scan(vals...)
if err != nil {
return errs.ErrScanErr
}
return nil
}
func (u *unSafeValuer) Field(fieldName string) (any, error) {
fd, ok := u.schema.FieldMap[fieldName]
if !ok {
return nil, errs.NewErrUnknownField(fieldName)
}
bAddr := unsafe.Pointer(uintptr(u.address) + fd.OffSet)
val := reflect.NewAt(fd.Typ, bAddr).Elem().Interface()
return val, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mosache/YFrame.git
git@gitee.com:mosache/YFrame.git
mosache
YFrame
YFrame
v0.1.84

搜索帮助