From 994b1a4286c55cc27b2cf9e832604e508f872ce2 Mon Sep 17 00:00:00 2001 From: bryan Date: Wed, 26 Jun 2024 18:37:58 +0800 Subject: [PATCH] save --- mdb.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 mdb.go diff --git a/mdb.go b/mdb.go new file mode 100644 index 0000000..1c941c1 --- /dev/null +++ b/mdb.go @@ -0,0 +1,41 @@ +type Config struct { + Host string `toml:"host"` //数据库名称 + Port int `toml:"port"` //数据库名称 + Debug bool `toml:"debug"` //调试开关(会在日志打印SQL) + Dbname string `toml:"dbname"` //数据库名称 + Username string `toml:"username"` //数据库用户名 + Password string `toml:"password"` //数据库连接密码 + ExtraParameters string `toml:"extra_parameters"` //数据库连接扩展参数 +} + +type Model struct { + DB *gorm.DB + DbTxStatus DbTxStatus + Redis redis.Conn + RedisPool *redis.Pool +} + +func NewModel() *Model { + return NewModelDefault() +} + +type DbTxStatus int + +const ( + DBTX_STATUS_TX_NONE DbTxStatus = 0 + DBTX_STATUS_TX_DOING DbTxStatus = 1 + DBTX_STATUS_TX_SUCCESS DbTxStatus = 2 + DBTX_STATUS_TX_FAILED DbTxStatus = 3 +) + +func (m *Model) Close() { + if m.Redis != nil { + m.Redis.Close() + } + if m.DB == nil { + m.DbTxStatus = DBTX_STATUS_TX_NONE + } else if m.DbTxStatus == DBTX_STATUS_TX_DOING { + m.DB.Rollback() + m.DbTxStatus = DBTX_STATUS_TX_FAILED + } +}