MySQL 9.0.0
Source Code Documentation
sp_rcontext Class Reference

#include <sp_rcontext.h>

Classes

class  Handler_call_frame
 This class represents a call frame of SQL-handler (one invocation of a handler). More...
 
class  sp_handler_entry
 This is an auxiliary class to store entering instruction pointer for an SQL-handler. More...
 

Public Member Functions

 ~sp_rcontext ()
 
bool set_variable (THD *thd, uint var_idx, Item **value)
 
Itemget_item (uint var_idx) const
 
Item ** get_item_addr (uint var_idx) const
 
Fieldget_return_field () const
 
bool set_return_value (THD *thd, Item **return_value_item)
 
bool is_return_value_set () const
 
bool push_handler (sp_handler *handler, uint first_ip)
 Create a new sp_handler_entry instance and push it to the handler call stack. More...
 
void pop_handlers (sp_pcontext *current_scope)
 Pop and delete given number of sp_handler_entry instances from the handler call stack. More...
 
Handler_call_framecurrent_handler_frame () const
 Get the Handler_call_frame representing the currently active handler. More...
 
bool handle_sql_condition (THD *thd, uint *ip, const sp_instr *cur_spi)
 Handle current SQL condition (if any). More...
 
void exit_handler (THD *thd, sp_pcontext *target_scope)
 Handle return from SQL-handler. More...
 
uint get_last_handler_continue_ip () const
 
bool push_cursor (sp_instr_cpush *i)
 Create a new sp_cursor instance and push it to the cursor stack. More...
 
void pop_cursors (uint count)
 Pop and delete given number of sp_cursor instance from the cursor stack. More...
 
void pop_all_cursors ()
 
sp_cursorget_cursor (uint i) const
 
bool set_case_expr (THD *thd, int case_expr_id, Item **case_expr_item_ptr)
 Set CASE expression to the specified value. More...
 
Itemget_case_expr (int case_expr_id) const
 
Item ** get_case_expr_addr (int case_expr_id) const
 

Static Public Member Functions

static sp_rcontextcreate (THD *thd, const sp_pcontext *root_parsing_ctx, Field *return_value_fld)
 Construct and properly initialize a new sp_rcontext instance. More...
 

Public Attributes

Query_arenacallers_arena
 Arena used to (re) allocate items on. More...
 
bool end_partial_result_set
 Flag to end an open result set before start executing an SQL-handler (if one is found). More...
 
sp_headsp
 The stored program for which this runtime context is created. More...
 

Private Member Functions

 sp_rcontext (const sp_pcontext *root_parsing_ctx, Field *return_value_fld, bool in_sub_stmt)
 
 sp_rcontext (const sp_rcontext &)
 
void operator= (sp_rcontext &)
 
bool alloc_arrays (THD *thd)
 Internal function to allocate memory for arrays. More...
 
bool init_var_table (THD *thd)
 Create and initialize a table to store SP-variables. More...
 
bool init_var_items (THD *thd)
 Create and initialize an Item-adapter (Item_field) for each SP-var field. More...
 
Item_cachecreate_case_expr_holder (THD *thd, const Item *item) const
 Create an instance of appropriate Item_cache class depending on the specified type in the callers arena. More...
 
bool set_variable (THD *thd, Field *field, Item **value)
 
void pop_handler_frame (THD *thd)
 Pop the Handler_call_frame on top of the stack of active handlers. More...
 

Private Attributes

const sp_pcontextm_root_parsing_ctx
 Top-level (root) parsing context for this runtime context. More...
 
TABLEm_var_table
 Virtual table for storing SP-variables. More...
 
Bounds_checked_array< Item * > m_var_items
 Collection of Item_field proxies, each of them points to the corresponding field in m_var_table. More...
 
Fieldm_return_value_fld
 This is a pointer to a field, which should contain return value for stored functions (only). More...
 
bool m_return_value_set
 Indicates whether the return value (in m_return_value_fld) has been set during execution. More...
 
bool m_in_sub_stmt
 Flag to tell if the runtime context is created for a sub-statement. More...
 
Prealloced_array< sp_handler_entry *, 16 > m_visible_handlers
 Stack of visible handlers. More...
 
Prealloced_array< Handler_call_frame *, 16 > m_activated_handlers
 Stack of caught SQL conditions. More...
 
Bounds_checked_array< sp_cursor * > m_cstack
 Stack of cursors. More...
 
uint m_ccount
 Current number of cursors in m_cstack. More...
 
Bounds_checked_array< Item_cache * > m_case_expr_holders
 Array of CASE expression holders. More...
 

Constructor & Destructor Documentation

◆ ~sp_rcontext()

sp_rcontext::~sp_rcontext ( )

◆ sp_rcontext() [1/2]

sp_rcontext::sp_rcontext ( const sp_pcontext root_parsing_ctx,
Field return_value_fld,
bool  in_sub_stmt 
)
private

◆ sp_rcontext() [2/2]

sp_rcontext::sp_rcontext ( const sp_rcontext )
private

Member Function Documentation

◆ alloc_arrays()

bool sp_rcontext::alloc_arrays ( THD thd)
private

Internal function to allocate memory for arrays.

Parameters
thdThread handle.
Returns
error flag: false on success, true in case of failure.

◆ create()

sp_rcontext * sp_rcontext::create ( THD thd,
const sp_pcontext root_parsing_ctx,
Field return_value_fld 
)
static

Construct and properly initialize a new sp_rcontext instance.

The static create-function is needed because we need a way to return an error from the constructor.

Parameters
thdThread handle.
root_parsing_ctxTop-level parsing context for this stored program.
return_value_fldField object to store the return value (for stored functions only).
Returns
valid sp_rcontext object or NULL in case of OOM-error.

◆ create_case_expr_holder()

Item_cache * sp_rcontext::create_case_expr_holder ( THD thd,
const Item item 
) const
private

Create an instance of appropriate Item_cache class depending on the specified type in the callers arena.

Note
We should create cache items in the callers arena, as they are used between in several instructions.
Parameters
thdThread handler.
itemItem to get the expression type.
Returns
Pointer to valid object on success, or NULL in case of error.

◆ current_handler_frame()

Handler_call_frame * sp_rcontext::current_handler_frame ( ) const
inline

Get the Handler_call_frame representing the currently active handler.

◆ exit_handler()

void sp_rcontext::exit_handler ( THD thd,
sp_pcontext target_scope 
)

Handle return from SQL-handler.

Parameters
thdThread handle.
target_scopeThe BEGIN..END block, containing the target (next) instruction.

◆ get_case_expr()

Item * sp_rcontext::get_case_expr ( int  case_expr_id) const
inline

◆ get_case_expr_addr()

Item ** sp_rcontext::get_case_expr_addr ( int  case_expr_id) const
inline

◆ get_cursor()

sp_cursor * sp_rcontext::get_cursor ( uint  i) const
inline

◆ get_item()

Item * sp_rcontext::get_item ( uint  var_idx) const
inline

◆ get_item_addr()

Item ** sp_rcontext::get_item_addr ( uint  var_idx) const
inline

◆ get_last_handler_continue_ip()

uint sp_rcontext::get_last_handler_continue_ip ( ) const
inline
Returns
the continue instruction pointer of the last activated CONTINUE handler. This function must not be called for the EXIT handlers.

◆ get_return_field()

Field * sp_rcontext::get_return_field ( ) const
inline

◆ handle_sql_condition()

bool sp_rcontext::handle_sql_condition ( THD thd,
uint *  ip,
const sp_instr cur_spi 
)

Handle current SQL condition (if any).

This is the public-interface function to handle SQL conditions in stored routines.

Parameters
thdThread handle.
[out]ipInstruction pointer to the first handler instruction.
cur_spiCurrent SP instruction.
Return values
trueif an SQL-handler has been activated. That means, all of the following conditions are satisfied:
  • the SP-instruction raised SQL-condition(s),
  • and there is an SQL-handler to process at least one of those SQL-conditions,
  • and that SQL-handler has been activated. Note, that the return value has nothing to do with "error flag" semantics.
falseotherwise.

◆ init_var_items()

bool sp_rcontext::init_var_items ( THD thd)
private

Create and initialize an Item-adapter (Item_field) for each SP-var field.

param thd Thread handle.

Returns
error flag.
Return values
falseon success.
trueon error.

◆ init_var_table()

bool sp_rcontext::init_var_table ( THD thd)
private

Create and initialize a table to store SP-variables.

param thd Thread handle.

Returns
error flag.
Return values
falseon success.
trueon error.

◆ is_return_value_set()

bool sp_rcontext::is_return_value_set ( ) const
inline

◆ operator=()

void sp_rcontext::operator= ( sp_rcontext )
private

◆ pop_all_cursors()

void sp_rcontext::pop_all_cursors ( )
inline

◆ pop_cursors()

void sp_rcontext::pop_cursors ( uint  count)

Pop and delete given number of sp_cursor instance from the cursor stack.

Parameters
countNumber of cursors to pop & delete.

◆ pop_handler_frame()

void sp_rcontext::pop_handler_frame ( THD thd)
private

Pop the Handler_call_frame on top of the stack of active handlers.

Also pop the matching Diagnostics Area and transfer conditions.

◆ pop_handlers()

void sp_rcontext::pop_handlers ( sp_pcontext current_scope)

Pop and delete given number of sp_handler_entry instances from the handler call stack.

Parameters
current_scopeThe current BEGIN..END block.

◆ push_cursor()

bool sp_rcontext::push_cursor ( sp_instr_cpush i)

Create a new sp_cursor instance and push it to the cursor stack.

Parameters
iCursor-push instruction.
Returns
error flag.
Return values
falseon success.
trueon error.

◆ push_handler()

bool sp_rcontext::push_handler ( sp_handler handler,
uint  first_ip 
)

Create a new sp_handler_entry instance and push it to the handler call stack.

Parameters
handlerSQL-handler object.
first_ipFirst instruction pointer of the handler.
Returns
error flag.
Return values
falseon success.
trueon error.

◆ set_case_expr()

bool sp_rcontext::set_case_expr ( THD thd,
int  case_expr_id,
Item **  case_expr_item_ptr 
)

Set CASE expression to the specified value.

Parameters
thdThread handler.
case_expr_idThe CASE expression identifier.
case_expr_item_ptrThe CASE expression value
Returns
error flag.
Return values
falseon success.
trueon error.
Note
The idea is to reuse Item_cache for the expression of the one CASE statement. This optimization takes place when there is CASE statement inside of a loop. So, in other words, we will use the same object on each iteration instead of creating a new one for each iteration.

TODO Hypothetically, a type of CASE expression can be different for each iteration. For instance, this can happen if the expression contains a session variable (something like @VAR) and its type is changed from one iteration to another.

In order to cope with this problem, we check type each time, when we use already created object. If the type does not match, we re-create Item. This also can (should?) be optimized.

◆ set_return_value()

bool sp_rcontext::set_return_value ( THD thd,
Item **  return_value_item 
)

◆ set_variable() [1/2]

bool sp_rcontext::set_variable ( THD thd,
Field field,
Item **  value 
)
private

◆ set_variable() [2/2]

bool sp_rcontext::set_variable ( THD thd,
uint  var_idx,
Item **  value 
)
inline

Member Data Documentation

◆ callers_arena

Query_arena* sp_rcontext::callers_arena

Arena used to (re) allocate items on.

E.g. reallocate INOUT/OUT SP-variables when they don't fit into prealloced items. This is common situation with String items. It is used mainly in sp_eval_func_item().

◆ end_partial_result_set

bool sp_rcontext::end_partial_result_set

Flag to end an open result set before start executing an SQL-handler (if one is found).

Otherwise the client will hang due to a violation of the client/server protocol.

◆ m_activated_handlers

Prealloced_array<Handler_call_frame *, 16> sp_rcontext::m_activated_handlers
private

Stack of caught SQL conditions.

◆ m_case_expr_holders

Bounds_checked_array<Item_cache *> sp_rcontext::m_case_expr_holders
private

Array of CASE expression holders.

◆ m_ccount

uint sp_rcontext::m_ccount
private

Current number of cursors in m_cstack.

◆ m_cstack

Bounds_checked_array<sp_cursor *> sp_rcontext::m_cstack
private

Stack of cursors.

◆ m_in_sub_stmt

bool sp_rcontext::m_in_sub_stmt
private

Flag to tell if the runtime context is created for a sub-statement.

◆ m_return_value_fld

Field* sp_rcontext::m_return_value_fld
private

This is a pointer to a field, which should contain return value for stored functions (only).

For stored procedures, this pointer is NULL.

◆ m_return_value_set

bool sp_rcontext::m_return_value_set
private

Indicates whether the return value (in m_return_value_fld) has been set during execution.

◆ m_root_parsing_ctx

const sp_pcontext* sp_rcontext::m_root_parsing_ctx
private

Top-level (root) parsing context for this runtime context.

◆ m_var_items

Bounds_checked_array<Item *> sp_rcontext::m_var_items
private

Collection of Item_field proxies, each of them points to the corresponding field in m_var_table.

◆ m_var_table

TABLE* sp_rcontext::m_var_table
private

Virtual table for storing SP-variables.

◆ m_visible_handlers

Prealloced_array<sp_handler_entry *, 16> sp_rcontext::m_visible_handlers
private

Stack of visible handlers.

◆ sp

sp_head* sp_rcontext::sp

The stored program for which this runtime context is created.


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