Skip to content

Commit

Permalink
fix buildKey failed
Browse files Browse the repository at this point in the history
Signed-off-by: charlie <[email protected]>
  • Loading branch information
Charlie17Li committed Mar 4, 2023
1 parent f2d7d8a commit 7a51b01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pkg/datasource/sql/exec/at/select_for_update_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func TestBuildSelectPKSQL(t *testing.T) {

selSQL, err := e.buildSelectPKSQL(ctx.SelectStmt, &metaData)
assert.Nil(t, err)
equal := "SELECT SQL_NO_CACHE order_id,id FROM t_user WHERE age>? FOR UPDATE" == selSQL ||
"SELECT SQL_NO_CACHE id,order_id FROM t_user WHERE age>? FOR UPDATE" == selSQL
assert.Equal(t, equal, true)
assert.Equal(t, "SELECT SQL_NO_CACHE id,order_id FROM t_user WHERE age>? FOR UPDATE" == selSQL, true)
}

func TestBuildLockKey(t *testing.T) {
Expand Down Expand Up @@ -123,6 +121,7 @@ func TestBuildLockKey(t *testing.T) {
ColumnName: "age",
},
},
ColumnNames: []string{"id", "order_id", "age"},
}
rows := mockRows{}
lockKey := e.buildLockKey(rows, &metaData)
Expand Down
13 changes: 13 additions & 0 deletions pkg/datasource/sql/types/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package types
import (
"fmt"
"reflect"
"sort"
)

// ColumnMeta
Expand Down Expand Up @@ -119,6 +120,18 @@ func (m TableMeta) GetPrimaryKeyOnlyName() []string {
}
}
}

// need sort again according the m.ColumnNames
order := make(map[string]int, len(m.ColumnNames))
for i, name := range m.ColumnNames {
order[name] = i
}
sort.Slice(keys, func(i, j int) bool {
if order == nil {
return false
}
return order[keys[i]] < order[keys[j]]
})
return keys
}

Expand Down

0 comments on commit 7a51b01

Please sign in to comment.