refactor:
- source 初始化数据 new 实现 - service mysql 与 pgsql 分开实现 - 代码备注优化
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/config"
|
||||
)
|
||||
|
||||
type InitDB struct {
|
||||
DBType string `json:"DBType"` // 数据库类型
|
||||
Host string `json:"host"` // 服务器地址
|
||||
@@ -8,3 +13,53 @@ type InitDB struct {
|
||||
Password string `json:"password"` // 数据库密码
|
||||
DBName string `json:"dbName" binding:"required"` // 数据库名
|
||||
}
|
||||
|
||||
// MysqlEmptyDsn msyql 空数据库 建库链接
|
||||
// Author SliverHorn
|
||||
func (i *InitDB) MysqlEmptyDsn() string {
|
||||
if i.Host == "" {
|
||||
i.Host = "127.0.0.1"
|
||||
}
|
||||
if i.Port == "" {
|
||||
i.Port = "3306"
|
||||
}
|
||||
return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port)
|
||||
}
|
||||
|
||||
// PgsqlEmptyDsn pgsql 空数据库 建库链接
|
||||
// Author SliverHorn
|
||||
func (i *InitDB) PgsqlEmptyDsn() string {
|
||||
if i.Host == "" {
|
||||
i.Host = "127.0.0.1"
|
||||
}
|
||||
if i.Port == "" {
|
||||
i.Port = "3306"
|
||||
}
|
||||
return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " " + "sslmode=disable TimeZone=Asia/Shanghai"
|
||||
}
|
||||
|
||||
// ToMysqlConfig 转换 config.Mysql
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (i *InitDB) ToMysqlConfig() config.Mysql {
|
||||
return config.Mysql{
|
||||
Path: i.Host,
|
||||
Port: i.Port,
|
||||
Dbname: i.DBName,
|
||||
Username: i.UserName,
|
||||
Password: i.Password,
|
||||
Config: "charset=utf8mb4&parseTime=True&loc=Local",
|
||||
}
|
||||
}
|
||||
|
||||
// ToPgsqlConfig 转换 config.Pgsql
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (i *InitDB) ToPgsqlConfig() config.Pgsql {
|
||||
return config.Pgsql{
|
||||
Path: i.Host,
|
||||
Port: i.Port,
|
||||
Dbname: i.DBName,
|
||||
Username: i.UserName,
|
||||
Password: i.Password,
|
||||
Config: "sslmode=disable TimeZone=Asia/Shanghai",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,79 @@
|
||||
package system
|
||||
|
||||
import "github.com/gookit/color"
|
||||
|
||||
type InitDBFunc interface {
|
||||
Init() (err error)
|
||||
}
|
||||
|
||||
const (
|
||||
Mysql = "mysql"
|
||||
Pgsql = "pgsql"
|
||||
InitSuccess = "\n[%v] --> 初始数据成功!\n"
|
||||
AuthorityMenu = "\n[%v] --> %v 视图已存在!\n"
|
||||
InitDataExist = "\n[%v] --> %v 表的初始数据已存在!\n"
|
||||
InitDataFailed = "\n[%v] --> %v 表初始数据失败! \nerr: %+v\n"
|
||||
InitDataSuccess = "\n[%v] --> %v 表初始数据成功!\n"
|
||||
)
|
||||
|
||||
type InitData interface {
|
||||
TableName() string
|
||||
Initialize() error
|
||||
CheckDataExist() bool
|
||||
}
|
||||
|
||||
// MysqlDataInitialize Mysql 初始化接口使用封装
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func MysqlDataInitialize(inits ...InitData) error {
|
||||
var entity SysMenu
|
||||
for i := 0; i < len(inits); i++ {
|
||||
if inits[i].TableName() == entity.TableName() {
|
||||
if k := inits[i].CheckDataExist(); k {
|
||||
color.Info.Printf(AuthorityMenu, Mysql, inits[i].TableName())
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if inits[i].CheckDataExist() {
|
||||
color.Info.Printf(InitDataExist, Mysql, inits[i].TableName())
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if err := inits[i].Initialize(); err != nil {
|
||||
color.Info.Printf(InitDataFailed, Mysql, err)
|
||||
continue
|
||||
} else {
|
||||
color.Info.Printf(InitDataSuccess, Mysql, inits[i].TableName())
|
||||
}
|
||||
}
|
||||
color.Info.Printf(InitSuccess, Mysql)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PgsqlDataInitialize Pgsql 初始化接口使用封装
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func PgsqlDataInitialize(inits ...InitData) error {
|
||||
var entity SysMenu
|
||||
for i := 0; i < len(inits); i++ {
|
||||
if inits[i].TableName() == entity.TableName() {
|
||||
if k := inits[i].CheckDataExist(); k {
|
||||
color.Info.Printf(AuthorityMenu, Pgsql, inits[i].TableName())
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if inits[i].CheckDataExist() {
|
||||
color.Info.Printf(InitDataExist, Pgsql, inits[i].TableName())
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if err := inits[i].Initialize(); err != nil {
|
||||
color.Info.Printf(InitDataFailed, Pgsql, err)
|
||||
continue
|
||||
} else {
|
||||
color.Info.Printf(InitDataSuccess, Pgsql, inits[i].TableName())
|
||||
}
|
||||
}
|
||||
color.Info.Printf(InitSuccess, Pgsql)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user