You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

update_executor_test.go 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package at
  18. import (
  19. "context"
  20. "database/sql/driver"
  21. "reflect"
  22. "testing"
  23. "github.com/agiledragon/gomonkey/v2"
  24. "github.com/stretchr/testify/assert"
  25. "seata.apache.org/seata-go/pkg/datasource/sql/datasource"
  26. "seata.apache.org/seata-go/pkg/datasource/sql/datasource/mysql"
  27. "seata.apache.org/seata-go/pkg/datasource/sql/exec"
  28. "seata.apache.org/seata-go/pkg/datasource/sql/parser"
  29. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  30. "seata.apache.org/seata-go/pkg/datasource/sql/undo"
  31. "seata.apache.org/seata-go/pkg/datasource/sql/util"
  32. _ "seata.apache.org/seata-go/pkg/util/log"
  33. )
  34. func TestBuildSelectSQLByUpdate(t *testing.T) {
  35. undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: true})
  36. datasource.RegisterTableCache(types.DBTypeMySQL, mysql.NewTableMetaInstance(nil, nil))
  37. stub := gomonkey.ApplyMethod(reflect.TypeOf(datasource.GetTableCache(types.DBTypeMySQL)), "GetTableMeta",
  38. func(_ *mysql.TableMetaCache, ctx context.Context, dbName, tableName string) (*types.TableMeta, error) {
  39. return &types.TableMeta{
  40. Indexs: map[string]types.IndexMeta{
  41. "id": {
  42. IType: types.IndexTypePrimaryKey,
  43. Columns: []types.ColumnMeta{
  44. {ColumnName: "id"},
  45. },
  46. },
  47. },
  48. }, nil
  49. })
  50. defer stub.Reset()
  51. tests := []struct {
  52. name string
  53. sourceQuery string
  54. sourceQueryArgs []driver.Value
  55. expectQuery string
  56. expectQueryArgs []driver.Value
  57. }{
  58. {
  59. sourceQuery: "update t_user set name = ?, age = ? where id = ?",
  60. sourceQueryArgs: []driver.Value{"Jack", 1, 100},
  61. expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? FOR UPDATE",
  62. expectQueryArgs: []driver.Value{100},
  63. },
  64. {
  65. sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age between ? and ?",
  66. sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28},
  67. expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE",
  68. expectQueryArgs: []driver.Value{100, 18, 28},
  69. },
  70. {
  71. sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?)",
  72. sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28},
  73. expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) FOR UPDATE",
  74. expectQueryArgs: []driver.Value{100, 18, 28},
  75. },
  76. {
  77. sourceQuery: "update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc limit ?",
  78. sourceQueryArgs: []driver.Value{"Jack", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2},
  79. expectQuery: "SELECT SQL_NO_CACHE name,age,id FROM t_user WHERE kk BETWEEN ? AND ? AND id=? AND addr IN (?,?) AND age>? ORDER BY name DESC LIMIT ? FOR UPDATE",
  80. expectQueryArgs: []driver.Value{10, 20, 17, "Beijing", "Guangzhou", 18, 2},
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. c, err := parser.DoParser(tt.sourceQuery)
  86. assert.Nil(t, err)
  87. executor := NewUpdateExecutor(c, &types.ExecContext{Values: tt.sourceQueryArgs, NamedValues: util.ValueToNamedValue(tt.sourceQueryArgs)}, []exec.SQLHook{})
  88. query, args, err := executor.(*updateExecutor).buildBeforeImageSQL(context.Background(), util.ValueToNamedValue(tt.sourceQueryArgs))
  89. assert.Nil(t, err)
  90. assert.Equal(t, tt.expectQuery, query)
  91. assert.Equal(t, tt.expectQueryArgs, util.NamedValueToValue(args))
  92. })
  93. }
  94. }