2024-01-12 13:15:30 +08:00
|
|
|
|
package mysql
|
2022-10-15 17:38:34 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2022-12-22 18:41:34 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"database/sql"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
"fmt"
|
2024-01-12 13:15:30 +08:00
|
|
|
|
"mayfly-go/internal/db/dbm/dbi"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2023-07-21 17:07:04 +08:00
|
|
|
|
"mayfly-go/pkg/utils/anyx"
|
2024-01-18 17:18:17 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
"regexp"
|
|
|
|
|
|
"strings"
|
2024-01-23 04:08:02 +00:00
|
|
|
|
"time"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
2023-05-24 12:32:17 +08:00
|
|
|
|
MYSQL_META_FILE = "metasql/mysql_meta.sql"
|
2023-12-26 22:31:51 +08:00
|
|
|
|
MYSQL_DBS = "MYSQL_DBS"
|
2023-05-24 12:32:17 +08:00
|
|
|
|
MYSQL_TABLE_INFO_KEY = "MYSQL_TABLE_INFO"
|
|
|
|
|
|
MYSQL_INDEX_INFO_KEY = "MYSQL_INDEX_INFO"
|
|
|
|
|
|
MYSQL_COLUMN_MA_KEY = "MYSQL_COLUMN_MA"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
type MysqlDialect struct {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
dc *dbi.DbConn
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (md *MysqlDialect) GetDbServer() (*dbi.DbServer, error) {
|
2023-12-20 17:29:16 +08:00
|
|
|
|
_, res, err := md.dc.Query("SELECT VERSION() version")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2024-01-12 13:15:30 +08:00
|
|
|
|
ds := &dbi.DbServer{
|
2023-12-20 17:29:16 +08:00
|
|
|
|
Version: anyx.ConvString(res[0]["version"]),
|
|
|
|
|
|
}
|
|
|
|
|
|
return ds, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (md *MysqlDialect) GetDbNames() ([]string, error) {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
_, res, err := md.dc.Query(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_DBS))
|
2023-11-26 21:21:35 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
databases := make([]string, 0)
|
|
|
|
|
|
for _, re := range res {
|
|
|
|
|
|
databases = append(databases, anyx.ConvString(re["dbname"]))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return databases, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-15 17:38:34 +08:00
|
|
|
|
// 获取表基础元信息, 如表名等
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (md *MysqlDialect) GetTables() ([]dbi.Table, error) {
|
|
|
|
|
|
_, res, err := md.dc.Query(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_TABLE_INFO_KEY))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
tables := make([]dbi.Table, 0)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, re := range res {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
tables = append(tables, dbi.Table{
|
2024-01-18 17:18:17 +08:00
|
|
|
|
TableName: anyx.ConvString(re["tableName"]),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
TableComment: anyx.ConvString(re["tableComment"]),
|
2023-11-02 12:46:21 +08:00
|
|
|
|
CreateTime: anyx.ConvString(re["createTime"]),
|
|
|
|
|
|
TableRows: anyx.ConvInt(re["tableRows"]),
|
|
|
|
|
|
DataLength: anyx.ConvInt64(re["dataLength"]),
|
|
|
|
|
|
IndexLength: anyx.ConvInt64(re["indexLength"]),
|
2023-05-24 12:32:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return tables, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取列元信息, 如列名等
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (md *MysqlDialect) GetColumns(tableNames ...string) ([]dbi.Column, error) {
|
2024-01-18 17:18:17 +08:00
|
|
|
|
dbType := md.dc.Info.Type
|
|
|
|
|
|
tableName := strings.Join(collx.ArrayMap[string, string](tableNames, func(val string) string {
|
|
|
|
|
|
return fmt.Sprintf("'%s'", dbType.RemoveQuote(val))
|
|
|
|
|
|
}), ",")
|
2023-05-24 12:32:17 +08:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
_, res, err := md.dc.Query(fmt.Sprintf(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_COLUMN_MA_KEY), tableName))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
columns := make([]dbi.Column, 0)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, re := range res {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
columns = append(columns, dbi.Column{
|
2024-01-18 17:18:17 +08:00
|
|
|
|
TableName: anyx.ConvString(re["tableName"]),
|
|
|
|
|
|
ColumnName: anyx.ConvString(re["columnName"]),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
ColumnType: anyx.ConvString(re["columnType"]),
|
|
|
|
|
|
ColumnComment: anyx.ConvString(re["columnComment"]),
|
|
|
|
|
|
Nullable: anyx.ConvString(re["nullable"]),
|
|
|
|
|
|
ColumnKey: anyx.ConvString(re["columnKey"]),
|
|
|
|
|
|
ColumnDefault: anyx.ConvString(re["columnDefault"]),
|
2023-11-23 10:36:20 +08:00
|
|
|
|
NumScale: anyx.ConvString(re["numScale"]),
|
2023-05-24 12:32:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return columns, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-23 20:48:37 +08:00
|
|
|
|
// 获取表主键字段名,不存在主键标识则默认第一个字段
|
2023-11-26 21:21:35 +08:00
|
|
|
|
func (md *MysqlDialect) GetPrimaryKey(tablename string) (string, error) {
|
|
|
|
|
|
columns, err := md.GetColumns(tablename)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(columns) == 0 {
|
|
|
|
|
|
return "", errorx.NewBiz("[%s] 表不存在", tablename)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-23 20:48:37 +08:00
|
|
|
|
for _, v := range columns {
|
2023-05-24 12:32:17 +08:00
|
|
|
|
if v.ColumnKey == "PRI" {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return v.ColumnName, nil
|
2022-11-23 20:48:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return columns[0].ColumnName, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取表索引信息
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (md *MysqlDialect) GetTableIndex(tableName string) ([]dbi.Index, error) {
|
|
|
|
|
|
_, res, err := md.dc.Query(dbi.GetLocalSql(MYSQL_META_FILE, MYSQL_INDEX_INFO_KEY), tableName)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
indexs := make([]dbi.Index, 0)
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, re := range res {
|
2024-01-12 13:15:30 +08:00
|
|
|
|
indexs = append(indexs, dbi.Index{
|
2024-01-18 17:18:17 +08:00
|
|
|
|
IndexName: anyx.ConvString(re["indexName"]),
|
2023-07-21 17:07:04 +08:00
|
|
|
|
ColumnName: anyx.ConvString(re["columnName"]),
|
|
|
|
|
|
IndexType: anyx.ConvString(re["indexType"]),
|
|
|
|
|
|
IndexComment: anyx.ConvString(re["indexComment"]),
|
|
|
|
|
|
NonUnique: anyx.ConvInt(re["nonUnique"]),
|
|
|
|
|
|
SeqInIndex: anyx.ConvInt(re["seqInIndex"]),
|
2023-05-24 12:32:17 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 把查询结果以索引名分组,索引字段以逗号连接
|
2024-01-12 13:15:30 +08:00
|
|
|
|
result := make([]dbi.Index, 0)
|
2022-10-29 20:08:15 +08:00
|
|
|
|
key := ""
|
2023-05-24 12:32:17 +08:00
|
|
|
|
for _, v := range indexs {
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 当前的索引名
|
2023-05-24 12:32:17 +08:00
|
|
|
|
in := v.IndexName
|
2022-10-29 20:08:15 +08:00
|
|
|
|
if key == in {
|
2023-03-19 14:49:12 +08:00
|
|
|
|
// 索引字段已根据名称和顺序排序,故取最后一个即可
|
|
|
|
|
|
i := len(result) - 1
|
2022-10-29 20:08:15 +08:00
|
|
|
|
// 同索引字段以逗号连接
|
2023-05-24 12:32:17 +08:00
|
|
|
|
result[i].ColumnName = result[i].ColumnName + "," + v.ColumnName
|
2022-10-29 20:08:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
key = in
|
|
|
|
|
|
result = append(result, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return result, nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取建表ddl
|
2023-12-20 17:29:16 +08:00
|
|
|
|
func (md *MysqlDialect) GetTableDDL(tableName string) (string, error) {
|
2023-11-26 21:21:35 +08:00
|
|
|
|
_, res, err := md.dc.Query(fmt.Sprintf("show create table `%s` ", tableName))
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
2024-01-18 17:18:17 +08:00
|
|
|
|
return anyx.ConvString(res[0]["Create Table"]) + ";", nil
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
2022-12-17 22:24:21 +08:00
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (md *MysqlDialect) WalkTableRecord(tableName string, walkFn dbi.WalkQueryRowsFunc) error {
|
2024-01-06 22:36:50 +08:00
|
|
|
|
return md.dc.WalkQueryRows(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walkFn)
|
2023-09-07 11:15:11 +08:00
|
|
|
|
}
|
2023-12-06 14:50:02 +08:00
|
|
|
|
|
2024-01-05 08:55:34 +08:00
|
|
|
|
func (md *MysqlDialect) GetSchemas() ([]string, error) {
|
2023-12-06 14:50:02 +08:00
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
2024-01-05 08:55:34 +08:00
|
|
|
|
|
|
|
|
|
|
// GetDbProgram 获取数据库程序模块,用于数据库备份与恢复
|
2024-01-12 13:15:30 +08:00
|
|
|
|
func (md *MysqlDialect) GetDbProgram() dbi.DbProgram {
|
2024-01-05 08:55:34 +08:00
|
|
|
|
return NewDbProgramMysql(md.dc)
|
|
|
|
|
|
}
|
2024-01-05 05:31:32 +00:00
|
|
|
|
|
2024-01-08 11:24:37 +08:00
|
|
|
|
func (md *MysqlDialect) BatchInsert(tx *sql.Tx, tableName string, columns []string, values [][]any) (int64, error) {
|
2024-01-06 22:36:50 +08:00
|
|
|
|
// 生成占位符字符串:如:(?,?)
|
|
|
|
|
|
// 重复字符串并用逗号连接
|
|
|
|
|
|
repeated := strings.Repeat("?,", len(columns))
|
|
|
|
|
|
// 去除最后一个逗号,占位符由括号包裹
|
|
|
|
|
|
placeholder := fmt.Sprintf("(%s)", strings.TrimSuffix(repeated, ","))
|
|
|
|
|
|
|
2024-01-05 05:31:32 +00:00
|
|
|
|
// 执行批量insert sql,mysql支持批量insert语法
|
|
|
|
|
|
// insert into table_name (column1, column2, ...) values (value1, value2, ...), (value1, value2, ...), ...
|
|
|
|
|
|
|
|
|
|
|
|
// 重复占位符字符串n遍
|
2024-01-06 22:36:50 +08:00
|
|
|
|
repeated = strings.Repeat(placeholder+",", len(values))
|
2024-01-05 05:31:32 +00:00
|
|
|
|
// 去除最后一个逗号
|
|
|
|
|
|
placeholder = strings.TrimSuffix(repeated, ",")
|
|
|
|
|
|
|
2024-01-11 12:35:44 +08:00
|
|
|
|
sqlStr := fmt.Sprintf("insert into %s (%s) values %s", md.dc.Info.Type.QuoteIdentifier(tableName), strings.Join(columns, ","), placeholder)
|
2024-01-05 05:31:32 +00:00
|
|
|
|
// 执行批量insert sql
|
|
|
|
|
|
// 把二维数组转为一维数组
|
|
|
|
|
|
var args []any
|
|
|
|
|
|
for _, v := range values {
|
|
|
|
|
|
args = append(args, v...)
|
|
|
|
|
|
}
|
2024-01-08 11:24:37 +08:00
|
|
|
|
return md.dc.TxExec(tx, sqlStr, args...)
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-24 08:29:16 +00:00
|
|
|
|
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`)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type DataConverter struct {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlDialect) GetDataConverter() dbi.DataConverter {
|
|
|
|
|
|
return new(DataConverter)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-01-05 05:31:32 +00:00
|
|
|
|
return dbColumnValue
|
|
|
|
|
|
}
|
2024-01-23 04:08:02 +00:00
|
|
|
|
|
|
|
|
|
|
func (md *MysqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
|
|
|
|
|
|
|
|
|
|
|
|
tableName := copy.TableName
|
|
|
|
|
|
|
|
|
|
|
|
// 生成新表名,为老表明+_copy_时间戳
|
|
|
|
|
|
newTableName := tableName + "_copy_" + time.Now().Format("20060102150405")
|
|
|
|
|
|
|
|
|
|
|
|
// 复制表结构创建表
|
|
|
|
|
|
_, err := md.dc.Exec(fmt.Sprintf("create table %s like %s", newTableName, tableName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 复制数据
|
|
|
|
|
|
if copy.CopyData {
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
_, _ = md.dc.Exec(fmt.Sprintf("insert into %s select * from %s", newTableName, tableName))
|
|
|
|
|
|
}()
|
|
|
|
|
|
}
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|