Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick v2 2.2311 #2408

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions sqle/driver/mysql/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysql

import (
"database/sql"
"encoding/hex"
"fmt"
"strconv"
"strings"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/pingcap/parser/ast"
_model "github.com/pingcap/parser/model"
parserMysql "github.com/pingcap/parser/mysql"
)

func (i *MysqlDriverImpl) GenerateRollbackSql(node ast.Node) (string, string, error) {
Expand Down Expand Up @@ -449,6 +451,12 @@ func (i *MysqlDriverImpl) generateInsertRollbackSql(stmt *ast.InsertStmt) (strin
return rollbackSql, "", nil
}

// 将二进制字段转化为十六进制字段
func getHexStrFromBytesStr(byteStr string) string {
encode := []byte(byteStr)
return hex.EncodeToString(encode)
}

// generateDeleteRollbackSql generate insert SQL for delete.
func (i *MysqlDriverImpl) generateDeleteRollbackSql(stmt *ast.DeleteStmt) (string, string, error) {
// not support multi-table syntax
Expand Down Expand Up @@ -497,8 +505,10 @@ func (i *MysqlDriverImpl) generateDeleteRollbackSql(stmt *ast.DeleteStmt) (strin
values := []string{}

columnsName := []string{}
colNameDefMap := make(map[string]*ast.ColumnDef)
for _, col := range createTableStmt.Cols {
columnsName = append(columnsName, col.Name.Name.String())
colNameDefMap[col.Name.Name.String()] = col
}
for _, record := range records {
if len(record) != len(columnsName) {
Expand All @@ -508,7 +518,13 @@ func (i *MysqlDriverImpl) generateDeleteRollbackSql(stmt *ast.DeleteStmt) (strin
for _, name := range columnsName {
v := "NULL"
if record[name].Valid {
v = fmt.Sprintf("'%s'", record[name].String)
colDef := colNameDefMap[name]
if parserMysql.HasBinaryFlag(colDef.Tp.Flag) {
hexStr := getHexStrFromBytesStr(record[name].String)
v = fmt.Sprintf("X'%s'", hexStr)
} else {
v = fmt.Sprintf("'%s'", record[name].String)
}
}
vs = append(vs, v)
}
Expand Down Expand Up @@ -581,13 +597,13 @@ func (i *MysqlDriverImpl) generateUpdateRollbackSql(stmt *ast.UpdateStmt) (strin
if err != nil {
return "", "", err
}
columnsName := []string{}
rollbackSql := ""
colNameDefMap := make(map[string]*ast.ColumnDef)
for _, col := range createTableStmt.Cols {
columnsName = append(columnsName, col.Name.Name.String())
colNameDefMap[col.Name.Name.String()] = col
}
for _, record := range records {
if len(record) != len(columnsName) {
if len(record) != len(colNameDefMap) {
return "", "", nil
}
where := []string{}
Expand All @@ -610,7 +626,13 @@ func (i *MysqlDriverImpl) generateUpdateRollbackSql(stmt *ast.UpdateStmt) (strin
name := col.Name.Name.O
v := "NULL"
if record[name].Valid {
v = fmt.Sprintf("'%s'", record[name].String)
colDef := colNameDefMap[name]
if parserMysql.HasBinaryFlag(colDef.Tp.Flag) {
hexStr := getHexStrFromBytesStr(record[name].String)
v = fmt.Sprintf("X'%s'", hexStr)
} else {
v = fmt.Sprintf("'%s'", record[name].String)
}
}

if colChanged {
Expand Down
2 changes: 1 addition & 1 deletion sqle/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Model struct {
func NewStorage(user, password, host, port, schema string, debug bool) (*Storage, error) {
log.Logger().Infof("connecting to storage, host: %s, port: %s, user: %s, schema: %s",
host, port, user, schema)
db, err := gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
db, err := gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
user, password, host, port, schema))
if err != nil {
log.Logger().Errorf("connect to storage failed, error: %v", err)
Expand Down
Loading