From 158942e381f15bd0976e16d647faa71bf8071d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 24 Dec 2022 21:37:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85=E4=BA=8B=E5=8A=A1=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/base.go b/base.go index f0517f2..a979ed3 100644 --- a/base.go +++ b/base.go @@ -6,6 +6,7 @@ package mysql import ( + "errors" "gorm.io/gorm" ) @@ -63,6 +64,23 @@ func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...Set return cnt, dbInstance.Table(table).Count(&cnt).Error } +// Tx 执行事务 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:31 2022/12/24 +func (b *BaseDao) Tx(dbInstance *gorm.DB, txFunc func(dbInstance *gorm.DB) error) error { + if nil == dbInstance { + return errors.New("db instance is null") + } + tx := b.Begin(dbInstance) + if err := txFunc(tx); nil != err { + _ = b.Rollback(tx) + return err + } + return b.Commit(tx) +} + // Begin 开启事务 // // Author : go_developer@163.com<白茶清欢>