My Project
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Friends
sp_head Class Reference

#include <sp_head.h>

List of all members.

Public Types

enum  {
  HAS_RETURN = 1, MULTI_RESULTS = 8, CONTAINS_DYNAMIC_SQL = 16, IS_INVOKED = 32,
  HAS_SET_AUTOCOMMIT_STMT = 64, HAS_COMMIT_OR_ROLLBACK = 128, LOG_SLOW_STATEMENTS = 256, LOG_GENERAL_LOG = 512,
  HAS_SQLCOM_RESET = 1024, HAS_SQLCOM_FLUSH = 2048, MODIFIES_DATA = 4096
}

Public Member Functions

bool is_invoked () const
 Is this routine being executed?
ulong sp_cache_version () const
void set_sp_cache_version (ulong sp_cache_version)
 Set the value of the SP cache version.
Stored_program_creation_ctxget_creation_ctx ()
void set_creation_ctx (Stored_program_creation_ctx *creation_ctx)
void set_body_start (THD *thd, const char *begin_ptr)
 Set the body-definition start position.
void set_body_end (THD *thd)
 Set the statement-definition (body-definition) end position.
bool execute_trigger (THD *thd, const LEX_STRING *db_name, const LEX_STRING *table_name, GRANT_INFO *grant_info)
bool execute_function (THD *thd, Item **args, uint argcount, Field *return_fld)
bool execute_procedure (THD *thd, List< Item > *args)
bool show_create_routine (THD *thd, enum_sp_type type)
bool add_instr (THD *thd, sp_instr *instr)
bool modifies_data () const
uint instructions ()
sp_instrlast_instruction ()
bool reset_lex (THD *thd)
bool restore_lex (THD *thd)
char * name (uint *lenp=0) const
char * create_string (THD *thd, ulong *lenp)
Fieldcreate_result_field (uint field_max_length, const char *field_name, TABLE *table)
void set_info (longlong created, longlong modified, st_sp_chistics *chistics, sql_mode_t sql_mode)
void set_definer (const char *definer, uint definerlen)
void set_definer (const LEX_STRING *user_name, const LEX_STRING *host_name)
void optimize ()
void add_mark_lead (uint ip, List< sp_instr > *leads)
sp_instrget_instr (uint i)
bool add_used_tables_to_table_list (THD *thd, TABLE_LIST ***query_tables_last_ptr, TABLE_LIST *belong_to_view)
bool is_not_allowed_in_function (const char *where)
bool show_routine_code (THD *thd)
void propagate_attributes (Query_tables_list *prelocking_ctx)
sp_pcontextget_root_parsing_context () const
MEM_ROOT * get_persistent_mem_root () const
MEM_ROOT * get_current_mem_root () const
bool check_show_access (THD *thd, bool *full_access)
bool set_security_ctx (THD *thd, Security_context **save_ctx)

Static Public Member Functions

static void * operator new (size_t size) throw ()
static void operator delete (void *ptr, size_t size) throw ()

Public Attributes

enum_sp_type m_type
 Stored program type.
uint m_flags
 Stored program flags.
Create_field m_return_field_def
sp_parser_data m_parser_data
 Attributes used during the parsing stage only.
st_sp_chistics * m_chistics
 Stored program characteristics.
sql_mode_t m_sql_mode
LEX_STRING m_qname
 Fully qualified name (<db name>="">.<sp name>="">).
bool m_explicit_name
 Prepend the db name? */.
LEX_STRING m_db
LEX_STRING m_name
LEX_STRING m_params
LEX_STRING m_body
LEX_STRING m_body_utf8
LEX_STRING m_defstr
LEX_STRING m_definer_user
LEX_STRING m_definer_host
longlong m_created
longlong m_modified
ulong m_recursion_level
 Recursion level of the current SP instance. The levels are numbered from 0.
sp_headm_next_cached_sp
sp_headm_first_instance
 Pointer to the first element of the above list.
sp_headm_first_free_instance
sp_headm_last_cached_sp
HASH m_sroutines
Security_context m_security_ctx
SQL_I_List< SQL_I_List
< Item_trigger_field > > 
m_list_of_trig_fields_item_lists
SQL_I_List< Item_trigger_fieldm_cur_instr_trig_field_items
st_trg_chistics m_trg_chistics
 Trigger characteristics.
class Table_triggers_listm_trg_list
 The Table_triggers_list instance, where this trigger belongs to.

Friends

sp_headsp_start_parsing (THD *, enum_sp_type, sp_name *)

Detailed Description

sp_head represents one instance of a stored program. It might be of any type (stored procedure, function, trigger, event).


Member Enumeration Documentation

anonymous enum

Possible values of m_flags

Enumerator:
MODIFIES_DATA 

Marks routines that directly (i.e. not by calling other routines) change tables. Note that this flag is set automatically based on type of statements used in the stored routine and is different from routine characteristic provided by user in a form of CONTAINS SQL, READS SQL DATA, MODIFIES SQL DATA clauses. The latter are accepted by parser but pretty much ignored after that. We don't rely on them: a) for compatibility reasons. b) because in CONTAINS SQL case they don't provide enough information anyway.


Member Function Documentation

bool sp_head::add_instr ( THD *  thd,
sp_instr instr 
)

Add instruction to SP.

Parameters:
thdThread context.
instrInstruction.
Returns:
Error status.
void sp_head::add_mark_lead ( uint  ip,
List< sp_instr > *  leads 
)

Helper used during flow analysis during code optimization. See the implementation of opt_mark().

Parameters:
ipthe instruction to add to the leads list
leadsthe list of remaining paths to explore in the graph that represents the code, during flow analysis.
bool sp_head::add_used_tables_to_table_list ( THD *  thd,
TABLE_LIST ***  query_tables_last_ptr,
TABLE_LIST belong_to_view 
)

Add tables used by routine to the table list.

Converts multi-set of tables used by this routine to table list and adds this list to the end of table list specified by 'query_tables_last_ptr'.

Elements of list will be allocated in PS memroot, so this list will be persistent between PS executions.

Parameters:
[in]thdThread context
[in,out]query_tables_last_ptrPointer to the next_global member of last element of the list where tables will be added (or to its root).
[in]belong_to_viewUppermost view which uses this routine, NULL if none.
Return values:
trueif some elements were added
falseotherwise.
bool sp_head::check_show_access ( THD *  thd,
bool *  full_access 
)

Check if a user has access right to a SP.

Parameters:
thdThread context.
[out]full_accessSet to 1 if the user has SELECT to the 'mysql.proc' table or is the owner of the stored program.
Returns:
Error status.
Field * sp_head::create_result_field ( uint  field_max_length,
const char *  field_name,
TABLE table 
)

Create Field-object corresponding to the RETURN field of a stored function. This operation makes sense for stored functions only.

Parameters:
field_max_lengththe max length (in the sense of Item classes).
field_namethe field name (item name).
tablethe field's table.
Returns:
newly created and initialized Field-instance, or NULL in case of error.
bool sp_head::execute_function ( THD *  thd,
Item **  args,
uint  argcount,
Field return_fld 
)

Execute a function.

  • evaluate parameters
  • changes security context for SUID routines
  • switch to new memroot
  • call sp_head::execute
  • restore old memroot
  • evaluate the return value
  • restores security context
Parameters:
thdThread context.
argpPassed arguments (these are items from containing statement?)
argcountNumber of passed arguments. We need to check if this is correct.
return_value_fldSave result here.
Todo:
We should create sp_rcontext once per command and reuse it on subsequent executions of a function/trigger.
Todo:
In future we should associate call arena/mem_root with sp_rcontext and allocate all these objects (and sp_rcontext itself) on it directly rather than juggle with arenas.
Returns:
Error status.
bool sp_head::execute_procedure ( THD *  thd,
List< Item > *  args 
)

Execute a procedure.

The function does the following steps:

  • Set all parameters
  • changes security context for SUID routines
  • call sp_head::execute
  • copy back values of INOUT and OUT parameters
  • restores security context
Parameters:
thdThread context.
argsList of values passed as arguments.
Returns:
Error status.
bool sp_head::execute_trigger ( THD *  thd,
const LEX_STRING *  db_name,
const LEX_STRING *  table_name,
GRANT_INFO grant_info 
)

Execute trigger stored program.

  • changes security context for triggers
  • switch to new memroot
  • call sp_head::execute
  • restore old memroot
  • restores security context
Parameters:
thdThread context
dbdatabase name
tabletable name
grant_infoGRANT_INFO structure to be filled with information about definer's privileges on subject table
Todo:
  • TODO: we should create sp_rcontext once per command and reuse it on subsequent executions of a trigger.
Returns:
Error status.
MEM_ROOT* sp_head::get_current_mem_root ( ) const [inline]
Returns:
currently used mem-root.
sp_instr* sp_head::get_instr ( uint  i) [inline]

Get SP-instruction at given index.

NOTE: it is important to have *unsigned* int here, sometimes we get (-1) passed here, so it get's converted to MAX_INT, and the result of the function call is NULL.

MEM_ROOT* sp_head::get_persistent_mem_root ( ) const [inline]
Returns:
SP-persistent mem-root. Instructions and expressions are stored in its memory between executions.
Returns:
root parsing context for this stored program.
bool sp_head::is_not_allowed_in_function ( const char *  where) [inline]

Check if this stored routine contains statements disallowed in a stored function or trigger, and set an appropriate error message if this is the case.

bool sp_head::modifies_data ( ) const [inline]

Returns true if any substatement in the routine directly (not through another routine) modifies data/changes table.

See also:
Comment for MODIFIES_DATA flag.

Do some minimal optimization of the code:

  1. Mark used instructions
  2. While doing this, shortcut jumps to jump instructions
  3. Compact the code, removing unused instructions.

This is the main mark and move loop; it relies on the following methods in sp_instr and its subclasses:

  • opt_mark() : Mark instruction as reachable
  • opt_shortcut_jump(): Shortcut jumps to the final destination; used by opt_mark().
  • opt_move() : Update moved instruction
  • set_destination() : Set the new destination (jump instructions only)
bool sp_head::reset_lex ( THD *  thd)

Reset LEX-object during parsing, before we parse a sub statement.

Parameters:
thdThread context.
Returns:
Error status.
bool sp_head::restore_lex ( THD *  thd)

Restore LEX-object during parsing, after we have parsed a sub statement.

Parameters:
thdThread context.
Returns:
Error status.
bool sp_head::set_security_ctx ( THD *  thd,
Security_context **  save_ctx 
)

Change routine security context, and check if there is an EXECUTE privilege in new context. If there is no EXECUTE privilege, change the context back and return an error.

Parameters:
thdThread context.
[out]save_ctxWhere to save the old security context.
Todo:
Cache if the definer has the rights to use the object on the first usage and reset the cache only if someone does a GRANT statement that 'may' affect this.
Returns:
Error status.
bool sp_head::show_create_routine ( THD *  thd,
enum_sp_type  type 
)

Implement SHOW CREATE statement for stored routines.

Parameters:
thdThread context.
typeStored routine type (SP_TYPE_PROCEDURE or SP_TYPE_FUNCTION)
Returns:
Error status.
bool sp_head::show_routine_code ( THD *  thd)

Return the routine instructions as a result set.

Returns:
Error status.
ulong sp_head::sp_cache_version ( ) const [inline]

Get the value of the SP cache version, as remembered when the routine was inserted into the cache.


Friends And Related Function Documentation

sp_head* sp_start_parsing ( THD *  ,
enum_sp_type  ,
sp_name  
) [friend]

Start parsing of a stored program.

This function encapsulates all the steps necessary to initialize sp_head to start parsing SP.

Every successful call of sp_start_parsing() must finish with sp_finish_parsing().

Parameters:
thdThread context.
sp_typeThe stored program type
sp_nameThe stored progam name
Returns:
properly initialized sp_head-instance in case of success, or NULL is case of out-of-memory error.

Member Data Documentation

List of all the Item_trigger_field items created while parsing sp instruction. After parsing, in add_instr method this list is moved to per instruction Item_trigger_field list "sp_lex_instr::m_trig_field_list".

Pointer to the first free (non-INVOKED) routine in the list of cached instances for this SP. This pointer is set only for the first SP in the list of instances (see above m_first_cached_sp pointer). The pointer equal to 0 if we have no free instances. For non-first instance value of this pointer meaningless (point to itself);

Pointer to the last element in the list of instances of the SP. For non-first instance value of this pointer meaningless (point to itself);

List of item (Item_trigger_field objects)'s lists representing fields in old/new version of row in trigger. We use this list for checking whether all such fields are valid or not at trigger creation time and for binding these fields to TABLE object at table open (although for latter pointer to table being opened is probably enough).

A list of diferent recursion level instances for the same procedure. For every recursion level we have a sp_head instance. This instances connected in the list. The list ordered by increasing recursion level (m_recursion_level).

Definition of the RETURN-field (from the RETURNS-clause). It's used (and valid) for stored functions only.

sql_mode_t sp_head::m_sql_mode

The value of sql_mode system variable at the CREATE-time.

It should be stored along with the character sets in the Stored_program_creation_ctx.

Set containing names of stored routines used by this routine. Note that unlike elements of similar set for statement elements of this set are not linked in one list. Because of this we are able save memory by using for this set same objects that are used in 'sroutines' sets for statements of which this stored routine consists.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines