Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(datasource,rm): insert on update executor #456

Merged
merged 12 commits into from
Feb 18, 2023
Merged
17 changes: 17 additions & 0 deletions pkg/datasource/sql/conn/oracle_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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 conn

import (
Expand Down
8 changes: 5 additions & 3 deletions pkg/datasource/sql/exec/at/at_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ func (e *ATExecutor) ExecWithNamedValue(ctx context.Context, execCtx *types.Exec
exec = NewUpdateExecutor(parser, execCtx, e.hooks)
case types.SQLTypeDelete:
exec = NewDeleteExecutor(parser, execCtx, e.hooks)
//case types.SQLTypeSelectForUpdate:
//case types.SQLTypeMultiDelete:
//case types.SQLTypeMultiUpdate:
case types.SQLTypeInsertOnUpdate:
exec = NewInsertOnUpdateExecutor(parser, execCtx, e.hooks)
// case types.SQLTypeSelectForUpdate:
// case types.SQLTypeMultiDelete:
// case types.SQLTypeMultiUpdate:
default:
exec = NewPlainExecutor(parser, execCtx)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/datasource/sql/exec/at/insert_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (i *insertExecutor) getPkValues(ctx context.Context, execCtx *types.ExecCon
pkColumnNameList := meta.GetPrimaryKeyOnlyName()
pkValuesMap := make(map[string][]interface{})
var err error
//when there is only one pk in the table
// when there is only one pk in the table
if len(pkColumnNameList) == 1 {
if i.containsPK(meta, parseCtx) {
// the insert sql contain pk value
Expand All @@ -214,9 +214,9 @@ func (i *insertExecutor) getPkValues(ctx context.Context, execCtx *types.ExecCon
}
}
} else {
//when there is multiple pk in the table
//1,all pk columns are filled value.
//2,the auto increment pk column value is null, and other pk value are not null.
// when there is multiple pk in the table
// 1,all pk columns are filled value.
// 2,the auto increment pk column value is null, and other pk value are not null.
pkValuesMap, err = i.getPkValuesByColumn(ctx, execCtx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -335,7 +335,7 @@ func (i *insertExecutor) parsePkValuesFromStatement(insertStmt *ast.InsertStmt,
pkValuesMap := make(map[string][]interface{})

if nameValues != nil && len(nameValues) > 0 {
//use prepared statements
// use prepared statements
insertRows, err := getInsertRows(insertStmt, pkIndexArray)
if err != nil {
return nil, err
Expand Down
60 changes: 35 additions & 25 deletions pkg/datasource/sql/exec/at/insert_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestMySQLInsertUndoLogBuilder_containPK(t *testing.T) {
executor.(*insertExecutor).businesSQLResult = tt.fields.InsertResult
executor.(*insertExecutor).incrementStep = tt.fields.IncrementStep

assert.Equalf(t, tt.want, executor.(*insertExecutor).containPK(tt.args.columnName, tt.args.meta), "containPK(%v, %v)", tt.args.columnName, tt.args.meta)
assert.Equalf(t, tt.want, executor.(*insertExecutor).containPK(tt.args.columnName, tt.args.meta), "isPKColumn(%v, %v)", tt.args.columnName, tt.args.meta)
})
}
}
Expand Down Expand Up @@ -407,22 +407,25 @@ func TestMySQLInsertUndoLogBuilder_getPkIndex(t *testing.T) {
},
},
},
meta: types.TableMeta{}}, want: map[string]int{}},
meta: types.TableMeta{},
}, want: map[string]int{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executor := NewInsertExecutor(nil, &types.ExecContext{}, []exec.SQLHook{})
executor.(*insertExecutor).businesSQLResult = tt.fields.InsertResult
executor.(*insertExecutor).incrementStep = tt.fields.IncrementStep
assert.Equalf(t, tt.want, executor.(*insertExecutor).getPkIndex(tt.args.InsertStmt, tt.args.meta), "getPkIndex(%v, %v)", tt.args.InsertStmt, tt.args.meta)
assert.Equalf(t, tt.want, executor.(*insertExecutor).getPkIndex(tt.args.InsertStmt, tt.args.meta), "getPkIndexArray(%v, %v)", tt.args.InsertStmt, tt.args.meta)
})
}
}

func genIntDatum(id int64) test_driver.Datum {
tmp := test_driver.Datum{}
tmp.SetInt64(id)
return tmp
}

func genStrDatum(str string) test_driver.Datum {
tmp := test_driver.Datum{}
tmp.SetBytesAsString([]byte(str))
Expand Down Expand Up @@ -455,12 +458,13 @@ func TestMySQLInsertUndoLogBuilder_parsePkValuesFromStatement(t *testing.T) {
Name: model.CIStr{O: "id", L: "id"},
},
},
Lists: [][]ast.ExprNode{{
&test_driver.ValueExpr{
Datum: genIntDatum(1),
Lists: [][]ast.ExprNode{
{
&test_driver.ValueExpr{
Datum: genIntDatum(1),
},
},
},
},
},
meta: types.TableMeta{
ColumnNames: []string{"id"},
Expand Down Expand Up @@ -503,12 +507,13 @@ func TestMySQLInsertUndoLogBuilder_parsePkValuesFromStatement(t *testing.T) {
Name: model.CIStr{O: "id", L: "id"},
},
},
Lists: [][]ast.ExprNode{{
&test_driver.ValueExpr{
Datum: genStrDatum("?"),
Lists: [][]ast.ExprNode{
{
&test_driver.ValueExpr{
Datum: genStrDatum("?"),
},
},
},
},
},
meta: types.TableMeta{
ColumnNames: []string{"id"},
Expand Down Expand Up @@ -605,15 +610,17 @@ func TestMySQLInsertUndoLogBuilder_getPkValuesByColumn(t *testing.T) {
Name: model.CIStr{O: "id", L: "id"},
},
},
Lists: [][]ast.ExprNode{{
&test_driver.ValueExpr{
Datum: genIntDatum(1),
Lists: [][]ast.ExprNode{
{
&test_driver.ValueExpr{
Datum: genIntDatum(1),
},
},
},
},
},
},
}},
},
},
want: map[string][]interface{}{
"id": {int64(1)},
},
Expand Down Expand Up @@ -705,15 +712,17 @@ func TestMySQLInsertUndoLogBuilder_getPkValuesByAuto(t *testing.T) {
Name: model.CIStr{O: "name", L: "name"},
},
},
Lists: [][]ast.ExprNode{{
&test_driver.ValueExpr{
Datum: genStrDatum("Tom"),
Lists: [][]ast.ExprNode{
{
&test_driver.ValueExpr{
Datum: genStrDatum("Tom"),
},
},
},
},
},
},
}},
},
},
want: map[string][]interface{}{
"id": {int64(100)},
},
Expand Down Expand Up @@ -795,12 +804,13 @@ func TestMySQLInsertUndoLogBuilder_autoGeneratePks(t *testing.T) {
Name: model.CIStr{O: "id", L: "id"},
},
},
Lists: [][]ast.ExprNode{{
&test_driver.ValueExpr{
Datum: genIntDatum(1),
Lists: [][]ast.ExprNode{
{
&test_driver.ValueExpr{
Datum: genIntDatum(1),
},
},
},
},
},
},
},
Expand Down
Loading