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

     1  // Copyright 2021 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/types"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  )
    25  
    26  type Call struct {
    27  	db        sql.Database
    28  	Name      string
    29  	Params    []sql.Expression
    30  	asOf      sql.Expression
    31  	Procedure *Procedure
    32  	Pref      *expression.ProcedureReference
    33  	cat       *sql.Catalog
    34  }
    35  
    36  var _ sql.Node = (*Call)(nil)
    37  var _ sql.CollationCoercible = (*Call)(nil)
    38  var _ sql.Expressioner = (*Call)(nil)
    39  var _ Versionable = (*Call)(nil)
    40  
    41  // NewCall returns a *Call node.
    42  func NewCall(db sql.Database, name string, params []sql.Expression, asOf sql.Expression, catalog *sql.Catalog) *Call {
    43  	return &Call{
    44  		db:     db,
    45  		Name:   name,
    46  		Params: params,
    47  		asOf:   asOf,
    48  		cat:    catalog,
    49  	}
    50  }
    51  
    52  // Resolved implements the sql.Node interface.
    53  func (c *Call) Resolved() bool {
    54  	if c.db != nil {
    55  		_, ok := c.db.(sql.UnresolvedDatabase)
    56  		if ok {
    57  			return false
    58  		}
    59  	}
    60  	for _, param := range c.Params {
    61  		if !param.Resolved() {
    62  			return false
    63  		}
    64  	}
    65  	return true
    66  }
    67  
    68  func (c *Call) IsReadOnly() bool {
    69  	if c.Procedure == nil {
    70  		return true
    71  	}
    72  	return c.Procedure.IsReadOnly()
    73  }
    74  
    75  // Schema implements the sql.Node interface.
    76  func (c *Call) Schema() sql.Schema {
    77  	if c.Procedure != nil {
    78  		return c.Procedure.Schema()
    79  	}
    80  	return types.OkResultSchema
    81  }
    82  
    83  // Children implements the sql.Node interface.
    84  func (c *Call) Children() []sql.Node {
    85  	return nil
    86  }
    87  
    88  // WithChildren implements the sql.Node interface.
    89  func (c *Call) WithChildren(children ...sql.Node) (sql.Node, error) {
    90  	return NillaryWithChildren(c, children...)
    91  }
    92  
    93  // CheckPrivileges implements the interface sql.Node.
    94  func (c *Call) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
    95  	// Procedure permissions checking is performed in the same way MySQL does it, with an exception where
    96  	// procedures which are marked as AdminOnly. These procedures are only accessible to users with explicit Execute
    97  	// permissions on the procedure in question.
    98  
    99  	adminOnly := false
   100  	if c.cat != nil {
   101  		paramCount := len(c.Params)
   102  		proc, err := (*c.cat).ExternalStoredProcedure(ctx, c.Name, paramCount)
   103  		// Not finding the procedure isn't great - but that's going to surface with a better error later in the
   104  		// query execution. For the permission check, we'll proceed as though the procedure exists, and is not AdminOnly.
   105  		if proc != nil && err == nil && proc.AdminOnly {
   106  			adminOnly = true
   107  		}
   108  	}
   109  
   110  	if !adminOnly {
   111  		subject := sql.PrivilegeCheckSubject{Database: c.Database().Name()}
   112  		if opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Execute)) {
   113  			return true
   114  		}
   115  	}
   116  
   117  	subject := sql.PrivilegeCheckSubject{Database: c.Database().Name(), Routine: c.Name, IsProcedure: true}
   118  	return opChecker.RoutineAdminCheck(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Execute))
   119  }
   120  
   121  // CollationCoercibility implements the interface sql.CollationCoercible.
   122  func (c *Call) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   123  	return c.Procedure.CollationCoercibility(ctx)
   124  }
   125  
   126  // Expressions implements the sql.Expressioner interface.
   127  func (c *Call) Expressions() []sql.Expression {
   128  	return c.Params
   129  }
   130  
   131  // AsOf implements the Versionable interface.
   132  func (c *Call) AsOf() sql.Expression {
   133  	return c.asOf
   134  }
   135  
   136  // WithExpressions implements the sql.Expressioner interface.
   137  func (c *Call) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
   138  	if len(exprs) != len(c.Params) {
   139  		return nil, fmt.Errorf("%s: invalid param number, got %d, expected %d", c.Name, len(exprs), len(c.Params))
   140  	}
   141  
   142  	nc := *c
   143  	nc.Params = exprs
   144  	return &nc, nil
   145  }
   146  
   147  // WithAsOf implements the Versionable interface.
   148  func (c *Call) WithAsOf(asOf sql.Expression) (sql.Node, error) {
   149  	nc := *c
   150  	nc.asOf = asOf
   151  	return &nc, nil
   152  }
   153  
   154  // WithProcedure returns a new *Call containing the given *sql.Procedure.
   155  func (c *Call) WithProcedure(proc *Procedure) *Call {
   156  	nc := *c
   157  	nc.Procedure = proc
   158  	return &nc
   159  }
   160  
   161  // WithParamReference returns a new *Call containing the given *expression.ProcedureReference.
   162  func (c *Call) WithParamReference(pRef *expression.ProcedureReference) *Call {
   163  	nc := *c
   164  	nc.Pref = pRef
   165  	return &nc
   166  }
   167  
   168  // String implements the sql.Node interface.
   169  func (c *Call) String() string {
   170  	paramStr := ""
   171  	for i, param := range c.Params {
   172  		if i > 0 {
   173  			paramStr += ", "
   174  		}
   175  		paramStr += param.String()
   176  	}
   177  	if c.db == nil {
   178  		return fmt.Sprintf("CALL %s(%s)", c.Name, paramStr)
   179  	} else {
   180  		return fmt.Sprintf("CALL %s.%s(%s)", c.db.Name(), c.Name, paramStr)
   181  	}
   182  }
   183  
   184  // DebugString implements sql.DebugStringer
   185  func (c *Call) DebugString() string {
   186  	paramStr := ""
   187  	for i, param := range c.Params {
   188  		if i > 0 {
   189  			paramStr += ", "
   190  		}
   191  		paramStr += sql.DebugString(param)
   192  	}
   193  	tp := sql.NewTreePrinter()
   194  	if c.db == nil {
   195  		tp.WriteNode("CALL %s(%s)", c.Name, paramStr)
   196  	} else {
   197  		tp.WriteNode("CALL %s.%s(%s)", c.db.Name(), c.Name, paramStr)
   198  	}
   199  	if c.Procedure != nil {
   200  		tp.WriteChildren(sql.DebugString(c.Procedure.Body))
   201  	}
   202  
   203  	return tp.String()
   204  }
   205  
   206  // Database implements the sql.Databaser interface.
   207  func (c *Call) Database() sql.Database {
   208  	if c.db == nil {
   209  		return sql.UnresolvedDatabase("")
   210  	}
   211  	return c.db
   212  }
   213  
   214  // WithDatabase implements the sql.Databaser interface.
   215  func (c *Call) WithDatabase(db sql.Database) (sql.Node, error) {
   216  	nc := *c
   217  	nc.db = db
   218  	return &nc, nil
   219  }
   220  
   221  func (c *Call) Dispose() {
   222  	if c.Procedure != nil {
   223  		disposeNode(c.Procedure)
   224  	}
   225  }