From cf6d693f76a260bf2dcc928b29fe91deb21ec6c4 Mon Sep 17 00:00:00 2001 From: yuanzhao <2206582181@qq.com> Date: Tue, 14 Mar 2023 14:04:16 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E8=BF=87=E6=BB=A4=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console/commands/orm/mysql.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/console/commands/orm/mysql.go b/console/commands/orm/mysql.go index ac8abaf..7054a40 100644 --- a/console/commands/orm/mysql.go +++ b/console/commands/orm/mysql.go @@ -10,6 +10,7 @@ import ( _ "github.com/go-sql-driver/mysql" "log" "os" + "strconv" "strings" "time" ) @@ -361,6 +362,8 @@ func (d *DB) GetDB() *sql.DB { return d.db } +// 获取所有表信息 +// 过滤分表信息, table_{1-9} 只返回table func (d *DB) tableColumns() map[string][]tableColumn { var sqlStr = `SELECT TABLE_CATALOG, @@ -446,7 +449,27 @@ ORDER BY tableColumns[col.TABLE_NAME] = append(tableColumns[col.TABLE_NAME], col) } - return tableColumns + return Filter(tableColumns) +} + +// Filter 过滤分表格式 +// table_{0-9} 只返回table +func Filter(tableColumns map[string][]tableColumn) map[string][]tableColumn { + got := make(map[string][]tableColumn) + for tableName, columns := range tableColumns { + arr := strings.Split(tableName, "_") + arrLen := len(arr) + if arrLen > 1 { + str := arr[arrLen-1] + _, err := strconv.Atoi(str) + if err == nil { + tableName = strings.ReplaceAll(tableName, "_"+str, "") + } + } + + got[tableName] = columns + } + return got } func (d *DB) tableIndex() map[string]map[string][]tableColumnIndex {