mirror of
https://github.com/duanhf2012/origin.git
synced 2026-05-19 07:37:29 +08:00
Merge branch 'master' of https://github.com/duanhf2012/Origin
This commit is contained in:
@@ -26,6 +26,9 @@ type DBResult struct {
|
|||||||
LastInsertID int64
|
LastInsertID int64
|
||||||
RowsAffected int64
|
RowsAffected int64
|
||||||
res *sql.Rows
|
res *sql.Rows
|
||||||
|
// 解码数据相关设置
|
||||||
|
tag string
|
||||||
|
blur bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next ...
|
// Next ...
|
||||||
@@ -44,39 +47,24 @@ func (slf *DBResult) Scan(arg ...interface{}) error {
|
|||||||
return slf.res.Scan(arg...)
|
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 ...
|
// SetSpecificTag ...
|
||||||
func (slf *SQLDecoder) SetSpecificTag(tag string) *SQLDecoder {
|
func (slf *DBResult) SetSpecificTag(tag string) *DBResult {
|
||||||
slf.tag = tag
|
slf.tag = tag
|
||||||
return slf
|
return slf
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStrictMode ...
|
// SetBlurMode ...
|
||||||
func (slf *SQLDecoder) SetStrictMode(strict bool) *SQLDecoder {
|
func (slf *DBResult) SetBlurMode(blur bool) *DBResult {
|
||||||
slf.strict = strict
|
slf.blur = blur
|
||||||
return slf
|
return slf
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnMarshal ...
|
// UnMarshal ...
|
||||||
func (slf *SQLDecoder) UnMarshal(out interface{}) error {
|
func (slf *DBResult) UnMarshal(out interface{}) error {
|
||||||
if slf.res.Err != nil {
|
if slf.Err != nil {
|
||||||
return slf.res.Err
|
return slf.Err
|
||||||
}
|
}
|
||||||
tbm, err := dbResult2Map(slf.res.res)
|
tbm, err := dbResult2Map(slf.res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -123,7 +111,7 @@ func dbResult2Map(rows *sql.Rows) ([]map[string]string, error) {
|
|||||||
return tableData, nil
|
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()
|
t := v.Type()
|
||||||
val := v.Elem()
|
val := v.Elem()
|
||||||
typ := t.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), ",")
|
vtag := strings.Split(strings.ToLower(tag), ",")
|
||||||
meta, ok := m[vtag[0]]
|
meta, ok := m[vtag[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
if slf.strict {
|
if !slf.blur {
|
||||||
return fmt.Errorf("没有在结果集中找到对应的字段 %s", tag)
|
return fmt.Errorf("没有在结果集中找到对应的字段 %s", tag)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@@ -190,7 +178,7 @@ func (slf *SQLDecoder) mapSingle2interface(m map[string]string, v reflect.Value)
|
|||||||
return nil
|
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)
|
length := len(data)
|
||||||
|
|
||||||
if length > 0 {
|
if length > 0 {
|
||||||
@@ -218,7 +206,7 @@ func (slf *SQLDecoder) mapSlice2interface(data []map[string]string, in interface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect ...
|
// Connect ...
|
||||||
func (slf *DBModule) Connect() error {
|
func (slf *DBModule) Connect(maxConn int) error {
|
||||||
cmd := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true&loc=%s",
|
cmd := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true&loc=%s",
|
||||||
slf.UserName,
|
slf.UserName,
|
||||||
slf.Password,
|
slf.Password,
|
||||||
@@ -237,6 +225,10 @@ func (slf *DBModule) Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
slf.db = db
|
slf.db = db
|
||||||
|
db.SetMaxOpenConns(maxConn)
|
||||||
|
db.SetMaxIdleConns(maxConn)
|
||||||
|
db.SetConnMaxLifetime(time.Second * 90)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
39
sysmodule/DBModule_test.go
Normal file
39
sysmodule/DBModule_test.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package sysmodule_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duanhf2012/origin/sysmodule"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDBModule(t *testing.T) {
|
||||||
|
db := sysmodule.DBModule{
|
||||||
|
URL: "192.168.0.5:3306",
|
||||||
|
UserName: "root",
|
||||||
|
Password: "Root!!2018",
|
||||||
|
DBName: "QuantFundsDB",
|
||||||
|
}
|
||||||
|
db.Connect(100)
|
||||||
|
|
||||||
|
res := db.Query("select * from tbl_fun_heelthrow where id >= 1")
|
||||||
|
if res.Err != nil {
|
||||||
|
t.Error(res.Err)
|
||||||
|
}
|
||||||
|
out := []struct {
|
||||||
|
Addtime int64 `json:"addtime"`
|
||||||
|
Tname string `json:"tname"`
|
||||||
|
Uuid string `json:"uuid,omitempty"`
|
||||||
|
AAAA string `json:"xxx"`
|
||||||
|
}{}
|
||||||
|
err := res.SetSpecificTag("json").SetBlurMode(true).UnMarshal(&out)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sres := db.SyncQuery("select * from tbl_fun_heelthrow where id >= 1")
|
||||||
|
res = sres.Get(1000)
|
||||||
|
if res.Err != nil {
|
||||||
|
t.Error(res.Err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duanhf2012/origin/sysmodule"
|
"github.com/duanhf2012/origin/sysmodule"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHttpClientPoolModule(t *testing.T) {
|
func TestHttpClientPoolModule(t *testing.T) {
|
||||||
@@ -28,34 +27,3 @@ func TestHttpClientPoolModule(t *testing.T) {
|
|||||||
fmt.Println(rsp1.Status)
|
fmt.Println(rsp1.Status)
|
||||||
fmt.Println(string(rsp1.Body))
|
fmt.Println(string(rsp1.Body))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDBModule(t *testing.T) {
|
|
||||||
db := sysmodule.DBModule{
|
|
||||||
URL: "192.168.0.5:3306",
|
|
||||||
UserName: "root",
|
|
||||||
Password: "Root!!2018",
|
|
||||||
DBName: "QuantFundsDB",
|
|
||||||
}
|
|
||||||
db.Connect()
|
|
||||||
|
|
||||||
res := db.Query("select * from tbl_fun_heelthrow where id >= 1")
|
|
||||||
if res.Err != nil {
|
|
||||||
t.Error(res.Err)
|
|
||||||
}
|
|
||||||
out := []struct {
|
|
||||||
Addtime int64 `json:"addtime"`
|
|
||||||
Tname string `json:"tname"`
|
|
||||||
Uuid string `json:"uuid,omitempty"`
|
|
||||||
AAAA string `json:"-"`
|
|
||||||
}{}
|
|
||||||
err := sysmodule.NewSQLDecoder(res).SetSpecificTag("json").SetStrictMode(true).UnMarshal(&out)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sres := db.SyncQuery("select * from tbl_fun_heelthrow where id >= 1")
|
|
||||||
res = sres.Get(1000)
|
|
||||||
if res.Err != nil {
|
|
||||||
t.Error(res.Err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user