MySQL 9.1.0
Source Code Documentation
sp_pcontext Class Reference

The class represents parse-time context, which keeps track of declared variables/parameters, conditions, handlers, cursors and labels. More...

#include <sp_pcontext.h>

Public Types

enum  enum_scope { REGULAR_SCOPE , HANDLER_SCOPE }
 

Public Member Functions

 sp_pcontext (THD *thd)
 
 ~sp_pcontext ()
 
sp_pcontextpush_context (THD *thd, enum_scope scope)
 Create and push a new context in the tree. More...
 
sp_pcontextpop_context ()
 Pop a node from the parsing context tree. More...
 
sp_pcontextparent_context () const
 
int get_level () const
 
size_t diff_handlers (const sp_pcontext *ctx, bool exclusive) const
 Calculate and return the number of handlers to pop between the given context and this one. More...
 
size_t diff_cursors (const sp_pcontext *ctx, bool exclusive) const
 Calculate and return the number of cursors to pop between the given context and this one. More...
 
uint max_var_index () const
 
uint current_var_count () const
 
uint context_var_count () const
 
uint var_context2runtime (uint i) const
 
sp_variableadd_variable (THD *thd, LEX_STRING name, enum enum_field_types type, sp_variable::enum_mode mode)
 Add SP-variable to the parsing context. More...
 
void retrieve_field_definitions (List< Create_field > *field_def_lst) const
 Retrieve full type information about SP-variables in this parsing context and its children. More...
 
sp_variablefind_variable (const char *name, size_t name_len, bool current_scope_only) const
 Find SP-variable by name. More...
 
sp_variablefind_variable (uint offset) const
 Find SP-variable by the offset in the root parsing context. More...
 
void declare_var_boundary (uint n)
 Set the current scope boundary (for default values). More...
 
int get_num_case_exprs () const
 
int push_case_expr_id ()
 
void pop_case_expr_id ()
 
int get_current_case_expr_id () const
 
sp_labelpush_label (THD *thd, LEX_CSTRING name, uint ip)
 
sp_labelfind_label (LEX_CSTRING name)
 
sp_labellast_label ()
 
sp_labelpop_label ()
 
bool add_condition (THD *thd, LEX_STRING name, sp_condition_value *value)
 
sp_condition_valuefind_condition (LEX_STRING name, bool current_scope_only) const
 See comment for find_variable() above. More...
 
sp_handleradd_handler (THD *thd, sp_handler::enum_type type)
 
bool check_duplicate_handler (const sp_condition_value *cond_value) const
 This is an auxiliary parsing-time function to check if an SQL-handler exists in the current parsing context (current scope) for the given SQL-condition. More...
 
sp_handlerfind_handler (const char *sql_state, uint sql_errno, Sql_condition::enum_severity_level severity) const
 Find an SQL handler for the given SQL condition according to the SQL-handler resolution rules. More...
 
bool add_cursor (LEX_STRING name)
 
bool find_cursor (LEX_STRING name, uint *poff, bool current_scope_only) const
 See comment for find_variable() above. More...
 
const LEX_STRINGfind_cursor (uint offset) const
 Find cursor by offset (for debugging only). More...
 
uint max_cursor_index () const
 
uint current_cursor_count () const
 

Private Member Functions

 sp_pcontext (THD *thd, sp_pcontext *prev, enum_scope scope)
 Constructor for a tree node. More...
 
void init (uint var_offset, uint cursor_offset, int num_case_expressions)
 
 sp_pcontext (const sp_pcontext &)
 
void operator= (sp_pcontext &)
 

Private Attributes

int m_level
 Level of the corresponding BEGIN..END block (0 means the topmost block). More...
 
uint m_max_var_index
 m_max_var_index – number of variables (including all types of arguments) in this context including all children contexts. More...
 
uint m_max_cursor_index
 The maximum sub context's framesizes. More...
 
sp_pcontextm_parent
 Parent context. More...
 
uint m_var_offset
 An index of the first SP-variable in this parsing context. More...
 
uint m_cursor_offset
 Cursor offset for this context. More...
 
uint m_pboundary
 Boundary for finding variables in this context. More...
 
int m_num_case_exprs
 
Mem_root_array< sp_variable * > m_vars
 SP parameters/variables. More...
 
Mem_root_array< int > m_case_expr_ids
 Stack of CASE expression ids. More...
 
Mem_root_array< sp_condition * > m_conditions
 Stack of SQL-conditions. More...
 
Mem_root_array< LEX_STRINGm_cursors
 Stack of cursors. More...
 
Mem_root_array< sp_handler * > m_handlers
 Stack of SQL-handlers. More...
 
List< sp_labelm_labels
 List of labels. More...
 
Mem_root_array< sp_pcontext * > m_children
 Children contexts, used for destruction. More...
 
enum_scope m_scope
 Scope of this parsing context. More...
 

Detailed Description

The class represents parse-time context, which keeps track of declared variables/parameters, conditions, handlers, cursors and labels.

sp_context objects are organized in a tree according to the following rules:

  • one sp_pcontext object corresponds for for each BEGIN..END block;
  • one sp_pcontext object corresponds for each exception handler;
  • one additional sp_pcontext object is created to contain Stored Program parameters.

sp_pcontext objects are used both at parse-time and at runtime.

During the parsing stage sp_pcontext objects are used:

  • to look up defined names (e.g. declared variables and visible labels);
  • to check for duplicates;
  • for error checking;
  • to calculate offsets to be used at runtime.

During the runtime phase, a tree of sp_pcontext objects is used:

  • for error checking (e.g. to check correct number of parameters);
  • to resolve SQL-handlers.

Member Enumeration Documentation

◆ enum_scope

Enumerator
REGULAR_SCOPE 

REGULAR_SCOPE designates regular BEGIN ... END blocks.

HANDLER_SCOPE 

HANDLER_SCOPE designates SQL-handler blocks.

Constructor & Destructor Documentation

◆ sp_pcontext() [1/3]

sp_pcontext::sp_pcontext ( THD thd)

◆ ~sp_pcontext()

sp_pcontext::~sp_pcontext ( )

◆ sp_pcontext() [2/3]

sp_pcontext::sp_pcontext ( THD thd,
sp_pcontext prev,
sp_pcontext::enum_scope  scope 
)
private

Constructor for a tree node.

Parameters
thdthread context
prevthe parent parsing context
scopescope of this parsing context

◆ sp_pcontext() [3/3]

sp_pcontext::sp_pcontext ( const sp_pcontext )
private

Member Function Documentation

◆ add_condition()

bool sp_pcontext::add_condition ( THD thd,
LEX_STRING  name,
sp_condition_value value 
)

◆ add_cursor()

bool sp_pcontext::add_cursor ( LEX_STRING  name)

◆ add_handler()

sp_handler * sp_pcontext::add_handler ( THD thd,
sp_handler::enum_type  type 
)

◆ add_variable()

sp_variable * sp_pcontext::add_variable ( THD thd,
LEX_STRING  name,
enum enum_field_types  type,
sp_variable::enum_mode  mode 
)

Add SP-variable to the parsing context.

Parameters
thdThread context.
nameName of the SP-variable.
typeType of the SP-variable.
modeMode of the SP-variable.
Returns
instance of newly added SP-variable.

◆ check_duplicate_handler()

bool sp_pcontext::check_duplicate_handler ( const sp_condition_value cond_value) const

This is an auxiliary parsing-time function to check if an SQL-handler exists in the current parsing context (current scope) for the given SQL-condition.

This function is used to check for duplicates during the parsing phase.

This function can not be used during the runtime phase to check SQL-handler existence because it searches for the SQL-handler in the current scope only (during runtime, current and parent scopes should be checked according to the SQL-handler resolution rules).

Parameters
cond_valuethe handler condition value (not SQL-condition!).
Return values
trueif such SQL-handler exists.
falseotherwise.

◆ context_var_count()

uint sp_pcontext::context_var_count ( ) const
inline
Returns
the number of variables in this context alone.

◆ current_cursor_count()

uint sp_pcontext::current_cursor_count ( ) const
inline

◆ current_var_count()

uint sp_pcontext::current_var_count ( ) const
inline
Returns
the current number of variables used in the parent contexts (from the root), including this context.

◆ declare_var_boundary()

void sp_pcontext::declare_var_boundary ( uint  n)
inline

Set the current scope boundary (for default values).

Parameters
nThe number of variables to skip.

◆ diff_cursors()

size_t sp_pcontext::diff_cursors ( const sp_pcontext ctx,
bool  exclusive 
) const

Calculate and return the number of cursors to pop between the given context and this one.

Parameters
ctxthe other parsing context.
exclusivespecifies if the last scope should be excluded.
Returns
the number of cursors to pop between the given context and this one. If 'exclusive' is true, don't count the last scope we are leaving; this is used for LEAVE where we will jump to the cpop instructions.

◆ diff_handlers()

size_t sp_pcontext::diff_handlers ( const sp_pcontext ctx,
bool  exclusive 
) const

Calculate and return the number of handlers to pop between the given context and this one.

Parameters
ctxthe other parsing context.
exclusivespecifies if the last scope should be excluded.
Returns
the number of handlers to pop between the given context and this one. If 'exclusive' is true, don't count the last scope we are leaving; this is used for LEAVE where we will jump to the hpop instructions.

◆ find_condition()

sp_condition_value * sp_pcontext::find_condition ( LEX_STRING  name,
bool  current_scope_only 
) const

See comment for find_variable() above.

◆ find_cursor() [1/2]

bool sp_pcontext::find_cursor ( LEX_STRING  name,
uint *  poff,
bool  current_scope_only 
) const

See comment for find_variable() above.

◆ find_cursor() [2/2]

const LEX_STRING * sp_pcontext::find_cursor ( uint  offset) const

Find cursor by offset (for debugging only).

◆ find_handler()

sp_handler * sp_pcontext::find_handler ( const char *  sql_state,
uint  sql_errno,
Sql_condition::enum_severity_level  severity 
) const

Find an SQL handler for the given SQL condition according to the SQL-handler resolution rules.

This function is used at runtime.

Parameters
sql_stateThe SQL condition state
sql_errnoThe error code
severityThe SQL condition severity level
Returns
a pointer to the found SQL-handler or NULL.

◆ find_label()

sp_label * sp_pcontext::find_label ( LEX_CSTRING  name)

◆ find_variable() [1/2]

sp_variable * sp_pcontext::find_variable ( const char *  name,
size_t  name_len,
bool  current_scope_only 
) const

Find SP-variable by name.

The function does a linear search (from newer to older variables, in case we have shadowed names).

The function is called only at parsing time.

Parameters
nameVariable name.
name_lenVariable name length.
current_scope_onlyA flag if we search only in current scope.
Returns
instance of found SP-variable, or NULL if not found.

◆ find_variable() [2/2]

sp_variable * sp_pcontext::find_variable ( uint  offset) const

Find SP-variable by the offset in the root parsing context.

The function is used for two things:

  • When evaluating parameters at the beginning, and setting out parameters at the end, of invocation. (Top frame only, so no recursion then.)
  • For printing of sp_instr_set. (Debug mode only.)
Parameters
offsetVariable offset in the root parsing context.
Returns
instance of found SP-variable, or NULL if not found.

◆ get_current_case_expr_id()

int sp_pcontext::get_current_case_expr_id ( ) const
inline

◆ get_level()

int sp_pcontext::get_level ( ) const
inline

◆ get_num_case_exprs()

int sp_pcontext::get_num_case_exprs ( ) const
inline

◆ init()

void sp_pcontext::init ( uint  var_offset,
uint  cursor_offset,
int  num_case_expressions 
)
private

◆ last_label()

sp_label * sp_pcontext::last_label ( )
inline

◆ max_cursor_index()

uint sp_pcontext::max_cursor_index ( ) const
inline

◆ max_var_index()

uint sp_pcontext::max_var_index ( ) const
inline
Returns
the maximum number of variables used in this and all child contexts. For the root parsing context, this gives us the number of slots needed for variables during the runtime phase.

◆ operator=()

void sp_pcontext::operator= ( sp_pcontext )
private

◆ parent_context()

sp_pcontext * sp_pcontext::parent_context ( ) const
inline

◆ pop_case_expr_id()

void sp_pcontext::pop_case_expr_id ( )
inline

◆ pop_context()

sp_pcontext * sp_pcontext::pop_context ( )

Pop a node from the parsing context tree.

Returns
the parent node.

◆ pop_label()

sp_label * sp_pcontext::pop_label ( )
inline

◆ push_case_expr_id()

int sp_pcontext::push_case_expr_id ( )
inline

◆ push_context()

sp_pcontext * sp_pcontext::push_context ( THD thd,
sp_pcontext::enum_scope  scope 
)

Create and push a new context in the tree.

Parameters
thdthread context.
scopescope of the new parsing context.
Returns
the node created.

◆ push_label()

sp_label * sp_pcontext::push_label ( THD thd,
LEX_CSTRING  name,
uint  ip 
)

◆ retrieve_field_definitions()

void sp_pcontext::retrieve_field_definitions ( List< Create_field > *  field_def_lst) const

Retrieve full type information about SP-variables in this parsing context and its children.

Parameters
[out]field_def_lstContainer to store type information.

◆ var_context2runtime()

uint sp_pcontext::var_context2runtime ( uint  i) const
inline
Returns
map index in this parsing context to runtime offset.

Member Data Documentation

◆ m_case_expr_ids

Mem_root_array<int> sp_pcontext::m_case_expr_ids
private

Stack of CASE expression ids.

◆ m_children

Mem_root_array<sp_pcontext *> sp_pcontext::m_children
private

Children contexts, used for destruction.

◆ m_conditions

Mem_root_array<sp_condition *> sp_pcontext::m_conditions
private

Stack of SQL-conditions.

◆ m_cursor_offset

uint sp_pcontext::m_cursor_offset
private

Cursor offset for this context.

◆ m_cursors

Mem_root_array<LEX_STRING> sp_pcontext::m_cursors
private

Stack of cursors.

◆ m_handlers

Mem_root_array<sp_handler *> sp_pcontext::m_handlers
private

Stack of SQL-handlers.

◆ m_labels

List<sp_label> sp_pcontext::m_labels
private

List of labels.

◆ m_level

int sp_pcontext::m_level
private

Level of the corresponding BEGIN..END block (0 means the topmost block).

◆ m_max_cursor_index

uint sp_pcontext::m_max_cursor_index
private

The maximum sub context's framesizes.

◆ m_max_var_index

uint sp_pcontext::m_max_var_index
private

m_max_var_index – number of variables (including all types of arguments) in this context including all children contexts.

m_max_var_index >= m_vars.size().

m_max_var_index of the root parsing context contains number of all variables (including arguments) in all enclosed contexts.

◆ m_num_case_exprs

int sp_pcontext::m_num_case_exprs
private

◆ m_parent

sp_pcontext* sp_pcontext::m_parent
private

Parent context.

◆ m_pboundary

uint sp_pcontext::m_pboundary
private

Boundary for finding variables in this context.

This is the number of variables currently "invisible" to default clauses. This is normally 0, but will be larger during parsing of DECLARE ... DEFAULT, to get the scope right for DEFAULT values.

◆ m_scope

enum_scope sp_pcontext::m_scope
private

Scope of this parsing context.

◆ m_var_offset

uint sp_pcontext::m_var_offset
private

An index of the first SP-variable in this parsing context.

The index belongs to a runtime table of SP-variables.

Note:

  • m_var_offset is 0 for root parsing context;
  • m_var_offset is different for all nested parsing contexts.

◆ m_vars

Mem_root_array<sp_variable *> sp_pcontext::m_vars
private

SP parameters/variables.


The documentation for this class was generated from the following files: