feat: 完成数据库设置界面

This commit is contained in:
ssongliu
2022-10-21 18:50:38 +08:00
committed by ssongliu
parent 2c529e8fa3
commit 7159e941aa
12 changed files with 686 additions and 38 deletions

View File

@@ -1,8 +1,13 @@
package service
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
@@ -13,6 +18,8 @@ type IMysqlService interface {
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
Create(mysqlDto dto.MysqlDBCreate) error
Delete(ids []uint) error
LoadStatus(version string) (*dto.MysqlStatus, error)
LoadConf(version string) (*dto.MysqlConf, error)
}
func NewIMysqlService() IMysqlService {
@@ -56,3 +63,63 @@ func (u *MysqlService) Delete(ids []uint) error {
}
return mysqlRepo.Delete(commonRepo.WithIdsIn(ids))
}
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
}