2022-10-20 18:45:47 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-11-18 17:50:52 +08:00
|
|
|
"context"
|
2023-09-01 23:10:14 +08:00
|
|
|
"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"
|
2023-09-01 23:10:14 +08:00
|
|
|
"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 {
|
2022-10-24 18:46:19 +08:00
|
|
|
Get(opts ...DBOption) (model.DatabaseMysql, error)
|
2022-10-31 23:52:39 +08:00
|
|
|
WithByMysqlName(mysqlName string) DBOption
|
2023-07-24 14:55:26 +08:00
|
|
|
WithByFrom(from string) DBOption
|
2022-10-24 18:46:19 +08:00
|
|
|
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
|
2022-10-24 18:46:19 +08:00
|
|
|
Update(id uint, vars map[string]interface{}) error
|
2023-08-06 21:02:00 +08:00
|
|
|
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)
|
|
|
|
}
|
2023-09-01 23:10:14 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-24 18:46:19 +08:00
|
|
|
func (u *MysqlRepo) List(opts ...DBOption) ([]model.DatabaseMysql, error) {
|
2023-09-01 23:10:14 +08:00
|
|
|
var mysqls []model.DatabaseMysql
|
2022-10-24 18:46:19 +08:00
|
|
|
db := global.DB.Model(&model.DatabaseMysql{})
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
2023-09-01 23:10:14 +08:00
|
|
|
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-24 18:46:19 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
func (u *MysqlRepo) Page(page, size int, opts ...DBOption) (int64, []model.DatabaseMysql, error) {
|
2023-09-01 23:10:14 +08:00
|
|
|
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)
|
2023-09-01 23:10:14 +08:00
|
|
|
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 {
|
2023-09-01 23:10:14 +08:00
|
|
|
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
|
|
|
}
|
2022-10-24 18:46:19 +08:00
|
|
|
|
2023-08-06 21:02:00 +08:00
|
|
|
func (u *MysqlRepo) DeleteLocal(ctx context.Context) error {
|
|
|
|
return getTx(ctx).Where("`from` = ?", "local").Delete(&model.DatabaseMysql{}).Error
|
2023-03-14 18:17:38 +08:00
|
|
|
}
|
|
|
|
|
2022-10-24 18:46:19 +08:00
|
|
|
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 {
|
2022-10-31 23:52:39 +08:00
|
|
|
return g.Where("mysql_name = ?", mysqlName)
|
2022-10-25 18:34:33 +08:00
|
|
|
}
|
|
|
|
}
|
2023-07-24 14:55:26 +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
|
|
|
|
}
|
|
|
|
}
|