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"
|
|
|
|
|
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 {
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|