From 5da7e422199a22450448e8d4a04c9e07124b888f Mon Sep 17 00:00:00 2001 From: mahdi Date: Sat, 2 Dec 2023 13:40:14 +0100 Subject: [PATCH] db - add SizeTable method --- db/db.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/db/db.go b/db/db.go index 8de203a..39c18fb 100644 --- a/db/db.go +++ b/db/db.go @@ -169,3 +169,31 @@ func (db *Db) SizeDb(dbname string) float32 { }) return rs.Size } + +type TableSize struct { + Table string + Size float32 +} + +func (db *Db) SizeTable(dbname string, table string) []TableSize { + rs := make([]TableSize, 1) + db.onDbExec("information_schema", func(sqlDb *sql.DB) bool { + query := "SELECT table_name AS `table`, round(((data_length + index_length) / 1024 / 1024), 2) `size' FROM TABLES WHERE table_schema = ? ORDER BY (data_length + index_length) DESC GROUP BY table_schema;" + rows, err := sqlDb.Query(query, dbname) + if err != nil { + fmt.Printf("cannot get table size of db %s : %v", dbname, err) + return false + } + defer rows.Close() + for rows.Next() { + ts := TableSize{Size: -1} + if err := rows.Scan(&ts.Table, &ts.Size); err != nil { + fmt.Printf("cannot get table size of db %s : %v", dbname, err) + return false + } + rs = append(rs, ts) + } + return true + }) + return rs +}