MySQL  8.0.20
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 "field_types.h"
31 #include "lex_string.h"
32 #include "map_helpers.h"
33 #include "my_dbug.h"
34 #include "my_inttypes.h"
36 #include "sql/item.h" // Item::Type
37 #include "sql/sp_head.h" // Stored_program_creation_ctx
38 #include "sql/sql_lex.h"
39 
41 class Query_arena;
42 class THD;
43 struct CHARSET_INFO;
44 struct LEX_USER;
45 struct MEM_ROOT;
46 
47 namespace dd {
48 class Routine;
49 class Schema;
50 } // namespace dd
51 
52 class Field;
54 class String;
55 class sp_cache;
56 struct TABLE;
57 struct TABLE_LIST;
58 
59 typedef ulonglong sql_mode_t;
60 template <typename T>
61 class SQL_I_List;
62 
63 enum class enum_sp_type;
64 
65 /* Tells what SP_DEFAULT_ACCESS should be mapped to */
66 #define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL
67 
68 /* Tells what SP_IS_DEFAULT_SUID should be mapped to */
69 #define SP_DEFAULT_SUID_MAPPING SP_IS_SUID
70 
71 /* Max length(LONGBLOB field type length) of stored routine body */
72 static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH = 4294967295U;
73 
74 /* Max length(TEXT field type length) of stored routine comment */
75 static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH = 65535;
76 
78  SP_OK = 0,
79 
80  // Schema does not exists
82 
83  // Routine does not exists
85 
86  // Routine already exists
88 
89  // Create routine failed
91 
92  // Drop routine failed
94 
95  // Routine load failed
97 
98  // Routine parse failed
100 
101  // Internal errors
103 };
104 
105 /*
106  Fields in mysql.proc table in 5.7. This enum is used to read and
107  update mysql.routines dictionary table during upgrade scenario.
108 
109  Note: This enum should not be used for other purpose
110  as it will be removed eventually.
111 */
112 enum {
134 };
135 
136 /*************************************************************************/
137 
138 /**
139  Stored_routine_creation_ctx -- creation context of stored routines
140  (stored procedures and functions).
141 */
142 
144  public:
145  static Stored_routine_creation_ctx *create_routine_creation_ctx(
146  const dd::Routine *routine);
147 
148  static Stored_routine_creation_ctx *load_from_db(THD *thd,
149  const sp_name *name,
150  TABLE *proc_tbl);
151 
152  public:
154 
155  protected:
156  Object_creation_ctx *create_backup_ctx(THD *thd) const override;
157  void delete_backup_ctx() override;
158 
159  private:
162 
164  const CHARSET_INFO *connection_cl,
165  const CHARSET_INFO *db_cl)
166  : Stored_program_creation_ctx(client_cs, connection_cl, db_cl) {}
167 };
168 
169 /* Drop all routines in database 'db' */
171 
172 /**
173  Acquires exclusive metadata lock on all stored routines in the
174  given database.
175 
176  @param thd Thread handler
177  @param schema Schema object
178 
179  @retval false Success
180  @retval true Failure
181  */
182 bool lock_db_routines(THD *thd, const dd::Schema &schema);
183 
185  sp_cache **cp, bool cache_only);
186 
188  sp_cache **cp);
189 
191  bool lookup_only, sp_head **sp);
192 
194  bool lookup_only, sp_head **sp);
195 
196 bool sp_exist_routines(THD *thd, TABLE_LIST *procs, bool is_proc);
197 
199 
201  THD *thd, enum_sp_type type, const char *sp_db, size_t sp_db_len,
202  const char *sp_name, size_t sp_name_len, sp_head **sphp,
203  sql_mode_t sql_mode, const char *params, const char *returns,
204  const char *body, st_sp_chistics *chistics, const char *definer_user,
205  const char *definer_host, longlong created, longlong modified,
206  Stored_program_creation_ctx *creation_ctx);
207 
208 bool sp_create_routine(THD *thd, sp_head *sp, const LEX_USER *definer);
209 
210 bool sp_update_routine(THD *thd, enum_sp_type type, sp_name *name,
211  st_sp_chistics *chistics);
212 
214 
215 /**
216  Structure that represents element in the set of stored routines
217  used by statement or routine.
218 */
219 
221  public:
222  /**
223  Key identifying routine or other object added to the set.
224 
225  Key format: "@<1-byte entry type@>@<db name@>\0@<routine/object name@>\0".
226 
227  @note We use binary comparison for these keys as the @<db name@> component
228  requires case-sensitive comparison on --lower-case-table-names=0
229  systems. On systems where --lower-case-table-names > 0 database
230  names which passed to functions working with this set are already
231  lowercased. So binary comparison is equivalent to case-insensitive
232  comparison for them.
233  Routine names are case and accent insensitive. To achieve such
234  comparison we normalize routine names by converting their characters
235  to their sort weights (according to case and accent insensitive
236  collation). In this case, the actual routine name is also stored in
237  the member m_object_name.
238 
239  @note For Foreign Key objects, '@<db name@>\0@<object name@>\0' part of the
240  key is compatible with keys used by MDL. So one can easily construct
241  MDL_key from this key.
242  */
243  char *m_key;
247 
248  enum entry_type {
252  /**
253  Parent table in a foreign key on which child table there was insert
254  or update. We will lookup new values in parent, so need to acquire
255  SR lock on it.
256  */
258  /**
259  Child table in a foreign key with RESTRICT/NO ACTION as corresponding
260  rule and on which parent table there was delete or update.
261  We will check if old parent key is referenced by child table,
262  so need to acquire SR lock on it.
263  */
265  /**
266  Child table in a foreign key with CASCADE/SET NULL/SET DEFAULT as
267  'on update' rule, on which parent there was update, or with SET NULL/
268  SET DEFAULT as 'on delete' rule, on which parent there was delete.
269  We might need to update rows in child table, so we need to acquire
270  SW lock on it. We also need to take into account that child table
271  might be parent for some other FKs, so such update needs
272  to be handled recursively.
273  */
275  /**
276  Child table in a foreign key with CASCADE as 'on delete' rule for
277  which there was delete from the parent table.
278  We might need to delete rows from the child table, so we need to
279  acquire SW lock on it.
280  We also need to take into account that child table might be parent
281  for some other FKs, so such delete needs to be handled recursively
282  (and even might result in updates).
283  */
284  FK_TABLE_ROLE_CHILD_DELETE
285  };
286 
287  entry_type type() const { return (entry_type)m_key[0]; }
288  const char *db() const { return (char *)m_key + 1; }
289  size_t db_length() const { return m_db_length; }
290  const char *name() const {
291  return use_normalized_key() ? m_object_name.str
292  : (char *)m_key + 1 + m_db_length + 1;
293  }
294  size_t name_length() const {
295  return use_normalized_key() ? m_object_name.length
296  : m_key_length - 1U - m_db_length - 1U - 1U;
297  }
298  bool use_normalized_key() const {
299  return (type() == FUNCTION || type() == PROCEDURE || type() == TRIGGER);
300  }
301 
302  const char *part_mdl_key() {
303  DBUG_ASSERT(!use_normalized_key());
304  return (char *)m_key + 1;
305  }
307  DBUG_ASSERT(!use_normalized_key());
308  return m_key_length - 1U;
309  }
310 
311  /**
312  Next element in list linking all routines in set. See also comments
313  for LEX::sroutine/sroutine_list and sp_head::m_sroutines.
314  */
316  /**
317  Uppermost view which directly or indirectly uses this routine.
318  0 if routine is not used in view. Note that it also can be 0 if
319  statement uses routine both via view and directly.
320  */
322  /**
323  This is for prepared statement validation purposes.
324  A statement looks up and pre-loads all its stored functions
325  at prepare. Later on, if a function is gone from the cache,
326  execute may fail. Similarly, tables involved in referential
327  constraints are also prelocked.
328  Remember the version of the cached item at prepare to be able to
329  invalidate the prepared statement at execute if it
330  changes.
331  */
333 };
334 
335 /*
336  Enum to indicate SP name normalization required when constructing a key
337  in sp_add_used_routine method.
338 */
340  LEAVE_AS_IS = 0, // No normalization needed.
341  LOWERCASE_NAME, // Lower case SP name.
342  UNACCENT_AND_LOWERCASE_NAME, // Lower case SP name and remove accent.
343 };
344 
345 /*
346  Procedures for handling sets of stored routines used by statement or routine.
347 */
348 bool sp_add_used_routine(Query_tables_list *prelocking_ctx, Query_arena *arena,
349  Sroutine_hash_entry::entry_type type, const char *db,
350  size_t db_length, const char *name, size_t name_length,
351  bool lowercase_db,
352  Sp_name_normalize_type name_normalize_type,
353  bool own_routine, TABLE_LIST *belong_to_view);
354 
355 /**
356  Convenience wrapper around sp_add_used_routine() for most common case -
357  stored procedure or function which are explicitly used by the statement.
358 */
359 
360 inline bool sp_add_own_used_routine(Query_tables_list *prelocking_ctx,
361  Query_arena *arena,
363  sp_name *sp_name) {
366 
367  return sp_add_used_routine(
368  prelocking_ctx, arena, type, sp_name->m_db.str, sp_name->m_db.length,
369  sp_name->m_name.str, sp_name->m_name.length, false,
371 }
372 
373 void sp_remove_not_own_routines(Query_tables_list *prelocking_ctx);
375  THD *thd, Query_tables_list *prelocking_ctx,
377  TABLE_LIST *belong_to_view);
378 void sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx,
380  TABLE_LIST *belong_to_view);
381 
382 const uchar *sp_sroutine_key(const uchar *ptr, size_t *plen);
383 
385  const dd::Routine *routine,
386  bool *free_sp_head);
387 
388 bool load_charset(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs,
389  const CHARSET_INFO **cs);
390 
391 bool load_collation(MEM_ROOT *mem_root, Field *field,
392  const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl);
393 
394 ///////////////////////////////////////////////////////////////////////////
395 
397 
398 void sp_finish_parsing(THD *thd);
399 
400 ///////////////////////////////////////////////////////////////////////////
401 
405 
406 bool sp_check_name(LEX_STRING *ident);
407 
408 TABLE_LIST *sp_add_to_query_tables(THD *thd, LEX *lex, const char *db,
409  const char *name);
410 
411 Item *sp_prepare_func_item(THD *thd, Item **it_addr);
412 
413 bool sp_eval_expr(THD *thd, Field *result_field, Item **expr_item_ptr);
414 
415 String *sp_get_item_value(THD *thd, Item *item, String *str);
416 
417 ///////////////////////////////////////////////////////////////////////////
418 
419 #endif /* _SP_H_ */
static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH
Definition: sp.h:75
Definition: sp.h:114
unsigned long long int ulonglong
Definition: my_inttypes.h:55
ulonglong sql_mode_t
Definition: sp.h:57
This file contains the field type.
#define U
Definition: ctype-tis620.cc:74
Definition: sp.h:124
unsigned char uchar
Definition: my_inttypes.h:51
char * str
Definition: mysql_lex_string.h:35
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:38
Definition: sp.h:117
const char * db_name
Definition: rules_table_service.cc:54
int64 m_cache_version
This is for prepared statement validation purposes.
Definition: sp.h:332
Definition: mysql_lex_string.h:34
Abstract base class for functions and procedures.
Definition: routine.h:60
Definition: sp.h:96
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:1791
Definition: sql_lex.h:3303
static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH
Definition: sp.h:72
Sroutine_hash_entry * next
Next element in list linking all routines in set.
Definition: sp.h:315
entry_type type() const
Definition: sp.h:287
const uchar * sp_sroutine_key(const uchar *ptr, size_t *plen)
Definition: sql_lex.h:2174
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:163
void sp_finish_parsing(THD *thd)
Finish parsing of a stored program.
Definition: sp.cc:2150
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:1758
Stored_routine_creation_ctx(THD *thd)
Definition: sp.h:160
Item * sp_prepare_func_item(THD *thd, Item **it_addr)
Prepare an Item for evaluation (call of fix_fields).
Definition: sp.cc:2399
Definition: sp_cache.cc:41
Definition: sp.h:84
const char * str
Definition: mysql_lex_string.h:40
Definition: field.h:694
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:360
char * m_key
Key identifying routine or other object added to the set.
Definition: sp.h:243
const char * name() const
Definition: sp.h:290
Definition: sp.h:250
Definition: mysql_lex_string.h:39
uint16 m_db_length
Definition: sp.h:246
Object_creation_ctx – interface for creation context of database objects (views, stored routines...
Definition: table.h:194
Simple intrusive linked list.
Definition: sp.h:61
bool sp_show_create_routine(THD *thd, enum_sp_type type, sp_name *name)
Implement SHOW CREATE statement for stored routines.
Definition: sp.cc:1346
const char * part_mdl_key()
Definition: sp.h:302
LEX_STRING m_name
Definition: sp_head.h:122
bool sp_create_routine(THD *thd, sp_head *sp, const LEX_USER *definer)
Creates a stored routine.
Definition: sp.cc:707
Structure that represents element in the set of stored routines used by statement or routine...
Definition: sp.h:220
Definition: sp.h:93
Item_result sp_map_result_type(enum enum_field_types type)
Definition: sp.cc:2161
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:1085
enum_sp_return_code
Definition: sp.h:77
Definition: sp.h:251
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:164
uint sp_get_flags_for_command(LEX *lex)
Definition: sp.cc:2211
static ORDER * clone(THD *thd, ORDER *order)
Shallow clone the list of ORDER objects using mem_root and return the cloned list.
Definition: window.cc:76
Definition: table.h:1306
Definition: sql_lex.h:2131
Definition: sp.h:127
Definition: sp.h:126
Definition: sp.h:87
Definition: table.h:2380
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
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:1445
Definition: sp.h:121
Definition: sp.h:122
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:2025
Definition: sp.h:119
Child table in a foreign key with RESTRICT/NO ACTION as corresponding rule and on which parent table ...
Definition: sp.h:264
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:1664
Definition: sp.h:78
Definition: sql_class.h:226
Definition: sp.h:133
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:90
int64_t int64
Definition: my_inttypes.h:67
size_t name_length() const
Definition: sp.h:294
size_t length
Definition: mysql_lex_string.h:41
bool sp_check_name(LEX_STRING *ident)
Check that the name &#39;ident&#39; is ok.
Definition: sp.cc:2370
String * sp_get_item_value(THD *thd, Item *item, String *str)
Return a string representation of the Item value.
Definition: sp.cc:2493
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
uint16_t uint16
Definition: my_inttypes.h:64
Definition: item.h:740
unsigned int uint
Definition: uca-dump.cc:29
case opt name
Definition: sslopt-case.h:32
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:776
enum_sp_type
enum_sp_type defines type codes of stored programs.
Definition: sql_lex.h:196
Definition: sp.h:123
Definition: m_ctype.h:359
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:274
sp_head represents one instance of a stored program.
Definition: sp_head.h:379
size_t length
Definition: mysql_lex_string.h:36
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:497
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
LEX_CSTRING m_db
Definition: sp_head.h:121
Definition: sp.h:81
uint16 m_key_length
Definition: sp.h:245
Parent table in a foreign key on which child table there was insert or update.
Definition: sp.h:257
Definition: sp.h:102
Definition: sp.h:116
bool load_collation(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl)
Definition: sp.cc:140
enum_sp_return_code sp_drop_routine(THD *thd, enum_sp_type type, sp_name *name)
Drops a stored routine.
Definition: sp.cc:841
Definition: sp.h:120
TABLE_LIST * belong_to_view
Uppermost view which directly or indirectly uses this routine.
Definition: sp.h:321
LEX_CSTRING m_object_name
Definition: sp.h:244
Definition: sp.h:128
ulonglong sql_mode_t
Definition: dd_event.h:36
Sp_name_normalize_type
Definition: sp.h:339
Definition: sp.h:125
Item::Type sp_map_item_type(enum enum_field_types type)
Definition: sp.cc:2182
Definition: sp.h:132
Definition: schema.h:61
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:972
Definition: sp.h:131
size_t part_mdl_key_length()
Definition: sp.h:306
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
Definition: sp.h:113
bool use_normalized_key() const
Definition: sp.h:298
Definition: sp.h:99
static const char * sql_mode
Definition: mysqlslap.cc:194
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:1833
Definition: table.h:2481
const char * db() const
Definition: sp.h:288
Definition: sp.h:249
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:119
Definition: sp.h:115
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:2111
entry_type
Definition: sp.h:248
size_t db_length() const
Definition: sp.h:289
Definition: dtoa.cc:601
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:1526
Definition: sp_head.h:119
Definition: sp.h:118
Stored_routine_creation_ctx – creation context of stored routines (stored procedures and functions)...
Definition: sp.h:143
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:1141
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:765
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:1410
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:2425