1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-19 13:49:26 +08:00
1Panel/backend/app/service/database_mysql.go

126 lines
3.2 KiB
Go
Raw Normal View History

2022-10-20 18:45:47 +08:00
package service
import (
2022-10-21 18:50:38 +08:00
"database/sql"
"encoding/json"
"fmt"
2022-10-20 18:45:47 +08:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
2022-10-21 18:50:38 +08:00
_ "github.com/go-sql-driver/mysql"
2022-10-20 18:45:47 +08:00
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
type MysqlService struct{}
type IMysqlService interface {
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
Create(mysqlDto dto.MysqlDBCreate) error
Delete(ids []uint) error
2022-10-21 18:50:38 +08:00
LoadStatus(version string) (*dto.MysqlStatus, error)
LoadConf(version string) (*dto.MysqlConf, error)
2022-10-20 18:45:47 +08:00
}
func NewIMysqlService() IMysqlService {
return &MysqlService{}
}
func (u *MysqlService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) {
total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info))
var dtoMysqls []dto.MysqlDBInfo
for _, mysql := range mysqls {
var item dto.MysqlDBInfo
if err := copier.Copy(&item, &mysql); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
dtoMysqls = append(dtoMysqls, item)
}
return total, dtoMysqls, err
}
func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
mysql, _ := mysqlRepo.Get(commonRepo.WithByName(mysqlDto.Name))
if mysql.ID != 0 {
return constant.ErrRecordExist
}
if err := copier.Copy(&mysql, &mysqlDto); err != nil {
return errors.WithMessage(constant.ErrStructTransform, err.Error())
}
if err := mysqlRepo.Create(&mysql); err != nil {
return err
}
return nil
}
func (u *MysqlService) Delete(ids []uint) error {
if len(ids) == 1 {
mysql, _ := mysqlRepo.Get(commonRepo.WithByID(ids[0]))
if mysql.ID == 0 {
return constant.ErrRecordNotFound
}
return mysqlRepo.Delete(commonRepo.WithByID(ids[0]))
}
return mysqlRepo.Delete(commonRepo.WithIdsIn(ids))
}
2022-10-21 18:50:38 +08:00
func (u *MysqlService) LoadConf(version string) (*dto.MysqlConf, error) {
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
db, err := sql.Open("mysql", connArgs)
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("show variables")
if err != nil {
return nil, err
}
variableMap := make(map[string]string)
for rows.Next() {
var variableName, variableValue string
if err := rows.Scan(&variableName, &variableValue); err != nil {
continue
}
variableMap[variableName] = variableValue
}
var info dto.MysqlConf
arr, err := json.Marshal(variableMap)
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
return &info, nil
}
func (u *MysqlService) LoadStatus(version string) (*dto.MysqlStatus, error) {
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
db, err := sql.Open("mysql", connArgs)
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("show status")
if err != nil {
return nil, err
}
variableMap := make(map[string]string)
for rows.Next() {
var variableName, variableValue string
if err := rows.Scan(&variableName, &variableValue); err != nil {
continue
}
variableMap[variableName] = variableValue
}
var info dto.MysqlStatus
arr, err := json.Marshal(variableMap)
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
return &info, nil
}