增加获取表结构的方法 + 单元测试

Author:  白茶清欢 <go_developer@163.com>
This commit is contained in:
白茶清欢 2022-05-16 11:49:05 +08:00
parent b2dcef0553
commit ffb2a4608a
2 changed files with 51 additions and 4 deletions

46
mysql_test.go Normal file
View File

@ -0,0 +1,46 @@
// Package mysql ...
//
// Description : mysql ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-05-16 11:23
package mysql
import (
"fmt"
"testing"
)
var (
testDBClient *DBClient
)
// getClient 获取数据库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:24 2022/5/16
func init() {
cfg := &DBConfig{
Host: "localhost",
Port: 3306,
Database: "gateway",
Username: "root",
Password: "zhangdeman",
Charset: "utf8mb4",
MaxOpenConnection: 100,
MaxIdleConnection: 100,
}
testDBClient, _ = NewDBClient(cfg, cfg, nil, nil, nil)
}
// TestGetCreateTableSQL 测试简表sql
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:24 2022/5/16
func TestGetCreateTableSQL(t *testing.T) {
sys := &SystemDao{}
fmt.Println(sys.GetCreateTableSQL(testDBClient.GetMaster(nil), "app"))
}

View File

@ -8,6 +8,8 @@
package mysql
import (
"fmt"
"gorm.io/gorm"
)
@ -44,12 +46,11 @@ func (sd *SystemDao) GetTableList(dbInstance *gorm.DB) ([]string, error) {
func (sd *SystemDao) GetCreateTableSQL(dbInstance *gorm.DB, table string) (string, error) {
var (
err error
result string
result map[string]interface{}
)
if err = dbInstance.Raw("SHOW CREATE TABLE " + table).Scan(&result).Error; nil != err {
err = dbInstance.Exec("SHOW CREATE TABLE `" + table + "`;").Row().Scan(&result)
if err = dbInstance.Raw("SHOW CREATE TABLE `" + table + "`").Scan(&result).Error; nil != err {
return "", err
}
return result, nil
return fmt.Sprintf("%v", result["Create Table"]), nil
}