github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/declare_handler.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"
    21  	"github.com/dolthub/go-mysql-server/sql/expression"
    22  )
    23  
    24  // DeclareHandler represents the DECLARE ... HANDLER statement.
    25  type DeclareHandler struct {
    26  	Action    expression.DeclareHandlerAction
    27  	Statement sql.Node
    28  	Pref      *expression.ProcedureReference
    29  	Condition expression.HandlerCondition
    30  }
    31  
    32  var _ sql.Node = (*DeclareHandler)(nil)
    33  var _ sql.CollationCoercible = (*DeclareHandler)(nil)
    34  var _ sql.DebugStringer = (*DeclareHandler)(nil)
    35  var _ expression.ProcedureReferencable = (*DeclareHandler)(nil)
    36  
    37  // NewDeclareHandler returns a new *DeclareHandler node.
    38  func NewDeclareHandler(action expression.DeclareHandlerAction, statement sql.Node, cond expression.HandlerCondition) (*DeclareHandler, error) {
    39  	if action == expression.DeclareHandlerAction_Undo {
    40  		return nil, sql.ErrDeclareHandlerUndo.New()
    41  	}
    42  	return &DeclareHandler{
    43  		Action:    action,
    44  		Statement: statement,
    45  		Condition: cond,
    46  	}, nil
    47  }
    48  
    49  // Resolved implements the interface sql.Node.
    50  func (d *DeclareHandler) Resolved() bool {
    51  	return true
    52  }
    53  
    54  func (d *DeclareHandler) IsReadOnly() bool {
    55  	return true
    56  }
    57  
    58  // String implements the interface sql.Node.
    59  func (d *DeclareHandler) String() string {
    60  	var action string
    61  	switch d.Action {
    62  	case expression.DeclareHandlerAction_Continue:
    63  		action = "CONTINUE"
    64  	case expression.DeclareHandlerAction_Exit:
    65  		action = "EXIT"
    66  	case expression.DeclareHandlerAction_Undo:
    67  		action = "UNDO"
    68  	}
    69  	return fmt.Sprintf("DECLARE %s HANDLER FOR NOT FOUND %s", action, d.Statement.String())
    70  }
    71  
    72  // DebugString implements the interface sql.DebugStringer.
    73  func (d *DeclareHandler) DebugString() string {
    74  	var action string
    75  	switch d.Action {
    76  	case expression.DeclareHandlerAction_Continue:
    77  		action = "CONTINUE"
    78  	case expression.DeclareHandlerAction_Exit:
    79  		action = "EXIT"
    80  	case expression.DeclareHandlerAction_Undo:
    81  		action = "UNDO"
    82  	}
    83  	return fmt.Sprintf("DECLARE %s HANDLER FOR NOT FOUND %s", action, sql.DebugString(d.Statement))
    84  }
    85  
    86  // Schema implements the interface sql.Node.
    87  func (d *DeclareHandler) Schema() sql.Schema {
    88  	return nil
    89  }
    90  
    91  // Children implements the interface sql.Node.
    92  func (d *DeclareHandler) Children() []sql.Node {
    93  	return []sql.Node{d.Statement}
    94  }
    95  
    96  // WithChildren implements the interface sql.Node.
    97  func (d *DeclareHandler) WithChildren(children ...sql.Node) (sql.Node, error) {
    98  	if len(children) != 1 {
    99  		return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
   100  	}
   101  
   102  	nd := *d
   103  	nd.Statement = children[0]
   104  	return &nd, nil
   105  }
   106  
   107  // CheckPrivileges implements the interface sql.Node.
   108  func (d *DeclareHandler) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   109  	return true
   110  }
   111  
   112  // CollationCoercibility implements the interface sql.CollationCoercible.
   113  func (*DeclareHandler) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   114  	return sql.Collation_binary, 7
   115  }
   116  
   117  // WithParamReference implements the interface expression.ProcedureReferencable.
   118  func (d *DeclareHandler) WithParamReference(pRef *expression.ProcedureReference) sql.Node {
   119  	nd := *d
   120  	nd.Pref = pRef
   121  	return &nd
   122  }