diff --git a/g/database/gdb/gdb_type_record.go b/g/database/gdb/gdb_type_record.go index fed0c80b5..a10878aec 100644 --- a/g/database/gdb/gdb_type_record.go +++ b/g/database/gdb/gdb_type_record.go @@ -10,6 +10,15 @@ import ( "gitee.com/johng/gf/g/util/gutil" ) +// 将Record转换为Map,其中最主要的区别是里面的键值被强制转换为string类型,方便json处理 +func (r Record) ToMap() Map { + m := make(map[string]interface{}) + for k, v := range r { + m[k] = v.String() + } + return m +} + // 将Map变量映射到指定的struct对象中,注意参数应当是一个对象的指针 func (r Record) ToStruct(obj interface{}) error { m := make(map[string]interface{}) diff --git a/g/database/gdb/gdb_type_result.go b/g/database/gdb/gdb_type_result.go index 37cd04cee..9dec2399f 100644 --- a/g/database/gdb/gdb_type_result.go +++ b/g/database/gdb/gdb_type_result.go @@ -10,8 +10,50 @@ import ( "gitee.com/johng/gf/g/util/gconv" ) +// 将结果集转换为List类型返回,便于json处理 +func (r Result) ToList() List { + l := make(List, len(r)) + for k, v := range r { + l[k] = v.ToMap() + } + return l +} + // 将结果列表按照指定的字段值做map[string]Map -func (r Result) ToStringMap(key string) map[string]Record { +func (r Result) ToStringMap(key string) map[string]Map { + m := make(map[string]Map) + for _, item := range r { + if v, ok := item[key]; ok { + m[gconv.String(v)] = item.ToMap() + } + } + return m +} + +// 将结果列表按照指定的字段值做map[int]Map +func (r Result) ToIntMap(key string) map[int]Map { + m := make(map[int]Map) + for _, item := range r { + if v, ok := item[key]; ok { + m[gconv.Int(v)] = item.ToMap() + } + } + return m +} + +// 将结果列表按照指定的字段值做map[uint]Map +func (r Result) ToUintMap(key string) map[uint]Map { + m := make(map[uint]Map) + for _, item := range r { + if v, ok := item[key]; ok { + m[gconv.Uint(v)] = item.ToMap() + } + } + return m +} + +// 将结果列表按照指定的字段值做map[string]Record +func (r Result) ToStringRecord(key string) map[string]Record { m := make(map[string]Record) for _, item := range r { if v, ok := item[key]; ok { @@ -21,8 +63,8 @@ func (r Result) ToStringMap(key string) map[string]Record { return m } -// 将结果列表按照指定的字段值做map[int]Map -func (r Result) ToIntMap(key string) map[int]Record { +// 将结果列表按照指定的字段值做map[int]Record +func (r Result) ToIntRecord(key string) map[int]Record { m := make(map[int]Record) for _, item := range r { if v, ok := item[key]; ok { @@ -32,8 +74,8 @@ func (r Result) ToIntMap(key string) map[int]Record { return m } -// 将结果列表按照指定的字段值做map[uint]Map -func (r Result) ToUintMap(key string) map[uint]Record { +// 将结果列表按照指定的字段值做map[uint]Record +func (r Result) ToUintRecord(key string) map[uint]Record { m := make(map[uint]Record) for _, item := range r { if v, ok := item[key]; ok { diff --git a/geg/database/mysql/mysql.go b/geg/database/mysql/mysql.go index ec090c63d..35fc56c7e 100644 --- a/geg/database/mysql/mysql.go +++ b/geg/database/mysql/mysql.go @@ -6,6 +6,7 @@ import ( "gitee.com/johng/gf/g/database/gdb" "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/frame/gins" + "gitee.com/johng/gf/g/encoding/gparser" ) // 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用 @@ -477,7 +478,10 @@ func mapToStruct() { } func main() { - mapToStruct() + r, _ := db.Table("user").Select() + j, _ := gparser.VarToJson(r.ToList()) + fmt.Println(string(j)) + fmt.Println(r) //create() //create() //insert() diff --git a/geg/frame/config.yml b/geg/frame/config.yml index 2cef71b90..550d322b1 100644 --- a/geg/frame/config.yml +++ b/geg/frame/config.yml @@ -6,7 +6,7 @@ database: - host: 127.0.0.1 port: 3306 user: root - pass: "8692651" + pass: "123456" name: test type: mysql role: master @@ -15,7 +15,7 @@ database: - host: 127.0.0.1 port: 3306 user: root - pass: "8692651" + pass: "123456" name: test type: mysql role: master