2024-01-12 13:15:30 +08:00
|
|
|
|
package dm
|
2023-12-06 14:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
"fmt"
|
2024-01-12 13:15:30 +08:00
|
|
|
|
"mayfly-go/internal/db/dbm/dbi"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
"mayfly-go/pkg/logx"
|
2024-01-18 17:18:17 +08:00
|
|
|
|
"mayfly-go/pkg/utils/collx"
|
2024-01-23 04:08:02 +00:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2023-12-06 14:50:02 +08:00
|
|
|
|
"strings"
|
2024-01-05 05:31:32 +00:00
|
|
|
|
"time"
|
2023-12-07 11:48:17 +08:00
|
|
|
|
|
2024-01-24 17:01:17 +08:00
|
|
|
|
"github.com/kanzihuang/vitess/go/vt/sqlparser"
|
|
|
|
|
|
|
2023-12-07 11:48:17 +08:00
|
|
|
|
_ "gitee.com/chunanyong/dm"
|
2023-12-06 14:50:02 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type DMDialect struct {
|
2024-03-18 12:25:40 +08:00
|
|
|
|
dbi.DefaultDialect
|
2023-12-06 14:50:02 +08:00
|
|
|
|
|
2024-03-18 12:25:40 +08:00
|
|
|
|
dc *dbi.DbConn
|
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 (dd *DMDialect) BatchInsert(tx *sql.Tx, tableName string, columns []string, values [][]any, duplicateStrategy int) (int64, error) {
|
2024-01-05 05:31:32 +00:00
|
|
|
|
// 执行批量insert sql
|
|
|
|
|
|
// insert into "table_name" ("column1", "column2", ...) values (value1, value2, ...)
|
|
|
|
|
|
|
2024-03-01 04:03:03 +00:00
|
|
|
|
// 无需处理重复数据,直接执行批量insert
|
|
|
|
|
|
if duplicateStrategy == dbi.DuplicateStrategyNone || duplicateStrategy == 0 {
|
2024-03-15 13:31:53 +08:00
|
|
|
|
return dd.batchInsertSimple(tx, tableName, columns, values)
|
2024-03-01 04:03:03 +00:00
|
|
|
|
} else { // 执行MERGE INTO语句
|
2024-03-15 13:31:53 +08:00
|
|
|
|
return dd.batchInsertMerge(tx, tableName, columns, values)
|
2024-03-01 04:03:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 13:31:53 +08:00
|
|
|
|
func (dd *DMDialect) batchInsertSimple(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-03-15 09:01:51 +00:00
|
|
|
|
identityInsert := fmt.Sprintf("set identity_insert \"%s\" on;", tableName)
|
|
|
|
|
|
|
|
|
|
|
|
sqlTemp := fmt.Sprintf("%s insert into %s (%s) values %s", identityInsert, dd.dc.GetMetaData().QuoteIdentifier(tableName), strings.Join(columns, ","), placeholder)
|
2024-01-06 22:36:50 +08:00
|
|
|
|
effRows := 0
|
2024-03-15 09:01:51 +00:00
|
|
|
|
// 设置允许填充自增列之后,显示指定列名可以插入自增列
|
2024-01-05 05:31:32 +00:00
|
|
|
|
for _, value := range values {
|
|
|
|
|
|
// 达梦数据库只能一条条的执行insert
|
2024-03-01 04:03:03 +00:00
|
|
|
|
res, err := dd.dc.TxExec(tx, sqlTemp, value...)
|
2024-01-05 05:31:32 +00:00
|
|
|
|
if err != nil {
|
2024-03-01 04:03:03 +00:00
|
|
|
|
logx.Errorf("执行sql失败:%s, sql: [ %s ]", err.Error(), sqlTemp)
|
2024-03-15 09:01:51 +00:00
|
|
|
|
return 0, err
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
2024-03-01 04:03:03 +00:00
|
|
|
|
effRows += int(res)
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
// 执行批量insert sql
|
2024-01-06 22:36:50 +08:00
|
|
|
|
return int64(effRows), nil
|
2024-01-05 05:31:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 13:31:53 +08:00
|
|
|
|
func (dd *DMDialect) batchInsertMerge(tx *sql.Tx, tableName string, columns []string, values [][]any) (int64, error) {
|
2024-03-01 04:03:03 +00:00
|
|
|
|
// 查询主键字段
|
|
|
|
|
|
uniqueCols := make([]string, 0)
|
|
|
|
|
|
caseSqls := make([]string, 0)
|
2024-03-15 13:31:53 +08:00
|
|
|
|
metadata := dd.dc.GetMetaData()
|
2024-03-11 20:04:20 +08:00
|
|
|
|
tableCols, _ := metadata.GetColumns(tableName)
|
2024-03-01 04:03:03 +00:00
|
|
|
|
identityCols := make([]string, 0)
|
|
|
|
|
|
for _, col := range tableCols {
|
|
|
|
|
|
if col.IsPrimaryKey {
|
|
|
|
|
|
uniqueCols = append(uniqueCols, col.ColumnName)
|
2024-03-15 13:31:53 +08:00
|
|
|
|
caseSqls = append(caseSqls, fmt.Sprintf("( T1.%s = T2.%s )", metadata.QuoteIdentifier(col.ColumnName), metadata.QuoteIdentifier(col.ColumnName)))
|
2024-03-01 04:03:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
if col.IsIdentity {
|
|
|
|
|
|
// 自增字段不放入insert内,即使是设置了identity_insert on也不起作用
|
2024-03-15 13:31:53 +08:00
|
|
|
|
identityCols = append(identityCols, metadata.QuoteIdentifier(col.ColumnName))
|
2024-03-01 04:03:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 查询唯一索引涉及到的字段,并组装到match条件内
|
2024-03-11 20:04:20 +08:00
|
|
|
|
indexs, _ := metadata.GetTableIndex(tableName)
|
2024-03-01 04:03:03 +00:00
|
|
|
|
if indexs != nil {
|
|
|
|
|
|
for _, index := range indexs {
|
|
|
|
|
|
if index.IsUnique {
|
|
|
|
|
|
cols := strings.Split(index.ColumnName, ",")
|
|
|
|
|
|
tmp := make([]string, 0)
|
|
|
|
|
|
for _, col := range cols {
|
|
|
|
|
|
uniqueCols = append(uniqueCols, col)
|
2024-03-15 13:31:53 +08:00
|
|
|
|
tmp = append(tmp, fmt.Sprintf(" T1.%s = T2.%s ", metadata.QuoteIdentifier(col), metadata.QuoteIdentifier(col)))
|
2024-03-01 04:03:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
caseSqls = append(caseSqls, fmt.Sprintf("( %s )", strings.Join(tmp, " AND ")))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 重复数据处理策略
|
|
|
|
|
|
phs := make([]string, 0)
|
|
|
|
|
|
insertVals := make([]string, 0)
|
|
|
|
|
|
upds := make([]string, 0)
|
|
|
|
|
|
insertCols := make([]string, 0)
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
|
|
phs = append(phs, fmt.Sprintf("? %s", column))
|
2024-03-15 13:31:53 +08:00
|
|
|
|
if !collx.ArrayContains(uniqueCols, metadata.RemoveQuote(column)) {
|
2024-03-01 04:03:03 +00:00
|
|
|
|
upds = append(upds, fmt.Sprintf("T1.%s = T2.%s", column, column))
|
|
|
|
|
|
}
|
|
|
|
|
|
if !collx.ArrayContains(identityCols, column) {
|
|
|
|
|
|
insertCols = append(insertCols, fmt.Sprintf("%s", column))
|
|
|
|
|
|
insertVals = append(insertVals, fmt.Sprintf("T2.%s", column))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
t2s := make([]string, 0)
|
|
|
|
|
|
for i := 0; i < len(values); i++ {
|
|
|
|
|
|
t2s = append(t2s, fmt.Sprintf("SELECT %s FROM dual", strings.Join(phs, ",")))
|
|
|
|
|
|
}
|
|
|
|
|
|
t2 := strings.Join(t2s, " UNION ALL ")
|
|
|
|
|
|
|
2024-03-15 13:31:53 +08:00
|
|
|
|
sqlTemp := "MERGE INTO " + metadata.QuoteIdentifier(tableName) + " T1 USING (" + t2 + ") T2 ON " + strings.Join(caseSqls, " OR ")
|
2024-03-01 04:03:03 +00:00
|
|
|
|
sqlTemp += "WHEN NOT MATCHED THEN INSERT (" + strings.Join(insertCols, ",") + ") VALUES (" + strings.Join(insertVals, ",") + ")"
|
|
|
|
|
|
sqlTemp += "WHEN MATCHED THEN UPDATE SET " + strings.Join(upds, ",")
|
|
|
|
|
|
|
|
|
|
|
|
// 把二维数组转为一维数组
|
|
|
|
|
|
var args []any
|
|
|
|
|
|
for _, v := range values {
|
|
|
|
|
|
args = append(args, v...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return dd.dc.TxExec(tx, sqlTemp, args...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-23 04:08:02 +00:00
|
|
|
|
func (dd *DMDialect) CopyTable(copy *dbi.DbCopyTable) error {
|
|
|
|
|
|
tableName := copy.TableName
|
2024-03-15 13:31:53 +08:00
|
|
|
|
metadata := dd.dc.GetMetaData()
|
2024-03-11 20:04:20 +08:00
|
|
|
|
ddl, err := metadata.GetTableDDL(tableName)
|
2024-01-23 04:08:02 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 生成新表名,为老表明+_copy_时间戳
|
|
|
|
|
|
newTableName := tableName + "_copy_" + time.Now().Format("20060102150405")
|
|
|
|
|
|
|
|
|
|
|
|
// 替换新表名
|
|
|
|
|
|
ddl = strings.ReplaceAll(ddl, fmt.Sprintf("\"%s\"", strings.ToUpper(tableName)), fmt.Sprintf("\"%s\"", strings.ToUpper(newTableName)))
|
|
|
|
|
|
// 去除空格换行
|
|
|
|
|
|
ddl = stringx.TrimSpaceAndBr(ddl)
|
2024-03-15 13:31:53 +08:00
|
|
|
|
sqls, err := sqlparser.SplitStatementToPieces(ddl, sqlparser.WithDialect(dd.dc.GetMetaData().SqlParserDialect()))
|
2024-01-23 04:08:02 +00:00
|
|
|
|
for _, sql := range sqls {
|
|
|
|
|
|
_, _ = dd.dc.Exec(sql)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 复制数据
|
|
|
|
|
|
if copy.CopyData {
|
|
|
|
|
|
go func() {
|
2024-03-15 09:01:51 +00:00
|
|
|
|
// 设置允许填充自增列之后,显示指定列名可以插入自增列\
|
|
|
|
|
|
identityInsert := fmt.Sprintf("set identity_insert \"%s\" on", newTableName)
|
2024-01-23 04:08:02 +00:00
|
|
|
|
// 获取列名
|
2024-03-11 20:04:20 +08:00
|
|
|
|
columns, _ := metadata.GetColumns(tableName)
|
2024-01-23 04:08:02 +00:00
|
|
|
|
columnArr := make([]string, 0)
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
|
|
columnArr = append(columnArr, fmt.Sprintf("\"%s\"", column.ColumnName))
|
|
|
|
|
|
}
|
|
|
|
|
|
columnStr := strings.Join(columnArr, ",")
|
|
|
|
|
|
// 插入新数据并显示指定列
|
2024-03-15 09:01:51 +00:00
|
|
|
|
_, _ = dd.dc.Exec(fmt.Sprintf("%s insert into \"%s\" (%s) select %s from \"%s\"", identityInsert, newTableName, columnStr, columnStr, tableName))
|
2024-01-23 04:08:02 +00:00
|
|
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
}
|
2024-03-15 09:01:51 +00:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
func (dd *DMDialect) ToCommonColumn(dialectColumn *dbi.Column) {
|
|
|
|
|
|
// 翻译为通用数据库类型
|
|
|
|
|
|
dataType := dialectColumn.DataType
|
|
|
|
|
|
t1 := commonColumnTypeMap[string(dataType)]
|
|
|
|
|
|
if t1 == "" {
|
|
|
|
|
|
dialectColumn.DataType = dbi.CommonTypeVarchar
|
|
|
|
|
|
dialectColumn.CharMaxLength = 2000
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dialectColumn.DataType = t1
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
2024-03-21 03:35:18 +00:00
|
|
|
|
}
|
2024-03-15 09:01:51 +00:00
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
func (dd *DMDialect) ToColumn(commonColumn *dbi.Column) {
|
|
|
|
|
|
ctype := dmColumnTypeMap[commonColumn.DataType]
|
|
|
|
|
|
|
|
|
|
|
|
if ctype == "" {
|
|
|
|
|
|
commonColumn.DataType = "VARCHAR"
|
|
|
|
|
|
commonColumn.CharMaxLength = 2000
|
|
|
|
|
|
} else {
|
|
|
|
|
|
commonColumn.DataType = dbi.ColumnDataType(ctype)
|
|
|
|
|
|
// 如果是date,不设长度
|
|
|
|
|
|
if collx.ArrayAnyMatches([]string{"date", "time"}, strings.ToLower(ctype)) {
|
|
|
|
|
|
commonColumn.CharMaxLength = 0
|
|
|
|
|
|
commonColumn.NumPrecision = 0
|
|
|
|
|
|
} else
|
|
|
|
|
|
// 如果是char且长度未设置,则默认长度2000
|
|
|
|
|
|
if collx.ArrayAnyMatches([]string{"char"}, strings.ToLower(ctype)) && commonColumn.CharMaxLength == 0 {
|
|
|
|
|
|
commonColumn.CharMaxLength = 2000
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
func (dd *DMDialect) CreateTable(columns []dbi.Column, tableInfo dbi.Table, dropOldTable bool) (int, error) {
|
2024-03-15 09:01:51 +00:00
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
sqlArr := dd.dc.GetMetaData().GenerateTableDDL(columns, tableInfo, dropOldTable)
|
|
|
|
|
|
// 达梦需要分开执行sql
|
|
|
|
|
|
if len(sqlArr) > 0 {
|
|
|
|
|
|
for _, sqlStr := range sqlArr {
|
|
|
|
|
|
_, err := dd.dc.Exec(sqlStr)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 0, err
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-21 03:35:18 +00:00
|
|
|
|
return len(sqlArr), nil
|
2024-03-15 09:01:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (dd *DMDialect) CreateIndex(tableInfo dbi.Table, indexs []dbi.Index) error {
|
2024-03-21 03:35:18 +00:00
|
|
|
|
sqls := dd.dc.GetMetaData().GenerateIndexDDL(indexs, tableInfo)
|
2024-03-15 09:01:51 +00:00
|
|
|
|
_, err := dd.dc.Exec(strings.Join(sqls, ";"))
|
2024-01-23 04:08:02 +00:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2024-03-15 09:01:51 +00:00
|
|
|
|
|
|
|
|
|
|
func (dd *DMDialect) UpdateSequence(tableName string, columns []dbi.Column) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|