Skip to content

Commit

Permalink
fix: mysql insert on update duplicate sensitive case not matched (#700)
Browse files Browse the repository at this point in the history
Signed-off-by: LeeHao <[email protected]>
  • Loading branch information
ForestLH committed Feb 6, 2025
1 parent 1bf4369 commit f902463
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/datasource/sql/exec/at/insert_on_update_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func (i *insertOnUpdateExecutor) buildBeforeImageSQL(insertStmt *ast.InsertStmt,
for _, columnMeta := range index.Columns {
columnName := columnMeta.ColumnName
imageParameters, ok := paramMap[columnName]
if !ok {
imageParameters = paramMap[strings.ToLower(columnName)]
}
if !ok && columnMeta.ColumnDef != nil {
if strings.EqualFold("PRIMARY", index.Name) {
i.beforeImageSqlPrimaryKeys[columnName] = true
Expand Down Expand Up @@ -373,6 +376,10 @@ func isIndexValueNull(indexMeta types.IndexMeta, imageParameterMap map[string][]
for _, colMeta := range indexMeta.Columns {
columnName := colMeta.ColumnName
imageParameters := imageParameterMap[columnName]
if imageParameters == nil {
imageParameters = imageParameterMap[strings.ToLower(columnName)]
}

if imageParameters == nil && colMeta.ColumnDef == nil {
return true
} else if imageParameters != nil && (rowIndex >= len(imageParameters) || imageParameters[rowIndex].Value == nil) {
Expand Down
36 changes: 35 additions & 1 deletion pkg/datasource/sql/exec/at/insert_on_update_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ func TestInsertOnUpdateBeforeImageSQL(t *testing.T) {
index2 = make(map[string]types.IndexMeta)
columnMeta1 []types.ColumnMeta
columnMeta2 []types.ColumnMeta
columnMeta3 []types.ColumnMeta
ColumnNames []string
)
columnId := types.ColumnMeta{
ColumnDef: nil,
ColumnName: "id",
}
columnCapitalId := types.ColumnMeta{
ColumnDef: nil,
ColumnName: "CAPITALIZED_ID",
}
columnName := types.ColumnMeta{
ColumnDef: nil,
ColumnName: "name",
Expand All @@ -56,10 +61,12 @@ func TestInsertOnUpdateBeforeImageSQL(t *testing.T) {
ColumnName: "age",
}
columns["id"] = columnId
columns["CAPITALIZED_ID"] = columnCapitalId
columns["name"] = columnName
columns["age"] = columnAge
columnMeta1 = append(columnMeta1, columnId)
columnMeta2 = append(columnMeta2, columnName, columnAge)
columnMeta3 = append(columnMeta3, columnCapitalId)
index["id"] = types.IndexMeta{
Name: "PRIMARY",
IType: types.IndexTypePrimaryKey,
Expand All @@ -70,6 +77,11 @@ func TestInsertOnUpdateBeforeImageSQL(t *testing.T) {
IType: types.IndexUnique,
Columns: columnMeta2,
}
index["capitalized_id_index"] = types.IndexMeta{
Name: "CAPITALIZED_ID_INDEX",
IType: types.IndexUnique,
Columns: columnMeta3,
}

ColumnNames = []string{"id", "name", "age"}
tableMeta1 = types.TableMeta{
Expand Down Expand Up @@ -117,12 +129,34 @@ func TestInsertOnUpdateBeforeImageSQL(t *testing.T) {
Query: "insert into t_user(id, name, age) values(1,'Jack1',?) on duplicate key update name = 'Michael',age = ?",
MetaDataMap: map[string]types.TableMeta{"t_user": tableMeta1},
},
sourceQueryArgs: []driver.Value{81, "Link", 18},
sourceQueryArgs: []driver.Value{81, "Link"},
expectQuery1: "SELECT * FROM t_user WHERE (id = ? ) OR (name = ? and age = ? ) ",
expectQueryArgs1: []driver.Value{int64(1), "Jack1", 81},
expectQuery2: "SELECT * FROM t_user WHERE (name = ? and age = ? ) OR (id = ? ) ",
expectQueryArgs2: []driver.Value{"Jack1", 81, int64(1)},
},
{
execCtx: &types.ExecContext{
Query: "insert into t_user(ID, name, age) values(1,'Jack1',?) on duplicate key update name = 'Michael',age = ?",
MetaDataMap: map[string]types.TableMeta{"t_user": tableMeta1},
},
sourceQueryArgs: []driver.Value{81, "Link"},
expectQuery1: "SELECT * FROM t_user WHERE (id = ? ) OR (name = ? and age = ? ) ",
expectQueryArgs1: []driver.Value{int64(1), "Jack1", 81},
expectQuery2: "SELECT * FROM t_user WHERE (name = ? and age = ? ) OR (id = ? ) ",
expectQueryArgs2: []driver.Value{"Jack1", 81, int64(1)},
},
{
execCtx: &types.ExecContext{
Query: "insert into t_user(ID, capitalized_id) values(1, 11) on duplicate key update name = 'Michael',age = ?",
MetaDataMap: map[string]types.TableMeta{"t_user": tableMeta1},
},
sourceQueryArgs: []driver.Value{"Jack1"},
expectQuery1: "SELECT * FROM t_user WHERE (id = ? ) OR (CAPITALIZED_ID = ? ) ",
expectQueryArgs1: []driver.Value{int64(1), int64(11)},
expectQuery2: "SELECT * FROM t_user WHERE (CAPITALIZED_ID = ? ) OR (id = ? ) ",
expectQueryArgs2: []driver.Value{int64(11), int64(1)},
},
// multi insert one index
{
execCtx: &types.ExecContext{
Expand Down

0 comments on commit f902463

Please sign in to comment.