1Panel/backend/app/repo/database.go

95 lines
2.6 KiB
Go
Raw Normal View History

2023-08-29 10:50:15 +08:00
package repo
import (
"context"
2023-08-29 10:50:15 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
"gorm.io/gorm"
)
type DatabaseRepo struct{}
type IDatabaseRepo interface {
GetList(opts ...DBOption) ([]model.Database, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.Database, error)
Create(ctx context.Context, database *model.Database) error
2023-08-29 10:50:15 +08:00
Update(id uint, vars map[string]interface{}) error
Delete(ctx context.Context, opts ...DBOption) error
2023-08-29 10:50:15 +08:00
Get(opts ...DBOption) (model.Database, error)
WithByFrom(from string) DBOption
WithoutByFrom(from string) DBOption
WithByMysqlList() DBOption
WithAppInstallID(appInstallID uint) DBOption
2023-08-29 10:50:15 +08:00
}
func NewIDatabaseRepo() IDatabaseRepo {
return &DatabaseRepo{}
}
func (d *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) {
2023-08-29 10:50:15 +08:00
var database model.Database
db := global.DB
for _, opt := range opts {
db = opt(db)
}
err := db.First(&database).Error
return database, err
}
func (d *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Database, error) {
2023-08-29 10:50:15 +08:00
var users []model.Database
db := global.DB.Model(&model.Database{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
err := db.Limit(size).Offset(size * (page - 1)).Find(&users).Error
return count, users, err
}
func (d *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) {
2023-08-29 10:50:15 +08:00
var databases []model.Database
db := global.DB.Model(&model.Database{})
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&databases).Error
return databases, err
}
func (d *DatabaseRepo) WithByMysqlList() DBOption {
2023-08-29 10:50:15 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("type = ? OR type = ?", "mysql", "mariadb")
2023-08-29 10:50:15 +08:00
}
}
func (d *DatabaseRepo) WithByFrom(from string) DBOption {
2023-08-29 10:50:15 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("`from` = ?", from)
2023-08-29 10:50:15 +08:00
}
}
func (d *DatabaseRepo) WithoutByFrom(from string) DBOption {
2023-08-29 10:50:15 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("`from` != ?", from)
}
}
func (d *DatabaseRepo) WithAppInstallID(appInstallID uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("app_install_id = ?", appInstallID)
}
}
2023-08-29 10:50:15 +08:00
func (d *DatabaseRepo) Create(ctx context.Context, database *model.Database) error {
return getTx(ctx).Create(database).Error
2023-08-29 10:50:15 +08:00
}
func (d *DatabaseRepo) Update(id uint, vars map[string]interface{}) error {
2023-08-29 10:50:15 +08:00
return global.DB.Model(&model.Database{}).Where("id = ?", id).Updates(vars).Error
}
func (d *DatabaseRepo) Delete(ctx context.Context, opts ...DBOption) error {
return getTx(ctx, opts...).Delete(&model.Database{}).Error
2023-08-29 10:50:15 +08:00
}