MySQL 9.2.0
Source Code Documentation
sp.h
Go to the documentation of this file.
1/* Copyright (c) 2002, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef _SP_H_
25#define _SP_H_
26
27#include <assert.h>
28#include <stddef.h>
29#include <stdint.h>
30#include <sys/types.h>
31
32#include <string>
33
34#include "field_types.h"
35#include "lex_string.h"
36#include "map_helpers.h"
37#include "my_inttypes.h"
39#include "sql/item.h" // Item::Type
40#include "sql/sp_head.h" // Stored_program_creation_ctx
41#include "sql/sql_lex.h"
42
44class Query_arena;
45class THD;
46struct CHARSET_INFO;
47struct LEX_USER;
48struct MEM_ROOT;
49
50namespace dd {
51class Routine;
52class Schema;
53} // namespace dd
54
55class Field;
57class String;
58class sp_cache;
59struct TABLE;
60class Table_ref;
61
62using sql_mode_t = uint64_t;
63template <typename T>
64class SQL_I_List;
65
66enum class enum_sp_type;
67
68/* Tells what SP_DEFAULT_ACCESS should be mapped to */
69#define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL
70
71/* Tells what SP_IS_DEFAULT_SUID should be mapped to */
72#define SP_DEFAULT_SUID_MAPPING SP_IS_SUID
73
74/* Max length(LONGBLOB field type length) of stored routine body */
75static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH = 4294967295U;
76
77/* Max length(TEXT field type length) of stored routine comment */
78static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH = 65535;
79
81 SP_OK = 0,
82
83 // Schema does not exists
85
86 // Routine does not exists
88
89 // Routine already exists
91
92 // Create routine failed
94
95 // Drop routine failed
97
98 // Routine load failed
100
101 // Routine parse failed
103
104 // Internal errors
107
108/*
109 Fields in mysql.proc table in 5.7. This enum is used to read and
110 update mysql.routines dictionary table during upgrade scenario.
111
112 Note: This enum should not be used for other purpose
113 as it will be removed eventually.
114*/
115enum {
138
139/*************************************************************************/
140
141/**
142 Stored_routine_creation_ctx -- creation context of stored routines
143 (stored procedures and functions).
144*/
145
147 public:
149 const dd::Routine *routine);
150
152 const sp_name *name,
153 TABLE *proc_tbl);
154
155 public:
157
158 protected:
159 Object_creation_ctx *create_backup_ctx(THD *thd) const override;
160 void delete_backup_ctx() override;
161
162 private:
165
167 const CHARSET_INFO *connection_cl,
168 const CHARSET_INFO *db_cl)
169 : Stored_program_creation_ctx(client_cs, connection_cl, db_cl) {}
170};
171
172/* Drop all routines in database 'db' */
173bool sp_drop_db_routines(THD *thd, const dd::Schema &schema);
174
175/**
176 Acquires exclusive metadata lock on all stored routines in the
177 given database.
178
179 @param thd Thread handler
180 @param schema Schema object
181
182 @retval false Success
183 @retval true Failure
184 */
185bool lock_db_routines(THD *thd, const dd::Schema &schema);
186
188 sp_cache **cp, bool cache_only);
189
191 sp_cache **cp);
192
194 bool lookup_only, sp_head **sp);
195
197 const sp_name *name, bool lookup_only,
198 sp_head **sp);
199
200bool sp_exists_library(THD *thd, sp_name *name);
201
202bool sp_exist_routines(THD *thd, Table_ref *procs, enum_sp_type sp_type);
203
205
207 THD *thd, enum_sp_type type, const char *sp_db, size_t sp_db_len,
208 const char *sp_name, size_t sp_name_len, sp_head **sphp,
209 sql_mode_t sql_mode, const char *params, const char *returns,
210 const char *body, st_sp_chistics *chistics, const char *definer_user,
211 const char *definer_host, longlong created, longlong modified,
212 Stored_program_creation_ctx *creation_ctx);
213
214bool sp_create_routine(THD *thd, sp_head *sp, const LEX_USER *definer,
215 bool if_not_exists, bool &sp_already_exists);
216
218 st_sp_chistics *chistics);
219
221
222/**
223 Structure that represents element in the set of stored routines
224 used by statement or routine.
225*/
226
228 public:
229 /**
230 Key identifying routine or other object added to the set.
231
232 Key format: "@<1-byte entry type@>@<db name@>\0@<routine/object name@>\0".
233
234 @note We use binary comparison for these keys as the @<db name@> component
235 requires case-sensitive comparison on --lower-case-table-names=0
236 systems. On systems where --lower-case-table-names > 0 database
237 names which passed to functions working with this set are already
238 lowercased. So binary comparison is equivalent to case-insensitive
239 comparison for them.
240 Routine names are case and accent insensitive. To achieve such
241 comparison we normalize routine names by converting their characters
242 to their sort weights (according to case and accent insensitive
243 collation). In this case, the actual routine name is also stored in
244 the member m_object_name.
245
246 @note For Foreign Key objects, '@<db name@>\0@<object name@>\0' part of the
247 key is compatible with keys used by MDL. So one can easily construct
248 MDL_key from this key.
249 */
250 char *m_key;
254
259 /**
260 Parent table in a foreign key on which child table there was insert
261 or update. We will lookup new values in parent, so need to acquire
262 SR lock on it.
263 */
265 /**
266 Child table in a foreign key with RESTRICT/NO ACTION as corresponding
267 rule and on which parent table there was delete or update.
268 We will check if old parent key is referenced by child table,
269 so need to acquire SR lock on it.
270 */
272 /**
273 Child table in a foreign key with CASCADE/SET NULL/SET DEFAULT as
274 'on update' rule, on which parent there was update, or with SET NULL/
275 SET DEFAULT as 'on delete' rule, on which parent there was delete.
276 We might need to update rows in child table, so we need to acquire
277 SW lock on it. We also need to take into account that child table
278 might be parent for some other FKs, so such update needs
279 to be handled recursively.
280 */
282 /**
283 Child table in a foreign key with CASCADE as 'on delete' rule for
284 which there was delete from the parent table.
285 We might need to delete rows from the child table, so we need to
286 acquire SW lock on it.
287 We also need to take into account that child table might be parent
288 for some other FKs, so such delete needs to be handled recursively
289 (and even might result in updates).
290 */
292 };
293
294 entry_type type() const { return (entry_type)m_key[0]; }
295 const char *db() const { return (char *)m_key + 1; }
296 size_t db_length() const { return m_db_length; }
297 const char *name() const {
299 : (char *)m_key + 1 + m_db_length + 1;
300 }
301 size_t name_length() const {
303 : m_key_length - 1U - m_db_length - 1U - 1U;
304 }
305 bool use_normalized_key() const {
306 return (type() == FUNCTION || type() == PROCEDURE || type() == TRIGGER);
307 }
308
309 const char *part_mdl_key() {
310 assert(!use_normalized_key());
311 return (char *)m_key + 1;
312 }
314 assert(!use_normalized_key());
315 return m_key_length - 1U;
316 }
317
318 /**
319 Next element in list linking all routines in set. See also comments
320 for LEX::sroutine/sroutine_list and sp_head::m_sroutines.
321 */
323 /**
324 Uppermost view which directly or indirectly uses this routine.
325 0 if routine is not used in view. Note that it also can be 0 if
326 statement uses routine both via view and directly.
327 */
329 /**
330 This is for prepared statement validation purposes.
331 A statement looks up and pre-loads all its stored functions
332 at prepare. Later on, if a function is gone from the cache,
333 execute may fail. Similarly, tables involved in referential
334 constraints are also prelocked.
335 Remember the version of the cached item at prepare to be able to
336 invalidate the prepared statement at execute if it
337 changes.
338 */
340};
341
342/*
343 Enum to indicate SP name normalization required when constructing a key
344 in sp_add_used_routine method.
345*/
347 LEAVE_AS_IS = 0, // No normalization needed.
348 LOWERCASE_NAME, // Lower case SP name.
349 UNACCENT_AND_LOWERCASE_NAME, // Lower case SP name and remove accent.
350};
351
352/*
353 Procedures for handling sets of stored routines used by statement or routine.
354*/
355bool sp_add_used_routine(Query_tables_list *prelocking_ctx, Query_arena *arena,
357 size_t db_length, const char *name, size_t name_length,
358 bool lowercase_db,
359 Sp_name_normalize_type name_normalize_type,
360 bool own_routine, Table_ref *belong_to_view);
361
362/**
363 Convenience wrapper around sp_add_used_routine() for most common case -
364 stored procedure or function which are explicitly used by the statement.
365*/
366
367inline bool sp_add_own_used_routine(Query_tables_list *prelocking_ctx,
368 Query_arena *arena,
370 sp_name *sp_name) {
373
374 return sp_add_used_routine(
375 prelocking_ctx, arena, type, sp_name->m_db.str, sp_name->m_db.length,
378}
379
382 THD *thd, Query_tables_list *prelocking_ctx,
384 Table_ref *belong_to_view);
385void sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx,
387 Table_ref *belong_to_view);
388
389const uchar *sp_sroutine_key(const uchar *ptr, size_t *plen);
390
391bool load_charset(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs,
392 const CHARSET_INFO **cs);
393
395 const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl);
396
397///////////////////////////////////////////////////////////////////////////
398
400
401void sp_finish_parsing(THD *thd);
402
403///////////////////////////////////////////////////////////////////////////
404
406
407bool sp_check_name(LEX_STRING *ident);
408
409Table_ref *sp_add_to_query_tables(THD *thd, LEX *lex, const char *db,
410 const char *name);
411
412Item *sp_prepare_func_item(THD *thd, bool standalone, Item **it_addr);
413
414bool sp_eval_expr(THD *thd, bool standalone, Field *result_field,
415 Item **expr_item_ptr);
416
417String *sp_get_item_value(THD *thd, Item *item, String *str);
418
419///////////////////////////////////////////////////////////////////////////
420
421#endif /* _SP_H_ */
Definition: field.h:577
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Object_creation_ctx – interface for creation context of database objects (views, stored routines,...
Definition: table.h:204
Definition: sql_class.h:348
Definition: sql_lex.h:2726
Simple intrusive linked list.
Definition: sql_list.h:48
Structure that represents element in the set of stored routines used by statement or routine.
Definition: sp.h:227
bool use_normalized_key() const
Definition: sp.h:305
entry_type type() const
Definition: sp.h:294
LEX_CSTRING m_object_name
Definition: sp.h:251
Table_ref * belong_to_view
Uppermost view which directly or indirectly uses this routine.
Definition: sp.h:328
Sroutine_hash_entry * next
Next element in list linking all routines in set.
Definition: sp.h:322
int64 m_cache_version
This is for prepared statement validation purposes.
Definition: sp.h:339
uint16 m_db_length
Definition: sp.h:253
const char * name() const
Definition: sp.h:297
const char * db() const
Definition: sp.h:295
size_t part_mdl_key_length()
Definition: sp.h:313
char * m_key
Key identifying routine or other object added to the set.
Definition: sp.h:250
entry_type
Definition: sp.h:255
@ FUNCTION
Definition: sp.h:256
@ FK_TABLE_ROLE_CHILD_UPDATE
Child table in a foreign key with CASCADE/SET NULL/SET DEFAULT as 'on update' rule,...
Definition: sp.h:281
@ FK_TABLE_ROLE_CHILD_DELETE
Child table in a foreign key with CASCADE as 'on delete' rule for which there was delete from the par...
Definition: sp.h:291
@ FK_TABLE_ROLE_PARENT_CHECK
Parent table in a foreign key on which child table there was insert or update.
Definition: sp.h:264
@ TRIGGER
Definition: sp.h:258
@ PROCEDURE
Definition: sp.h:257
@ FK_TABLE_ROLE_CHILD_CHECK
Child table in a foreign key with RESTRICT/NO ACTION as corresponding rule and on which parent table ...
Definition: sp.h:271
const char * part_mdl_key()
Definition: sp.h:309
uint16 m_key_length
Definition: sp.h:252
size_t db_length() const
Definition: sp.h:296
size_t name_length() const
Definition: sp.h:301
Stored_program_creation_ctx – base class for creation context of stored programs (stored routines,...
Definition: sp_head.h:93
Stored_routine_creation_ctx – creation context of stored routines (stored procedures and functions).
Definition: sp.h:146
Object_creation_ctx * create_backup_ctx(THD *thd) const override
Definition: sp.cc:270
static Stored_routine_creation_ctx * load_from_db(THD *thd, const sp_name *name, TABLE *proc_tbl)
Definition: sp.cc:207
static Stored_routine_creation_ctx * create_routine_creation_ctx(const dd::Routine *routine)
Definition: sp.cc:186
Stored_routine_creation_ctx(const CHARSET_INFO *client_cs, const CHARSET_INFO *connection_cl, const CHARSET_INFO *db_cl)
Definition: sp.h:166
Stored_program_creation_ctx * clone(MEM_ROOT *mem_root) override
Definition: sp.cc:264
void delete_backup_ctx() override
Definition: sp.cc:276
Stored_routine_creation_ctx(THD *thd)
Definition: sp.h:163
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2900
Abstract base class for functions and procedures.
Definition: routine.h:61
Definition: schema.h:64
std::unordered_map, but with my_malloc, so that you can track the memory used using PSI memory keys.
Definition: map_helpers.h:157
Definition: sp_cache.cc:42
sp_head represents one instance of a stored program.
Definition: sp_head.h:389
Definition: sp_head.h:124
LEX_STRING m_name
Definition: sp_head.h:127
LEX_CSTRING m_db
Definition: sp_head.h:126
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
uint64_t sql_mode_t
Definition: dd_event.h:39
This file contains the field type.
enum_sp_type
enum_sp_type defines type codes of stored programs.
Definition: sql_lex.h:225
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
int64_t int64
Definition: my_inttypes.h:68
long long int longlong
Definition: my_inttypes.h:55
uint16_t uint16
Definition: my_inttypes.h:65
static const char * sql_mode
Definition: mysqlslap.cc:199
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1117
borrowable::session_track::Schema< true > Schema
Definition: classic_protocol_session_track.h:288
Definition: commit_order_queue.h:34
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
required string type
Definition: replication_group_member_actions.proto:34
bool sp_exists_library(THD *thd, sp_name *name)
This is used by sp_exist_routines() and sql_parse.cc to find if the library exists.
Definition: sp.cc:1828
Item * sp_prepare_func_item(THD *thd, bool standalone, Item **it_addr)
Prepare an Item for evaluation (call of fix_fields).
Definition: sp.cc:2816
bool sp_check_name(LEX_STRING *ident)
Check that the name 'ident' is ok.
Definition: sp.cc:2784
@ MYSQL_PROC_FIELD_PARAM_LIST
Definition: sp.h:124
@ MYSQL_PROC_FIELD_DETERMINISTIC
Definition: sp.h:122
@ MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT
Definition: sp.h:132
@ MYSQL_PROC_FIELD_RETURNS
Definition: sp.h:125
@ MYSQL_PROC_FIELD_SECURITY_TYPE
Definition: sp.h:123
@ MYSQL_PROC_FIELD_NAME
Definition: sp.h:117
@ MYSQL_PROC_FIELD_COUNT
Definition: sp.h:136
@ MYSQL_PROC_FIELD_LANGUAGE
Definition: sp.h:120
@ MYSQL_PROC_MYSQL_TYPE
Definition: sp.h:118
@ MYSQL_PROC_FIELD_SPECIFIC_NAME
Definition: sp.h:119
@ MYSQL_PROC_FIELD_COMMENT
Definition: sp.h:131
@ MYSQL_PROC_FIELD_SQL_MODE
Definition: sp.h:130
@ MYSQL_PROC_FIELD_DB_COLLATION
Definition: sp.h:134
@ MYSQL_PROC_FIELD_BODY
Definition: sp.h:126
@ MYSQL_PROC_FIELD_CREATED
Definition: sp.h:128
@ MYSQL_PROC_FIELD_ACCESS
Definition: sp.h:121
@ MYSQL_PROC_FIELD_COLLATION_CONNECTION
Definition: sp.h:133
@ MYSQL_PROC_FIELD_DB
Definition: sp.h:116
@ MYSQL_PROC_FIELD_MODIFIED
Definition: sp.h:129
@ MYSQL_PROC_FIELD_BODY_UTF8
Definition: sp.h:135
@ MYSQL_PROC_FIELD_DEFINER
Definition: sp.h:127
Table_ref * sp_add_to_query_tables(THD *thd, LEX *lex, const char *db, const char *name)
bool sp_eval_expr(THD *thd, bool standalone, Field *result_field, Item **expr_item_ptr)
Evaluate an expression and store the result in the field.
Definition: sp.cc:2858
enum_sp_return_code sp_drop_routine(THD *thd, enum_sp_type type, sp_name *name)
Drops a stored routine.
Definition: sp.cc:973
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.
Definition: sp.cc:2137
sp_head * sp_setup_routine(THD *thd, enum_sp_type type, sp_name *name, sp_cache **cp)
Setup a cached routine for execution.
Definition: sp.cc:1750
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 sta...
Definition: sp.cc:2104
Sp_name_normalize_type
Definition: sp.h:346
uint sp_get_flags_for_command(LEX *lex)
Definition: sp.cc:2623
bool load_charset(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs, const CHARSET_INFO **cs)
Definition: sp.cc:141
sp_head * sp_start_parsing(THD *thd, enum_sp_type sp_type, sp_name *sp_name)
Start parsing of a stored program.
Definition: sp.cc:2568
bool sp_show_create_routine(THD *thd, enum_sp_type type, sp_name *name)
Implement SHOW CREATE statement for stored routines.
Definition: sp.cc:1647
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.
Definition: sp.cc:844
String * sp_get_item_value(THD *thd, Item *item, String *str)
Return a string representation of the Item value.
Definition: sp.cc:2927
bool lock_db_routines(THD *thd, const dd::Schema &schema)
Acquires exclusive metadata lock on all stored routines in the given database.
Definition: sp.cc:1230
enum_sp_return_code
Definition: sp.h:80
@ SP_ALREADY_EXISTS
Definition: sp.h:90
@ SP_INTERNAL_ERROR
Definition: sp.h:105
@ SP_NO_DB_ERROR
Definition: sp.h:84
@ SP_LOAD_FAILED
Definition: sp.h:99
@ SP_PARSE_ERROR
Definition: sp.h:102
@ SP_DOES_NOT_EXISTS
Definition: sp.h:87
@ SP_DROP_FAILED
Definition: sp.h:96
@ SP_STORE_FAILED
Definition: sp.h:93
@ SP_OK
Definition: sp.h:81
static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH
Definition: sp.h:75
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 statemen...
Definition: sp.cc:2009
bool sp_drop_db_routines(THD *thd, const dd::Schema &schema)
Drop all routines in database 'db'.
Definition: sp.cc:1298
bool sp_update_routine(THD *thd, enum_sp_type type, sp_name *name, st_sp_chistics *chistics)
Updates(Alter) a stored routine.
Definition: sp.cc:1108
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 ...
Definition: sp.cc:2179
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)
Definition: sp.cc:529
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 ...
Definition: sp.h:367
void sp_finish_parsing(THD *thd)
Finish parsing of a stored program.
Definition: sp.cc:2604
bool load_collation(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl)
Definition: sp.cc:162
const uchar * sp_sroutine_key(const uchar *ptr, size_t *plen)
bool sp_exist_routines(THD *thd, Table_ref *procs, enum_sp_type sp_type)
This is used by sql_acl.cc:mysql_routine_grant() and is used to find the routines in 'routines'.
Definition: sp.cc:1855
static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH
Definition: sp.h:78
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 loo...
Definition: sp.cc:1715
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:421
Definition: table.h:2767
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3958
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_lex_string.h:40
const char * str
Definition: mysql_lex_string.h:41
size_t length
Definition: mysql_lex_string.h:42
Definition: mysql_lex_string.h:35
char * str
Definition: mysql_lex_string.h:36
size_t length
Definition: mysql_lex_string.h:37
Definition: table.h:1421
Definition: sql_lex.h:2601