From e13e6cbca5ba1e8e69976c6362676fafdf13b190 Mon Sep 17 00:00:00 2001 From: huanghua_2017 <550329393@qq.com> Date: Fri, 1 Feb 2019 15:05:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E7=A0=81=E5=99=A8=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=88=B0=E6=95=B0=E6=8D=AE=E9=9B=86=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sysmodule/DBModule.go | 40 +++++++++++++------------------------- sysmodule/DBModule_test.go | 4 ++-- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/sysmodule/DBModule.go b/sysmodule/DBModule.go index d274c97..55c7ab1 100644 --- a/sysmodule/DBModule.go +++ b/sysmodule/DBModule.go @@ -26,6 +26,9 @@ type DBResult struct { LastInsertID int64 RowsAffected int64 res *sql.Rows + // 解码数据相关设置 + tag string + blur bool } // Next ... @@ -44,39 +47,24 @@ func (slf *DBResult) Scan(arg ...interface{}) error { return slf.res.Scan(arg...) } -// SQLDecoder ... -type SQLDecoder struct { - res DBResult - tag string - strict bool -} - -// NewSQLDecoder ... -func NewSQLDecoder(res DBResult) *SQLDecoder { - return &SQLDecoder{ - res: res, - tag: "col", - } -} - // SetSpecificTag ... -func (slf *SQLDecoder) SetSpecificTag(tag string) *SQLDecoder { +func (slf *DBResult) SetSpecificTag(tag string) *DBResult { slf.tag = tag return slf } -// SetStrictMode ... -func (slf *SQLDecoder) SetStrictMode(strict bool) *SQLDecoder { - slf.strict = strict +// SetBlurMode ... +func (slf *DBResult) SetBlurMode(blur bool) *DBResult { + slf.blur = blur return slf } // UnMarshal ... -func (slf *SQLDecoder) UnMarshal(out interface{}) error { - if slf.res.Err != nil { - return slf.res.Err +func (slf *DBResult) UnMarshal(out interface{}) error { + if slf.Err != nil { + return slf.Err } - tbm, err := dbResult2Map(slf.res.res) + tbm, err := dbResult2Map(slf.res) if err != nil { return err } @@ -123,7 +111,7 @@ func dbResult2Map(rows *sql.Rows) ([]map[string]string, error) { return tableData, nil } -func (slf *SQLDecoder) mapSingle2interface(m map[string]string, v reflect.Value) error { +func (slf *DBResult) mapSingle2interface(m map[string]string, v reflect.Value) error { t := v.Type() val := v.Elem() typ := t.Elem() @@ -144,7 +132,7 @@ func (slf *SQLDecoder) mapSingle2interface(m map[string]string, v reflect.Value) vtag := strings.Split(strings.ToLower(tag), ",") meta, ok := m[vtag[0]] if !ok { - if slf.strict { + if !slf.blur { return fmt.Errorf("没有在结果集中找到对应的字段 %s", tag) } continue @@ -190,7 +178,7 @@ func (slf *SQLDecoder) mapSingle2interface(m map[string]string, v reflect.Value) return nil } -func (slf *SQLDecoder) mapSlice2interface(data []map[string]string, in interface{}) error { +func (slf *DBResult) mapSlice2interface(data []map[string]string, in interface{}) error { length := len(data) if length > 0 { diff --git a/sysmodule/DBModule_test.go b/sysmodule/DBModule_test.go index de96e95..f7582a4 100644 --- a/sysmodule/DBModule_test.go +++ b/sysmodule/DBModule_test.go @@ -24,9 +24,9 @@ func TestDBModule(t *testing.T) { Addtime int64 `json:"addtime"` Tname string `json:"tname"` Uuid string `json:"uuid,omitempty"` - AAAA string `json:"-"` + AAAA string `json:"xxx"` }{} - err := sysmodule.NewSQLDecoder(res).SetSpecificTag("json").SetStrictMode(true).UnMarshal(&out) + err := res.SetSpecificTag("json").SetBlurMode(false).UnMarshal(&out) if err != nil { t.Error(err) }