|
| 1 | +/* |
| 2 | +Copyright 2023 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package engine |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "vitess.io/vitess/go/sqltypes" |
| 24 | + querypb "vitess.io/vitess/go/vt/proto/query" |
| 25 | + topodatapb "vitess.io/vitess/go/vt/proto/topodata" |
| 26 | + vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" |
| 27 | + "vitess.io/vitess/go/vt/vterrors" |
| 28 | +) |
| 29 | + |
| 30 | +var _ Primitive = (*Upsert)(nil) |
| 31 | + |
| 32 | +// Upsert Primitive will execute the insert primitive first and |
| 33 | +// if there is `Duplicate Key` error, it executes the update primitive. |
| 34 | +type Upsert struct { |
| 35 | + Upserts []upsert |
| 36 | + |
| 37 | + txNeeded |
| 38 | +} |
| 39 | + |
| 40 | +type upsert struct { |
| 41 | + Insert Primitive |
| 42 | + Update Primitive |
| 43 | +} |
| 44 | + |
| 45 | +// AddUpsert appends to the Upsert Primitive. |
| 46 | +func (u *Upsert) AddUpsert(ins, upd Primitive) { |
| 47 | + u.Upserts = append(u.Upserts, upsert{ |
| 48 | + Insert: ins, |
| 49 | + Update: upd, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +// RouteType implements Primitive interface type. |
| 54 | +func (u *Upsert) RouteType() string { |
| 55 | + return "UPSERT" |
| 56 | +} |
| 57 | + |
| 58 | +// GetKeyspaceName implements Primitive interface type. |
| 59 | +func (u *Upsert) GetKeyspaceName() string { |
| 60 | + if len(u.Upserts) > 0 { |
| 61 | + return u.Upserts[0].Insert.GetKeyspaceName() |
| 62 | + } |
| 63 | + return "" |
| 64 | +} |
| 65 | + |
| 66 | +// GetTableName implements Primitive interface type. |
| 67 | +func (u *Upsert) GetTableName() string { |
| 68 | + if len(u.Upserts) > 0 { |
| 69 | + return u.Upserts[0].Insert.GetTableName() |
| 70 | + } |
| 71 | + return "" |
| 72 | +} |
| 73 | + |
| 74 | +// GetFields implements Primitive interface type. |
| 75 | +func (u *Upsert) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) { |
| 76 | + return nil, vterrors.VT13001("unexpected to receive GetFields call for insert on duplicate key update query") |
| 77 | +} |
| 78 | + |
| 79 | +// TryExecute implements Primitive interface type. |
| 80 | +func (u *Upsert) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) { |
| 81 | + result := &sqltypes.Result{} |
| 82 | + for _, up := range u.Upserts { |
| 83 | + qr, err := execOne(ctx, vcursor, bindVars, wantfields, up) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + result.RowsAffected += qr.RowsAffected |
| 88 | + } |
| 89 | + return result, nil |
| 90 | +} |
| 91 | + |
| 92 | +func execOne(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, up upsert) (*sqltypes.Result, error) { |
| 93 | + insQr, err := vcursor.ExecutePrimitive(ctx, up.Insert, bindVars, wantfields) |
| 94 | + if err == nil { |
| 95 | + return insQr, nil |
| 96 | + } |
| 97 | + if vterrors.Code(err) != vtrpcpb.Code_ALREADY_EXISTS { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + updQr, err := vcursor.ExecutePrimitive(ctx, up.Update, bindVars, wantfields) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + // To match mysql, need to report +1 on rows affected if there is any change. |
| 105 | + if updQr.RowsAffected > 0 { |
| 106 | + updQr.RowsAffected += 1 |
| 107 | + } |
| 108 | + return updQr, nil |
| 109 | +} |
| 110 | + |
| 111 | +// TryStreamExecute implements Primitive interface type. |
| 112 | +func (u *Upsert) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error { |
| 113 | + qr, err := u.TryExecute(ctx, vcursor, bindVars, wantfields) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + return callback(qr) |
| 118 | +} |
| 119 | + |
| 120 | +// Inputs implements Primitive interface type. |
| 121 | +func (u *Upsert) Inputs() ([]Primitive, []map[string]any) { |
| 122 | + var inputs []Primitive |
| 123 | + var inputsMap []map[string]any |
| 124 | + for i, up := range u.Upserts { |
| 125 | + inputs = append(inputs, up.Insert, up.Update) |
| 126 | + inputsMap = append(inputsMap, |
| 127 | + map[string]any{inputName: fmt.Sprintf("Insert-%d", i+1)}, |
| 128 | + map[string]any{inputName: fmt.Sprintf("Update-%d", i+1)}) |
| 129 | + } |
| 130 | + return inputs, inputsMap |
| 131 | +} |
| 132 | + |
| 133 | +func (u *Upsert) description() PrimitiveDescription { |
| 134 | + return PrimitiveDescription{ |
| 135 | + OperatorType: "Upsert", |
| 136 | + TargetTabletType: topodatapb.TabletType_PRIMARY, |
| 137 | + } |
| 138 | +} |
0 commit comments