2021-05-08 18:00:33 +08:00
|
|
|
|
package entity
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2022-07-20 23:25:52 +08:00
|
|
|
|
"fmt"
|
2022-08-02 21:44:01 +08:00
|
|
|
|
"mayfly-go/internal/common/utils"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2021-05-08 18:00:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Db struct {
|
|
|
|
|
|
model.Model
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
Name string `orm:"column(name)" json:"name"`
|
|
|
|
|
|
Type string `orm:"column(type)" json:"type"` // 类型,mysql oracle等
|
|
|
|
|
|
Host string `orm:"column(host)" json:"host"`
|
|
|
|
|
|
Port int `orm:"column(port)" json:"port"`
|
|
|
|
|
|
Network string `orm:"column(network)" json:"network"`
|
|
|
|
|
|
Username string `orm:"column(username)" json:"username"`
|
|
|
|
|
|
Password string `orm:"column(password)" json:"-"`
|
|
|
|
|
|
Database string `orm:"column(database)" json:"database"`
|
2022-07-10 12:14:06 +08:00
|
|
|
|
Params string `json:"params"`
|
2021-07-28 18:03:19 +08:00
|
|
|
|
ProjectId uint64
|
|
|
|
|
|
Project string
|
|
|
|
|
|
EnvId uint64
|
|
|
|
|
|
Env string
|
2022-07-20 01:37:25 +00:00
|
|
|
|
|
2022-07-20 23:25:52 +08:00
|
|
|
|
EnableSshTunnel int8 `orm:"column(enable_ssh_tunnel)" json:"enableSshTunnel"` // 是否启用ssh隧道
|
|
|
|
|
|
SshTunnelMachineId uint64 `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
|
2021-05-08 18:00:33 +08:00
|
|
|
|
}
|
2022-07-20 23:25:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取数据库连接网络, 若没有使用ssh隧道,则直接返回。否则返回拼接的网络需要注册至指定dial
|
2022-08-02 21:44:01 +08:00
|
|
|
|
func (d *Db) GetNetwork() string {
|
2022-07-20 23:25:52 +08:00
|
|
|
|
network := d.Network
|
2022-08-02 21:44:01 +08:00
|
|
|
|
if d.EnableSshTunnel == 0 || d.EnableSshTunnel == -1 {
|
2022-07-20 23:25:52 +08:00
|
|
|
|
if network == "" {
|
|
|
|
|
|
return "tcp"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return network
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return fmt.Sprintf("%s+ssh:%d", d.Type, d.SshTunnelMachineId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-02 21:44:01 +08:00
|
|
|
|
func (d *Db) PwdEncrypt() {
|
|
|
|
|
|
// 密码替换为加密后的密码
|
|
|
|
|
|
d.Password = utils.PwdAesEncrypt(d.Password)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *Db) PwdDecrypt() {
|
|
|
|
|
|
// 密码替换为解密后的密码
|
|
|
|
|
|
d.Password = utils.PwdAesDecrypt(d.Password)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-20 23:25:52 +08:00
|
|
|
|
const (
|
|
|
|
|
|
DbTypeMysql = "mysql"
|
|
|
|
|
|
DbTypePostgres = "postgres"
|
|
|
|
|
|
)
|