MySQL 8.0.29
Source Code Documentation
sp_head.h
Go to the documentation of this file.
1/* Copyright (c) 2002, 2021, 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 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_HEAD_H_
24#define _SP_HEAD_H_
25
26#include <stddef.h>
27#include <sys/types.h>
28#include <string>
29#include <vector>
30
31#include "lex_string.h"
32#include "map_helpers.h"
33#include "my_alloc.h"
34#include "my_dbug.h"
35#include "my_inttypes.h"
36#include "my_psi_config.h"
37#include "my_sqlcommand.h"
38#include "my_sys.h"
40#include "mysqld_error.h"
42#include "sql/create_field.h"
43#include "sql/mem_root_array.h" // Mem_root_array
44#include "sql/sql_lex.h"
45#include "sql/sql_list.h"
47#include "sql/table.h"
48
49class Field;
50class Item;
54class THD;
55class sp_head;
56struct CHARSET_INFO;
57struct MY_BITMAP;
58
59/**
60 @defgroup Stored_Routines Stored Routines
61 @ingroup Runtime_Environment
62 @{
63*/
64
65class sp_branch_instr;
66class sp_instr;
67class sp_label;
69class sp_pcontext;
70
71/**
72 Number of PSI_statement_info instruments
73 for internal stored programs statements.
74*/
75#define SP_PSI_STATEMENT_INFO_COUNT 16
76
77#ifdef HAVE_PSI_INTERFACE
78void init_sp_psi_keys(void);
79#endif
80
81///////////////////////////////////////////////////////////////////////////
82
83/**
84 Stored_program_creation_ctx -- base class for creation context of stored
85 programs (stored routines, triggers, events).
86*/
87
89 public:
90 const CHARSET_INFO *get_db_cl() { return m_db_cl; }
91
92 public:
94
95 protected:
96 explicit Stored_program_creation_ctx(THD *thd);
97
99 const CHARSET_INFO *connection_cl,
100 const CHARSET_INFO *db_cl)
101 : Default_object_creation_ctx(client_cs, connection_cl), m_db_cl(db_cl) {}
102
103 protected:
104 void change_env(THD *thd) const override;
105
106 protected:
107 /**
108 db_cl stores the value of the database collation. Both character set
109 and collation attributes are used.
110
111 Database collation is included into the context because it defines the
112 default collation for stored-program variables.
113 */
115};
116
117///////////////////////////////////////////////////////////////////////////
118
119class sp_name {
120 public:
124 bool m_explicit_name; /**< Prepend the db name? */
125
126 sp_name(const LEX_CSTRING &db, const LEX_STRING &name, bool use_explicit_name)
127 : m_db(db), m_name(name), m_explicit_name(use_explicit_name) {
128 m_qname.str = nullptr;
129 m_qname.length = 0;
130 }
131
132 /** Create temporary sp_name object for Sroutine_hash_entry. */
133 sp_name(const Sroutine_hash_entry *rt, char *qname_buff);
134
135 // Init. the qualified name from the db and name.
136 void init_qname(THD *thd); // thd for memroot allocation
137};
138
139///////////////////////////////////////////////////////////////////////////
140
141/**
142 sp_parser_data provides a scope for attributes used at the SP-parsing
143 stage only.
144*/
146 private:
150 };
151
152 public:
159 m_cont_level(0),
162
163 ///////////////////////////////////////////////////////////////////////
164
165 /**
166 Start parsing a stored program body statement.
167
168 This method switches THD::mem_root and THD::m_item_list in order to parse
169 SP-body. The current values are kept to be restored after the body
170 statement is parsed.
171
172 @param thd Thread context.
173 @param sp Stored Program being parsed.
174 */
175 void start_parsing_sp_body(THD *thd, sp_head *sp);
176
177 /**
178 Finish parsing of a stored program body statement.
179
180 This method switches THD::mem_root and THD::m_item_list back when SP-body
181 parsing is completed.
182
183 @param thd Thread context.
184 */
185 void finish_parsing_sp_body(THD *thd);
186
187 /**
188 @retval true if SP-body statement is being parsed.
189 @retval false otherwise.
190 */
191 bool is_parsing_sp_body() const { return m_saved_memroot != nullptr; }
192
193 ///////////////////////////////////////////////////////////////////////
194
195 void process_new_sp_instr(THD *thd, sp_instr *i);
196
197 ///////////////////////////////////////////////////////////////////////
198
199 const char *get_current_stmt_start_ptr() const {
201 }
202
203 void set_current_stmt_start_ptr(const char *stmt_start_ptr) {
204 m_current_stmt_start_ptr = stmt_start_ptr;
205 }
206
207 ///////////////////////////////////////////////////////////////////////
208
209 const char *get_option_start_ptr() const { return m_option_start_ptr; }
210
211 void set_option_start_ptr(const char *option_start_ptr) {
212 m_option_start_ptr = option_start_ptr;
213 }
214
215 ///////////////////////////////////////////////////////////////////////
216
217 const char *get_parameter_start_ptr() const { return m_param_start_ptr; }
218
219 void set_parameter_start_ptr(const char *ptr) { m_param_start_ptr = ptr; }
220
221 const char *get_parameter_end_ptr() const { return m_param_end_ptr; }
222
223 void set_parameter_end_ptr(const char *ptr) { m_param_end_ptr = ptr; }
224
225 ///////////////////////////////////////////////////////////////////////
226
227 const char *get_body_start_ptr() const { return m_body_start_ptr; }
228
229 void set_body_start_ptr(const char *ptr) { m_body_start_ptr = ptr; }
230
231 ///////////////////////////////////////////////////////////////////////
232
233 void push_lex(LEX *lex) { m_lex_stack.push_front(lex); }
234
235 LEX *pop_lex() { return m_lex_stack.pop(); }
236
237 ///////////////////////////////////////////////////////////////////////
238 // Backpatch-list operations.
239 ///////////////////////////////////////////////////////////////////////
240
241 /**
242 Put the instruction on the backpatch list, associated with the label.
243
244 @param i The SP-instruction.
245 @param label The label.
246
247 @return Error flag.
248 */
250
251 /**
252 Update all instruction with the given label in the backpatch list
253 to the given instruction pointer.
254
255 @param label The label.
256 @param dest The instruction pointer.
257 */
258 void do_backpatch(sp_label *label, uint dest);
259
260 ///////////////////////////////////////////////////////////////////////
261 // Backpatch operations for supporting CONTINUE handlers.
262 ///////////////////////////////////////////////////////////////////////
263
264 /**
265 Start a new backpatch level for the SP-instruction requiring continue
266 destination. If the SP-instruction is NULL, the level is just increased.
267
268 @note Only subclasses of sp_lex_branch_instr need backpatching of
269 continue destinations (and no other classes do):
270 - sp_instr_jump_if_not
271 - sp_instr_set_case_expr
272 - sp_instr_jump_case_when
273
274 That's why the methods below accept sp_lex_branch_instr to make this
275 relationship clear. And these two functions are the only places where
276 set_cont_dest() is used, so set_cont_dest() is also a member of
277 sp_lex_branch_instr.
278
279 @todo These functions should probably be declared in a separate
280 interface class, but currently we try to minimize the sp_instr
281 hierarchy.
282
283 @return false always.
284 */
286 ++m_cont_level;
287 return false;
288 }
289
290 /**
291 Add a SP-instruction to the current level.
292
293 @param i The SP-instruction.
294
295 @return Error flag.
296 */
298
299 /**
300 Backpatch (and pop) the current level to the given instruction pointer.
301
302 @param dest The instruction pointer.
303 */
304 void do_cont_backpatch(uint dest);
305
306 private:
307 /// Start of the current statement's query string.
309
310 /// Start of the SET-expression query string.
312
313 /**
314 Stack of LEX-objects. It's needed to handle processing of
315 sub-statements.
316 */
318
319 /**
320 Position in the CREATE PROCEDURE- or CREATE FUNCTION-statement's query
321 string corresponding to the start of parameter declarations (stored
322 procedure or stored function parameters).
323 */
324 const char *m_param_start_ptr;
325
326 /**
327 Position in the CREATE PROCEDURE- or CREATE FUNCTION-statement's query
328 string corresponding to the end of parameter declarations (stored
329 procedure or stored function parameters).
330 */
331 const char *m_param_end_ptr;
332
333 /**
334 Position in the CREATE-/ALTER-stored-program statement's query string
335 corresponding to the start of the first SQL-statement.
336 */
337 const char *m_body_start_ptr;
338
339 /// Instructions needing backpatching
341
342 /**
343 We need a special list for backpatching of instructions with a continue
344 destination (in the case of a continue handler catching an error in
345 the test), since it would otherwise interfere with the normal backpatch
346 mechanism - e.g. jump_if_not instructions have two different destinations
347 which are to be patched differently.
348 Since these occur in a more restricted way (always the same "level" in
349 the code), we don't need the label.
350 */
352
353 /// The current continue backpatch level
355
356 /**********************************************************************
357 The following attributes are used to store THD values during parsing
358 of stored program body.
359
360 @sa start_parsing_sp_body()
361 @sa finish_parsing_sp_body()
362 **********************************************************************/
363
364 /// THD's memroot.
366
367 /// THD's item list.
369};
370
371///////////////////////////////////////////////////////////////////////////
372
373struct SP_TABLE;
374
375/**
376 sp_head represents one instance of a stored program. It might be of any type
377 (stored procedure, function, trigger, event).
378*/
379class sp_head {
380 public:
381 /** Possible values of m_flags */
382 enum {
383 HAS_RETURN = 1, // For FUNCTIONs only: is set if has RETURN
384 MULTI_RESULTS = 8, // Is set if a procedure with SELECT(s)
385 CONTAINS_DYNAMIC_SQL = 16, // Is set if a procedure with PREPARE/EXECUTE
386 IS_INVOKED = 32, // Is set if this sp_head is being used
388 64, // Is set if a procedure with 'set autocommit'
389 /* Is set if a procedure with COMMIT (implicit or explicit) | ROLLBACK */
391 LOG_SLOW_STATEMENTS = 256, // Used by events
392 LOG_GENERAL_LOG = 512, // Used by events
395
396 /**
397 Marks routines that directly (i.e. not by calling other routines)
398 change tables. Note that this flag is set automatically based on
399 type of statements used in the stored routine and is different
400 from routine characteristic provided by user in a form of CONTAINS
401 SQL, READS SQL DATA, MODIFIES SQL DATA clauses. The latter are
402 accepted by parser but pretty much ignored after that.
403 We don't rely on them:
404 a) for compatibility reasons.
405 b) because in CONTAINS SQL case they don't provide enough
406 information anyway.
407 */
409 /**
410 Set when a stored program contains sub-statement(s) that creates or drops
411 temporary table(s). When set, used to mark invoking statement as unsafe to
412 be binlogged in STATEMENT format, when in MIXED mode.
413 */
414 HAS_TEMP_TABLE_DDL = 8192
415 };
416
417 public:
418 /************************************************************************
419 Public attributes.
420 ************************************************************************/
421
422 /// Stored program type.
424
425 /// Stored program flags.
427
428 /**
429 Instrumentation interface for SP.
430 */
432
433 /**
434 Definition of the RETURN-field (from the RETURNS-clause).
435 It's used (and valid) for stored functions only.
436 */
438
439 /// Attributes used during the parsing stage only.
441
442 /// Stored program characteristics.
444
445 /**
446 The value of sql_mode system variable at the CREATE-time.
447
448 It should be stored along with the character sets in the
449 Stored_program_creation_ctx.
450 */
452
453 /// Fully qualified name (@<db name@>.@<sp name@>).
455
456 bool m_explicit_name; ///< Prepend the db name? */
457
466
469
470 /// Recursion level of the current SP instance. The levels are numbered from
471 /// 0.
473
474 /**
475 A list of diferent recursion level instances for the same procedure.
476 For every recursion level we have a sp_head instance. This instances
477 connected in the list. The list ordered by increasing recursion level
478 (m_recursion_level).
479 */
481
482 /// Pointer to the first element of the above list
484
485 /**
486 Pointer to the first free (non-INVOKED) routine in the list of
487 cached instances for this SP. This pointer is set only for the first
488 SP in the list of instances (see above m_first_cached_sp pointer).
489 The pointer equal to 0 if we have no free instances.
490 For non-first instance value of this pointer meaningless (point to itself);
491 */
493
494 /**
495 Pointer to the last element in the list of instances of the SP.
496 For non-first instance value of this pointer meaningless (point to itself);
497 */
499
500 /**
501 Set containing names of stored routines used by this routine.
502 Note that unlike elements of similar set for statement elements of this
503 set are not linked in one list. Because of this we are able save memory
504 by using for this set same objects that are used in 'sroutines' sets
505 for statements of which this stored routine consists.
506
507 See Sroutine_hash_entry for explanation why this hash uses binary
508 key comparison.
509 */
511
512 /*
513 Security context for stored routine which should be run under
514 definer privileges.
515 */
517
518 /////////////////////////////////////////////////////////////////////////
519 // Trigger-specific public attributes.
520 /////////////////////////////////////////////////////////////////////////
521
522 /**
523 List of item (Item_trigger_field objects)'s lists representing fields
524 in old/new version of row in trigger. We use this list for checking
525 whether all such fields are valid or not at trigger creation time and for
526 binding these fields to TABLE object at table open (although for latter
527 pointer to table being opened is probably enough).
528 */
530 /**
531 List of all the Item_trigger_field items created while parsing
532 sp instruction. After parsing, in add_instr method this list
533 is moved to per instruction Item_trigger_field list
534 "sp_lex_instr::m_trig_field_list".
535 */
537
538 /// Trigger characteristics.
540
541 /// The Table_trigger_dispatcher instance, where this trigger belongs to.
543
544 public:
545 static void destroy(sp_head *sp);
546
547 /// Is this routine being executed?
548 bool is_invoked() const { return m_flags & IS_INVOKED; }
549
550 /**
551 Get the value of the SP cache version, as remembered
552 when the routine was inserted into the cache.
553 */
555
556 /// Set the value of the SP cache version.
559 }
560
562
564 m_creation_ctx = creation_ctx->clone(&main_mem_root);
565 }
566
567 /// Set the body-definition start position.
568 void set_body_start(THD *thd, const char *begin_ptr);
569
570 /// Set the statement-definition (body-definition) end position.
571 void set_body_end(THD *thd);
572
574 GRANT_INFO *subject_table_grant,
575 bool need_fix_fields);
576
577 void mark_used_trigger_fields(TABLE *subject_table);
578
579 bool has_updated_trigger_fields(const MY_BITMAP *used_fields) const;
580
581 /**
582 Execute trigger stored program.
583
584 - changes security context for triggers
585 - switch to new memroot
586 - call sp_head::execute
587 - restore old memroot
588 - restores security context
589
590 @param thd Thread context
591 @param db_name database name
592 @param table_name table name
593 @param grant_info GRANT_INFO structure to be filled with
594 information about definer's privileges
595 on subject table
596
597 @todo
598 We should create sp_rcontext once per command and reuse it
599 on subsequent executions of a trigger.
600
601 @return Error status.
602 */
603 bool execute_trigger(THD *thd, const LEX_CSTRING &db_name,
604 const LEX_CSTRING &table_name, GRANT_INFO *grant_info);
605
606 /**
607 Execute a function.
608
609 - evaluate parameters
610 - changes security context for SUID routines
611 - switch to new memroot
612 - call sp_head::execute
613 - restore old memroot
614 - evaluate the return value
615 - restores security context
616
617 @param thd Thread context.
618 @param args Passed arguments (these are items from containing
619 statement?)
620 @param argcount Number of passed arguments. We need to check if
621 this is correct.
622 @param return_fld Save result here.
623
624 @todo
625 We should create sp_rcontext once per command and reuse
626 it on subsequent executions of a function/trigger.
627
628 @todo
629 In future we should associate call arena/mem_root with
630 sp_rcontext and allocate all these objects (and sp_rcontext
631 itself) on it directly rather than juggle with arenas.
632
633 @return Error status.
634 */
635 bool execute_function(THD *thd, Item **args, uint argcount,
636 Field *return_fld);
637
638 /**
639 Execute a procedure.
640
641 The function does the following steps:
642 - Set all parameters
643 - changes security context for SUID routines
644 - call sp_head::execute
645 - copy back values of INOUT and OUT parameters
646 - restores security context
647
648 @param thd Thread context.
649 @param args List of values passed as arguments.
650
651 @return Error status.
652 */
653
655
656 /**
657 Add instruction to SP.
658
659 @param thd Thread context.
660 @param instr Instruction.
661
662 @return Error status.
663 */
664 bool add_instr(THD *thd, sp_instr *instr);
665
666 /**
667 Returns true if any substatement in the routine directly
668 (not through another routine) modifies data/changes table.
669
670 @sa Comment for MODIFIES_DATA flag.
671 */
672 bool modifies_data() const { return m_flags & MODIFIES_DATA; }
673
674 /**
675 @returns true if stored program has sub-statement(s) to CREATE/DROP
676 temporary table(s).
677 @retval true if HAS_TEMP_TABLE_DDL is set in m_flags.
678 @retval false Otherwise.
679 */
681
682 uint instructions() { return static_cast<uint>(m_instructions.size()); }
683
685
686 /**
687 Reset LEX-object during parsing, before we parse a sub statement.
688
689 @param thd Thread context.
690
691 @return Error status.
692 */
693 bool reset_lex(THD *thd);
694
695 /**
696 Restore LEX-object during parsing, after we have parsed a sub statement.
697
698 @param thd Thread context.
699
700 @return Error status.
701 */
702 bool restore_lex(THD *thd);
703
704 char *name(uint *lenp = nullptr) const {
705 if (lenp) *lenp = (uint)m_name.length;
706 return m_name.str;
707 }
708
709 /**
710 Create Field-object corresponding to the RETURN field of a stored function.
711 This operation makes sense for stored functions only.
712
713 @param thd thread context.
714 @param field_max_length the max length (in the sense of Item classes).
715 @param field_name the field name (item name).
716 @param table the field's table.
717
718 @return newly created and initialized Field-instance,
719 or NULL in case of error.
720 */
721 Field *create_result_field(THD *thd, size_t field_max_length,
722 const char *field_name, TABLE *table) const;
723
724 void returns_type(THD *thd, String *result) const;
725
726 void set_info(longlong created, longlong modified, st_sp_chistics *chistics,
728
729 void set_definer(const char *definer, size_t definerlen);
730 void set_definer(const LEX_CSTRING &user_name, const LEX_CSTRING &host_name);
731
732 /**
733 Do some minimal optimization of the code:
734 -# Mark used instructions
735 -# While doing this, shortcut jumps to jump instructions
736 -# Compact the code, removing unused instructions.
737
738 This is the main mark and move loop; it relies on the following methods
739 in sp_instr and its subclasses:
740
741 - opt_mark() : Mark instruction as reachable
742 - opt_shortcut_jump(): Shortcut jumps to the final destination;
743 used by opt_mark().
744 - opt_move() : Update moved instruction
745 - set_destination() : Set the new destination (jump instructions only)
746 */
747 void optimize();
748
749 /**
750 Helper used during flow analysis during code optimization.
751 See the implementation of <code>opt_mark()</code>.
752 @param ip the instruction to add to the leads list
753 @param leads the list of remaining paths to explore in the graph that
754 represents the code, during flow analysis.
755 */
756 void add_mark_lead(uint ip, List<sp_instr> *leads);
757
758 /**
759 Get SP-instruction at given index.
760
761 NOTE: it is important to have *unsigned* int here, sometimes we get (-1)
762 passed here, so it get's converted to MAX_INT, and the result of the
763 function call is NULL.
764 */
766 return (i < (uint)m_instructions.size()) ? m_instructions.at(i) : NULL;
767 }
768
769 /**
770 Add tables used by routine to the table list.
771
772 Converts multi-set of tables used by this routine to table list and adds
773 this list to the end of table list specified by 'query_tables_last_ptr'.
774
775 Elements of list will be allocated in PS memroot, so this list will be
776 persistent between PS executions.
777
778 @param[in] thd Thread context
779 @param[in,out] query_tables_last_ptr Pointer to the next_global member of
780 last element of the list where tables
781 will be added (or to its root).
782 @param[in] sql_command SQL-command for which we are adding
783 elements to the table list.
784 @param[in] belong_to_view Uppermost view which uses this
785 routine, NULL if none.
786 */
788 TABLE_LIST ***query_tables_last_ptr,
789 enum_sql_command sql_command,
790 TABLE_LIST *belong_to_view);
791
792 /**
793 Check if this stored routine contains statements disallowed
794 in a stored function or trigger, and set an appropriate error message
795 if this is the case.
796 */
799 my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "Dynamic SQL");
800 else if (m_flags & MULTI_RESULTS)
801 my_error(ER_SP_NO_RETSET, MYF(0), where);
803 my_error(ER_SP_CANT_SET_AUTOCOMMIT, MYF(0));
805 my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
806 else if (m_flags & HAS_SQLCOM_RESET)
807 my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "RESET");
808 else if (m_flags & HAS_SQLCOM_FLUSH)
809 my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "FLUSH");
810
811 return (m_flags &
814 }
815
816#ifndef NDEBUG
817 /**
818 Return the routine instructions as a result set.
819 @return Error status.
820 */
821 bool show_routine_code(THD *thd);
822#endif
823
824 /*
825 This method is intended for attributes of a routine which need
826 to propagate upwards to the Query_tables_list of the caller (when
827 a property of a sp_head needs to "taint" the calling statement).
828 */
830 /*
831 If this routine needs row-based binary logging, the entire top statement
832 too (we cannot switch from statement-based to row-based only for this
833 routine, as in statement-based the top-statement may be binlogged and
834 the sub-statements not).
835 */
836 DBUG_PRINT("info", ("lex->get_stmt_unsafe_flags(): 0x%x",
837 prelocking_ctx->get_stmt_unsafe_flags()));
838 DBUG_PRINT("info", ("sp_head(0x%p=%s)->unsafe_flags: 0x%x", this, name(),
839 unsafe_flags));
840 prelocking_ctx->set_stmt_unsafe_flags(unsafe_flags);
841
842 /*
843 If temporary table is created or dropped in the stored program then
844 statement is unsafe to be logged in STATEMENT format, when in MIXED mode.
845 */
847 }
848
849 /**
850 @return root parsing context for this stored program.
851 */
853 return const_cast<sp_pcontext *>(m_root_parsing_ctx);
854 }
855
856 /**
857 @return SP-persistent mem-root. Instructions and expressions are stored in
858 its memory between executions.
859 */
861 return const_cast<MEM_ROOT *>(&main_mem_root);
862 }
863
864 /**
865 Check if a user has access right to a SP.
866
867 @param thd Thread context.
868 @param[out] full_access Set to 1 if the user is the owner
869 of the stored program.
870
871 @return Error status.
872 */
873 bool check_show_access(THD *thd, bool *full_access);
874
875 /**
876 Change routine security context, and check if there is an EXECUTE privilege
877 in new context. If there is no EXECUTE privilege, change the context back
878 and return an error.
879
880 @param thd Thread context.
881 @param[out] save_ctx Where to save the old security context.
882
883 @todo Cache if the definer has the rights to use the object on the first
884 usage and reset the cache only if someone does a GRANT statement that 'may'
885 affect this.
886
887 @return Error status.
888 */
889 bool set_security_ctx(THD *thd, Security_context **save_ctx);
890
891 private:
892 /// Use sp_start_parsing() to create instances of sp_head.
894
895 /// Use destroy() to destoy instances of sp_head.
896 ~sp_head();
897
898 /// SP-persistent memory root (for instructions and expressions).
900
901 /// Root parsing context (topmost BEGIN..END block) of this SP.
903
904 /// The SP-instructions.
906
907 /**
908 Multi-set representing optimized list of tables to be locked by this
909 routine. Does not include tables which are used by invoked routines.
910
911 @note
912 For prelocking-free SPs this multiset is constructed too.
913 We do so because the same instance of sp_head may be called both
914 in prelocked mode and in non-prelocked mode.
915 */
917
918 /*
919 The same information as in m_sptabs, but sorted (by an arbitrary key).
920 This is useful to get consistent locking order, which makes MTR tests
921 more deterministic across platforms. It does not have a bearing on the
922 actual behavior of the server.
923 */
924 std::vector<SP_TABLE *> m_sptabs_sorted;
925
926 /**
927 Version of the stored routine cache at the moment when the
928 routine was added to it. Is used only for functions and
929 procedures, not used for triggers or events. When sp_head is
930 created, its version is 0. When it's added to the cache, the
931 version is assigned the global value 'Cversion'.
932 If later on Cversion is incremented, we know that the routine
933 is obsolete and should not be used --
934 sp_cache_flush_obsolete() will purge it.
935 */
937
938 /// Snapshot of several system variables at CREATE-time.
940
941 /// Flags of LEX::enum_binlog_stmt_unsafe.
943
944 private:
945 /// Copy sp name from parser.
946 void init_sp_name(THD *thd, sp_name *spname);
947
948 /**
949 Execute the routine. The main instruction jump loop is there.
950 Assume the parameters already set.
951
952 @param thd Thread context.
953 @param merge_da_on_success Flag specifying if Warning Info should be
954 propagated to the caller on Completion
955 Condition or not.
956
957 @todo
958 - Will write this SP statement into binlog separately
959 (TODO: consider changing the condition to "not inside event union")
960
961 @return Error status.
962 */
963 bool execute(THD *thd, bool merge_da_on_success);
964
965 /**
966 Perform a forward flow analysis in the generated code.
967 Mark reachable instructions, for the optimizer.
968 */
969 void opt_mark();
970
971 /**
972 Merge the list of tables used by some query into the multi-set of
973 tables used by routine.
974
975 @param thd Thread context.
976 @param table Table list.
977 @param lex_for_tmp_check LEX of the query for which we are merging
978 table list.
979
980 @note
981 This method will use LEX provided to check whenever we are creating
982 temporary table and mark it as such in target multi-set.
983
984 @return Error status.
985 */
986 bool merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check);
987
988 friend sp_head *sp_start_parsing(THD *thd, enum_sp_type sp_type,
990
991 // Prevent use of copy constructor and assignment operator.
992 sp_head(const sp_head &);
994};
995
996///////////////////////////////////////////////////////////////////////////
997
998/**
999 @} (end of group Stored_Routines)
1000*/
1001
1002#endif /* _SP_HEAD_H_ */
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:50
Default_object_creation_ctx – default implementation of Object_creation_ctx.
Definition: table.h:218
Definition: field.h:573
Represents NEW/OLD version of field of row which is changed/read in trigger.
Definition: item.h:6393
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Definition: sql_list.h:433
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:421
Definition: sql_lex.h:2465
void set_stmt_unsafe_flags(uint32 flags)
Set the bits of binlog_stmt_flags determining the type of unsafeness of the current statement.
Definition: sql_lex.h:2789
uint32 get_stmt_unsafe_flags() const
Return a binary combination of all unsafe warnings for the statement.
Definition: sql_lex.h:2802
void set_stmt_unsafe_with_mixed_mode()
Definition: sql_lex.h:3027
Simple intrusive linked list.
Definition: sql_list.h:45
A set of THD members describing the current authenticated user.
Definition: sql_security_ctx.h:53
Structure that represents element in the set of stored routines used by statement or routine.
Definition: sp.h:223
Stored_program_creation_ctx – base class for creation context of stored programs (stored routines,...
Definition: sp_head.h:88
const CHARSET_INFO * m_db_cl
db_cl stores the value of the database collation.
Definition: sp_head.h:114
virtual Stored_program_creation_ctx * clone(MEM_ROOT *mem_root)=0
void change_env(THD *thd) const override
Definition: sp_head.cc:3509
Stored_program_creation_ctx(THD *thd)
Definition: sp_head.cc:3505
Stored_program_creation_ctx(const CHARSET_INFO *client_cs, const CHARSET_INFO *connection_cl, const CHARSET_INFO *db_cl)
Definition: sp_head.h:98
const CHARSET_INFO * get_db_cl()
Definition: sp_head.h:90
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:945
This class holds all information about triggers of a table.
Definition: table_trigger_dispatcher.h:62
This is an interface to be used from Item_trigger_field to access information about table trigger fie...
Definition: table_trigger_field_support.h:43
std::unordered_map, but with my_malloc and collation-aware comparison.
Definition: map_helpers.h:209
std::unordered_map, but with my_malloc, so that you can track the memory used using PSI memory keys.
Definition: map_helpers.h:147
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
An interface for all SP-instructions with destinations that need to be updated by the SP-optimizer.
Definition: sp_instr.h:77
sp_head represents one instance of a stored program.
Definition: sp_head.h:379
void set_creation_ctx(Stored_program_creation_ctx *creation_ctx)
Definition: sp_head.h:563
bool add_instr(THD *thd, sp_instr *instr)
Add instruction to SP.
Definition: sp_head.cc:3055
LEX_STRING m_name
Definition: sp_head.h:459
bool execute_function(THD *thd, Item **args, uint argcount, Field *return_fld)
Execute a function.
Definition: sp_head.cc:2507
st_sp_chistics * m_chistics
Stored program characteristics.
Definition: sp_head.h:443
sp_head(MEM_ROOT &&mem_root, enum_sp_type type)
Use sp_start_parsing() to create instances of sp_head.
Definition: sp_head.cc:1698
int64 sp_cache_version() const
Get the value of the SP cache version, as remembered when the routine was inserted into the cache.
Definition: sp_head.h:554
void set_definer(const char *definer, size_t definerlen)
Definition: sp_head.cc:3031
void operator=(sp_head &)
LEX_CSTRING m_body
Definition: sp_head.h:461
bool setup_trigger_fields(THD *thd, Table_trigger_field_support *tfs, GRANT_INFO *subject_table_grant, bool need_fix_fields)
Definition: sp_head.cc:1825
bool has_updated_trigger_fields(const MY_BITMAP *used_fields) const
Check whether any table's fields are used in trigger.
Definition: sp_head.cc:1877
void init_sp_name(THD *thd, sp_name *spname)
Copy sp name from parser.
Definition: sp_head.cc:1748
bool execute_procedure(THD *thd, mem_root_deque< Item * > *args)
Execute a procedure.
Definition: sp_head.cc:2720
void set_body_end(THD *thd)
Set the statement-definition (body-definition) end position.
Definition: sp_head.cc:1777
void set_info(longlong created, longlong modified, st_sp_chistics *chistics, sql_mode_t sql_mode)
Definition: sp_head.cc:3017
ulong m_recursion_level
Recursion level of the current SP instance.
Definition: sp_head.h:472
longlong m_created
Definition: sp_head.h:467
bool merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check)
Merge the list of tables used by some query into the multi-set of tables used by routine.
Definition: sp_head.cc:3210
sp_head * m_first_instance
Pointer to the first element of the above list.
Definition: sp_head.h:483
uint instructions()
Definition: sp_head.h:682
collation_unordered_map< std::string, SP_TABLE * > m_sptabs
Multi-set representing optimized list of tables to be locked by this routine.
Definition: sp_head.h:916
bool execute(THD *thd, bool merge_da_on_success)
Execute the routine.
Definition: sp_head.cc:1982
Security_context m_security_ctx
Definition: sp_head.h:516
Create_field m_return_field_def
Definition of the RETURN-field (from the RETURNS-clause).
Definition: sp_head.h:437
LEX_STRING m_defstr
Definition: sp_head.h:463
uint m_flags
Stored program flags.
Definition: sp_head.h:426
SQL_I_List< SQL_I_List< Item_trigger_field > > m_list_of_trig_fields_item_lists
List of item (Item_trigger_field objects)'s lists representing fields in old/new version of row in tr...
Definition: sp_head.h:529
friend 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:2237
int64 m_sp_cache_version
Version of the stored routine cache at the moment when the routine was added to it.
Definition: sp_head.h:936
sp_pcontext * m_root_parsing_ctx
Root parsing context (topmost BEGIN..END block) of this SP.
Definition: sp_head.h:902
LEX_STRING m_params
Definition: sp_head.h:460
bool show_routine_code(THD *thd)
Return the routine instructions as a result set.
Definition: sp_head.cc:3160
class Table_trigger_dispatcher * m_trg_list
The Table_trigger_dispatcher instance, where this trigger belongs to.
Definition: sp_head.h:542
malloc_unordered_map< std::string, Sroutine_hash_entry * > m_sroutines
Set containing names of stored routines used by this routine.
Definition: sp_head.h:510
std::vector< SP_TABLE * > m_sptabs_sorted
Definition: sp_head.h:924
LEX_STRING m_db
Definition: sp_head.h:458
Field * create_result_field(THD *thd, size_t field_max_length, const char *field_name, TABLE *table) const
Create Field-object corresponding to the RETURN field of a stored function.
Definition: sp_head.cc:1924
bool check_show_access(THD *thd, bool *full_access)
Check if a user has access right to a SP.
Definition: sp_head.cc:3373
void set_sp_cache_version(int64 sp_cache_version)
Set the value of the SP cache version.
Definition: sp_head.h:557
bool m_explicit_name
Prepend the db name? *‍/.
Definition: sp_head.h:456
bool is_not_allowed_in_function(const char *where)
Check if this stored routine contains statements disallowed in a stored function or trigger,...
Definition: sp_head.h:797
void mark_used_trigger_fields(TABLE *subject_table)
Definition: sp_head.cc:1847
void returns_type(THD *thd, String *result) const
Definition: sp_head.cc:1955
LEX_CSTRING m_body_utf8
Definition: sp_head.h:462
Mem_root_array< sp_instr * > m_instructions
The SP-instructions.
Definition: sp_head.h:905
uint32 unsafe_flags
Flags of LEX::enum_binlog_stmt_unsafe.
Definition: sp_head.h:942
Stored_program_creation_ctx * m_creation_ctx
Snapshot of several system variables at CREATE-time.
Definition: sp_head.h:939
bool is_invoked() const
Is this routine being executed?
Definition: sp_head.h:548
void add_used_tables_to_table_list(THD *thd, TABLE_LIST ***query_tables_last_ptr, enum_sql_command sql_command, TABLE_LIST *belong_to_view)
Add tables used by routine to the table list.
Definition: sp_head.cc:3300
LEX_STRING m_qname
Fully qualified name (<db name>.<sp name>).
Definition: sp_head.h:454
LEX_STRING m_definer_host
Definition: sp_head.h:465
sql_mode_t m_sql_mode
The value of sql_mode system variable at the CREATE-time.
Definition: sp_head.h:451
MEM_ROOT * get_persistent_mem_root() const
Definition: sp_head.h:860
longlong m_modified
Definition: sp_head.h:468
bool restore_lex(THD *thd)
Restore LEX-object during parsing, after we have parsed a sub statement.
Definition: sp_head.cc:2967
static void destroy(sp_head *sp)
Definition: sp_head.cc:1687
~sp_head()
Use destroy() to destoy instances of sp_head.
Definition: sp_head.cc:1896
sp_instr * last_instruction()
Definition: sp_head.h:684
LEX_STRING m_definer_user
Definition: sp_head.h:464
char * name(uint *lenp=nullptr) const
Definition: sp_head.h:704
void set_body_start(THD *thd, const char *begin_ptr)
Set the body-definition start position.
Definition: sp_head.cc:1771
void add_mark_lead(uint ip, List< sp_instr > *leads)
Helper used during flow analysis during code optimization.
Definition: sp_head.cc:3119
bool has_temp_table_ddl() const
Definition: sp_head.h:680
bool execute_trigger(THD *thd, const LEX_CSTRING &db_name, const LEX_CSTRING &table_name, GRANT_INFO *grant_info)
Execute trigger stored program.
Definition: sp_head.cc:2401
PSI_sp_share * m_sp_share
Instrumentation interface for SP.
Definition: sp_head.h:431
sp_parser_data m_parser_data
Attributes used during the parsing stage only.
Definition: sp_head.h:440
Stored_program_creation_ctx * get_creation_ctx()
Definition: sp_head.h:561
sp_head * m_next_cached_sp
A list of diferent recursion level instances for the same procedure.
Definition: sp_head.h:480
bool reset_lex(THD *thd)
Reset LEX-object during parsing, before we parse a sub statement.
Definition: sp_head.cc:2943
MEM_ROOT main_mem_root
SP-persistent memory root (for instructions and expressions).
Definition: sp_head.h:899
st_trg_chistics m_trg_chistics
Trigger characteristics.
Definition: sp_head.h:539
sp_head * m_first_free_instance
Pointer to the first free (non-INVOKED) routine in the list of cached instances for this SP.
Definition: sp_head.h:492
void optimize()
Do some minimal optimization of the code:
Definition: sp_head.cc:3086
enum_sp_type m_type
Stored program type.
Definition: sp_head.h:423
bool modifies_data() const
Returns true if any substatement in the routine directly (not through another routine) modifies data/...
Definition: sp_head.h:672
void propagate_attributes(Query_tables_list *prelocking_ctx)
Definition: sp_head.h:829
sp_head * m_last_cached_sp
Pointer to the last element in the list of instances of the SP.
Definition: sp_head.h:498
void opt_mark()
Perform a forward flow analysis in the generated code.
Definition: sp_head.cc:3125
sp_pcontext * get_root_parsing_context() const
Definition: sp_head.h:852
@ MULTI_RESULTS
Definition: sp_head.h:384
@ HAS_TEMP_TABLE_DDL
Set when a stored program contains sub-statement(s) that creates or drops temporary table(s).
Definition: sp_head.h:414
@ HAS_SQLCOM_RESET
Definition: sp_head.h:393
@ MODIFIES_DATA
Marks routines that directly (i.e.
Definition: sp_head.h:408
@ LOG_GENERAL_LOG
Definition: sp_head.h:392
@ HAS_COMMIT_OR_ROLLBACK
Definition: sp_head.h:390
@ IS_INVOKED
Definition: sp_head.h:386
@ CONTAINS_DYNAMIC_SQL
Definition: sp_head.h:385
@ HAS_SQLCOM_FLUSH
Definition: sp_head.h:394
@ LOG_SLOW_STATEMENTS
Definition: sp_head.h:391
@ HAS_RETURN
Definition: sp_head.h:383
@ HAS_SET_AUTOCOMMIT_STMT
Definition: sp_head.h:387
sp_head(const sp_head &)
sp_instr * get_instr(uint i)
Get SP-instruction at given index.
Definition: sp_head.h:765
bool set_security_ctx(THD *thd, Security_context **save_ctx)
Change routine security context, and check if there is an EXECUTE privilege in new context.
Definition: sp_head.cc:3387
SQL_I_List< Item_trigger_field > m_cur_instr_trig_field_items
List of all the Item_trigger_field items created while parsing sp instruction.
Definition: sp_head.h:536
Base class for every SP-instruction.
Definition: sp_instr.h:104
This class represents an SQL/PSM label.
Definition: sp_pcontext.h:93
sp_lex_branch_instr is a base class for SP-instructions, which might perform conditional jump dependi...
Definition: sp_instr.h:788
Definition: sp_head.h:119
LEX_STRING m_qname
Definition: sp_head.h:123
bool m_explicit_name
Prepend the db name?
Definition: sp_head.h:124
void init_qname(THD *thd)
Init the qualified name from the db and name.
Definition: sp_head.cc:1673
LEX_STRING m_name
Definition: sp_head.h:122
sp_name(const LEX_CSTRING &db, const LEX_STRING &name, bool use_explicit_name)
Definition: sp_head.h:126
LEX_CSTRING m_db
Definition: sp_head.h:121
sp_parser_data provides a scope for attributes used at the SP-parsing stage only.
Definition: sp_head.h:145
const char * m_current_stmt_start_ptr
Start of the current statement's query string.
Definition: sp_head.h:308
void push_lex(LEX *lex)
Definition: sp_head.h:233
uint m_cont_level
The current continue backpatch level.
Definition: sp_head.h:354
bool add_backpatch_entry(sp_branch_instr *i, sp_label *label)
Put the instruction on the backpatch list, associated with the label.
Definition: sp_head.cc:3452
void start_parsing_sp_body(THD *thd, sp_head *sp)
Start parsing a stored program body statement.
Definition: sp_head.cc:3419
Item * m_saved_item_list
THD's item list.
Definition: sp_head.h:368
const char * get_option_start_ptr() const
Definition: sp_head.h:209
const char * m_param_end_ptr
Position in the CREATE PROCEDURE- or CREATE FUNCTION-statement's query string corresponding to the en...
Definition: sp_head.h:331
void do_cont_backpatch(uint dest)
Backpatch (and pop) the current level to the given instruction pointer.
Definition: sp_head.cc:3477
bool is_parsing_sp_body() const
Definition: sp_head.h:191
MEM_ROOT * m_saved_memroot
THD's memroot.
Definition: sp_head.h:365
sp_parser_data()
Definition: sp_head.h:153
const char * m_param_start_ptr
Position in the CREATE PROCEDURE- or CREATE FUNCTION-statement's query string corresponding to the st...
Definition: sp_head.h:324
void process_new_sp_instr(THD *thd, sp_instr *i)
Definition: sp_head.cc:3488
const char * get_parameter_end_ptr() const
Definition: sp_head.h:221
void set_body_start_ptr(const char *ptr)
Definition: sp_head.h:229
void set_parameter_start_ptr(const char *ptr)
Definition: sp_head.h:219
void set_current_stmt_start_ptr(const char *stmt_start_ptr)
Definition: sp_head.h:203
const char * m_body_start_ptr
Position in the CREATE-/ALTER-stored-program statement's query string corresponding to the start of t...
Definition: sp_head.h:337
void set_parameter_end_ptr(const char *ptr)
Definition: sp_head.h:223
List< Backpatch_info > m_backpatch
Instructions needing backpatching.
Definition: sp_head.h:340
LEX * pop_lex()
Definition: sp_head.h:235
bool add_cont_backpatch_entry(sp_lex_branch_instr *i)
Add a SP-instruction to the current level.
Definition: sp_head.cc:3472
List< LEX > m_lex_stack
Stack of LEX-objects.
Definition: sp_head.h:317
const char * m_option_start_ptr
Start of the SET-expression query string.
Definition: sp_head.h:311
List< sp_lex_branch_instr > m_cont_backpatch
We need a special list for backpatching of instructions with a continue destination (in the case of a...
Definition: sp_head.h:351
void set_option_start_ptr(const char *option_start_ptr)
Definition: sp_head.h:211
const char * get_body_start_ptr() const
Definition: sp_head.h:227
bool new_cont_backpatch()
Start a new backpatch level for the SP-instruction requiring continue destination.
Definition: sp_head.h:285
void finish_parsing_sp_body(THD *thd)
Finish parsing of a stored program body statement.
Definition: sp_head.cc:3430
const char * get_parameter_start_ptr() const
Definition: sp_head.h:217
const char * get_current_stmt_start_ptr() const
Definition: sp_head.h:199
void do_backpatch(sp_label *label, uint dest)
Update all instruction with the given label in the backpatch list to the given instruction pointer.
Definition: sp_head.cc:3463
The class represents parse-time context, which keeps track of declared variables/parameters,...
Definition: sp_pcontext.h:251
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
ulonglong sql_mode_t
Definition: dd_event.h:36
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
enum_sp_type
enum_sp_type defines type codes of stored programs.
Definition: sql_lex.h:220
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:215
void init_sp_psi_keys(void)
Definition: sp_head.cc:1541
struct PSI_sp_share PSI_sp_share
Definition: psi_statement_bits.h:103
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:180
Some integer typedefs for easier portability.
int64_t int64
Definition: my_inttypes.h:67
long long int longlong
Definition: my_inttypes.h:54
#define MYF(v)
Definition: my_inttypes.h:96
uint32_t uint32
Definition: my_inttypes.h:66
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
enum_sql_command
Definition: my_sqlcommand.h:45
Common header for many mysys elements.
static char * where
Definition: mysqldump.cc:133
static const char * sql_mode
Definition: mysqlslap.cc:193
const char * table_name
Definition: rules_table_service.cc:55
const char * db_name
Definition: rules_table_service.cc:54
Performance schema instrumentation interface.
required string type
Definition: replication_group_member_actions.proto:33
case opt name
Definition: sslopt-case.h:32
Definition: m_ctype.h:354
The current state of the privilege checking process for the current user, SQL statement and SQL objec...
Definition: table.h:356
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3659
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
Definition: mysql_lex_string.h:39
Definition: mysql_lex_string.h:34
char * str
Definition: mysql_lex_string.h:35
size_t length
Definition: mysql_lex_string.h:36
Definition: my_bitmap.h:42
SP_TABLE represents all instances of one table in an optimized multi-set of tables used by a stored p...
Definition: sp_head.cc:1567
Definition: table.h:2684
Definition: table.h:1394
Definition: result.h:29
Definition: sp_head.h:147
sp_label * label
Definition: sp_head.h:148
sp_branch_instr * instr
Definition: sp_head.h:149
Definition: sql_lex.h:2424
Definition: sql_lex.h:2433
#define NULL
Definition: types.h:54
unsigned int uint
Definition: uca-dump.cc:29