github.com/dolthub/go-mysql-server@v0.18.0/sql/procedures.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 sql
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  )
    21  
    22  // StoredProcedureDetails are the details of the stored procedure. Integrators only need to store and retrieve the given
    23  // details for a stored procedure, as the engine handles all parsing and processing.
    24  type StoredProcedureDetails struct {
    25  	Name            string    // The name of this stored procedure. Names must be unique within a database.
    26  	CreateStatement string    // The CREATE statement for this stored procedure.
    27  	CreatedAt       time.Time // The time that the stored procedure was created.
    28  	ModifiedAt      time.Time // The time of the last modification to the stored procedure.
    29  	SqlMode         string    // The SQL_MODE when this procedure was defined.
    30  }
    31  
    32  // ExternalStoredProcedureDetails are the details of an external stored procedure. Compared to standard stored
    33  // procedures, external ones are considered "built-in", in that they're not created by the user, and may not be modified
    34  // or deleted by a user. In addition, they're implemented as a function taking standard parameters, compared to stored
    35  // procedures being implemented as expressions.
    36  type ExternalStoredProcedureDetails struct {
    37  	// Name is the name of the external stored procedure. If two external stored procedures share a name, then they're
    38  	// considered overloaded. Standard stored procedures do not support overloading.
    39  	Name string
    40  	// Schema describes the row layout of the RowIter returned from Function.
    41  	Schema Schema
    42  	// Function is the implementation of the external stored procedure. All functions should have the following definition:
    43  	// `func(*Context, <PARAMETERS>) (RowIter, error)`. The <PARAMETERS> may be any of the following types: `bool`,
    44  	// `string`, `[]byte`, `int8`-`int64`, `uint8`-`uint64`, `float32`, `float64`, `time.Time`, or `Decimal`
    45  	// (shopspring/decimal). The architecture-dependent types `int` and `uint` (without a number) are also supported.
    46  	// It is valid to return a nil RowIter if there are no rows to be returned.
    47  	//
    48  	// Each parameter, by default, is an IN parameter. If the parameter type is a pointer, e.g. `*int32`, then it
    49  	// becomes an INOUT parameter. INOUT parameters will be given their zero value if the parameter's value is nil.
    50  	// There is no way to set a parameter as an OUT parameter.
    51  	//
    52  	// Values are converted to their nearest type before being passed in, following the conversion rules of their
    53  	// related SQL types. The exceptions are `time.Time` (treated as a `DATETIME`), string (treated as a `LONGTEXT` with
    54  	// the default collation) and Decimal (treated with a larger precision and scale). Take extra care when using decimal
    55  	// for an INOUT parameter, to ensure that the returned value fits the original's precision and scale, else an error
    56  	// will occur.
    57  	//
    58  	// As functions support overloading, each variant must have a completely unique function signature to prevent
    59  	// ambiguity. Uniqueness is determined by the number of parameters. If two functions are returned that have the same
    60  	// name and same number of parameters, then an error is thrown. If the last parameter is variadic, then the stored
    61  	// procedure functions as though it has the integer-max number of parameters. When an exact match is not found for
    62  	// overloaded functions, the largest function is used (which in this case will be the variadic function). Also, due
    63  	// to the usage of the integer-max for the parameter count, only one variadic function is allowed per function name.
    64  	// The type of the variadic parameter may not have a pointer type.
    65  	Function interface{}
    66  	// If true, the procedure is ReadOnly and can be run against a locked or read-only server.
    67  	ReadOnly bool
    68  	// If true, then this procedure's access control requires that the user must have explicit Execute permissions
    69  	// on the procedure in question. If false, then the user will be granted access to the procedure if they have Execute
    70  	// permissions on the DB. MySQL does not support anything like this, but it is useful for Dolt procedures which
    71  	// grant elevated access.
    72  	AdminOnly bool
    73  }
    74  
    75  // FakeCreateProcedureStmt returns a parseable CREATE PROCEDURE statement for this external stored procedure, as some
    76  // tools (such as Java's JDBC connector) require a valid statement in some situations.
    77  func (espd ExternalStoredProcedureDetails) FakeCreateProcedureStmt() string {
    78  	return fmt.Sprintf("CREATE PROCEDURE %s() SELECT 'External stored procedure';", espd.Name)
    79  }