gdb包增加gdb.GetStruct/gdb.Model.Struct方法,获取查询结果记录自动转换为指定对象

This commit is contained in:
John
2018-05-03 19:02:24 +08:00
parent c9e69dfc63
commit c4c6f2ece6
2 changed files with 20 additions and 1 deletions

View File

@ -102,7 +102,7 @@ func (db *Db) GetAll(query string, args ...interface{}) (Result, error) {
return records, nil
}
// 数据库查询,获取查询结果,以关联数组结构返回
// 数据库查询,获取查询结果记录,以关联数组结构返回
func (db *Db) GetOne(query string, args ...interface{}) (Record, error) {
list, err := db.GetAll(query, args ...)
if err != nil {
@ -114,6 +114,16 @@ func (db *Db) GetOne(query string, args ...interface{}) (Record, error) {
return nil, nil
}
// 数据库查询获取查询结果记录自动映射数据到给定的struct对象中
func (db *Db) GetStruct(obj interface{}, query string, args ...interface{}) error {
one, err := db.GetOne(query, args...)
if err != nil {
return err
}
return one.ToStruct(obj)
}
// 数据库查询,获取查询字段值
func (db *Db) GetValue(query string, args ...interface{}) (Value, error) {
one, err := db.GetOne(query, args ...)

View File

@ -275,6 +275,15 @@ func (md *Model) One() (Record, error) {
return nil, nil
}
// 链式操作查询单条记录并自动转换为struct对象
func (md *Model) Struct(obj interface{}) error {
one, err := md.One()
if err != nil {
return err
}
return one.ToStruct(obj)
}
// 链式操作,查询字段值
func (md *Model) Value() (Value, error) {
one, err := md.One()