房间麦克风热点计数
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

74 lines
918 B

package model
import (
"context"
"gorm.io/gorm"
)
type Tx struct {
db *gorm.DB
tx *gorm.DB
txs []*Tx
}
func NewTx(db *gorm.DB) *Tx {
return &Tx{
db: db,
}
}
func (t *Tx) Add(tx *Tx) {
t.txs = append(t.txs, tx)
}
func (t *Tx) Db(ctx context.Context) *gorm.DB {
var db *gorm.DB
if t.tx != nil {
db = t.tx
} else {
db = t.db
}
return db
}
func (t *Tx) GetTx() *gorm.DB {
return t.tx
}
func (t *Tx) SetTx(tx *Tx) *Tx {
t.tx = tx.GetTx()
tx.Add(t)
return tx
}
func (t *Tx) Begin(ctx context.Context) *Tx {
// 开启连的链接
tx := *t.db
t.tx = &tx
t.tx = t.tx.Begin()
return t
}
func (t *Tx) Rollback() {
if t.tx != nil {
t.tx.Rollback()
}
t.tx = nil
return
}
func (t *Tx) Commit() {
if t.tx != nil {
t.tx.Commit()
}
if len(t.txs) > 0 {
for _, tx := range t.txs {
//tx.GetTx().Commit()
tx.Close()
}
}
t.Close()
return
}
func (t *Tx) Close() {
t.tx = nil
}