plpgsql

package
v0.51.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GetTypesCollectionFromContext func(ctx *sql.Context) (*typecollection.TypeCollection, error)

GetTypesCollectionFromContext is declared within the core package, but is assigned to this variable to work around import cycles.

Functions

func Call

func Call(ctx *sql.Context, iFunc InterpretedFunction, runner sql.StatementRunner, paramsAndReturn []*pgtypes.DoltgresType, vals []any) (any, error)

Call runs the contained operations on the given runner.

func OperationSizeForStatements

func OperationSizeForStatements(stmts []Statement) int32

OperationSizeForStatements returns the sum of OperationSize for every statement.

func TriggerCall added in v0.50.1

func TriggerCall(ctx *sql.Context, iFunc InterpretedFunction, runner sql.StatementRunner, sch sql.Schema, oldRow sql.Row, newRow sql.Row) (any, error)

TriggerCall runs the contained trigger operations on the given runner.

Types

type Assignment

type Assignment struct {
	VariableName  string
	Expression    string
	VariableIndex int32 // TODO: figure out what this is used for, probably to get around shadowed variables?
}

Assignment represents an assignment statement.

func (Assignment) AppendOperations

func (stmt Assignment) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (Assignment) OperationSize

func (Assignment) OperationSize() int32

OperationSize implements the interface Statement.

type Block

type Block struct {
	TriggerNew int32 // When non-zero, indicates that the NEW record exists for use with triggers
	TriggerOld int32 // When non-zero, indicates that the OLD record exists for use with triggers
	Variables  []Variable
	Records    []Record
	Body       []Statement
	Label      string
	IsLoop     bool
}

Block contains a collection of statements, alongside the variables that were declared for the block. Only the top-level block will contain parameter variables.

func (Block) AppendOperations

func (stmt Block) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (Block) OperationSize

func (stmt Block) OperationSize() int32

OperationSize implements the interface Statement.

type ExecuteSQL added in v0.17.1

type ExecuteSQL struct {
	Statement string
	Target    string
}

ExecuteSQL represents a standard SQL statement's execution (including the INTO syntax).

func (ExecuteSQL) AppendOperations added in v0.17.1

func (stmt ExecuteSQL) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (ExecuteSQL) OperationSize added in v0.17.1

func (ExecuteSQL) OperationSize() int32

OperationSize implements the interface Statement.

type Goto

type Goto struct {
	Offset         int32
	Label          string
	NearestScopeOp bool
}

Goto jumps to the counter at the given offset.

func (Goto) AppendOperations

func (stmt Goto) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (Goto) OperationSize

func (Goto) OperationSize() int32

OperationSize implements the interface Statement.

type If

type If struct {
	Condition  string
	GotoOffset int32
}

If represents an IF condition, alongside its Goto offset if the condition is true.

func (If) AppendOperations

func (stmt If) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (If) OperationSize

func (If) OperationSize() int32

OperationSize implements the interface Statement.

type InterpretedFunction

type InterpretedFunction interface {
	GetParameters() []*pgtypes.DoltgresType
	GetParameterNames() []string
	GetReturn() *pgtypes.DoltgresType
	GetStatements() []InterpreterOperation
	QueryMultiReturn(ctx *sql.Context, stack InterpreterStack, stmt string, bindings []string) (rows []sql.Row, err error)
	QuerySingleReturn(ctx *sql.Context, stack InterpreterStack, stmt string, targetType *pgtypes.DoltgresType, bindings []string) (val any, err error)
}

InterpretedFunction is an interface that essentially mirrors the implementation of InterpretedFunction in the framework package.

type InterpreterOperation

type InterpreterOperation struct {
	OpCode        OpCode
	PrimaryData   string            // This will represent the "main" data, such as the query for PERFORM, expression for IF, etc.
	SecondaryData []string          // This represents auxiliary data, such as bindings, strictness, etc.
	Target        string            // This is the variable that will store the results (if applicable)
	Index         int               // This is the index that should be set for operations that move the function counter
	Options       map[string]string // This is extra data for operations that need it
}

InterpreterOperation is an operation that will be performed by the interpreter.

func Parse

func Parse(fullCreateFunctionString string) ([]InterpreterOperation, error)

Parse parses the given CREATE FUNCTION string (which must be the entire string, not just the body) into a Block containing the contents of the body.

type InterpreterScopeDetails

type InterpreterScopeDetails struct {
	// contains filtered or unexported fields
}

InterpreterScopeDetails contains all of the details that are relevant to a particular scope.

type InterpreterStack

type InterpreterStack struct {
	// contains filtered or unexported fields
}

InterpreterStack represents the working information that an interpreter will use during execution. It is not exactly the same as a stack in the traditional programming sense, but rather is a loose abstraction that serves the same general purpose.

func NewInterpreterStack

func NewInterpreterStack(runner sql.StatementRunner) InterpreterStack

NewInterpreterStack creates a new InterpreterStack.

func (*InterpreterStack) Details

Details returns the details for the current scope.

func (*InterpreterStack) GetCurrentLabel added in v0.17.1

func (is *InterpreterStack) GetCurrentLabel() string

GetCurrentLabel traverses the stack (starting from the top) returning the first label found. Returns an empty string if no labels were set.

func (*InterpreterStack) GetVariable

GetVariable traverses the stack (starting from the top) to find a variable with a matching name. Returns nil if no variable was found.

func (*InterpreterStack) ListVariables

func (is *InterpreterStack) ListVariables() map[string][]string

ListVariables returns a map with the names of all variables. The attached slice represents field names for records. All names are lowercased.

func (*InterpreterStack) NewRecord added in v0.50.1

func (is *InterpreterStack) NewRecord(name string, sch sql.Schema, val sql.Row)

NewRecord creates a new record in the current scope. If a record with the same name exists in a previous scope, then that record will be shadowed until the current scope exits.

func (*InterpreterStack) NewVariable

func (is *InterpreterStack) NewVariable(name string, typ *pgtypes.DoltgresType)

NewVariable creates a new variable in the current scope. If a variable with the same name exists in a previous scope, then that variable will be shadowed until the current scope exits.

func (*InterpreterStack) NewVariableAlias

func (is *InterpreterStack) NewVariableAlias(alias string, target string)

NewVariableAlias creates a new variable alias, named |alias|, in the current frame of this stack, pointing to the specified |variable|.

func (*InterpreterStack) NewVariableWithValue

func (is *InterpreterStack) NewVariableWithValue(name string, typ *pgtypes.DoltgresType, val any)

NewVariableWithValue creates a new variable in the current scope, setting its initial value to the one given.

func (*InterpreterStack) PopScope

func (is *InterpreterStack) PopScope()

PopScope removes the current scope.

func (*InterpreterStack) PushScope

func (is *InterpreterStack) PushScope()

PushScope creates a new scope.

func (*InterpreterStack) Runner

func (is *InterpreterStack) Runner() sql.StatementRunner

Runner returns the runner that is being used for the function's execution.

func (*InterpreterStack) SetAnonymousLabel added in v0.17.1

func (is *InterpreterStack) SetAnonymousLabel()

SetAnonymousLabel sets the label for the current scope to a guaranteed unique value.

func (*InterpreterStack) SetLabel added in v0.17.1

func (is *InterpreterStack) SetLabel(label string)

SetLabel sets the label for the current scope.

func (*InterpreterStack) SetVariable

func (is *InterpreterStack) SetVariable(ctx *sql.Context, name string, val any) error

SetVariable sets the first variable found, with a matching name, to the value given. This does not ensure that the value matches the expectations of the type, so it should be validated before this is called. Returns an error if the variable cannot be found.

type InterpreterVariableReference added in v0.50.1

type InterpreterVariableReference struct {
	Type  *pgtypes.DoltgresType
	Value *any
}

InterpreterVariableReference is a reference to a variable that lives on the stack. If the type is not null, then it is valid to dereference the value for assignment. We make use of references rather than directly interacting with the variables as this allows for interacting with sections of aggregate types (such as record) as well as normal variable interaction.

type NoticeLevel added in v0.18.0

type NoticeLevel uint8

NoticeLevel represents the severity, or level, of a notice created by a RAISE statement.

const (
	NoticeLevelDebug     NoticeLevel = 14
	NoticeLevelLog       NoticeLevel = 15
	NoticeLevelInfo      NoticeLevel = 17
	NoticeLevelNotice    NoticeLevel = 18
	NoticeLevelWarning   NoticeLevel = 19
	NoticeLevelException NoticeLevel = 21
)

func (NoticeLevel) String added in v0.18.0

func (nl NoticeLevel) String() string

String returns a string representation of this NoticeLevel.

type NoticeOptionType added in v0.18.0

type NoticeOptionType uint8

NoticeOptionType represents the type of option specified for a notice in the USING clause of a RAISE statement.

const (
	NoticeOptionTypeErrCode    NoticeOptionType = 0
	NoticeOptionTypeMessage    NoticeOptionType = 1
	NoticeOptionTypeDetail     NoticeOptionType = 2
	NoticeOptionTypeHint       NoticeOptionType = 3
	NoticeOptionTypeConstraint NoticeOptionType = 5
	NoticeOptionTypeDataType   NoticeOptionType = 6
	NoticeOptionTypeTable      NoticeOptionType = 7
	NoticeOptionTypeSchema     NoticeOptionType = 8
)

type OpCode

type OpCode uint16

OpCode states the operation to be performed. Most operations have a direct analogue to a Pl/pgSQL operation, however some exist only in Doltgres (specific to our interpreter implementation).

const (
	OpCode_Alias      OpCode = iota // https://www.postgresql.org/docs/15/plpgsql-declarations.html#PLPGSQL-DECLARATION-ALIAS
	OpCode_Assign                   // https://www.postgresql.org/docs/15/plpgsql-statements.html#PLPGSQL-STATEMENTS-ASSIGNMENT
	OpCode_Case                     // https://www.postgresql.org/docs/15/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS
	OpCode_Declare                  // https://www.postgresql.org/docs/15/plpgsql-declarations.html
	OpCode_DeleteInto               // https://www.postgresql.org/docs/15/plpgsql-statements.html
	OpCode_Exception                // https://www.postgresql.org/docs/15/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING
	OpCode_Execute                  // Executing a standard SQL statement (expects no rows returned unless Target is specified)
	OpCode_Get                      // https://www.postgresql.org/docs/15/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS
	OpCode_Goto                     // All control-flow structures can be represented using Goto
	OpCode_If                       // https://www.postgresql.org/docs/15/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS
	OpCode_InsertInto               // https://www.postgresql.org/docs/15/plpgsql-statements.html
	OpCode_Perform                  // https://www.postgresql.org/docs/15/plpgsql-statements.html
	OpCode_Raise                    // https://www.postgresql.org/docs/15/plpgsql-errors-and-messages.html
	OpCode_Return                   // https://www.postgresql.org/docs/15/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING
	OpCode_ScopeBegin               // This is used for scope control, specific to Doltgres
	OpCode_ScopeEnd                 // This is used for scope control, specific to Doltgres
	OpCode_SelectInto               // https://www.postgresql.org/docs/15/plpgsql-statements.html
	OpCode_UpdateInto               // https://www.postgresql.org/docs/15/plpgsql-statements.html
)

type Perform added in v0.17.1

type Perform struct {
	Statement string
}

Perform represents a PERFORM statement.

func (Perform) AppendOperations added in v0.17.1

func (stmt Perform) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (Perform) OperationSize added in v0.17.1

func (Perform) OperationSize() int32

OperationSize implements the interface Statement.

type Raise added in v0.18.0

type Raise struct {
	Level   string
	Message string
	Params  []string
	Options map[string]string
}

Raise represents a RAISE statement

func (Raise) AppendOperations added in v0.18.0

func (r Raise) AppendOperations(ops *[]InterpreterOperation, _ *InterpreterStack) error

AppendOperations implements the interface Statement.

func (Raise) OperationSize added in v0.18.0

func (r Raise) OperationSize() int32

OperationSize implements the interface Statement.

type Record added in v0.50.1

type Record struct {
	Name   string
	Fields []string
}

Record represents a record (along with known fields for future access). These are exclusively found within Block.

type Return

type Return struct {
	Expression string
}

Return represents a RETURN statement.

func (Return) AppendOperations

func (stmt Return) AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error

AppendOperations implements the interface Statement.

func (Return) OperationSize

func (Return) OperationSize() int32

OperationSize implements the interface Statement.

type Statement

type Statement interface {
	// OperationSize reports the number of operations that the statement will convert to.
	OperationSize() int32
	// AppendOperations adds the statement to the operation slice.
	AppendOperations(ops *[]InterpreterOperation, stack *InterpreterStack) error
}

Statement represents a PL/pgSQL statement.

type Variable

type Variable struct {
	Name        string
	Type        string
	IsParameter bool
}

Variable represents a variable. These are exclusively found within Block.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL