159 lines
3.9 KiB
Go
159 lines
3.9 KiB
Go
// Package database ...
|
|
//
|
|
// Description : mysql ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2022-05-15 19:32
|
|
package database
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/database/define"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// SystemDao 执行一些系统的mysql命令
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 19:32 2022/5/15
|
|
type SystemDao struct {
|
|
}
|
|
|
|
// 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{})
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
result map[string]any
|
|
)
|
|
|
|
if err = dbInstance.Raw("SHOW CREATE TABLE `" + table + "`").Scan(&result).Error; nil != err {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%v", result["Create Table"]), nil
|
|
}
|
|
|
|
// GetTableDesc 获取数据表的描述
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:37 2023/8/16
|
|
func (sd *SystemDao) GetTableDesc(dbInstance *gorm.DB, database string, tableName string) ([]*define.DescTableItem, error) {
|
|
var (
|
|
err error
|
|
result []*define.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) ([]*define.ColumnInfo, error) {
|
|
var (
|
|
list []*define.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
|
|
}
|
|
|
|
// 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 ...any) error {
|
|
return dbInstance.Exec(anySql, value...).Error
|
|
}
|