github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/prepare.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/types"
    22  )
    23  
    24  // PrepareQuery is a node that prepares the query
    25  type PrepareQuery struct {
    26  	Name  string
    27  	Child sql.Node
    28  }
    29  
    30  var _ sql.Node = (*PrepareQuery)(nil)
    31  var _ sql.CollationCoercible = (*PrepareQuery)(nil)
    32  
    33  // NewPrepareQuery creates a new PrepareQuery node.
    34  func NewPrepareQuery(name string, child sql.Node) *PrepareQuery {
    35  	return &PrepareQuery{Name: name, Child: child}
    36  }
    37  
    38  // Schema implements the Node interface.
    39  func (p *PrepareQuery) Schema() sql.Schema {
    40  	return types.OkResultSchema
    41  }
    42  
    43  func (p *PrepareQuery) IsReadOnly() bool {
    44  	return true
    45  }
    46  
    47  // PrepareInfo is the Info for OKResults returned by Update nodes.
    48  type PrepareInfo struct {
    49  }
    50  
    51  // String implements fmt.Stringer
    52  func (pi PrepareInfo) String() string {
    53  	return "Statement prepared"
    54  }
    55  
    56  // RowIter implements the Node interface.
    57  func (p *PrepareQuery) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
    58  	return sql.RowsToRowIter(sql.NewRow(types.OkResult{RowsAffected: 0, Info: PrepareInfo{}})), nil
    59  }
    60  
    61  func (p *PrepareQuery) Resolved() bool {
    62  	return true
    63  }
    64  
    65  // Children implements the Node interface.
    66  func (p *PrepareQuery) Children() []sql.Node {
    67  	return nil // TODO: maybe just make it Opaque instead?
    68  }
    69  
    70  // WithChildren implements the Node interface.
    71  func (p *PrepareQuery) WithChildren(children ...sql.Node) (sql.Node, error) {
    72  	if len(children) > 0 {
    73  		return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 0)
    74  	}
    75  	return p, nil
    76  }
    77  
    78  // CheckPrivileges implements the interface sql.Node.
    79  func (p *PrepareQuery) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    80  	return p.Child.CheckPrivileges(ctx, opChecker)
    81  }
    82  
    83  // CollationCoercibility implements the interface sql.CollationCoercible.
    84  func (*PrepareQuery) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    85  	return sql.Collation_binary, 7
    86  }
    87  
    88  func (p *PrepareQuery) String() string {
    89  	return fmt.Sprintf("Prepare(%s)", p.Child.String())
    90  }
    91  
    92  // ExecuteQuery is a node that prepares the query
    93  type ExecuteQuery struct {
    94  	Name     string
    95  	BindVars []sql.Expression
    96  }
    97  
    98  var _ sql.Node = (*ExecuteQuery)(nil)
    99  var _ sql.CollationCoercible = (*ExecuteQuery)(nil)
   100  
   101  // NewExecuteQuery executes a prepared statement
   102  func NewExecuteQuery(name string, bindVars ...sql.Expression) *ExecuteQuery {
   103  	return &ExecuteQuery{Name: name, BindVars: bindVars}
   104  }
   105  
   106  // Schema implements the Node interface.
   107  func (p *ExecuteQuery) Schema() sql.Schema {
   108  	panic("ExecuteQuery methods shouldn't be used")
   109  }
   110  
   111  // RowIter implements the Node interface.
   112  func (p *ExecuteQuery) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
   113  	panic("ExecuteQuery methods shouldn't be used")
   114  }
   115  
   116  func (p *ExecuteQuery) Resolved() bool {
   117  	panic("ExecuteQuery methods shouldn't be used")
   118  }
   119  
   120  func (p *ExecuteQuery) IsReadOnly() bool {
   121  	panic("ExecuteQuery methods shouldn't be used")
   122  }
   123  
   124  // Children implements the Node interface.
   125  func (p *ExecuteQuery) Children() []sql.Node {
   126  	panic("ExecuteQuery methods shouldn't be used")
   127  }
   128  
   129  // WithChildren implements the Node interface.
   130  func (p *ExecuteQuery) WithChildren(children ...sql.Node) (sql.Node, error) {
   131  	panic("ExecuteQuery methods shouldn't be used")
   132  }
   133  
   134  // CheckPrivileges implements the interface sql.Node.
   135  func (p *ExecuteQuery) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   136  	panic("ExecuteQuery methods shouldn't be used")
   137  }
   138  
   139  // CollationCoercibility implements the interface sql.CollationCoercible.
   140  func (*ExecuteQuery) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   141  	return sql.Collation_binary, 7
   142  }
   143  
   144  func (p *ExecuteQuery) String() string {
   145  	panic("ExecuteQuery methods shouldn't be used")
   146  }
   147  
   148  // DeallocateQuery is a node that prepares the query
   149  type DeallocateQuery struct {
   150  	Name string
   151  }
   152  
   153  var _ sql.Node = (*DeallocateQuery)(nil)
   154  var _ sql.CollationCoercible = (*DeallocateQuery)(nil)
   155  
   156  // NewDeallocateQuery executes a prepared statement
   157  func NewDeallocateQuery(name string) *DeallocateQuery {
   158  	return &DeallocateQuery{Name: name}
   159  }
   160  
   161  // Schema implements the Node interface.
   162  func (p *DeallocateQuery) Schema() sql.Schema {
   163  	return types.OkResultSchema
   164  }
   165  
   166  // RowIter implements the Node interface.
   167  func (p *DeallocateQuery) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
   168  	return sql.RowsToRowIter(sql.NewRow(types.OkResult{})), nil
   169  }
   170  
   171  func (p *DeallocateQuery) Resolved() bool {
   172  	return true
   173  }
   174  
   175  func (p *DeallocateQuery) IsReadOnly() bool {
   176  	return true
   177  }
   178  
   179  // Children implements the Node interface.
   180  func (p *DeallocateQuery) Children() []sql.Node {
   181  	return nil
   182  }
   183  
   184  // WithChildren implements the Node interface.
   185  func (p *DeallocateQuery) WithChildren(children ...sql.Node) (sql.Node, error) {
   186  	if len(children) > 0 {
   187  		return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 0)
   188  	}
   189  	return p, nil
   190  }
   191  
   192  // CheckPrivileges implements the interface sql.Node.
   193  func (p *DeallocateQuery) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   194  	return true
   195  }
   196  
   197  // CollationCoercibility implements the interface sql.CollationCoercible.
   198  func (*DeallocateQuery) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   199  	return sql.Collation_binary, 7
   200  }
   201  
   202  func (p *DeallocateQuery) String() string {
   203  	return fmt.Sprintf("Deallocate(%s)", p.Name)
   204  }