2022-05-15 20:44:16 +08:00
|
|
|
// Package mysql ...
|
|
|
|
//
|
|
|
|
// Description : mysql ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-05-15 19:32
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2022-05-16 11:49:05 +08:00
|
|
|
"fmt"
|
2023-04-09 18:03:39 +08:00
|
|
|
"gorm.io/driver/mysql"
|
2022-05-16 11:49:05 +08:00
|
|
|
|
2022-05-15 20:44:16 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SystemDao 执行一些系统的mysql命令
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 19:32 2022/5/15
|
|
|
|
type SystemDao struct {
|
|
|
|
}
|
|
|
|
|
2023-04-09 18:03:39 +08:00
|
|
|
// Connect 连接数据库
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:58 2023/4/9
|
|
|
|
func (sd *SystemDao) Connect(host string, port string, user string, password string, database string) (*gorm.DB, error) {
|
|
|
|
// db, err := gorm.Open("mysql", "root:root@(localhost)/gorm-test?charset=utf8mb4&parseTime=True&loc=Local")
|
|
|
|
conn := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)
|
|
|
|
return gorm.Open(mysql.Open(conn), &gorm.Config{})
|
|
|
|
}
|
|
|
|
|
2023-04-09 17:51:04 +08:00
|
|
|
// SelectDatabase 切换数据库
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:44 2023/4/9
|
|
|
|
func (sd *SystemDao) SelectDatabase(dbInstance *gorm.DB, database string) error {
|
|
|
|
return dbInstance.Raw("USE " + database).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDatabaseList 获取数据库列表
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:46 2023/4/9
|
|
|
|
func (sd *SystemDao) GetDatabaseList(dbInstance *gorm.DB) ([]string, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
result []string
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = dbInstance.Raw("SHOW DATABASES").Scan(&result).Error; nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:44:16 +08:00
|
|
|
// GetTableList 查询数据表
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 19:36 2022/5/15
|
|
|
|
func (sd *SystemDao) GetTableList(dbInstance *gorm.DB) ([]string, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
result []string
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = dbInstance.Raw("SHOW TABLES").Scan(&result).Error; nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2022-05-15 21:29:39 +08:00
|
|
|
|
|
|
|
// GetCreateTableSQL 获取建表SQL
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:58 2022/5/15
|
|
|
|
func (sd *SystemDao) GetCreateTableSQL(dbInstance *gorm.DB, table string) (string, error) {
|
|
|
|
var (
|
|
|
|
err error
|
2022-05-16 11:49:05 +08:00
|
|
|
result map[string]interface{}
|
2022-05-15 21:29:39 +08:00
|
|
|
)
|
|
|
|
|
2022-05-16 11:49:05 +08:00
|
|
|
if err = dbInstance.Raw("SHOW CREATE TABLE `" + table + "`").Scan(&result).Error; nil != err {
|
2022-05-15 21:29:39 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2022-05-16 11:49:05 +08:00
|
|
|
return fmt.Sprintf("%v", result["Create Table"]), nil
|
2022-05-15 21:29:39 +08:00
|
|
|
}
|