2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
datasource.go 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-04 11:45 . upd:目录结构调整
package model
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"gitee.com/linqwen/momo/app/conf/dao"
"gitee.com/linqwen/momo/base"
"gitee.com/linqwen/momo/cache"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func init() {
dao.ConfDb.AutoMigrate(&DataSourceEntity{})
InitBizDb()
}
type DataSourceEntity struct {
base.IdTimeStampsModel
base.GenericModel
base.SoftDeleteModel
Alias string `gorm:"column:alias;not null" json:"Alias" form:"Alias" comment:"Alias"`
UserName string `gorm:"column:user_name" json:"UserName" form:"UserName" comment:"UserName"`
Password string `gorm:"column:password" json:"Password" form:"Password" comment:"Password"`
Host string `gorm:"column:host;not null" json:"Host" form:"Host" comment:"Host"`
Port int `gorm:"column:port" json:"Port" form:"Port" comment:"Port"`
DriverClass string `gorm:"column:driver_class" json:"DriverClass" form:"DriverClass" comment:"DriverClass"`
DbName string `gorm:"column:db_name" json:"DbName" form:"DbName" comment:"DbName"`
}
func (DataSourceEntity) TableName() string { return "conf_datasource" }
func (s *DataSourceEntity) AfterSave(tx *gorm.DB) (err error) {
go InitBizDb()
return nil
}
func (s *DataSourceEntity) AfterDelete(tx *gorm.DB) (err error) {
go InitBizDb()
return nil
}
var mu sync.Mutex
type datasource struct {
DriverClass string
Alias string
Host string
DbName string
User string
}
func InitBizDb() {
time.Sleep(100 * time.Millisecond)
var objs []DataSourceEntity
var datasources = []datasource{}
err := dao.ConfDb.Where("status = ?", 1).Order("sort DESC").Find(&objs).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return
}
return
}
mu.Lock()
defer mu.Unlock()
for _, d := range objs {
ds := datasource{
DriverClass: d.DriverClass,
Alias: d.Alias,
Host: d.Host,
User: d.UserName,
DbName: d.DbName,
}
datasources = append(datasources, ds)
if d.DriverClass == "sqlite" {
db, err := gorm.Open(sqlite.Open(d.Host), &gorm.Config{})
if err != nil {
fmt.Printf("数据库初始化失败:%v\n", err.Error())
return
}
dao.BizDbs[d.Alias] = db
} else {
db, err := initDataSource(d.DriverClass, d.UserName, d.Password, d.Host, d.DbName, d.Port)
if err != nil {
return
}
dao.BizDbs[d.Alias] = db
}
}
cache.SetCache("datasource", datasources, time.Now().Add(365*24*time.Hour))
}
func initDataSource(driver, username, password, host, dbname string, port int) (*gorm.DB, error) {
var dsn string
var err error
var db *gorm.DB
switch strings.ToLower(driver) {
case "postgres", "postgresql":
if dbname == "" {
dbname = "data_test"
}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", host, username, password, dbname, port)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
case "mysql":
if dbname == "" {
dbname = "INFORMATION_SCHEMA"
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", username, password, host, port, dbname)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
default:
return nil, fmt.Errorf("unsupported database type: %s", dsn)
}
if err != nil {
return nil, err
}
return db, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.0.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385