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

124 lines
3.4 KiB
Go
Raw Normal View History

2022-10-20 18:45:47 +08:00
package repo
import (
2022-11-18 17:50:52 +08:00
"context"
"fmt"
2022-10-25 18:34:33 +08:00
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
2022-10-25 18:34:33 +08:00
"gorm.io/gorm"
2022-10-20 18:45:47 +08:00
)
type MysqlRepo struct{}
type IMysqlRepo interface {
Get(opts ...DBOption) (model.DatabaseMysql, error)
WithByMysqlName(mysqlName string) DBOption
WithByFrom(from string) DBOption
List(opts ...DBOption) ([]model.DatabaseMysql, error)
2022-10-20 18:45:47 +08:00
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
2022-11-18 17:50:52 +08:00
Create(ctx context.Context, mysql *model.DatabaseMysql) error
Delete(ctx context.Context, opts ...DBOption) error
Update(id uint, vars map[string]interface{}) error
DeleteLocal(ctx context.Context) error
2022-10-20 18:45:47 +08:00
}
func NewIMysqlRepo() IMysqlRepo {
return &MysqlRepo{}
}
func (u *MysqlRepo) Get(opts ...DBOption) (model.DatabaseMysql, error) {
var mysql model.DatabaseMysql
db := global.DB
for _, opt := range opts {
db = opt(db)
}
if err := db.First(&mysql).Error; err != nil {
return mysql, err
}
pass, err := encrypt.StringDecrypt(mysql.Password)
if err != nil {
global.LOG.Errorf("decrypt database db %s password failed, err: %v", mysql.Name, err)
}
mysql.Password = pass
2022-10-20 18:45:47 +08:00
return mysql, err
}
func (u *MysqlRepo) List(opts ...DBOption) ([]model.DatabaseMysql, error) {
var mysqls []model.DatabaseMysql
db := global.DB.Model(&model.DatabaseMysql{})
for _, opt := range opts {
db = opt(db)
}
if err := db.Find(&mysqls).Error; err != nil {
return mysqls, err
}
for i := 0; i < len(mysqls); i++ {
pass, err := encrypt.StringDecrypt(mysqls[i].Password)
if err != nil {
global.LOG.Errorf("decrypt database db %s password failed, err: %v", mysqls[i].Name, err)
}
mysqls[i].Password = pass
}
return mysqls, nil
}
2022-10-20 18:45:47 +08:00
func (u *MysqlRepo) Page(page, size int, opts ...DBOption) (int64, []model.DatabaseMysql, error) {
var mysqls []model.DatabaseMysql
2022-10-20 18:45:47 +08:00
db := global.DB.Model(&model.DatabaseMysql{})
for _, opt := range opts {
db = opt(db)
}
count := int64(0)
db = db.Count(&count)
if err := db.Limit(size).Offset(size * (page - 1)).Find(&mysqls).Error; err != nil {
return count, mysqls, err
}
for i := 0; i < len(mysqls); i++ {
pass, err := encrypt.StringDecrypt(mysqls[i].Password)
if err != nil {
global.LOG.Errorf("decrypt database db %s password failed, err: %v", mysqls[i].Name, err)
}
mysqls[i].Password = pass
}
return count, mysqls, nil
2022-10-20 18:45:47 +08:00
}
2022-11-18 17:50:52 +08:00
func (u *MysqlRepo) Create(ctx context.Context, mysql *model.DatabaseMysql) error {
pass, err := encrypt.StringEncrypt(mysql.Password)
if err != nil {
return fmt.Errorf("decrypt database db %s password failed, err: %v", mysql.Name, err)
}
mysql.Password = pass
2022-11-18 17:50:52 +08:00
return getTx(ctx).Create(mysql).Error
2022-10-20 18:45:47 +08:00
}
2022-11-18 17:50:52 +08:00
func (u *MysqlRepo) Delete(ctx context.Context, opts ...DBOption) error {
return getTx(ctx, opts...).Delete(&model.DatabaseMysql{}).Error
2022-10-20 18:45:47 +08:00
}
func (u *MysqlRepo) DeleteLocal(ctx context.Context) error {
return getTx(ctx).Where("`from` = ?", "local").Delete(&model.DatabaseMysql{}).Error
}
func (u *MysqlRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.DatabaseMysql{}).Where("id = ?", id).Updates(vars).Error
}
2022-10-25 18:34:33 +08:00
2022-11-18 17:50:52 +08:00
func (u *MysqlRepo) WithByMysqlName(mysqlName string) DBOption {
2022-10-25 18:34:33 +08:00
return func(g *gorm.DB) *gorm.DB {
return g.Where("mysql_name = ?", mysqlName)
2022-10-25 18:34:33 +08:00
}
}
func (u *MysqlRepo) WithByFrom(from string) DBOption {
return func(g *gorm.DB) *gorm.DB {
if len(from) != 0 {
return g.Where("`from` = ?", from)
}
return g
}
}