2023-08-29 10:50:15 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2023-08-29 22:22:18 +08:00
|
|
|
"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)
|
2023-08-29 22:22:18 +08:00
|
|
|
Create(ctx context.Context, database *model.Database) error
|
2023-08-29 10:50:15 +08:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
2023-08-29 22:22:18 +08:00
|
|
|
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
|
2023-08-29 22:22:18 +08:00
|
|
|
WithAppInstallID(appInstallID uint) DBOption
|
2023-08-29 10:50:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDatabaseRepo() IDatabaseRepo {
|
|
|
|
return &DatabaseRepo{}
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +08:00
|
|
|
func (d *DatabaseRepo) WithByMysqlList() DBOption {
|
2023-08-29 10:50:15 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2023-08-29 22:22:18 +08:00
|
|
|
return g.Where("type = ? OR type = ?", "mysql", "mariadb")
|
2023-08-29 10:50:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +08:00
|
|
|
func (d *DatabaseRepo) WithByFrom(from string) DBOption {
|
2023-08-29 10:50:15 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2023-08-29 22:22:18 +08:00
|
|
|
return g.Where("`from` = ?", from)
|
2023-08-29 10:50:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +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)
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 22:22:18 +08:00
|
|
|
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
|
|
|
|
2023-08-29 22:22:18 +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
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +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
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:22:18 +08:00
|
|
|
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
|
|
|
}
|