From cc42a572fe797039dc4342f6d5abbf93caa754e2 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 2 May 2018 18:52:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgdb.Result=E4=B8=8Egdb.List,?= =?UTF-8?q?=20gdb.Record=E4=B8=8Egdb.Map=E4=B9=8B=E9=97=B4=E7=9A=84?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=EF=BC=8C=E4=BE=BF=E4=BA=8E?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E5=B1=82=E6=95=B0=E6=8D=AE=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E5=A4=84=E7=90=86(=E5=A6=82json/xml)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/database/gdb/gdb_type_record.go | 9 ++++++ g/database/gdb/gdb_type_result.go | 52 ++++++++++++++++++++++++++++--- geg/database/mysql/mysql.go | 6 +++- geg/frame/config.yml | 4 +-- 4 files changed, 63 insertions(+), 8 deletions(-) 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