gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.79k stars 1.61k forks source link

database/gdb: Scan, Array, Value deserialization enhancement #3977

Open cyjaysong opened 4 days ago

cyjaysong commented 4 days ago

Description

目前Scan不支持将查询的某个字段结果直接反序列化至指定变量,如下:

案例表结构
type User struct {
    Id       int64
    Username string
    Balance  decimal.Decimal
        State    bool
}

1、
var  balance decimal.Decimal
err := db.Model("user").Fields("balance").Where("id", 1).Scan(&balance)
2、
var  totalBalance decimal.Decimal
err := db.Model("user").FieldSum("balance").WhereIn("id", []int64{1,2,3,4,5}).Scan(&totalBalance)
3、
var  username string
err := db.Model("user").Fields("username").Where("id", 1).Scan(&username)
4、
var ids []int64
err := db.Model("user").Fields("id").Where("state", true).Scan(&ids)

我尝试过封装ValueScan,但是当需要承接数据的变量为基本数据类型的变量时还是存在问题,所以我关闭了这个PR

var  balance decimal.Decimal
val, err := db.Model("user").Fields("balance").Where("id", 1).Value()
val.Scan(&balance)  //此时,balance 能够获取到正确的值

var  id int64
val, err := db.Model("user").Fields("id").Where("id", 1).Where("state", true).Value()
err = val.Scan(&id)  //此时,id 不能能获取到正确的值,err不为空,不支持基本数据类型

Additional

No response

github-actions[bot] commented 4 days ago

Hello @cyjaysong. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it! 你好 @cyjaysong。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。