1 Star 1 Fork 0

csingo/cDB

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Init.go 3.25 KB
一键复制 编辑 原始数据 按行查看 历史
joe 提交于 1年前 . update
package cDB
import (
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"gitee.com/csingo/cLog"
)
func WithContextModel(ctx *gin.Context, model ModelInterface) *gorm.DB {
name := model.Connection(ctx)
connection := container.Get(name).(*gorm.DB)
if connection == nil {
cLog.SetCallerSkip(2)
defer cLog.SetCallerSkip(1)
cLog.WithContext(ctx, map[string]any{
"source": "cDB.WithContextModel",
"connection": name,
}).Panic("database connection is not exists")
}
query := connection.WithContext(ctx).Model(model)
connector := container.GetConnector(name)
if connector == nil {
cLog.SetCallerSkip(2)
defer cLog.SetCallerSkip(1)
cLog.WithContext(ctx, map[string]any{
"source": "cDB.WithContextModel",
"connector": name,
}).Error("database connector is not exists")
return query
}
driver := connector.GetDriver()
if driver.IsDebug() {
query = query.Debug()
}
if tabler, ok := model.(schema.Tabler); ok {
table := tabler.TableName()
query = query.Table(table)
}
return query
}
func WithQueryModel(ctx *gin.Context, model ModelInterface) *QueryDB {
db := &QueryDB{}
query := WithContextModel(ctx, model)
db.SetQuery(query)
return db
}
func WithConnectionModel(ctx *gin.Context, model ModelInterface, name string) *gorm.DB {
connection := container.Get(name).(*gorm.DB)
if connection == nil {
cLog.SetCallerSkip(2)
defer cLog.SetCallerSkip(1)
cLog.WithContext(ctx, map[string]any{
"source": "cDB.WithConnectionModel",
"connection": name,
}).Panic("database connection is not exists")
}
query := connection.WithContext(ctx).Model(model)
connector := container.GetConnector(name)
if connector == nil {
cLog.SetCallerSkip(2)
defer cLog.SetCallerSkip(1)
cLog.WithContext(ctx, map[string]any{
"source": "cDB.WithConnectionModel",
"connector": name,
}).Error("database connector is not exists")
return query
}
driver := connector.GetDriver()
if driver.IsDebug() {
query = query.Debug()
}
return query
}
func AddConnection(ctx *gin.Context, name string, connection DriverInterface) error {
var connector ConnectorInterface
var err error
driver := connection.DriverName()
switch driver {
case MysqlDriver:
connector, err = NewMysqlConnector(name, connection.(*DatabaseConf_MySql))
case SqliteDriver:
connector, err = NewSqliteConnector(name, connection.(*DatabaseConf_Sqlite))
case ClickhouseDriver:
connector, err = NewClickhouseConnector(name, connection.(*DatabaseConf_Clickhouse))
}
if err != nil {
cLog.SetCallerSkip(2)
defer cLog.SetCallerSkip(1)
cLog.WithContext(ctx, map[string]any{
"source": "cDB.AddConnection",
"name": name,
"config": connection,
"err": err.Error(),
}).Error("数据库连接异常")
return err
}
if container.Is(connector) {
container.Save(connector)
}
return err
}
func GetConnection(ctx *gin.Context, name string) (*gorm.DB, error) {
db := container.Get(name)
if db == nil {
return nil, fmt.Errorf("connection is not exists")
}
return db.(*gorm.DB), nil
}
func GetConnectionWithModel(ctx *gin.Context, model ModelInterface) (*gorm.DB, error) {
name := model.Connection(ctx)
db := container.Get(name)
if db == nil {
return nil, fmt.Errorf("connection is not exists")
}
return db.(*gorm.DB), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/csingo/cDB.git
git@gitee.com:csingo/cDB.git
csingo
cDB
cDB
v0.4.7

搜索帮助