vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/update_target.go (about)

     1  /*
     2  Copyright 2020 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  
    22  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    23  	"vitess.io/vitess/go/vt/vterrors"
    24  
    25  	"vitess.io/vitess/go/sqltypes"
    26  	"vitess.io/vitess/go/vt/proto/query"
    27  )
    28  
    29  var _ Primitive = (*UpdateTarget)(nil)
    30  
    31  // UpdateTarget is an operator to update target string.
    32  type UpdateTarget struct {
    33  	// Target string to be updated
    34  	Target string
    35  
    36  	noInputs
    37  
    38  	noTxNeeded
    39  }
    40  
    41  func (updTarget *UpdateTarget) description() PrimitiveDescription {
    42  	return PrimitiveDescription{
    43  		OperatorType: "UpdateTarget",
    44  		Other:        map[string]any{"target": updTarget.Target},
    45  	}
    46  }
    47  
    48  // RouteType implements the Primitive interface
    49  func (updTarget *UpdateTarget) RouteType() string {
    50  	return "UpdateTarget"
    51  }
    52  
    53  // GetKeyspaceName implements the Primitive interface
    54  func (updTarget *UpdateTarget) GetKeyspaceName() string {
    55  	return updTarget.Target
    56  }
    57  
    58  // GetTableName implements the Primitive interface
    59  func (updTarget *UpdateTarget) GetTableName() string {
    60  	return ""
    61  }
    62  
    63  // TryExecute implements the Primitive interface
    64  func (updTarget *UpdateTarget) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*query.BindVariable, wantfields bool) (*sqltypes.Result, error) {
    65  	err := vcursor.Session().SetTarget(updTarget.Target)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return &sqltypes.Result{}, nil
    70  }
    71  
    72  // TryStreamExecute implements the Primitive interface
    73  func (updTarget *UpdateTarget) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*query.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
    74  	result, err := updTarget.TryExecute(ctx, vcursor, bindVars, wantfields)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	return callback(result)
    79  }
    80  
    81  // GetFields implements the Primitive interface
    82  func (updTarget *UpdateTarget) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*query.BindVariable) (*sqltypes.Result, error) {
    83  	return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] GetFields not reachable for use statement")
    84  }