1 Star 0 Fork 0

danlansky/go-library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mysql_plugin.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
zhangminghua 提交于 2024-08-20 14:32 . feat:基础工具包
package mysqldb
import (
"gorm.io/gorm"
"time"
)
type MonMysqlPlugin struct {
DBName string
}
func (p MonMysqlPlugin) Name() string {
return "mysqlMonPlugin"
}
func (p MonMysqlPlugin) Initialize(db *gorm.DB) error {
_ = db.Callback().Create().Before("gorm:before_create").Register("monCallBackBeforeCreate", before)
_ = db.Callback().Query().Before("gorm:query").Register("monCallBackBeforeQuery", before)
_ = db.Callback().Delete().Before("gorm:before_delete").Register("monCallBackBeforeDelete", before)
_ = db.Callback().Update().Before("gorm:setup_reflect_value").Register("monCallBackBeforeUpdate", before)
_ = db.Callback().Row().Before("gorm:row").Register("monCallBackBeforeRow", before)
_ = db.Callback().Raw().Before("gorm:raw").Register("monCallBackBeforeRaw", before)
// 结束后
_ = db.Callback().Create().After("gorm:after_create").Register("monCallBackAfterCreate", getAfter(p.DBName))
_ = db.Callback().Query().After("gorm:after_query").Register("monCallBackAfterQuery", getAfter(p.DBName))
_ = db.Callback().Delete().After("gorm:after_delete").Register("monCallBackAfterDelete", getAfter(p.DBName))
_ = db.Callback().Update().After("gorm:after_update").Register("monCallBackAfterUpdate", getAfter(p.DBName))
_ = db.Callback().Row().After("gorm:row").Register("monCallBackAfterRow", getAfter(p.DBName))
_ = db.Callback().Raw().After("gorm:raw").Register("monCallBackAfterRaw", getAfter(p.DBName))
return nil
}
func before(db *gorm.DB) {
db.InstanceSet("start_time", time.Now())
}
func getAfter(dbName string) func(db *gorm.DB) {
return func(db *gorm.DB) {
_, isExist := db.InstanceGet("start_time")
if !isExist {
return
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/danlansky/go-library.git
git@gitee.com:danlansky/go-library.git
danlansky
go-library
go-library
v1.0.0

搜索帮助