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
|
|
|
|
"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"
|
2024-03-15 09:01:51 +00:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
"strings"
|
2024-01-23 04:08:02 +00:00
|
|
|
|
"time"
|
2022-10-15 17:38:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-11-26 21:21:35 +08:00
|
|
|
|
type MysqlDialect struct {
|
2024-03-18 12:25:40 +08:00
|
|
|
|
dbi.DefaultDialect
|
|
|
|
|
|
|
2024-01-12 13:15:30 +08:00
|
|
|
|
dc *dbi.DbConn
|
2022-10-15 17:38:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-05 08:55:34 +08:00
|
|
|
|
// GetDbProgram 获取数据库程序模块,用于数据库备份与恢复
|
2024-02-06 07:16:56 +00:00
|
|
|
|
func (md *MysqlDialect) GetDbProgram() (dbi.DbProgram, error) {
|
|
|
|
|
|
return NewDbProgramMysql(md.dc), nil
|
2024-01-05 08:55:34 +08:00
|
|
|
|
}
|
2024-01-05 05:31:32 +00:00
|
|
|
|
|
2024-03-01 04:03:03 +00:00
|
|
|
|
func (md *MysqlDialect) BatchInsert(tx *sql.Tx, tableName string, columns []string, values [][]any, duplicateStrategy int) (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-03-01 04:03:03 +00:00
|
|
|
|
prefix := "insert into"
|
|
|
|
|
|
if duplicateStrategy == 1 {
|
|
|
|
|
|
prefix = "insert ignore into"
|
|
|
|
|
|
} else if duplicateStrategy == 2 {
|
|
|
|
|
|
prefix = "replace into"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 13:31:53 +08:00
|
|
|
|
sqlStr := fmt.Sprintf("%s %s (%s) values %s", prefix, md.dc.GetMetaData().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-03-15 09:01:51 +00:00
|
|
|
|
func (md *MysqlDialect) GetDataConverter() dbi.DataConverter {
|
|
|
|
|
|
return converter
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
2024-03-15 09:01:51 +00:00
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
func (md *MysqlDialect) ToCommonColumn(column *dbi.Column) {
|
|
|
|
|
|
dataType := column.DataType
|
|
|
|
|
|
|
|
|
|
|
|
t1 := commonColumnTypeMap[string(dataType)]
|
|
|
|
|
|
commonColumnType := dbi.CommonTypeVarchar
|
|
|
|
|
|
|
|
|
|
|
|
if t1 != "" {
|
|
|
|
|
|
commonColumnType = t1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
column.DataType = commonColumnType
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlDialect) ToColumn(column *dbi.Column) {
|
|
|
|
|
|
ctype := mysqlColumnTypeMap[column.DataType]
|
|
|
|
|
|
if ctype == "" {
|
|
|
|
|
|
column.DataType = "varchar"
|
|
|
|
|
|
column.CharMaxLength = 1000
|
|
|
|
|
|
} else {
|
|
|
|
|
|
column.DataType = dbi.ColumnDataType(ctype)
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
func (md *MysqlDialect) CreateTable(columns []dbi.Column, tableInfo dbi.Table, dropOldTable bool) (int, error) {
|
2024-03-15 09:01:51 +00:00
|
|
|
|
if dropOldTable {
|
|
|
|
|
|
_, _ = md.dc.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", tableInfo.TableName))
|
|
|
|
|
|
}
|
|
|
|
|
|
// 组装建表语句
|
|
|
|
|
|
createSql := fmt.Sprintf("CREATE TABLE %s (\n", tableInfo.TableName)
|
|
|
|
|
|
fields := make([]string, 0)
|
|
|
|
|
|
pks := make([]string, 0)
|
|
|
|
|
|
// 把通用类型转换为达梦类型
|
2024-03-18 12:25:40 +08:00
|
|
|
|
for _, column := range columns {
|
2024-03-15 09:01:51 +00:00
|
|
|
|
if column.IsPrimaryKey {
|
|
|
|
|
|
pks = append(pks, column.ColumnName)
|
|
|
|
|
|
}
|
|
|
|
|
|
fields = append(fields, md.genColumnBasicSql(column))
|
|
|
|
|
|
}
|
|
|
|
|
|
createSql += strings.Join(fields, ",")
|
|
|
|
|
|
if len(pks) > 0 {
|
|
|
|
|
|
createSql += fmt.Sprintf(", PRIMARY KEY (%s)", strings.Join(pks, ","))
|
|
|
|
|
|
}
|
2024-03-18 12:25:40 +08:00
|
|
|
|
createSql += ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 "
|
2024-03-15 09:01:51 +00:00
|
|
|
|
if tableInfo.TableComment != "" {
|
|
|
|
|
|
replacer := strings.NewReplacer(";", "", "'", "")
|
|
|
|
|
|
createSql += fmt.Sprintf(" COMMENT '%s'", replacer.Replace(tableInfo.TableComment))
|
|
|
|
|
|
}
|
|
|
|
|
|
_, err := md.dc.Exec(createSql)
|
|
|
|
|
|
|
|
|
|
|
|
return 1, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlDialect) genColumnBasicSql(column dbi.Column) string {
|
|
|
|
|
|
|
|
|
|
|
|
incr := ""
|
|
|
|
|
|
if column.IsIdentity {
|
|
|
|
|
|
incr = " AUTO_INCREMENT"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nullAble := ""
|
|
|
|
|
|
if column.Nullable == "NO" {
|
|
|
|
|
|
nullAble = " NOT NULL"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defVal := "" // 默认值需要判断引号,如函数是不需要引号的 // 为了防止跨源函数不支持 当默认值是函数时,不需要设置默认值
|
|
|
|
|
|
if column.ColumnDefault != "" && !strings.Contains(column.ColumnDefault, "(") {
|
|
|
|
|
|
// 哪些字段类型默认值需要加引号
|
|
|
|
|
|
mark := false
|
|
|
|
|
|
if collx.ArrayAnyMatches([]string{"char", "text", "date", "time", "lob"}, strings.ToLower(column.ColumnType)) {
|
|
|
|
|
|
// 当数据类型是日期时间,默认值是日期时间函数时,默认值不需要引号
|
|
|
|
|
|
if collx.ArrayAnyMatches([]string{"date", "time"}, strings.ToLower(column.ColumnType)) &&
|
|
|
|
|
|
collx.ArrayAnyMatches([]string{"DATE", "TIME"}, strings.ToUpper(column.ColumnDefault)) {
|
|
|
|
|
|
mark = false
|
|
|
|
|
|
} else {
|
|
|
|
|
|
mark = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if mark {
|
|
|
|
|
|
defVal = fmt.Sprintf(" DEFAULT '%s'", column.ColumnDefault)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
defVal = fmt.Sprintf(" DEFAULT %s", column.ColumnDefault)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
comment := ""
|
|
|
|
|
|
if column.ColumnComment != "" {
|
|
|
|
|
|
// 防止注释内含有特殊字符串导致sql出错
|
|
|
|
|
|
replacer := strings.NewReplacer(";", "", "'", "")
|
|
|
|
|
|
commentStr := replacer.Replace(column.ColumnComment)
|
|
|
|
|
|
comment = fmt.Sprintf(" COMMENT '%s'", commentStr)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
columnSql := fmt.Sprintf(" %s %s %s %s %s %s", md.dc.GetMetaData().QuoteIdentifier(column.ColumnName), column.GetColumnType(), nullAble, incr, defVal, comment)
|
2024-03-15 09:01:51 +00:00
|
|
|
|
return columnSql
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (md *MysqlDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) error {
|
|
|
|
|
|
meta := md.dc.GetMetaData()
|
|
|
|
|
|
for _, index := range indexs {
|
|
|
|
|
|
// 通过字段、表名拼接索引名
|
|
|
|
|
|
columnName := strings.ReplaceAll(index.ColumnName, "-", "")
|
|
|
|
|
|
columnName = strings.ReplaceAll(columnName, "_", "")
|
|
|
|
|
|
colName := strings.ReplaceAll(columnName, ",", "_")
|
|
|
|
|
|
|
|
|
|
|
|
keyType := "normal"
|
|
|
|
|
|
unique := ""
|
|
|
|
|
|
if index.IsUnique {
|
|
|
|
|
|
keyType = "unique"
|
|
|
|
|
|
unique = "unique"
|
|
|
|
|
|
}
|
|
|
|
|
|
indexName := fmt.Sprintf("%s_key_%s_%s", keyType, tableInfo.TableName, colName)
|
|
|
|
|
|
sqlTmp := "ALTER TABLE %s ADD %s INDEX %s(%s) USING BTREE COMMENT '%s'"
|
|
|
|
|
|
replacer := strings.NewReplacer(";", "", "'", "")
|
|
|
|
|
|
_, err := md.dc.Exec(fmt.Sprintf(sqlTmp, meta.QuoteIdentifier(tableInfo.TableName), unique, indexName, index.ColumnName, replacer.Replace(index.IndexComment)))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|