MySQL  8.0.18
Source Code Documentation
sp.h
Go to the documentation of this file.
1 /* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef _SP_H_
24 #define _SP_H_
25 
26 #include <stddef.h>
27 #include <sys/types.h>
28 #include <string>
29 
30 #include "lex_string.h"
31 #include "m_ctype.h"
32 #include "map_helpers.h"
33 #include "my_alloc.h"
34 #include "my_dbug.h"
35 #include "my_inttypes.h"
37 #include "sql/item.h" // Item::Type
38 #include "sql/sp_head.h" // Stored_program_creation_ctx
39 #include "sql/sql_lex.h"
40 
42 class Query_arena;
43 class THD;
44 struct LEX_USER;
45 
46 namespace dd {
47 class Routine;
48 class Schema;
49 } // namespace dd
50 
51 class Field;
53 class String;
54 class sp_cache;
55 struct TABLE;
56 struct TABLE_LIST;
57 
58 typedef ulonglong sql_mode_t;
59 template <typename T>
60 class SQL_I_List;
61 
62 enum class enum_sp_type;
63 
64 /* Tells what SP_DEFAULT_ACCESS should be mapped to */
65 #define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL
66 
67 /* Tells what SP_IS_DEFAULT_SUID should be mapped to */
68 #define SP_DEFAULT_SUID_MAPPING SP_IS_SUID
69 
70 /* Max length(LONGBLOB field type length) of stored routine body */
71 static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH = 4294967295U;
72 
73 /* Max length(TEXT field type length) of stored routine comment */
74 static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH = 65535;
75 
77  SP_OK = 0,
78 
79  // Schema does not exists
81 
82  // Routine does not exists
84 
85  // Routine already exists
87 
88  // Create routine failed
90 
91  // Drop routine failed
93 
94  // Routine load failed
96 
97  // Routine parse failed
99 
100  // Internal errors
102 };
103 
104 /*
105  Fields in mysql.proc table in 5.7. This enum is used to read and
106  update mysql.routines dictionary table during upgrade scenario.
107 
108  Note: This enum should not be used for other purpose
109  as it will be removed eventually.
110 */
111 enum {
133 };
134 
135 /*************************************************************************/
136 
137 /**
138  Stored_routine_creation_ctx -- creation context of stored routines
139  (stored procedures and functions).
140 */
141 
143  public:
145  const dd::Routine *routine);
146 
148  const sp_name *name,
149  TABLE *proc_tbl);
150 
151  public:
153  return new (mem_root)
155  }
156 
157  protected:
159  DBUG_TRACE;
160  return new (thd->mem_root) Stored_routine_creation_ctx(thd);
161  }
162 
163  virtual void delete_backup_ctx() { destroy(this); }
164 
165  private:
167 
169  const CHARSET_INFO *connection_cl,
170  const CHARSET_INFO *db_cl)
171  : Stored_program_creation_ctx(client_cs, connection_cl, db_cl) {}
172 };
173 
174 /* Drop all routines in database 'db' */
176 
177 /**
178  Acquires exclusive metadata lock on all stored routines in the
179  given database.
180 
181  @param thd Thread handler
182  @param schema Schema object
183 
184  @retval false Success
185  @retval true Failure
186  */
187 bool lock_db_routines(THD *thd, const dd::Schema &schema);
188 
190  sp_cache **cp, bool cache_only);
191 
193  sp_cache **cp);
194 
196  bool lookup_only, sp_head **sp);
197 
199  bool lookup_only, sp_head **sp);
200 
201 bool sp_exist_routines(THD *thd, TABLE_LIST *procs, bool is_proc);
202 
204 
206  THD *thd, enum_sp_type type, const char *sp_db, size_t sp_db_len,
207  const char *sp_name, size_t sp_name_len, sp_head **sphp,
208  sql_mode_t sql_mode, const char *params, const char *returns,
209  const char *body, st_sp_chistics *chistics, const char *definer_user,
210  const char *definer_host, longlong created, longlong modified,
211  Stored_program_creation_ctx *creation_ctx);
212 
213 bool sp_create_routine(THD *thd, sp_head *sp, const LEX_USER *definer);
214 
216  st_sp_chistics *chistics);
217 
219 
220 /**
221  Structure that represents element in the set of stored routines
222  used by statement or routine.
223 */
224 
226  public:
227  /**
228  Key identifying routine or other object added to the set.
229 
230  Key format: "@<1-byte entry type@>@<db name@>\0@<routine/object name@>\0".
231 
232  @note We use binary comparison for these keys as the @<db name@> component
233  requires case-sensitive comparison on --lower-case-table-names=0
234  systems. On systems where --lower-case-table-names > 0 database
235  names which passed to functions working with this set are already
236  lowercased. So binary comparison is equivalent to case-insensitive
237  comparison for them.
238  Routine names are case and accent insensitive. To achieve such
239  comparison we normalize routine names by converting their characters
240  to their sort weights (according to case and accent insensitive
241  collation). In this case, the actual routine name is also stored in
242  the member m_object_name.
243 
244  @note For Foreign Key objects, '@<db name@>\0@<object name@>\0' part of the
245  key is compatible with keys used by MDL. So one can easily construct
246  MDL_key from this key.
247  */
248  char *m_key;
252 
253  enum entry_type {
257  /**
258  Parent table in a foreign key on which child table there was insert
259  or update. We will lookup new values in parent, so need to acquire
260  SR lock on it.
261  */
263  /**
264  Child table in a foreign key with RESTRICT/NO ACTION as corresponding
265  rule and on which parent table there was delete or update.
266  We will check if old parent key is referenced by child table,
267  so need to acquire SR lock on it.
268  */
270  /**
271  Child table in a foreign key with CASCADE/SET NULL/SET DEFAULT as
272  'on update' rule, on which parent there was update, or with SET NULL/
273  SET DEFAULT as 'on delete' rule, on which parent there was delete.
274  We might need to update rows in child table, so we need to acquire
275  SW lock on it. We also need to take into account that child table
276  might be parent for some other FKs, so such update needs
277  to be handled recursively.
278  */
280  /**
281  Child table in a foreign key with CASCADE as 'on delete' rule for
282  which there was delete from the parent table.
283  We might need to delete rows from the child table, so we need to
284  acquire SW lock on it.
285  We also need to take into account that child table might be parent
286  for some other FKs, so such delete needs to be handled recursively
287  (and even might result in updates).
288  */
290  };
291 
292  entry_type type() const { return (entry_type)m_key[0]; }
293  const char *db() const { return (char *)m_key + 1; }
294  size_t db_length() const { return m_db_length; }
295  const char *name() const {
297  : (char *)m_key + 1 + m_db_length + 1;
298  }
299  size_t name_length() const {
301  : m_key_length - 1U - m_db_length - 1U - 1U;
302  }
303  bool use_normalized_key() const {
304  return (type() == FUNCTION || type() == PROCEDURE || type() == TRIGGER);
305  }
306 
307  const char *part_mdl_key() {
309  return (char *)m_key + 1;
310  }
313  return m_key_length - 1U;
314  }
315 
316  /**
317  Next element in list linking all routines in set. See also comments
318  for LEX::sroutine/sroutine_list and sp_head::m_sroutines.
319  */
321  /**
322  Uppermost view which directly or indirectly uses this routine.
323  0 if routine is not used in view. Note that it also can be 0 if
324  statement uses routine both via view and directly.
325  */
327  /**
328  This is for prepared statement validation purposes.
329  A statement looks up and pre-loads all its stored functions
330  at prepare. Later on, if a function is gone from the cache,
331  execute may fail. Similarly, tables involved in referential
332  constraints are also prelocked.
333  Remember the version of the cached item at prepare to be able to
334  invalidate the prepared statement at execute if it
335  changes.
336  */
338 };
339 
340 /*
341  Enum to indicate SP name normalization required when constructing a key
342  in sp_add_used_routine method.
343 */
345  LEAVE_AS_IS = 0, // No normalization needed.
346  LOWERCASE_NAME, // Lower case SP name.
347  UNACCENT_AND_LOWERCASE_NAME, // Lower case SP name and remove accent.
348 };
349 
350 /*
351  Procedures for handling sets of stored routines used by statement or routine.
352 */
353 bool sp_add_used_routine(Query_tables_list *prelocking_ctx, Query_arena *arena,
354  Sroutine_hash_entry::entry_type type, const char *db,
355  size_t db_length, const char *name, size_t name_length,
356  bool lowercase_db,
357  Sp_name_normalize_type name_normalize_type,
358  bool own_routine, TABLE_LIST *belong_to_view);
359 
360 /**
361  Convenience wrapper around sp_add_used_routine() for most common case -
362  stored procedure or function which are explicitly used by the statement.
363 */
364 
365 inline bool sp_add_own_used_routine(Query_tables_list *prelocking_ctx,
366  Query_arena *arena,
368  sp_name *sp_name) {
371 
372  return sp_add_used_routine(
373  prelocking_ctx, arena, type, sp_name->m_db.str, sp_name->m_db.length,
376 }
377 
378 void sp_remove_not_own_routines(Query_tables_list *prelocking_ctx);
380  THD *thd, Query_tables_list *prelocking_ctx,
382  TABLE_LIST *belong_to_view);
383 void sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx,
385  TABLE_LIST *belong_to_view);
386 
387 const uchar *sp_sroutine_key(const uchar *ptr, size_t *plen);
388 
390  const dd::Routine *routine,
391  bool *free_sp_head);
392 
393 bool load_charset(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs,
394  const CHARSET_INFO **cs);
395 
396 bool load_collation(MEM_ROOT *mem_root, Field *field,
397  const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl);
398 
399 ///////////////////////////////////////////////////////////////////////////
400 
402 
403 void sp_finish_parsing(THD *thd);
404 
405 ///////////////////////////////////////////////////////////////////////////
406 
410 
411 bool sp_check_name(LEX_STRING *ident);
412 
413 TABLE_LIST *sp_add_to_query_tables(THD *thd, LEX *lex, const char *db,
414  const char *name);
415 
416 Item *sp_prepare_func_item(THD *thd, Item **it_addr);
417 
418 bool sp_eval_expr(THD *thd, Field *result_field, Item **expr_item_ptr);
419 
420 String *sp_get_item_value(THD *thd, Item *item, String *str);
421 
422 ///////////////////////////////////////////////////////////////////////////
423 
424 #endif /* _SP_H_ */
static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH
Definition: sp.h:74
unsigned long long int ulonglong
Definition: my_inttypes.h:55
ulonglong sql_mode_t
Definition: sp.h:56
#define U
Definition: ctype-tis620.cc:72
unsigned char uchar
Definition: my_inttypes.h:51
char * str
Definition: mysql_lex_string.h:35
Definition: sp.h:124
Item_result
Type of the user defined function return slot and arguments
Definition: udf_registration_types.h:38
virtual Object_creation_ctx * create_backup_ctx(THD *thd) const
Definition: sp.h:158
const char * db_name
Definition: rules_table_service.cc:54
Child table in a foreign key with CASCADE as &#39;on delete&#39; rule for which there was delete from the par...
Definition: sp.h:289
int64 m_cache_version
This is for prepared statement validation purposes.
Definition: sp.h:337
Definition: mysql_lex_string.h:34
Abstract base class for functions and procedures.
Definition: routine.h:60
static Stored_routine_creation_ctx * load_from_db(THD *thd, const sp_name *name, TABLE *proc_tbl)
Definition: sp.cc:186
Definition: sp.h:95
void sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx, malloc_unordered_map< std::string, Sroutine_hash_entry *> *src, TABLE_LIST *belong_to_view)
Add contents of hash representing set of routines to the set of routines used by statement.
Definition: sp.cc:1809
Definition: sql_lex.h:3192
static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH
Definition: sp.h:71
Definition: sp.h:113
Sroutine_hash_entry * next
Next element in list linking all routines in set.
Definition: sp.h:320
const string name("\ame\)
entry_type type() const
Definition: sp.h:292
const uchar * sp_sroutine_key(const uchar *ptr, size_t *plen)
Definition: sql_lex.h:2062
Definition: sp.h:115
Some integer typedefs for easier portability.
Stored_routine_creation_ctx(const CHARSET_INFO *client_cs, const CHARSET_INFO *connection_cl, const CHARSET_INFO *db_cl)
Definition: sp.h:168
void sp_finish_parsing(THD *thd)
Finish parsing of a stored program.
Definition: sp.cc:2168
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:1776
Stored_routine_creation_ctx(THD *thd)
Definition: sp.h:166
Item * sp_prepare_func_item(THD *thd, Item **it_addr)
Prepare an Item for evaluation (call of fix_fields).
Definition: sp.cc:2417
Definition: sp_cache.cc:41
Definition: sp.h:83
const char * str
Definition: mysql_lex_string.h:40
Definition: field.h:700
Definition: sp.h:132
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:365
char * m_key
Key identifying routine or other object added to the set.
Definition: sp.h:248
const char * name() const
Definition: sp.h:295
Definition: sp.h:255
Definition: mysql_lex_string.h:39
uint16 m_db_length
Definition: sp.h:251
Object_creation_ctx – interface for creation context of database objects (views, stored routines, events, triggers).
Definition: table.h:193
Simple intrusive linked list.
Definition: item.h:81
bool sp_show_create_routine(THD *thd, enum_sp_type type, sp_name *name)
Implement SHOW CREATE statement for stored routines.
Definition: sp.cc:1363
const char * part_mdl_key()
Definition: sp.h:307
Definition: sp.h:130
LEX_STRING m_name
Definition: sp_head.h:128
bool sp_create_routine(THD *thd, sp_head *sp, const LEX_USER *definer)
Creates a stored routine.
Definition: sp.cc:715
Structure that represents element in the set of stored routines used by statement or routine...
Definition: sp.h:225
Definition: sp.h:92
Item_result sp_map_result_type(enum enum_field_types type)
Definition: sp.cc:2179
Definition: sp.h:116
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:1093
enum_sp_return_code
Definition: sp.h:76
Definition: sp.h:256
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:161
Definition: sp.h:118
uint sp_get_flags_for_command(LEX *lex)
Definition: sp.cc:2229
Definition: table.h:1301
Definition: sp.h:131
Definition: sql_lex.h:2019
Definition: sp.h:86
static Stored_routine_creation_ctx * create_routine_creation_ctx(const dd::Routine *routine)
Definition: sp.cc:165
Definition: table.h:2367
virtual void delete_backup_ctx()
Definition: sp.h:163
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
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:1462
sp_head * sp_load_for_information_schema(THD *thd, LEX_CSTRING db_name, const dd::Routine *routine, bool *free_sp_head)
The function loads sp_head struct for information schema purposes (used for I_S ROUTINES & PARAMETERS...
Definition: sp.cc:2043
Child table in a foreign key with RESTRICT/NO ACTION as corresponding rule and on which parent table ...
Definition: sp.h:269
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_LIST *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:1682
Definition: sp.h:77
Definition: sql_class.h:229
std::unordered_map, but with my_malloc, so that you can track the memory used using PSI memory keys...
Definition: map_helpers.h:175
Definition: sp.h:89
Definition: sp.h:120
int64_t int64
Definition: my_inttypes.h:67
size_t name_length() const
Definition: sp.h:299
size_t length
Definition: mysql_lex_string.h:41
Definition: sp.h:123
bool sp_check_name(LEX_STRING *ident)
Check that the name &#39;ident&#39; is ok.
Definition: sp.cc:2388
Definition: sp.h:122
String * sp_get_item_value(THD *thd, Item *item, String *str)
Return a string representation of the Item value.
Definition: sp.cc:2511
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
uint16_t uint16
Definition: my_inttypes.h:64
Definition: item.h:668
unsigned int uint
Definition: uca-dump.cc:29
TABLE_LIST * sp_add_to_query_tables(THD *thd, LEX *lex, const char *db, const char *name)
long long int longlong
Definition: my_inttypes.h:54
Type
Definition: item.h:704
enum_sp_type
enum_sp_type defines type codes of stored programs.
Definition: sql_lex.h:193
Definition: m_ctype.h:359
Definition: sp.h:114
Child table in a foreign key with CASCADE/SET NULL/SET DEFAULT as &#39;on update&#39; rule, on which parent there was update, or with SET NULL/ SET DEFAULT as &#39;on delete&#39; rule, on which parent there was delete.
Definition: sp.h:279
const CHARSET_INFO * m_db_cl
db_cl stores the value of the database collation.
Definition: sp_head.h:120
sp_head represents one instance of a stored program.
Definition: sp_head.h:405
size_t length
Definition: mysql_lex_string.h:36
const CHARSET_INFO * m_client_cs
client_cs stores the value of character_set_client session variable.
Definition: table.h:245
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:484
DBUG_TRACE
Definition: do_ctype.cc:46
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
MEM_ROOT * mem_root
Definition: sql_class.h:238
LEX_CSTRING m_db
Definition: sp_head.h:127
Definition: sp.h:80
Definition: sp.h:112
uint16 m_key_length
Definition: sp.h:250
Parent table in a foreign key on which child table there was insert or update.
Definition: sp.h:262
Definition: sp.h:101
int type
Definition: http_common.h:411
Definition: sp.h:121
bool load_collation(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl)
Definition: sp.cc:141
enum_sp_return_code sp_drop_routine(THD *thd, enum_sp_type type, sp_name *name)
Drops a stored routine.
Definition: sp.cc:849
TABLE_LIST * belong_to_view
Uppermost view which directly or indirectly uses this routine.
Definition: sp.h:326
LEX_CSTRING m_object_name
Definition: sp.h:249
void destroy(T *ptr)
Definition: my_alloc.h:381
const CHARSET_INFO * m_connection_cl
connection_cl stores the value of collation_connection session variable.
Definition: table.h:255
Definition: sp.h:119
ulonglong sql_mode_t
Definition: dd_event.h:36
Sp_name_normalize_type
Definition: sp.h:344
Item::Type sp_map_item_type(enum enum_field_types type)
Definition: sp.cc:2200
Definition: schema.h:61
A better implementation of the UNIX ctype(3) library.
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:980
size_t part_mdl_key_length()
Definition: sp.h:311
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
bool use_normalized_key() const
Definition: sp.h:303
Definition: sp.h:98
static const char * sql_mode
Definition: mysqlslap.cc:193
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
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 &#39;sp_name&#39; is eradicated as a ...
Definition: sp.cc:1851
Definition: table.h:2468
const char * db() const
Definition: sp.h:293
Definition: sp.h:254
Stored_program_creation_ctx – base class for creation context of stored programs (stored routines...
Definition: sp_head.h:88
bool load_charset(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs, const CHARSET_INFO **cs)
Definition: sp.cc:120
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:2129
entry_type
Definition: sp.h:253
size_t db_length() const
Definition: sp.h:294
Definition: dtoa.cc:603
bool sp_exist_routines(THD *thd, TABLE_LIST *procs, bool is_proc)
This is used by sql_acl.cc:mysql_routine_grant() and is used to find the routines in &#39;routines&#39;...
Definition: sp.cc:1544
Definition: sp_head.h:125
Definition: sp.h:117
Definition: sp.h:126
virtual Stored_program_creation_ctx * clone(MEM_ROOT *mem_root)
Definition: sp.h:152
Stored_routine_creation_ctx – creation context of stored routines (stored procedures and functions)...
Definition: sp.h:142
enum_sp_return_code sp_drop_db_routines(THD *thd, const dd::Schema &schema)
Drop all routines in database &#39;db&#39;.
Definition: sp.cc:1149
Definition: sp.h:127
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
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:1427
bool sp_eval_expr(THD *thd, Field *result_field, Item **expr_item_ptr)
Evaluate an expression and store the result in the field.
Definition: sp.cc:2443
Definition: sp.h:125