vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/rows.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  	"vitess.io/vitess/go/sqltypes"
    23  	querypb "vitess.io/vitess/go/vt/proto/query"
    24  )
    25  
    26  var _ Primitive = (*Rows)(nil)
    27  
    28  // Rows simply returns a number or rows
    29  type Rows struct {
    30  	rows   [][]sqltypes.Value
    31  	fields []*querypb.Field
    32  
    33  	noInputs
    34  	noTxNeeded
    35  }
    36  
    37  // NewRowsPrimitive returns a new Rows primitie
    38  func NewRowsPrimitive(rows [][]sqltypes.Value, fields []*querypb.Field) Primitive {
    39  	return &Rows{rows: rows, fields: fields}
    40  }
    41  
    42  // RouteType implements the Primitive interface
    43  func (r *Rows) RouteType() string {
    44  	return "Rows"
    45  }
    46  
    47  // GetKeyspaceName implements the Primitive interface
    48  func (r *Rows) GetKeyspaceName() string {
    49  	return ""
    50  }
    51  
    52  // GetTableName implements the Primitive interface
    53  func (r *Rows) GetTableName() string {
    54  	return ""
    55  }
    56  
    57  // TryExecute implements the Primitive interface
    58  func (r *Rows) TryExecute(context.Context, VCursor, map[string]*querypb.BindVariable, bool) (*sqltypes.Result, error) {
    59  	return &sqltypes.Result{
    60  		Fields:   r.fields,
    61  		InsertID: 0,
    62  		Rows:     r.rows,
    63  	}, nil
    64  }
    65  
    66  // TryStreamExecute implements the Primitive interface
    67  func (r *Rows) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
    68  	result, err := r.TryExecute(ctx, vcursor, bindVars, wantfields)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	return callback(result)
    73  }
    74  
    75  // GetFields implements the Primitive interface
    76  func (r *Rows) GetFields(context.Context, VCursor, map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
    77  	return &sqltypes.Result{
    78  		Fields:   r.fields,
    79  		InsertID: 0,
    80  		Rows:     nil,
    81  	}, nil
    82  }
    83  
    84  func (r *Rows) description() PrimitiveDescription {
    85  	others := map[string]any{}
    86  	if len(r.fields) != 0 {
    87  		fieldsMap := map[string]string{}
    88  		for _, field := range r.fields {
    89  			fieldsMap[field.Name] = field.Type.String()
    90  		}
    91  		others["Fields"] = fieldsMap
    92  	}
    93  	if len(r.rows) != 0 {
    94  		others["RowCount"] = len(r.rows)
    95  	}
    96  	return PrimitiveDescription{OperatorType: "Rows", Other: others}
    97  }