add Tx struct and some method for tx

This commit is contained in:
zhengguanghao
2019-09-17 00:44:04 +08:00
parent 1aa5a780f3
commit ed78b8985d
2 changed files with 268 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
package sysmodule_test
import (
"fmt"
"sync"
"testing"
@@ -13,38 +14,76 @@ func TestDBModule(t *testing.T) {
db.ExitChan = make(chan bool)
db.WaitGroup = new(sync.WaitGroup)
db.Init(100, "192.168.0.5:3306", "root", "Root!!2018", "QuantFundsDB")
// db.Init(100, "192.168.0.5:3306", "root", "Root!!2018", "QuantFundsDB")
db.Init(100, "127.0.0.1:3306", "root", "zgh50221", "rebort_message")
db.OnInit()
res, err := db.QueryEx("select * from tbl_fun_heelthrow where id >= 1")
tx, err := db.GetTx()
if err != nil {
t.Error(err)
fmt.Println("err 1", err)
return
}
res, err := tx.QueryEx("select id as Id, info_type as InfoType, info_type_Name as InfoTypeName from tbl_info_type where id >= 1")
if err != nil {
fmt.Println("err 2", err)
tx.Rollback()
return
}
out := []struct {
Addtime int64 `json:"addtime"`
Tname string `json:"tname"`
Uuid string `json:"uuid,omitempty"`
AAAA string `json:"xxx"`
Id int64
InfoType string
InfoTypeName string
}{}
err = res.UnMarshal(&out)
if err != nil {
t.Error(err)
fmt.Println("err 3", err)
tx.Rollback()
return
}
sres := db.SyncQuery("select * from tbl_fun_heelthrow where id >= 1")
res, err = sres.Get(2000)
fmt.Println(out)
_, err = tx.Exec("insert into tbl_info_type(info_type, info_type_name) VALUES (?, ?)", "4", "weibo")
if err != nil {
t.Error(err)
fmt.Println("err 4", err)
tx.Rollback()
return
}
out2 := []struct {
Addtime int64 `json:"addtime"`
Tname string `json:"tname"`
Uuid string `json:"uuid,omitempty"`
AAAA string `json:"xxx"`
}{}
err = res.UnMarshal(&out2)
_, err = tx.Exec("update tbl_info_type set info_types = ? Where id = ?", "5", 0)
if err != nil {
t.Error(err)
fmt.Println("err 4", err)
tx.Rollback()
return
}
tx.Commit()
// res, err := db.QueryEx("select * from tbl_fun_heelthrow where id >= 1")
// if err != nil {
// t.Error(err)
// }
// out := []struct {
// Addtime int64 `json:"addtime"`
// Tname string `json:"tname"`
// Uuid string `json:"uuid,omitempty"`
// AAAA string `json:"xxx"`
// }{}
// err = res.UnMarshal(&out)
// if err != nil {
// t.Error(err)
// }
// sres := db.SyncQuery("select * from tbl_fun_heelthrow where id >= 1")
// res, err = sres.Get(2000)
// if err != nil {
// t.Error(err)
// }
// out2 := []struct {
// Addtime int64 `json:"addtime"`
// Tname string `json:"tname"`
// Uuid string `json:"uuid,omitempty"`
// AAAA string `json:"xxx"`
// }{}
// err = res.UnMarshal(&out2)
// if err != nil {
// t.Error(err)
// }
}