2023-04-18 18:25:39 +08:00
|
|
|
// Package database ...
|
2022-05-15 20:44:16 +08:00
|
|
|
//
|
|
|
|
// Description : mysql ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-05-15 19:32
|
2023-04-18 18:25:39 +08:00
|
|
|
package database
|
2022-05-15 20:44:16 +08:00
|
|
|
|
|
|
|
import (
|
2022-05-16 11:49:05 +08:00
|
|
|
"fmt"
|
2023-04-18 18:25:39 +08:00
|
|
|
|
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
|
|
|
}
|
2023-08-16 23:16:35 +08:00
|
|
|
|
|
|
|
// GetTableDesc 获取数据表的描述
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 22:37 2023/8/16
|
|
|
|
func (sd *SystemDao) GetTableDesc(dbInstance *gorm.DB, database string, tableName string) ([]*DescTableItem, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
result []*DescTableItem
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = dbInstance.Raw("DESC `" + tableName + "`").Scan(&result).Error; nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// 查询comment信息
|
|
|
|
tableInfo, _ := sd.GetTableInfo(dbInstance, database, tableName)
|
|
|
|
tableInfoTable := make(map[string]string)
|
|
|
|
for _, item := range tableInfo {
|
|
|
|
tableInfoTable[item.ColumnName] = item.ColumnComment
|
|
|
|
}
|
|
|
|
for _, item := range result {
|
|
|
|
item.Comment = tableInfoTable[item.Field]
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTableInfo 查询别熬结构
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 23:10 2023/8/16
|
|
|
|
func (sd *SystemDao) GetTableInfo(dbInstance *gorm.DB, database string, tableName string) ([]*ColumnInfo, error) {
|
|
|
|
var (
|
|
|
|
list []*ColumnInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
err = dbInstance.Table("INFORMATION_SCHEMA.COLUMNS").Where(map[string]string{
|
|
|
|
"TABLE_SCHEMA": database,
|
|
|
|
"TABLE_NAME": tableName,
|
|
|
|
}).Scan(&list).Error
|
|
|
|
return list, err
|
|
|
|
}
|
2023-09-30 16:39:11 +08:00
|
|
|
|
|
|
|
// CreateTable 创建数据表
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:34 2023/9/30
|
|
|
|
func (sd *SystemDao) CreateTable(dbInstance *gorm.DB, createTableSql string) error {
|
|
|
|
return sd.ExecuteSql(dbInstance, createTableSql)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteSql 执行任意sql语句
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:35 2023/9/30
|
|
|
|
func (sd *SystemDao) ExecuteSql(dbInstance *gorm.DB, anySql string, value ...interface{}) error {
|
|
|
|
return dbInstance.Exec(anySql, value...).Error
|
|
|
|
}
|