mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
fix: mysql导出修复
This commit is contained in:
@@ -2,6 +2,7 @@ package application
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mayfly-go/internal/db/dbm/dbi"
|
"mayfly-go/internal/db/dbm/dbi"
|
||||||
"mayfly-go/internal/db/domain/entity"
|
"mayfly-go/internal/db/domain/entity"
|
||||||
@@ -326,6 +327,8 @@ func (app *dbTransferAppImpl) transfer2Target(taskId uint64, targetConn *dbi.DbC
|
|||||||
columnNames = append(columnNames, targetMeta.QuoteIdentifier(col.ColumnName))
|
columnNames = append(columnNames, targetMeta.QuoteIdentifier(col.ColumnName))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dataHelper := targetMeta.GetDataHelper()
|
||||||
|
|
||||||
// 从目标库数据中取出源库字段对应的值
|
// 从目标库数据中取出源库字段对应的值
|
||||||
values := make([][]any, 0)
|
values := make([][]any, 0)
|
||||||
for _, record := range result {
|
for _, record := range result {
|
||||||
@@ -342,6 +345,14 @@ func (app *dbTransferAppImpl) transfer2Target(taskId uint64, targetConn *dbi.DbC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dataHelper.GetDataType(string(tc.DataType)) == dbi.DataTypeBlob {
|
||||||
|
decodeBytes, err := hex.DecodeString(val.(string))
|
||||||
|
if err == nil {
|
||||||
|
val = decodeBytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rawValue = append(rawValue, val)
|
rawValue = append(rawValue, val)
|
||||||
}
|
}
|
||||||
values = append(values, rawValue)
|
values = append(values, rawValue)
|
||||||
|
|||||||
@@ -214,6 +214,9 @@ func valueConvert(data []byte, colType *sql.ColumnType) any {
|
|||||||
if strings.Contains(colDatabaseTypeName, "bit") {
|
if strings.Contains(colDatabaseTypeName, "bit") {
|
||||||
return data[0]
|
return data[0]
|
||||||
}
|
}
|
||||||
|
if colDatabaseTypeName == "blob" {
|
||||||
|
return fmt.Sprintf("%x", data)
|
||||||
|
}
|
||||||
|
|
||||||
// 这里把[]byte数据转成string
|
// 这里把[]byte数据转成string
|
||||||
stringV := string(data)
|
stringV := string(data)
|
||||||
|
|||||||
@@ -84,6 +84,13 @@ type Column struct {
|
|||||||
|
|
||||||
// 拼接数据类型与长度等。如varchar(2000),decimal(20,2)
|
// 拼接数据类型与长度等。如varchar(2000),decimal(20,2)
|
||||||
func (c *Column) GetColumnType() string {
|
func (c *Column) GetColumnType() string {
|
||||||
|
// 哪些mysql数据类型不需要添加字段长度
|
||||||
|
if collx.ArrayAnyMatches([]string{"int", "blob", "float", "double", "date", "year", "json"}, string(c.DataType)) {
|
||||||
|
return string(c.DataType)
|
||||||
|
}
|
||||||
|
if c.DataType == "timestamp" {
|
||||||
|
return "timestamp(6)"
|
||||||
|
}
|
||||||
if c.CharMaxLength > 0 {
|
if c.CharMaxLength > 0 {
|
||||||
return fmt.Sprintf("%s(%d)", c.DataType, c.CharMaxLength)
|
return fmt.Sprintf("%s(%d)", c.DataType, c.CharMaxLength)
|
||||||
}
|
}
|
||||||
@@ -124,6 +131,7 @@ const (
|
|||||||
CommonTypeVarbinary ColumnDataType = "varbinary"
|
CommonTypeVarbinary ColumnDataType = "varbinary"
|
||||||
|
|
||||||
CommonTypeInt ColumnDataType = "int"
|
CommonTypeInt ColumnDataType = "int"
|
||||||
|
CommonTypeBit ColumnDataType = "bit"
|
||||||
CommonTypeSmallint ColumnDataType = "smallint"
|
CommonTypeSmallint ColumnDataType = "smallint"
|
||||||
CommonTypeTinyint ColumnDataType = "tinyint"
|
CommonTypeTinyint ColumnDataType = "tinyint"
|
||||||
CommonTypeNumber ColumnDataType = "number"
|
CommonTypeNumber ColumnDataType = "number"
|
||||||
@@ -146,6 +154,7 @@ const (
|
|||||||
DataTypeDate DataType = "date"
|
DataTypeDate DataType = "date"
|
||||||
DataTypeTime DataType = "time"
|
DataTypeTime DataType = "time"
|
||||||
DataTypeDateTime DataType = "datetime"
|
DataTypeDateTime DataType = "datetime"
|
||||||
|
DataTypeBlob DataType = "blob"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 列数据处理帮助方法
|
// 列数据处理帮助方法
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ var (
|
|||||||
// 时间类型
|
// 时间类型
|
||||||
timeRegexp = regexp.MustCompile(`(?i)time`)
|
timeRegexp = regexp.MustCompile(`(?i)time`)
|
||||||
|
|
||||||
|
blobRegexp = regexp.MustCompile(`(?i)blob`)
|
||||||
|
|
||||||
// mysql数据类型 映射 公共数据类型
|
// mysql数据类型 映射 公共数据类型
|
||||||
commonColumnTypeMap = map[string]dbi.ColumnDataType{
|
commonColumnTypeMap = map[string]dbi.ColumnDataType{
|
||||||
"bigint": dbi.CommonTypeBigint,
|
"bigint": dbi.CommonTypeBigint,
|
||||||
@@ -37,6 +39,7 @@ var (
|
|||||||
"longtext": dbi.CommonTypeLongtext,
|
"longtext": dbi.CommonTypeLongtext,
|
||||||
"mediumblob": dbi.CommonTypeBlob,
|
"mediumblob": dbi.CommonTypeBlob,
|
||||||
"mediumtext": dbi.CommonTypeText,
|
"mediumtext": dbi.CommonTypeText,
|
||||||
|
"bit": dbi.CommonTypeBit,
|
||||||
"set": dbi.CommonTypeVarchar,
|
"set": dbi.CommonTypeVarchar,
|
||||||
"smallint": dbi.CommonTypeSmallint,
|
"smallint": dbi.CommonTypeSmallint,
|
||||||
"text": dbi.CommonTypeText,
|
"text": dbi.CommonTypeText,
|
||||||
@@ -60,6 +63,7 @@ var (
|
|||||||
dbi.CommonTypeMediumtext: "text",
|
dbi.CommonTypeMediumtext: "text",
|
||||||
dbi.CommonTypeVarbinary: "varbinary",
|
dbi.CommonTypeVarbinary: "varbinary",
|
||||||
dbi.CommonTypeInt: "int",
|
dbi.CommonTypeInt: "int",
|
||||||
|
dbi.CommonTypeBit: "bit",
|
||||||
dbi.CommonTypeSmallint: "smallint",
|
dbi.CommonTypeSmallint: "smallint",
|
||||||
dbi.CommonTypeTinyint: "tinyint",
|
dbi.CommonTypeTinyint: "tinyint",
|
||||||
dbi.CommonTypeNumber: "decimal",
|
dbi.CommonTypeNumber: "decimal",
|
||||||
@@ -92,6 +96,10 @@ func (dc *DataHelper) GetDataType(dbColumnType string) dbi.DataType {
|
|||||||
if timeRegexp.MatchString(dbColumnType) {
|
if timeRegexp.MatchString(dbColumnType) {
|
||||||
return dbi.DataTypeTime
|
return dbi.DataTypeTime
|
||||||
}
|
}
|
||||||
|
// blob类型
|
||||||
|
if blobRegexp.MatchString(dbColumnType) {
|
||||||
|
return dbi.DataTypeBlob
|
||||||
|
}
|
||||||
return dbi.DataTypeString
|
return dbi.DataTypeString
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,6 +165,8 @@ func (dc *DataHelper) WrapValue(dbColumnValue any, dataType dbi.DataType) string
|
|||||||
case dbi.DataTypeDate, dbi.DataTypeDateTime, dbi.DataTypeTime:
|
case dbi.DataTypeDate, dbi.DataTypeDateTime, dbi.DataTypeTime:
|
||||||
// mysql时间类型无需格式化
|
// mysql时间类型无需格式化
|
||||||
return fmt.Sprintf("'%s'", dbColumnValue)
|
return fmt.Sprintf("'%s'", dbColumnValue)
|
||||||
|
case dbi.DataTypeBlob:
|
||||||
|
return fmt.Sprintf("unhex('%s')", dbColumnValue)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("'%s'", dbColumnValue)
|
return fmt.Sprintf("'%s'", dbColumnValue)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user