Skip to content

Commit

Permalink
fix: undo log sql (#724)
Browse files Browse the repository at this point in the history
* fix the problem of mock replacing mysql.GetTableMeta in TestBuildSelectSQLByUpdate

* fix the problem of MySQLUpdateUndoLogBuilder failing to generate undo log sql

---------

Co-authored-by: JayLiu <[email protected]>
  • Loading branch information
FinnTew and luky116 authored Dec 7, 2024
1 parent 0fa0272 commit 406b8ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/datasource/sql/undo/builder/basic_undo_log_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (b *BasicUndoLogBuilder) traversalArgs(node ast.Node, argsIndex *[]int32) {
b.traversalArgs(exprs[i], argsIndex)
}
break
case *ast.ParenthesesExpr:
expr := node.(*ast.ParenthesesExpr)
b.traversalArgs(expr.Expr, argsIndex)
break
case *test_driver.ParamMarkerExpr:
*argsIndex = append(*argsIndex, int32(node.(*test_driver.ParamMarkerExpr).Order))
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ import (
)

func TestBuildSelectSQLByUpdate(t *testing.T) {
t.SkipNow()
//t.SkipNow()
var (
builder = MySQLUpdateUndoLogBuilder{}
)

stub := gomonkey.ApplyMethod(reflect.TypeOf(datasource.GetTableCache(types.DBTypeMySQL)), "GetTableMeta", func(_ *mysql.TableMetaCache, ctx context.Context, dbName, tableName string, conn driver.Conn) (*types.TableMeta, error) {
datasource.RegisterTableCache(types.DBTypeMySQL, &mysql.TableMetaCache{})

stub := gomonkey.ApplyMethod(reflect.TypeOf(datasource.GetTableCache(types.DBTypeMySQL)), "GetTableMeta", func(_ *mysql.TableMetaCache, ctx context.Context, dbName, tableName string) (*types.TableMeta, error) {
return &types.TableMeta{
Indexs: map[string]types.IndexMeta{
"id": {
Expand Down Expand Up @@ -77,6 +79,12 @@ func TestBuildSelectSQLByUpdate(t *testing.T) {
expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE",
expectQueryArgs: []driver.Value{100, 18, 28},
},
{
sourceQuery: "update t_user set name = ?, age = ? where (id = ? or name = 'Jack') and age between ? and ?",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28},
expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE (id=? OR name=_UTF8MB4Jack) AND age BETWEEN ? AND ? FOR UPDATE",
expectQueryArgs: []driver.Value{100, 18, 28},
},
{
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?)",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28},
Expand Down

0 comments on commit 406b8ba

Please sign in to comment.