MySQL 8.4.0
Source Code Documentation
sp.h File Reference
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <string>
#include "field_types.h"
#include "lex_string.h"
#include "map_helpers.h"
#include "my_inttypes.h"
#include "mysql/udf_registration_types.h"
#include "sql/item.h"
#include "sql/sp_head.h"
#include "sql/sql_lex.h"

Go to the source code of this file.

Classes

class  Stored_routine_creation_ctx
 Stored_routine_creation_ctx – creation context of stored routines (stored procedures and functions). More...
 
class  Sroutine_hash_entry
 Structure that represents element in the set of stored routines used by statement or routine. More...
 

Namespaces

namespace  dd
 The version of the current data dictionary table definitions.
 

Macros

#define SP_DEFAULT_ACCESS_MAPPING   SP_CONTAINS_SQL
 
#define SP_DEFAULT_SUID_MAPPING   SP_IS_SUID
 

Typedefs

using sql_mode_t = uint64_t
 

Enumerations

enum  enum_sp_return_code {
  SP_OK = 0 , SP_NO_DB_ERROR , SP_DOES_NOT_EXISTS , SP_ALREADY_EXISTS ,
  SP_STORE_FAILED , SP_DROP_FAILED , SP_LOAD_FAILED , SP_PARSE_ERROR ,
  SP_INTERNAL_ERROR
}
 
enum  {
  MYSQL_PROC_FIELD_DB = 0 , MYSQL_PROC_FIELD_NAME , MYSQL_PROC_MYSQL_TYPE , MYSQL_PROC_FIELD_SPECIFIC_NAME ,
  MYSQL_PROC_FIELD_LANGUAGE , MYSQL_PROC_FIELD_ACCESS , MYSQL_PROC_FIELD_DETERMINISTIC , MYSQL_PROC_FIELD_SECURITY_TYPE ,
  MYSQL_PROC_FIELD_PARAM_LIST , MYSQL_PROC_FIELD_RETURNS , MYSQL_PROC_FIELD_BODY , MYSQL_PROC_FIELD_DEFINER ,
  MYSQL_PROC_FIELD_CREATED , MYSQL_PROC_FIELD_MODIFIED , MYSQL_PROC_FIELD_SQL_MODE , MYSQL_PROC_FIELD_COMMENT ,
  MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT , MYSQL_PROC_FIELD_COLLATION_CONNECTION , MYSQL_PROC_FIELD_DB_COLLATION , MYSQL_PROC_FIELD_BODY_UTF8 ,
  MYSQL_PROC_FIELD_COUNT
}
 
enum class  Sp_name_normalize_type { LEAVE_AS_IS = 0 , LOWERCASE_NAME , UNACCENT_AND_LOWERCASE_NAME }
 

Functions

bool sp_drop_db_routines (THD *thd, const dd::Schema &schema)
 Drop all routines in database 'db'. More...
 
bool lock_db_routines (THD *thd, const dd::Schema &schema)
 Acquires exclusive metadata lock on all stored routines in the given database. More...
 
sp_headsp_find_routine (THD *thd, enum_sp_type type, sp_name *name, sp_cache **cp, bool cache_only)
 Obtain object representing stored procedure/function by its name from stored procedures cache and looking into data dictionary if needed. More...
 
sp_headsp_setup_routine (THD *thd, enum_sp_type type, sp_name *name, sp_cache **cp)
 Setup a cached routine for execution. More...
 
enum_sp_return_code sp_cache_routine (THD *thd, Sroutine_hash_entry *rt, bool lookup_only, sp_head **sp)
 A helper wrapper around sp_cache_routine() to use from prelocking until 'sp_name' is eradicated as a class. More...
 
enum_sp_return_code sp_cache_routine (THD *thd, enum_sp_type type, const sp_name *name, bool lookup_only, sp_head **sp)
 Ensure that routine is present in cache by loading it from the data dictionary if needed. More...
 
bool sp_exist_routines (THD *thd, Table_ref *procs, bool is_proc)
 This is used by sql_acl.cc:mysql_routine_grant() and is used to find the routines in 'routines'. More...
 
bool sp_show_create_routine (THD *thd, enum_sp_type type, sp_name *name)
 Implement SHOW CREATE statement for stored routines. More...
 
enum_sp_return_code db_load_routine (THD *thd, enum_sp_type type, const char *sp_db, size_t sp_db_len, const char *sp_name, size_t sp_name_len, sp_head **sphp, sql_mode_t sql_mode, const char *params, const char *returns, const char *body, st_sp_chistics *chistics, const char *definer_user, const char *definer_host, longlong created, longlong modified, Stored_program_creation_ctx *creation_ctx)
 
bool sp_create_routine (THD *thd, sp_head *sp, const LEX_USER *definer, bool if_not_exists, bool &sp_already_exists)
 Creates a stored routine. More...
 
bool sp_update_routine (THD *thd, enum_sp_type type, sp_name *name, st_sp_chistics *chistics)
 Updates(Alter) a stored routine. More...
 
enum_sp_return_code sp_drop_routine (THD *thd, enum_sp_type type, sp_name *name)
 Drops a stored routine. More...
 
bool sp_add_used_routine (Query_tables_list *prelocking_ctx, Query_arena *arena, Sroutine_hash_entry::entry_type type, const char *db, size_t db_length, const char *name, size_t name_length, bool lowercase_db, Sp_name_normalize_type name_normalize_type, bool own_routine, Table_ref *belong_to_view)
 Add routine or trigger which is used by statement to the set of stored routines used by this statement. More...
 
bool sp_add_own_used_routine (Query_tables_list *prelocking_ctx, Query_arena *arena, Sroutine_hash_entry::entry_type type, sp_name *sp_name)
 Convenience wrapper around sp_add_used_routine() for most common case - stored procedure or function which are explicitly used by the statement. More...
 
void sp_remove_not_own_routines (Query_tables_list *prelocking_ctx)
 Remove routines which are only indirectly used by statement from the set of routines used by this statement. More...
 
void sp_update_stmt_used_routines (THD *thd, Query_tables_list *prelocking_ctx, malloc_unordered_map< std::string, Sroutine_hash_entry * > *src, Table_ref *belong_to_view)
 Add contents of hash representing set of routines to the set of routines used by statement. More...
 
void sp_update_stmt_used_routines (THD *thd, Query_tables_list *prelocking_ctx, SQL_I_List< Sroutine_hash_entry > *src, Table_ref *belong_to_view)
 Add contents of list representing set of routines to the set of routines used by statement. More...
 
const ucharsp_sroutine_key (const uchar *ptr, size_t *plen)
 
bool load_charset (MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs, const CHARSET_INFO **cs)
 
bool load_collation (MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl)
 
sp_headsp_start_parsing (THD *thd, enum_sp_type sp_type, sp_name *sp_name)
 Start parsing of a stored program. More...
 
void sp_finish_parsing (THD *thd)
 Finish parsing of a stored program. More...
 
uint sp_get_flags_for_command (LEX *lex)
 
bool sp_check_name (LEX_STRING *ident)
 Check that the name 'ident' is ok. More...
 
Table_refsp_add_to_query_tables (THD *thd, LEX *lex, const char *db, const char *name)
 
Itemsp_prepare_func_item (THD *thd, Item **it_addr)
 Prepare an Item for evaluation (call of fix_fields). More...
 
bool sp_eval_expr (THD *thd, Field *result_field, Item **expr_item_ptr)
 Evaluate an expression and store the result in the field. More...
 
Stringsp_get_item_value (THD *thd, Item *item, String *str)
 Return a string representation of the Item value. More...
 

Variables

static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH = 4294967295U
 
static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH = 65535
 

Macro Definition Documentation

◆ SP_DEFAULT_ACCESS_MAPPING

#define SP_DEFAULT_ACCESS_MAPPING   SP_CONTAINS_SQL

◆ SP_DEFAULT_SUID_MAPPING

#define SP_DEFAULT_SUID_MAPPING   SP_IS_SUID

Typedef Documentation

◆ sql_mode_t

using sql_mode_t = uint64_t

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MYSQL_PROC_FIELD_DB 
MYSQL_PROC_FIELD_NAME 
MYSQL_PROC_MYSQL_TYPE 
MYSQL_PROC_FIELD_SPECIFIC_NAME 
MYSQL_PROC_FIELD_LANGUAGE 
MYSQL_PROC_FIELD_ACCESS 
MYSQL_PROC_FIELD_DETERMINISTIC 
MYSQL_PROC_FIELD_SECURITY_TYPE 
MYSQL_PROC_FIELD_PARAM_LIST 
MYSQL_PROC_FIELD_RETURNS 
MYSQL_PROC_FIELD_BODY 
MYSQL_PROC_FIELD_DEFINER 
MYSQL_PROC_FIELD_CREATED 
MYSQL_PROC_FIELD_MODIFIED 
MYSQL_PROC_FIELD_SQL_MODE 
MYSQL_PROC_FIELD_COMMENT 
MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT 
MYSQL_PROC_FIELD_COLLATION_CONNECTION 
MYSQL_PROC_FIELD_DB_COLLATION 
MYSQL_PROC_FIELD_BODY_UTF8 
MYSQL_PROC_FIELD_COUNT 

◆ enum_sp_return_code

Enumerator
SP_OK 
SP_NO_DB_ERROR 
SP_DOES_NOT_EXISTS 
SP_ALREADY_EXISTS 
SP_STORE_FAILED 
SP_DROP_FAILED 
SP_LOAD_FAILED 
SP_PARSE_ERROR 
SP_INTERNAL_ERROR 

◆ Sp_name_normalize_type

enum class Sp_name_normalize_type
strong
Enumerator
LEAVE_AS_IS 
LOWERCASE_NAME 
UNACCENT_AND_LOWERCASE_NAME 

Function Documentation

◆ db_load_routine()

enum_sp_return_code db_load_routine ( THD thd,
enum_sp_type  type,
const char *  sp_db,
size_t  sp_db_len,
const char *  sp_name,
size_t  sp_name_len,
sp_head **  sphp,
sql_mode_t  sql_mode,
const char *  params,
const char *  returns,
const char *  body,
st_sp_chistics chistics,
const char *  definer_user,
const char *  definer_host,
longlong  created,
longlong  modified,
Stored_program_creation_ctx creation_ctx 
)

◆ load_charset()

bool load_charset ( MEM_ROOT mem_root,
Field field,
const CHARSET_INFO dflt_cs,
const CHARSET_INFO **  cs 
)

◆ load_collation()

bool load_collation ( MEM_ROOT mem_root,
Field field,
const CHARSET_INFO dflt_cl,
const CHARSET_INFO **  cl 
)

◆ lock_db_routines()

bool lock_db_routines ( THD thd,
const dd::Schema schema 
)

Acquires exclusive metadata lock on all stored routines in the given database.

Parameters
thdThread handler
schemaSchema object
Return values
falseSuccess
trueFailure

◆ sp_add_own_used_routine()

bool sp_add_own_used_routine ( Query_tables_list prelocking_ctx,
Query_arena arena,
Sroutine_hash_entry::entry_type  type,
sp_name sp_name 
)
inline

Convenience wrapper around sp_add_used_routine() for most common case - stored procedure or function which are explicitly used by the statement.

◆ sp_add_to_query_tables()

Table_ref * sp_add_to_query_tables ( THD thd,
LEX lex,
const char *  db,
const char *  name 
)

◆ sp_add_used_routine()

bool sp_add_used_routine ( Query_tables_list prelocking_ctx,
Query_arena arena,
Sroutine_hash_entry::entry_type  type,
const char *  db,
size_t  db_length,
const char *  name,
size_t  name_length,
bool  lowercase_db,
Sp_name_normalize_type  name_normalize_type,
bool  own_routine,
Table_ref belong_to_view 
)

Add routine or trigger which is used by statement to the set of stored routines used by this statement.

To be friendly towards prepared statements one should pass persistent arena as second argument.

Parameters
prelocking_ctxPrelocking context of the statement
arenaArena in which memory for new element of the set will be allocated
typeRoutine type (one of FUNCTION/PROCEDURE/ TRIGGER ...)
dbDatabase name
db_lengthDatabase name length
nameRoutine name
name_lengthRoutine name length
lowercase_dbIndicates whether db needs to be lowercased when constructing key.
name_normalize_typeIndicates if names needs to be normalized (lowercased / accent needs to be removed).
own_routineIndicates whether routine is explicitly or implicitly used.
belong_to_viewUppermost view which uses this routine (nullptr if routine is not used by view)
Note
Will also add element to end of 'Query_tables_list::sroutines_list' list (and will take into account if this is an explicitly used routine).
Return values
True- new element was added.
False- element was not added (because it is already present in the set).

◆ sp_cache_routine() [1/2]

enum_sp_return_code sp_cache_routine ( THD thd,
enum_sp_type  type,
const sp_name name,
bool  lookup_only,
sp_head **  sp 
)

Ensure that routine is present in cache by loading it from the data dictionary if needed.

If the routine is present but old, reload it. Emit an appropriate error if there was a problem during loading.

Parameters
[in]thdThread context.
[in]typeType of object (FUNCTION or PROCEDURE).
[in]nameName of routine.
[in]lookup_onlyOnly check that the routine is in the cache. If it's not, don't try to load. If it is present, but old, don't try to reload.
[out]spPointer to sp_head object for routine, NULL if routine was not found.
Return values
SP_OKEither routine is found and was successfully loaded into cache or it does not exist.
non-SP_OKError while loading routine from DD table.

◆ sp_cache_routine() [2/2]

enum_sp_return_code sp_cache_routine ( THD thd,
Sroutine_hash_entry rt,
bool  lookup_only,
sp_head **  sp 
)

A helper wrapper around sp_cache_routine() to use from prelocking until 'sp_name' is eradicated as a class.

◆ sp_check_name()

bool sp_check_name ( LEX_STRING ident)

Check that the name 'ident' is ok.

It's assumed to be an 'ident' from the parser, so we only have to check length and trailing spaces. The former is a standard requirement (and 'show status' assumes a non-empty name), the latter is a mysql:ism as trailing spaces are removed by get_field().

Return values
truebad name
falsename is ok

◆ sp_create_routine()

bool sp_create_routine ( THD thd,
sp_head sp,
const LEX_USER definer,
bool  if_not_exists,
bool &  sp_already_exists 
)

Creates a stored routine.

Atomicity: The operation to create a stored routine is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. Data-dictionary, stored routines and table definition caches are in sync with operation state. Cache do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log.

Parameters
thdThread context.
spStored routine object to store.
definerDefiner of the SP.
if_not_existsTrue if 'IF NOT EXISTS' clause was specified.
[out]sp_already_existsSet to true if routine already exists.
Return values
falseSuccess.
trueError.

◆ sp_drop_db_routines()

bool sp_drop_db_routines ( THD thd,
const dd::Schema schema 
)

Drop all routines in database 'db'.

Parameters
thdThread context.
schemaSchema object.
Return values
falseSuccess
trueError

◆ sp_drop_routine()

enum_sp_return_code sp_drop_routine ( THD thd,
enum_sp_type  type,
sp_name name 
)

Drops a stored routine.

Atomicity: The operation to drop a stored routine is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. Data-dictionary, stored routines and table definition caches are in sync with operation state. Cache do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log.

Parameters
thdThread context.
typeStored routine type (PROCEDURE or FUNCTION)
nameStored routine name.
Returns
Error code. SP_OK is returned on success. Other SP_ constants are used to indicate about errors.

◆ sp_eval_expr()

bool sp_eval_expr ( THD thd,
Field result_field,
Item **  expr_item_ptr 
)

Evaluate an expression and store the result in the field.

Parameters
thdcurrent thread object
result_fieldthe field to store the result
expr_item_ptrthe root item of the expression
Return values
falseon success
trueon error

◆ sp_exist_routines()

bool sp_exist_routines ( THD thd,
Table_ref routines,
bool  is_proc 
)

This is used by sql_acl.cc:mysql_routine_grant() and is used to find the routines in 'routines'.

Parameters
thdThread handler
routinesList of needles in the hay stack
is_procIndicates whether routines in the list are procedures or functions.
Return values
falseFound.
trueNot found

◆ sp_find_routine()

sp_head * sp_find_routine ( THD thd,
enum_sp_type  type,
sp_name name,
sp_cache **  cp,
bool  cache_only 
)

Obtain object representing stored procedure/function by its name from stored procedures cache and looking into data dictionary if needed.

Parameters
thdthread context
typetype of object (FUNCTION or PROCEDURE)
namename of procedure
cphash to look routine in
cache_onlyif true perform cache-only lookup (Don't look in data dictionary)
Return values
NonNULLpointer to sp_head object for the procedure
NULLin case of error.

◆ sp_finish_parsing()

void sp_finish_parsing ( THD thd)

Finish parsing of a stored program.

This is a counterpart of sp_start_parsing().

Parameters
thdThread context.

◆ sp_get_flags_for_command()

uint sp_get_flags_for_command ( LEX lex)
Parameters
lexLEX-object, representing an SQL-statement inside SP.
Returns
a combination of:
  • sp_head::MULTI_RESULTS: added if the 'cmd' is a command that might result in multiple result sets being sent back.
  • sp_head::CONTAINS_DYNAMIC_SQL: added if 'cmd' is one of PREPARE, EXECUTE, DEALLOCATE.

◆ sp_get_item_value()

String * sp_get_item_value ( THD thd,
Item item,
String str 
)

Return a string representation of the Item value.

Parameters
thdThread context.
itemThe item to evaluate
strString buffer for representation of the value.
Note
If the item has a string result type, the string is escaped according to its character set.
Return values
NULLon error
non-NULLa pointer to valid a valid string on success

◆ sp_prepare_func_item()

Item * sp_prepare_func_item ( THD thd,
Item **  it_addr 
)

Prepare an Item for evaluation (call of fix_fields).

Parameters
thdthread handler
it_addrpointer on item reference
Return values
NULLerror
non-NULLprepared item

◆ sp_remove_not_own_routines()

void sp_remove_not_own_routines ( Query_tables_list prelocking_ctx)

Remove routines which are only indirectly used by statement from the set of routines used by this statement.

Parameters
prelocking_ctxPrelocking context of the statement

◆ sp_setup_routine()

sp_head * sp_setup_routine ( THD thd,
enum_sp_type  type,
sp_name name,
sp_cache **  cp 
)

Setup a cached routine for execution.

Parameters
thdthread context
typetype of object (FUNCTION or PROCEDURE)
namename of procedure
cphash to look routine in
Return values
NonNULLpointer to sp_head object for the procedure
NULLin case of error.

◆ sp_show_create_routine()

bool sp_show_create_routine ( THD thd,
enum_sp_type  type,
sp_name name 
)

Implement SHOW CREATE statement for stored routines.

The operation finds the stored routine object specified by name and then calls show_create_routine_from_dd_routine().

Parameters
thdThread context.
typeStored routine type (PROCEDURE or FUNCTION)
nameStored routine name.
Return values
falseon success
trueon error

◆ sp_sroutine_key()

const uchar * sp_sroutine_key ( const uchar ptr,
size_t *  plen 
)

◆ sp_start_parsing()

sp_head * sp_start_parsing ( THD thd,
enum_sp_type  sp_type,
sp_name sp_name 
)

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 program name
Returns
properly initialized sp_head-instance in case of success, or NULL is case of out-of-memory error.

◆ sp_update_routine()

bool sp_update_routine ( THD thd,
enum_sp_type  type,
sp_name name,
st_sp_chistics chistics 
)

Updates(Alter) a stored routine.

Atomicity: The operation to Update(Alter) a stored routine is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. Data-dictionary and stored routines caches caches are in sync with operation state. Cache do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log.

Parameters
thdThread context.
typeStored routine type (PROCEDURE or FUNCTION)
nameStored routine name.
chisticsNew values of stored routine attributes to write.
Return values
falseSuccess.
trueError.

◆ sp_update_stmt_used_routines() [1/2]

void sp_update_stmt_used_routines ( THD thd,
Query_tables_list prelocking_ctx,
malloc_unordered_map< std::string, Sroutine_hash_entry * > *  src,
Table_ref belong_to_view 
)

Add contents of hash representing set of routines to the set of routines used by statement.

Parameters
thdThread context
prelocking_ctxPrelocking context of the statement
srcHash representing set from which routines will be added
belong_to_viewUppermost view which uses these routines, 0 if none
Note
It will also add elements to end of 'Query_tables_list::sroutines_list' list.

◆ sp_update_stmt_used_routines() [2/2]

void sp_update_stmt_used_routines ( THD thd,
Query_tables_list prelocking_ctx,
SQL_I_List< Sroutine_hash_entry > *  src,
Table_ref belong_to_view 
)

Add contents of list representing set of routines to the set of routines used by statement.

Parameters
thdThread context
prelocking_ctxPrelocking context of the statement
srcList representing set from which routines will be added
belong_to_viewUppermost view which uses these routines, 0 if none
Note
It will also add elements to end of 'Query_tables_list::sroutines_list' list.

Variable Documentation

◆ MYSQL_STORED_ROUTINE_BODY_LENGTH

const uint MYSQL_STORED_ROUTINE_BODY_LENGTH = 4294967295U
static

◆ MYSQL_STORED_ROUTINE_COMMENT_LENGTH

const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH = 65535
static