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

     1  /*
     2  Copyright 2021 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  	"vitess.io/vitess/go/sqltypes"
    23  	querypb "vitess.io/vitess/go/vt/proto/query"
    24  	"vitess.io/vitess/go/vt/vterrors"
    25  )
    26  
    27  // SessionPrimitive the session primitive is a very small primitive used
    28  // when we have simple engine code that needs to interact with the Session
    29  type SessionPrimitive struct {
    30  	action func(sa SessionActions) (*sqltypes.Result, error)
    31  	name   string
    32  
    33  	noInputs
    34  	noTxNeeded
    35  }
    36  
    37  var _ Primitive = (*SessionPrimitive)(nil)
    38  
    39  // NewSessionPrimitive creates a SessionPrimitive
    40  func NewSessionPrimitive(name string, action func(sa SessionActions) (*sqltypes.Result, error)) *SessionPrimitive {
    41  	return &SessionPrimitive{
    42  		action: action,
    43  		name:   name,
    44  	}
    45  }
    46  
    47  // RouteType implements the Primitive interface
    48  func (s *SessionPrimitive) RouteType() string {
    49  	return "SHOW"
    50  }
    51  
    52  // GetKeyspaceName implements the Primitive interface
    53  func (s *SessionPrimitive) GetKeyspaceName() string {
    54  	return ""
    55  }
    56  
    57  // GetTableName implements the Primitive interface
    58  func (s *SessionPrimitive) GetTableName() string {
    59  	return ""
    60  }
    61  
    62  // TryExecute implements the Primitive interface
    63  func (s *SessionPrimitive) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
    64  	return s.action(vcursor.Session())
    65  }
    66  
    67  // TryStreamExecute implements the Primitive interface
    68  func (s *SessionPrimitive) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
    69  	qr, err := s.action(vcursor.Session())
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return callback(qr)
    74  }
    75  
    76  // GetFields implements the Primitive interface
    77  func (s *SessionPrimitive) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
    78  	return nil, vterrors.VT13001("GetFields is not supported for SessionPrimitive")
    79  }
    80  
    81  // description implements the Primitive interface
    82  func (s *SessionPrimitive) description() PrimitiveDescription {
    83  	return PrimitiveDescription{
    84  		OperatorType: s.name,
    85  	}
    86  }