mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-16 12:39:15 +08:00
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
"github.com/1Panel-dev/1Panel/backend/utils/mysql/client"
|
|
)
|
|
|
|
type MysqlClient interface {
|
|
Create(info client.CreateInfo) error
|
|
Delete(info client.DeleteInfo) error
|
|
|
|
ChangePassword(info client.PasswordChangeInfo) error
|
|
ChangeAccess(info client.AccessChangeInfo) error
|
|
|
|
Backup(info client.BackupInfo) error
|
|
Recover(info client.RecoverInfo) error
|
|
|
|
SyncDB(version string) ([]client.SyncDBInfo, error)
|
|
Close()
|
|
}
|
|
|
|
func NewMysqlClient(conn client.DBInfo) (MysqlClient, error) {
|
|
if conn.From == "local" {
|
|
connArgs := []string{"exec", conn.Address, "mysql", "-u" + conn.Username, "-p" + conn.Password, "-e"}
|
|
return client.NewLocal(connArgs, conn.Address, conn.Password, conn.Database), nil
|
|
}
|
|
|
|
if strings.Contains(conn.Address, ":") {
|
|
conn.Address = fmt.Sprintf("[%s]", conn.Address)
|
|
}
|
|
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", conn.Username, conn.Password, conn.Address, conn.Port)
|
|
db, err := sql.Open("mysql", connArgs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conn.Timeout)*time.Second)
|
|
defer cancel()
|
|
if err := db.PingContext(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return nil, buserr.New(constant.ErrExecTimeOut)
|
|
}
|
|
|
|
return client.NewRemote(client.Remote{
|
|
Client: db,
|
|
Database: conn.Database,
|
|
User: conn.Username,
|
|
Password: conn.Password,
|
|
Address: conn.Address,
|
|
Port: conn.Port,
|
|
}), nil
|
|
}
|