github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/declare_cursor.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  
    20  	"github.com/dolthub/go-mysql-server/sql/expression"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  )
    24  
    25  // DeclareCursor represents the DECLARE ... CURSOR statement.
    26  type DeclareCursor struct {
    27  	Name   string
    28  	Select sql.Node
    29  	Pref   *expression.ProcedureReference
    30  }
    31  
    32  var _ sql.Node = (*DeclareCursor)(nil)
    33  var _ sql.CollationCoercible = (*DeclareCursor)(nil)
    34  var _ sql.DebugStringer = (*DeclareCursor)(nil)
    35  var _ expression.ProcedureReferencable = (*DeclareCursor)(nil)
    36  
    37  // NewDeclareCursor returns a new *DeclareCursor node.
    38  func NewDeclareCursor(name string, selectStatement sql.Node) *DeclareCursor {
    39  	return &DeclareCursor{
    40  		Name:   name,
    41  		Select: selectStatement,
    42  	}
    43  }
    44  
    45  // Resolved implements the interface sql.Node.
    46  func (d *DeclareCursor) Resolved() bool {
    47  	return d.Select.Resolved()
    48  }
    49  
    50  func (d *DeclareCursor) IsReadOnly() bool {
    51  	return d.Select.IsReadOnly()
    52  }
    53  
    54  // String implements the interface sql.Node.
    55  func (d *DeclareCursor) String() string {
    56  	return fmt.Sprintf("DECLARE %s CURSOR FOR %s", d.Name, d.Select.String())
    57  }
    58  
    59  // DebugString implements the interface sql.DebugStringer.
    60  func (d *DeclareCursor) DebugString() string {
    61  	return fmt.Sprintf("DECLARE %s CURSOR FOR %s", d.Name, sql.DebugString(d.Select))
    62  }
    63  
    64  // Schema implements the interface sql.Node.
    65  func (d *DeclareCursor) Schema() sql.Schema {
    66  	return d.Select.Schema()
    67  }
    68  
    69  // Children implements the interface sql.Node.
    70  func (d *DeclareCursor) Children() []sql.Node {
    71  	return []sql.Node{d.Select}
    72  }
    73  
    74  // WithChildren implements the interface sql.Node.
    75  func (d *DeclareCursor) WithChildren(children ...sql.Node) (sql.Node, error) {
    76  	if len(children) != 1 {
    77  		return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
    78  	}
    79  
    80  	nd := *d
    81  	nd.Select = children[0]
    82  	return &nd, nil
    83  }
    84  
    85  // CheckPrivileges implements the interface sql.Node.
    86  func (d *DeclareCursor) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    87  	return d.Select.CheckPrivileges(ctx, opChecker)
    88  }
    89  
    90  // CollationCoercibility implements the interface sql.CollationCoercible.
    91  func (*DeclareCursor) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    92  	return sql.Collation_binary, 7
    93  }
    94  
    95  // WithParamReference implements the interface expression.ProcedureReferencable.
    96  func (d *DeclareCursor) WithParamReference(pRef *expression.ProcedureReference) sql.Node {
    97  	nd := *d
    98  	nd.Pref = pRef
    99  	return &nd
   100  }