github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/fetch.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plan
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/expression"
    23  )
    24  
    25  // Fetch represents the FETCH statement, which handles value acquisition from cursors.
    26  type Fetch struct {
    27  	Name     string
    28  	InnerSet *Set
    29  	ToSet    []sql.Expression
    30  	Pref     *expression.ProcedureReference
    31  	Sch      sql.Schema
    32  }
    33  
    34  var _ sql.Node = (*Fetch)(nil)
    35  var _ sql.CollationCoercible = (*Fetch)(nil)
    36  var _ expression.ProcedureReferencable = (*Fetch)(nil)
    37  
    38  // NewFetch returns a new *Fetch node.
    39  func NewFetch(name string, toSet []sql.Expression) *Fetch {
    40  	return &Fetch{
    41  		Name:  name,
    42  		ToSet: toSet,
    43  	}
    44  }
    45  
    46  // Resolved implements the interface sql.Node.
    47  func (f *Fetch) Resolved() bool {
    48  	for _, e := range f.ToSet {
    49  		if !e.Resolved() {
    50  			return false
    51  		}
    52  	}
    53  	return true
    54  }
    55  
    56  // String implements the interface sql.Node.
    57  func (f *Fetch) String() string {
    58  	vars := make([]string, len(f.ToSet))
    59  	for i, e := range f.ToSet {
    60  		vars[i] = e.String()
    61  	}
    62  	return fmt.Sprintf("FETCH %s INTO %s", f.Name, strings.Join(vars, ", "))
    63  }
    64  
    65  // DebugString implements the interface sql.DebugStringer.
    66  func (f *Fetch) DebugString() string {
    67  	vars := make([]string, len(f.ToSet))
    68  	for i, e := range f.ToSet {
    69  		vars[i] = sql.DebugString(e)
    70  	}
    71  	return fmt.Sprintf("FETCH %s INTO %s", f.Name, strings.Join(vars, ", "))
    72  }
    73  
    74  // Schema implements the interface sql.Node.
    75  func (f *Fetch) Schema() sql.Schema {
    76  	return nil
    77  }
    78  
    79  func (f *Fetch) Expressions() []sql.Expression {
    80  	return f.ToSet
    81  }
    82  
    83  func (f *Fetch) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
    84  	if len(exprs) != len(f.ToSet) {
    85  		return nil, sql.ErrInvalidExpressionNumber.New(len(exprs), len(f.ToSet))
    86  	}
    87  	ret := *f
    88  	ret.ToSet = exprs
    89  	return &ret, nil
    90  }
    91  
    92  // Children implements the interface sql.Node.
    93  func (f *Fetch) Children() []sql.Node {
    94  	return nil
    95  }
    96  
    97  func (f *Fetch) IsReadOnly() bool {
    98  	return true
    99  }
   100  
   101  // WithChildren implements the interface sql.Node.
   102  func (f *Fetch) WithChildren(children ...sql.Node) (sql.Node, error) {
   103  	return f, nil
   104  }
   105  
   106  // CheckPrivileges implements the interface sql.Node.
   107  func (f *Fetch) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   108  	return true
   109  }
   110  
   111  // CollationCoercibility implements the interface sql.CollationCoercible.
   112  func (*Fetch) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   113  	return sql.Collation_binary, 7
   114  }
   115  
   116  // WithParamReference implements the interface expression.ProcedureReferencable.
   117  func (f *Fetch) WithParamReference(pRef *expression.ProcedureReference) sql.Node {
   118  	nf := *f
   119  	nf.Pref = pRef
   120  	return &nf
   121  }
   122  
   123  // fetchIter is the sql.RowIter of *Fetch.
   124  type fetchIter struct {
   125  	*Fetch
   126  	rowIter sql.RowIter
   127  }
   128  
   129  var _ sql.RowIter = (*fetchIter)(nil)
   130  
   131  // Next implements the interface sql.RowIter.
   132  func (f *fetchIter) Next(ctx *sql.Context) (sql.Row, error) {
   133  	row, err := f.rowIter.Next(ctx)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return row, nil
   138  }
   139  
   140  // Close implements the interface sql.RowIter.
   141  func (f *fetchIter) Close(ctx *sql.Context) error {
   142  	return f.rowIter.Close(ctx)
   143  }