From ffb2a4608a69e7b2b572bb3a089eac321e38471a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 16 May 2022 11:49:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E8=A1=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E7=9A=84=E6=96=B9=E6=B3=95=20+=20=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Author: 白茶清欢 --- mysql_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ system.go | 9 +++++---- 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 mysql_test.go diff --git a/mysql_test.go b/mysql_test.go new file mode 100644 index 0000000..c293726 --- /dev/null +++ b/mysql_test.go @@ -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")) +} diff --git a/system.go b/system.go index 3b534ae..b656c16 100644 --- a/system.go +++ b/system.go @@ -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 }