2023-10-27 17:41:45 +08:00
|
|
|
|
package dbm
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
2023-05-24 12:32:17 +08:00
|
|
|
|
import (
|
|
|
|
|
|
"embed"
|
|
|
|
|
|
"mayfly-go/pkg/biz"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2023-05-24 12:32:17 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 表信息
|
|
|
|
|
|
type Table struct {
|
|
|
|
|
|
TableName string `json:"tableName"` // 表名
|
|
|
|
|
|
TableComment string `json:"tableComment"` // 表备注
|
|
|
|
|
|
CreateTime string `json:"createTime"` // 创建时间
|
|
|
|
|
|
TableRows int `json:"tableRows"`
|
|
|
|
|
|
DataLength int64 `json:"dataLength"`
|
|
|
|
|
|
IndexLength int64 `json:"indexLength"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 表的列信息
|
|
|
|
|
|
type Column struct {
|
|
|
|
|
|
TableName string `json:"tableName"` // 表名
|
|
|
|
|
|
ColumnName string `json:"columnName"` // 列名
|
|
|
|
|
|
ColumnType string `json:"columnType"` // 列类型
|
|
|
|
|
|
ColumnComment string `json:"columnComment"` // 列备注
|
2023-11-23 10:36:20 +08:00
|
|
|
|
ColumnKey string `json:"columnKey"` // 是否为主键,逐渐的话值钱为PRI
|
|
|
|
|
|
ColumnDefault string `json:"columnDefault"` // 默认值
|
|
|
|
|
|
Nullable string `json:"nullable"` // 是否可为null
|
|
|
|
|
|
NumScale string `json:"numScale"` // 小数点
|
|
|
|
|
|
Extra string `json:"extra"` // 其他信息
|
2023-05-24 12:32:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 表索引信息
|
|
|
|
|
|
type Index struct {
|
|
|
|
|
|
IndexName string `json:"indexName"` // 索引名
|
|
|
|
|
|
ColumnName string `json:"columnName"` // 列名
|
|
|
|
|
|
IndexType string `json:"indexType"` // 索引类型
|
|
|
|
|
|
IndexComment string `json:"indexComment"` // 备注
|
|
|
|
|
|
SeqInIndex int `json:"seqInIndex"`
|
|
|
|
|
|
NonUnique int `json:"nonUnique"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-16 14:22:19 +08:00
|
|
|
|
// -----------------------------------元数据接口定义------------------------------------------
|
2023-11-26 21:21:35 +08:00
|
|
|
|
// 数据库方言、元信息接口(表、列、获取表数据等元信息)
|
|
|
|
|
|
type DbDialect interface {
|
|
|
|
|
|
// 获取数据库名称列表
|
|
|
|
|
|
GetDbNames() ([]string, error)
|
2022-10-16 14:22:19 +08:00
|
|
|
|
|
2023-11-02 12:46:21 +08:00
|
|
|
|
// 获取表信息
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetTables() ([]Table, error)
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取指定表名的所有列元信息
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetColumns(tableNames ...string) ([]Column, error)
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
2022-11-23 20:48:37 +08:00
|
|
|
|
// 获取表主键字段名,没有主键标识则默认第一个字段
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetPrimaryKey(tablename string) (string, error)
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取表索引信息
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetTableIndex(tableName string) ([]Index, error)
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取建表ddl
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetCreateTableDdl(tableName string) (string, error)
|
2022-12-17 22:24:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取指定表的数据-分页查询
|
|
|
|
|
|
// @return columns: 列字段名;result: 结果集;error: 错误
|
2023-06-01 12:31:32 +08:00
|
|
|
|
GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error)
|
2023-09-07 11:15:11 +08:00
|
|
|
|
|
|
|
|
|
|
// WalkTableRecord 遍历指定表的数据
|
|
|
|
|
|
WalkTableRecord(tableName string, walk func(record map[string]any, columns []string)) error
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
|
|
|
|
|
// ------------------------- 元数据sql操作 -------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
//go:embed metasql/*
|
|
|
|
|
|
var metasql embed.FS
|
|
|
|
|
|
|
|
|
|
|
|
// sql缓存 key: sql备注的key 如:MYSQL_TABLE_MA value: sql内容
|
|
|
|
|
|
var sqlCache = make(map[string]string, 20)
|
|
|
|
|
|
|
|
|
|
|
|
// 获取本地文件的sql内容,并进行解析,获取对应key的sql内容
|
|
|
|
|
|
func GetLocalSql(file, key string) string {
|
|
|
|
|
|
sql := sqlCache[key]
|
|
|
|
|
|
if sql != "" {
|
|
|
|
|
|
return sql
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bytes, err := metasql.ReadFile(file)
|
|
|
|
|
|
biz.ErrIsNilAppendErr(err, "获取sql meta文件内容失败: %s")
|
|
|
|
|
|
allSql := string(bytes)
|
|
|
|
|
|
|
2023-06-17 16:04:21 +08:00
|
|
|
|
sqls := strings.Split(allSql, "---------------------------------------")
|
2023-05-24 12:32:17 +08:00
|
|
|
|
var resSql string
|
|
|
|
|
|
for _, sql := range sqls {
|
2023-07-21 17:07:04 +08:00
|
|
|
|
sql = stringx.TrimSpaceAndBr(sql)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
// 获取sql第一行的sql备注信息如:--MYSQL_TABLE_MA 表信息元数据
|
|
|
|
|
|
info := strings.SplitN(sql, "\n", 2)
|
|
|
|
|
|
// 原始sql,即去除第一行的key与备注信息
|
|
|
|
|
|
rowSql := info[1]
|
|
|
|
|
|
// 获取sql key;如:MYSQL_TABLE_MA
|
|
|
|
|
|
sqlKey := strings.Split(strings.Split(info[0], " ")[0], "--")[1]
|
|
|
|
|
|
if key == sqlKey {
|
|
|
|
|
|
resSql = rowSql
|
|
|
|
|
|
}
|
|
|
|
|
|
sqlCache[sqlKey] = rowSql
|
|
|
|
|
|
}
|
|
|
|
|
|
return resSql
|
|
|
|
|
|
}
|