-
Notifications
You must be signed in to change notification settings - Fork 267
/
multi_update_excutor_test.go
168 lines (151 loc) · 8.04 KB
/
multi_update_excutor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at
import (
"database/sql/driver"
"testing"
"github.com/stretchr/testify/assert"
"github.com/seata/seata-go/pkg/datasource/sql/datasource"
"github.com/seata/seata-go/pkg/datasource/sql/datasource/mysql"
"github.com/seata/seata-go/pkg/datasource/sql/exec"
"github.com/seata/seata-go/pkg/datasource/sql/parser"
"github.com/seata/seata-go/pkg/datasource/sql/types"
"github.com/seata/seata-go/pkg/datasource/sql/undo"
"github.com/seata/seata-go/pkg/datasource/sql/util"
)
func TestBuildSelectSQLByMultiUpdate(t *testing.T) {
undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: true})
datasource.RegisterTableCache(types.DBTypeMySQL, mysql.NewTableMetaInstance(nil))
tests := []struct {
name string
sourceQuery string
sourceQueryArgs []driver.Value
expectQuery string
expectQueryArgs []driver.Value
}{
{
sourceQuery: "update t_user set name = ?, age = ? where id = ?;" +
"update t_user set name = ?, age = ? where id = ?;",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, "TOM", 2, 200, "TOM", 2, 200},
expectQuery: "SELECT SQL_NO_CACHE name,age FROM t_user WHERE id=? OR id=? FOR UPDATE",
expectQueryArgs: []driver.Value{100, 200},
},
{
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age between ? and ?;" +
"update t_user set name = ?, age = ? where id = ? and name = 'Jack2' and age between ? and ?",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28, "Jack2", 2, 200, 28, 38},
expectQuery: "SELECT SQL_NO_CACHE name,age FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? OR id=? AND name=_UTF8MB4Jack2 AND age BETWEEN ? AND ? FOR UPDATE",
expectQueryArgs: []driver.Value{100, 18, 28, 200, 28, 38},
},
{
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?);" +
"update t_user set name = ?, age = ? where id = ? and name = 'Jack2' and age in (?,?)",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28, "Jack2", 2, 200, 48, 58},
expectQuery: "SELECT SQL_NO_CACHE name,age FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) OR id=? AND name=_UTF8MB4Jack2 AND age IN (?,?) FOR UPDATE",
expectQueryArgs: []driver.Value{100, 18, 28, 200, 48, 58},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := parser.DoParser(tt.sourceQuery)
assert.Nil(t, err)
//updateStmts := make([]*ast.UpdateStmt, 0, len(c.MultiStmt))
//for _, v := range c.MultiStmt {
// updateStmts = append(updateStmts, v.UpdateStmt)
//}
executor := NewMultiUpdateExecutor(c, &types.ExecContext{NamedValues: util.ValueToNamedValue(tt.sourceQueryArgs)}, []exec.SQLHook{})
query, args, err := executor.buildBeforeImageSQL(util.ValueToNamedValue(tt.sourceQueryArgs), &types.TableMeta{})
assert.NoError(t, err)
assert.Equal(t, tt.expectQuery, query)
assert.Equal(t, tt.expectQueryArgs, util.NamedValueToValue(args))
})
}
sourceQuery := "update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc;" +
"update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name;"
sourceQueryArgs := []driver.Value{"Jack", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2, "Jack2", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2}
c, err := parser.DoParser(sourceQuery)
assert.NoError(t, err)
//updateStmts := make([]*ast.UpdateStmt, 0, len(c.MultiStmt))
//for _, v := range c.MultiStmt {
// updateStmts = append(updateStmts, v.UpdateStmt)
//}
executor := NewMultiUpdateExecutor(c, &types.ExecContext{NamedValues: util.ValueToNamedValue(sourceQueryArgs)}, []exec.SQLHook{})
_, _, err = executor.buildBeforeImageSQL(util.ValueToNamedValue(sourceQueryArgs), &types.TableMeta{})
assert.Error(t, err)
assert.Equal(t, err.Error(), "multi update SQL with orderBy condition is not support yet")
}
func TestBuildSelectSQLByMultiUpdateAllColumns(t *testing.T) {
undo.InitUndoConfig(undo.Config{OnlyCareUpdateColumns: false})
datasource.RegisterTableCache(types.DBTypeMySQL, mysql.NewTableMetaInstance(nil))
tests := []struct {
name string
sourceQuery string
sourceQueryArgs []driver.Value
expectQuery string
expectQueryArgs []driver.Value
}{
{
sourceQuery: "update t_user set name = ?, age = ? where id = ?;" +
"update t_user set name = ?, age = ? where id = ?;",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, "TOM", 2, 200, "TOM", 2, 200},
expectQuery: "SELECT SQL_NO_CACHE name,age,sex,birthdate FROM t_user WHERE id=? OR id=? FOR UPDATE",
expectQueryArgs: []driver.Value{100, 200},
},
{
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age between ? and ?;" +
"update t_user set name = ?, age = ? where id = ? and name = 'Jack2' and age between ? and ?",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28, "Jack2", 2, 200, 28, 38},
expectQuery: "SELECT SQL_NO_CACHE name,age,sex,birthdate FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? OR id=? AND name=_UTF8MB4Jack2 AND age BETWEEN ? AND ? FOR UPDATE",
expectQueryArgs: []driver.Value{100, 18, 28, 200, 28, 38},
},
{
sourceQuery: "update t_user set name = ?, age = ? where id = ? and name = 'Jack' and age in (?,?);" +
"update t_user set name = ?, age = ? where id = ? and name = 'Jack2' and age in (?,?)",
sourceQueryArgs: []driver.Value{"Jack", 1, 100, 18, 28, "Jack2", 2, 200, 48, 58},
expectQuery: "SELECT SQL_NO_CACHE name,age,sex,birthdate FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) OR id=? AND name=_UTF8MB4Jack2 AND age IN (?,?) FOR UPDATE",
expectQueryArgs: []driver.Value{100, 18, 28, 200, 48, 58},
},
}
meta := types.TableMeta{
TableName: "t_user",
ColumnNames: []string{"name", "age", "sex", "birthdate"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := parser.DoParser(tt.sourceQuery)
assert.Nil(t, err)
//updateStmts := make([]*ast.UpdateStmt, 0, len(c.MultiStmt))
//for _, v := range c.MultiStmt {
// updateStmts = append(updateStmts, v.UpdateStmt)
//}
executor := NewMultiUpdateExecutor(c, &types.ExecContext{NamedValues: util.ValueToNamedValue(tt.sourceQueryArgs)}, []exec.SQLHook{})
query, args, err := executor.buildBeforeImageSQL(util.ValueToNamedValue(tt.sourceQueryArgs), &meta)
assert.NoError(t, err)
assert.Equal(t, tt.expectQuery, query)
assert.Equal(t, tt.expectQueryArgs, util.NamedValueToValue(args))
})
}
sourceQuery := "update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc;" +
"update t_user set name = ?, age = ? where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name;"
sourceQueryArgs := []driver.Value{"Jack", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2, "Jack2", 1, 10, 20, 17, "Beijing", "Guangzhou", 18, 2}
c, err := parser.DoParser(sourceQuery)
assert.NoError(t, err)
executor := NewMultiUpdateExecutor(c, &types.ExecContext{NamedValues: util.ValueToNamedValue(sourceQueryArgs)}, []exec.SQLHook{})
_, _, err = executor.buildBeforeImageSQL(util.ValueToNamedValue(sourceQueryArgs), &meta)
assert.Error(t, err)
assert.Equal(t, err.Error(), "multi update SQL with orderBy condition is not support yet")
}