2024-03-11 20:04:20 +08:00
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"mayfly-go/internal/db/dbm/dbi"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
|
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-03-18 12:25:40 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2024-03-15 13:31:53 +08:00
|
|
|
|
"regexp"
|
2024-03-11 20:04:20 +08:00
|
|
|
|
"strings"
|
2024-03-15 13:31:53 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/kanzihuang/vitess/go/vt/sqlparser"
|
2024-03-11 20:04:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
2024-03-18 12:25:40 +08:00
|
|
|
|
MYSQL_META_FILE = "metasql/mysql_meta.sql"
|
|
|
|
|
|
MYSQL_DBS = "MYSQL_DBS"
|
|
|
|
|
|
MYSQL_TABLE_INFO_KEY = "MYSQL_TABLE_INFO"
|
|
|
|
|
|
MYSQL_INDEX_INFO_KEY = "MYSQL_INDEX_INFO"
|
|
|
|
|
|
MYSQL_COLUMN_MA_KEY = "MYSQL_COLUMN_MA"
|
2024-03-11 20:04:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type MysqlMetaData struct {
|
2024-03-15 13:31:53 +08:00
|
|
|
|
dbi.DefaultMetaData
|
|
|
|
|
|
|
2024-03-11 20:04:20 +08:00
|
|
|
|
dc *dbi.DbConn
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) GetDbServer() (*dbi.DbServer, error) {
|
|
|
|
|
|
_, res, err := md.dc.Query("SELECT VERSION() version")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
ds := &dbi.DbServer{
|
|
|
|
|
|
Version: anyx.ConvString(res[0]["version"]),
|
|
|
|
|
|
}
|
|
|
|
|
|
return ds, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) GetDbNames() ([]string, error) {
|
|
|
|
|
|
_, res, err := md.dc.Query(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_DBS))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
databases := make([]string, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
databases = append(databases, anyx.ConvString(re["dbname"]))
|
|
|
|
|
|
}
|
|
|
|
|
|
return databases, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 09:01:51 +00:00
|
|
|
|
func (md *MysqlMetaData) GetTables(tableNames ...string) ([]dbi.Table, error) {
|
|
|
|
|
|
meta := md.dc.GetMetaData()
|
|
|
|
|
|
names := strings.Join(collx.ArrayMap[string, string](tableNames, func(val string) string {
|
|
|
|
|
|
return fmt.Sprintf("'%s'", meta.RemoveQuote(val))
|
|
|
|
|
|
}), ",")
|
|
|
|
|
|
|
|
|
|
|
|
var res []map[string]any
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
sql, err := stringx.TemplateParse(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_TABLE_INFO_KEY), collx.M{"tableNames": names})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
_, res, err = md.dc.Query(sql)
|
2024-03-11 20:04:20 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tables := make([]dbi.Table, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
tables = append(tables, dbi.Table{
|
|
|
|
|
|
TableName: anyx.ConvString(re["tableName"]),
|
|
|
|
|
|
TableComment: anyx.ConvString(re["tableComment"]),
|
|
|
|
|
|
CreateTime: anyx.ConvString(re["createTime"]),
|
|
|
|
|
|
TableRows: anyx.ConvInt(re["tableRows"]),
|
|
|
|
|
|
DataLength: anyx.ConvInt64(re["dataLength"]),
|
|
|
|
|
|
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return tables, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取列元信息, 如列名等
|
|
|
|
|
|
func (md *MysqlMetaData) GetColumns(tableNames ...string) ([]dbi.Column, error) {
|
2024-03-15 09:01:51 +00:00
|
|
|
|
meta := md.dc.GetMetaData()
|
2024-03-11 20:04:20 +08:00
|
|
|
|
tableName := strings.Join(collx.ArrayMap[string, string](tableNames, func(val string) string {
|
2024-03-15 09:01:51 +00:00
|
|
|
|
return fmt.Sprintf("'%s'", meta.RemoveQuote(val))
|
2024-03-11 20:04:20 +08:00
|
|
|
|
}), ",")
|
|
|
|
|
|
|
|
|
|
|
|
_, res, err := md.dc.Query(fmt.Sprintf(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_COLUMN_MA_KEY), tableName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
columns := make([]dbi.Column, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
columns = append(columns, dbi.Column{
|
|
|
|
|
|
TableName: anyx.ConvString(re["tableName"]),
|
|
|
|
|
|
ColumnName: anyx.ConvString(re["columnName"]),
|
2024-03-15 09:01:51 +00:00
|
|
|
|
ColumnType: strings.Replace(anyx.ConvString(re["columnType"]), " unsigned", "", 1),
|
2024-03-18 12:25:40 +08:00
|
|
|
|
DataType: dbi.ColumnDataType(anyx.ConvString(re["dataType"])),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
ColumnComment: anyx.ConvString(re["columnComment"]),
|
|
|
|
|
|
Nullable: anyx.ConvString(re["nullable"]),
|
|
|
|
|
|
IsPrimaryKey: anyx.ConvInt(re["isPrimaryKey"]) == 1,
|
|
|
|
|
|
IsIdentity: anyx.ConvInt(re["isIdentity"]) == 1,
|
|
|
|
|
|
ColumnDefault: anyx.ConvString(re["columnDefault"]),
|
2024-03-18 12:25:40 +08:00
|
|
|
|
CharMaxLength: anyx.ConvInt(re["charMaxLength"]),
|
|
|
|
|
|
NumPrecision: anyx.ConvInt(re["numPrecision"]),
|
|
|
|
|
|
NumScale: anyx.ConvInt(re["numScale"]),
|
2024-03-11 20:04:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return columns, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表主键字段名,不存在主键标识则默认第一个字段
|
|
|
|
|
|
func (md *MysqlMetaData) GetPrimaryKey(tablename string) (string, error) {
|
|
|
|
|
|
columns, err := md.GetColumns(tablename)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(columns) == 0 {
|
|
|
|
|
|
return "", errorx.NewBiz("[%s] 表不存在", tablename)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range columns {
|
|
|
|
|
|
if v.IsPrimaryKey {
|
|
|
|
|
|
return v.ColumnName, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return columns[0].ColumnName, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表索引信息
|
|
|
|
|
|
func (md *MysqlMetaData) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
|
|
|
|
|
_, res, err := md.dc.Query(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_INDEX_INFO_KEY), tableName)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
indexs := make([]dbi.Index, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
indexs = append(indexs, dbi.Index{
|
|
|
|
|
|
IndexName: anyx.ConvString(re["indexName"]),
|
|
|
|
|
|
ColumnName: anyx.ConvString(re["columnName"]),
|
|
|
|
|
|
IndexType: anyx.ConvString(re["indexType"]),
|
|
|
|
|
|
IndexComment: anyx.ConvString(re["indexComment"]),
|
|
|
|
|
|
IsUnique: anyx.ConvInt(re["isUnique"]) == 1,
|
|
|
|
|
|
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
// 把查询结果以索引名分组,索引字段以逗号连接
|
|
|
|
|
|
result := make([]dbi.Index, 0)
|
|
|
|
|
|
key := ""
|
|
|
|
|
|
for _, v := range indexs {
|
|
|
|
|
|
// 当前的索引名
|
|
|
|
|
|
in := v.IndexName
|
|
|
|
|
|
if key == in {
|
|
|
|
|
|
// 索引字段已根据名称和顺序排序,故取最后一个即可
|
|
|
|
|
|
i := len(result) - 1
|
|
|
|
|
|
// 同索引字段以逗号连接
|
|
|
|
|
|
result[i].ColumnName = result[i].ColumnName + "," + v.ColumnName
|
|
|
|
|
|
} else {
|
|
|
|
|
|
key = in
|
|
|
|
|
|
result = append(result, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取建表ddl
|
|
|
|
|
|
func (md *MysqlMetaData) GetTableDDL(tableName string) (string, error) {
|
|
|
|
|
|
_, res, err := md.dc.Query(fmt.Sprintf("show create table `%s` ", tableName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
return anyx.ConvString(res[0]["Create Table"]) + ";", nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) GetSchemas() ([]string, error) {
|
|
|
|
|
|
return nil, errors.New("不支持schema")
|
|
|
|
|
|
}
|
2024-03-15 13:31:53 +08:00
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) GetIdentifierQuoteString() string {
|
|
|
|
|
|
return "`"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) QuoteLiteral(literal string) string {
|
|
|
|
|
|
literal = strings.ReplaceAll(literal, `\`, `\\`)
|
|
|
|
|
|
literal = strings.ReplaceAll(literal, `'`, `''`)
|
|
|
|
|
|
return "'" + literal + "'"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) SqlParserDialect() sqlparser.Dialect {
|
|
|
|
|
|
return sqlparser.MysqlDialect{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlMetaData) GetDataConverter() dbi.DataConverter {
|
|
|
|
|
|
return converter
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
// 数字类型
|
|
|
|
|
|
numberRegexp = regexp.MustCompile(`(?i)int|double|float|number|decimal|byte|bit`)
|
|
|
|
|
|
// 日期时间类型
|
|
|
|
|
|
datetimeRegexp = regexp.MustCompile(`(?i)datetime|timestamp`)
|
|
|
|
|
|
// 日期类型
|
|
|
|
|
|
dateRegexp = regexp.MustCompile(`(?i)date`)
|
|
|
|
|
|
// 时间类型
|
|
|
|
|
|
timeRegexp = regexp.MustCompile(`(?i)time`)
|
|
|
|
|
|
|
|
|
|
|
|
converter = new(DataConverter)
|
2024-03-15 09:01:51 +00:00
|
|
|
|
|
|
|
|
|
|
// mysql数据类型 映射 公共数据类型
|
2024-03-18 12:25:40 +08:00
|
|
|
|
commonColumnTypeMap = map[string]dbi.ColumnDataType{
|
2024-03-15 09:01:51 +00:00
|
|
|
|
"bigint": dbi.CommonTypeBigint,
|
|
|
|
|
|
"binary": dbi.CommonTypeBinary,
|
|
|
|
|
|
"blob": dbi.CommonTypeBlob,
|
|
|
|
|
|
"char": dbi.CommonTypeChar,
|
|
|
|
|
|
"datetime": dbi.CommonTypeDatetime,
|
|
|
|
|
|
"date": dbi.CommonTypeDate,
|
|
|
|
|
|
"decimal": dbi.CommonTypeNumber,
|
|
|
|
|
|
"double": dbi.CommonTypeNumber,
|
|
|
|
|
|
"enum": dbi.CommonTypeEnum,
|
|
|
|
|
|
"float": dbi.CommonTypeNumber,
|
|
|
|
|
|
"int": dbi.CommonTypeInt,
|
|
|
|
|
|
"json": dbi.CommonTypeJSON,
|
|
|
|
|
|
"longblob": dbi.CommonTypeLongblob,
|
|
|
|
|
|
"longtext": dbi.CommonTypeLongtext,
|
|
|
|
|
|
"mediumblob": dbi.CommonTypeBlob,
|
|
|
|
|
|
"mediumtext": dbi.CommonTypeText,
|
|
|
|
|
|
"set": dbi.CommonTypeVarchar,
|
|
|
|
|
|
"smallint": dbi.CommonTypeSmallint,
|
|
|
|
|
|
"text": dbi.CommonTypeText,
|
|
|
|
|
|
"time": dbi.CommonTypeTime,
|
|
|
|
|
|
"timestamp": dbi.CommonTypeTimestamp,
|
|
|
|
|
|
"tinyint": dbi.CommonTypeTinyint,
|
|
|
|
|
|
"varbinary": dbi.CommonTypeVarbinary,
|
|
|
|
|
|
"varchar": dbi.CommonTypeVarchar,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 公共数据类型 映射 mysql数据类型
|
2024-03-18 12:25:40 +08:00
|
|
|
|
mysqlColumnTypeMap = map[dbi.ColumnDataType]string{
|
2024-03-15 09:01:51 +00:00
|
|
|
|
dbi.CommonTypeVarchar: "varchar",
|
|
|
|
|
|
dbi.CommonTypeChar: "char",
|
|
|
|
|
|
dbi.CommonTypeText: "text",
|
|
|
|
|
|
dbi.CommonTypeBlob: "blob",
|
|
|
|
|
|
dbi.CommonTypeLongblob: "longblob",
|
|
|
|
|
|
dbi.CommonTypeLongtext: "longtext",
|
|
|
|
|
|
dbi.CommonTypeBinary: "binary",
|
|
|
|
|
|
dbi.CommonTypeMediumblob: "blob",
|
|
|
|
|
|
dbi.CommonTypeMediumtext: "text",
|
|
|
|
|
|
dbi.CommonTypeVarbinary: "varbinary",
|
|
|
|
|
|
dbi.CommonTypeInt: "int",
|
|
|
|
|
|
dbi.CommonTypeSmallint: "smallint",
|
|
|
|
|
|
dbi.CommonTypeTinyint: "tinyint",
|
|
|
|
|
|
dbi.CommonTypeNumber: "decimal",
|
|
|
|
|
|
dbi.CommonTypeBigint: "bigint",
|
|
|
|
|
|
dbi.CommonTypeDatetime: "datetime",
|
|
|
|
|
|
dbi.CommonTypeDate: "date",
|
|
|
|
|
|
dbi.CommonTypeTime: "time",
|
|
|
|
|
|
dbi.CommonTypeTimestamp: "timestamp",
|
|
|
|
|
|
dbi.CommonTypeEnum: "enum",
|
|
|
|
|
|
dbi.CommonTypeJSON: "json",
|
|
|
|
|
|
}
|
2024-03-15 13:31:53 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type DataConverter struct {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (dc *DataConverter) GetDataType(dbColumnType string) dbi.DataType {
|
|
|
|
|
|
if numberRegexp.MatchString(dbColumnType) {
|
|
|
|
|
|
return dbi.DataTypeNumber
|
|
|
|
|
|
}
|
|
|
|
|
|
// 日期时间类型
|
|
|
|
|
|
if datetimeRegexp.MatchString(dbColumnType) {
|
|
|
|
|
|
return dbi.DataTypeDateTime
|
|
|
|
|
|
}
|
|
|
|
|
|
// 日期类型
|
|
|
|
|
|
if dateRegexp.MatchString(dbColumnType) {
|
|
|
|
|
|
return dbi.DataTypeDate
|
|
|
|
|
|
}
|
|
|
|
|
|
// 时间类型
|
|
|
|
|
|
if timeRegexp.MatchString(dbColumnType) {
|
|
|
|
|
|
return dbi.DataTypeTime
|
|
|
|
|
|
}
|
|
|
|
|
|
return dbi.DataTypeString
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (dc *DataConverter) FormatData(dbColumnValue any, dataType dbi.DataType) string {
|
|
|
|
|
|
return anyx.ToString(dbColumnValue)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (dc *DataConverter) ParseData(dbColumnValue any, dataType dbi.DataType) any {
|
|
|
|
|
|
// 如果dataType是datetime而dbColumnValue是string类型,则需要转换为time.Time类型
|
|
|
|
|
|
_, ok := dbColumnValue.(string)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
if dataType == dbi.DataTypeDateTime {
|
|
|
|
|
|
res, _ := time.Parse(time.DateTime, anyx.ToString(dbColumnValue))
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
if dataType == dbi.DataTypeDate {
|
|
|
|
|
|
res, _ := time.Parse(time.DateOnly, anyx.ToString(dbColumnValue))
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
if dataType == dbi.DataTypeTime {
|
|
|
|
|
|
res, _ := time.Parse(time.TimeOnly, anyx.ToString(dbColumnValue))
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return dbColumnValue
|
|
|
|
|
|
}
|