mirror of https://github.com/1Panel-dev/1Panel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.4 KiB
124 lines
3.4 KiB
2 years ago
|
package repo
|
||
|
|
||
|
import (
|
||
2 years ago
|
"context"
|
||
1 year ago
|
"fmt"
|
||
2 years ago
|
|
||
2 years ago
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||
|
"github.com/1Panel-dev/1Panel/backend/global"
|
||
1 year ago
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
||
2 years ago
|
"gorm.io/gorm"
|
||
2 years ago
|
)
|
||
|
|
||
|
type MysqlRepo struct{}
|
||
|
|
||
|
type IMysqlRepo interface {
|
||
2 years ago
|
Get(opts ...DBOption) (model.DatabaseMysql, error)
|
||
1 year ago
|
WithByMysqlName(mysqlName string) DBOption
|
||
1 year ago
|
WithByFrom(from string) DBOption
|
||
2 years ago
|
List(opts ...DBOption) ([]model.DatabaseMysql, error)
|
||
2 years ago
|
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
|
||
2 years ago
|
Create(ctx context.Context, mysql *model.DatabaseMysql) error
|
||
|
Delete(ctx context.Context, opts ...DBOption) error
|
||
2 years ago
|
Update(id uint, vars map[string]interface{}) error
|
||
1 year ago
|
DeleteLocal(ctx context.Context) error
|
||
2 years ago
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
1 year ago
|
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
|
||
2 years ago
|
return mysql, err
|
||
|
}
|
||
|
|
||
2 years ago
|
func (u *MysqlRepo) List(opts ...DBOption) ([]model.DatabaseMysql, error) {
|
||
1 year ago
|
var mysqls []model.DatabaseMysql
|
||
2 years ago
|
db := global.DB.Model(&model.DatabaseMysql{})
|
||
|
for _, opt := range opts {
|
||
|
db = opt(db)
|
||
|
}
|
||
1 year ago
|
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
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (u *MysqlRepo) Page(page, size int, opts ...DBOption) (int64, []model.DatabaseMysql, error) {
|
||
1 year ago
|
var mysqls []model.DatabaseMysql
|
||
2 years ago
|
db := global.DB.Model(&model.DatabaseMysql{})
|
||
|
for _, opt := range opts {
|
||
|
db = opt(db)
|
||
|
}
|
||
|
count := int64(0)
|
||
|
db = db.Count(&count)
|
||
1 year ago
|
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
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (u *MysqlRepo) Create(ctx context.Context, mysql *model.DatabaseMysql) error {
|
||
1 year ago
|
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
|
||
2 years ago
|
return getTx(ctx).Create(mysql).Error
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (u *MysqlRepo) Delete(ctx context.Context, opts ...DBOption) error {
|
||
|
return getTx(ctx, opts...).Delete(&model.DatabaseMysql{}).Error
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
1 year ago
|
func (u *MysqlRepo) DeleteLocal(ctx context.Context) error {
|
||
|
return getTx(ctx).Where("`from` = ?", "local").Delete(&model.DatabaseMysql{}).Error
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (u *MysqlRepo) Update(id uint, vars map[string]interface{}) error {
|
||
|
return global.DB.Model(&model.DatabaseMysql{}).Where("id = ?", id).Updates(vars).Error
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
func (u *MysqlRepo) WithByMysqlName(mysqlName string) DBOption {
|
||
2 years ago
|
return func(g *gorm.DB) *gorm.DB {
|
||
1 year ago
|
return g.Where("mysql_name = ?", mysqlName)
|
||
2 years ago
|
}
|
||
|
}
|
||
1 year ago
|
|
||
|
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
|
||
|
}
|
||
|
}
|