| 1 | /* -*- C++ -*- */ |
| 2 | /* |
| 3 | Copyright (c) 2002, 2011, Oracle and/or its affiliates. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; version 2 of the License. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 17 | |
| 18 | #ifndef _SP_HEAD_H_ |
| 19 | #define _SP_HEAD_H_ |
| 20 | |
| 21 | #ifdef USE_PRAGMA_INTERFACE |
| 22 | #pragma interface /* gcc class implementation */ |
| 23 | #endif |
| 24 | |
| 25 | /* |
| 26 | It is necessary to include set_var.h instead of item.h because there |
| 27 | are dependencies on include order for set_var.h and item.h. This |
| 28 | will be resolved later. |
| 29 | */ |
| 30 | #include "sql_class.h" // THD, set_var.h: THD |
| 31 | #include "set_var.h" // Item |
| 32 | #include "sp_pcontext.h" // sp_pcontext |
| 33 | #include <stddef.h> |
| 34 | #include "sp.h" |
| 35 | |
| 36 | /** |
| 37 | @defgroup Stored_Routines Stored Routines |
| 38 | @ingroup Runtime_Environment |
| 39 | @{ |
| 40 | */ |
| 41 | |
| 42 | Item::Type |
| 43 | sp_map_item_type(const Type_handler *handler); |
| 44 | |
| 45 | uint |
| 46 | sp_get_flags_for_command(LEX *lex); |
| 47 | |
| 48 | class sp_instr; |
| 49 | class sp_instr_opt_meta; |
| 50 | class sp_instr_jump_if_not; |
| 51 | |
| 52 | /*************************************************************************/ |
| 53 | |
| 54 | /** |
| 55 | Stored_program_creation_ctx -- base class for creation context of stored |
| 56 | programs (stored routines, triggers, events). |
| 57 | */ |
| 58 | |
| 59 | class Stored_program_creation_ctx :public Default_object_creation_ctx |
| 60 | { |
| 61 | public: |
| 62 | CHARSET_INFO *get_db_cl() |
| 63 | { |
| 64 | return m_db_cl; |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | virtual Stored_program_creation_ctx *clone(MEM_ROOT *mem_root) = 0; |
| 69 | |
| 70 | protected: |
| 71 | Stored_program_creation_ctx(THD *thd) |
| 72 | : Default_object_creation_ctx(thd), |
| 73 | m_db_cl(thd->variables.collation_database) |
| 74 | { } |
| 75 | |
| 76 | Stored_program_creation_ctx(CHARSET_INFO *client_cs, |
| 77 | CHARSET_INFO *connection_cl, |
| 78 | CHARSET_INFO *db_cl) |
| 79 | : Default_object_creation_ctx(client_cs, connection_cl), |
| 80 | m_db_cl(db_cl) |
| 81 | { } |
| 82 | |
| 83 | protected: |
| 84 | virtual void change_env(THD *thd) const |
| 85 | { |
| 86 | thd->variables.collation_database= m_db_cl; |
| 87 | |
| 88 | Default_object_creation_ctx::change_env(thd); |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | /** |
| 93 | db_cl stores the value of the database collation. Both character set |
| 94 | and collation attributes are used. |
| 95 | |
| 96 | Database collation is included into the context because it defines the |
| 97 | default collation for stored-program variables. |
| 98 | */ |
| 99 | CHARSET_INFO *m_db_cl; |
| 100 | }; |
| 101 | |
| 102 | /*************************************************************************/ |
| 103 | |
| 104 | class sp_name : public Sql_alloc, |
| 105 | public Database_qualified_name |
| 106 | { |
| 107 | public: |
| 108 | bool m_explicit_name; /**< Prepend the db name? */ |
| 109 | |
| 110 | sp_name(const LEX_CSTRING *db, const LEX_CSTRING *name, |
| 111 | bool use_explicit_name) |
| 112 | : Database_qualified_name(db, name), m_explicit_name(use_explicit_name) |
| 113 | { |
| 114 | if (lower_case_table_names && m_db.str) |
| 115 | m_db.length= my_casedn_str(files_charset_info, (char*) m_db.str); |
| 116 | } |
| 117 | |
| 118 | /** Create temporary sp_name object from MDL key. Store in qname_buff */ |
| 119 | sp_name(const MDL_key *key, char *qname_buff); |
| 120 | |
| 121 | ~sp_name() |
| 122 | {} |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | bool |
| 127 | check_routine_name(const LEX_CSTRING *ident); |
| 128 | |
| 129 | class sp_head :private Query_arena, |
| 130 | public Database_qualified_name |
| 131 | { |
| 132 | sp_head(const sp_head &); /**< Prevent use of these */ |
| 133 | void operator=(sp_head &); |
| 134 | |
| 135 | protected: |
| 136 | MEM_ROOT main_mem_root; |
| 137 | public: |
| 138 | /** Possible values of m_flags */ |
| 139 | enum { |
| 140 | HAS_RETURN= 1, // For FUNCTIONs only: is set if has RETURN |
| 141 | MULTI_RESULTS= 8, // Is set if a procedure with SELECT(s) |
| 142 | CONTAINS_DYNAMIC_SQL= 16, // Is set if a procedure with PREPARE/EXECUTE |
| 143 | IS_INVOKED= 32, // Is set if this sp_head is being used |
| 144 | HAS_SET_AUTOCOMMIT_STMT= 64,// Is set if a procedure with 'set autocommit' |
| 145 | /* Is set if a procedure with COMMIT (implicit or explicit) | ROLLBACK */ |
| 146 | HAS_COMMIT_OR_ROLLBACK= 128, |
| 147 | LOG_SLOW_STATEMENTS= 256, // Used by events |
| 148 | LOG_GENERAL_LOG= 512, // Used by events |
| 149 | HAS_SQLCOM_RESET= 1024, |
| 150 | HAS_SQLCOM_FLUSH= 2048, |
| 151 | |
| 152 | /** |
| 153 | Marks routines that directly (i.e. not by calling other routines) |
| 154 | change tables. Note that this flag is set automatically based on |
| 155 | type of statements used in the stored routine and is different |
| 156 | from routine characteristic provided by user in a form of CONTAINS |
| 157 | SQL, READS SQL DATA, MODIFIES SQL DATA clauses. The latter are |
| 158 | accepted by parser but pretty much ignored after that. |
| 159 | We don't rely on them: |
| 160 | a) for compatibility reasons. |
| 161 | b) because in CONTAINS SQL case they don't provide enough |
| 162 | information anyway. |
| 163 | */ |
| 164 | MODIFIES_DATA= 4096, |
| 165 | /* |
| 166 | Marks routines that have column type references: DECLARE a t1.a%TYPE; |
| 167 | */ |
| 168 | HAS_COLUMN_TYPE_REFS= 8192, |
| 169 | /* Set if has FETCH GROUP NEXT ROW instr. Used to ensure that only |
| 170 | functions with AGGREGATE keyword use the instr. */ |
| 171 | HAS_AGGREGATE_INSTR= 16384 |
| 172 | }; |
| 173 | |
| 174 | sp_package *m_parent; |
| 175 | const Sp_handler *m_handler; |
| 176 | uint m_flags; // Boolean attributes of a stored routine |
| 177 | |
| 178 | Column_definition m_return_field_def; /**< This is used for FUNCTIONs only. */ |
| 179 | |
| 180 | const char *m_tmp_query; ///< Temporary pointer to sub query string |
| 181 | private: |
| 182 | /* |
| 183 | Private to guarantee that m_chistics.comment is properly set to: |
| 184 | - a string which is alloced on this->mem_root |
| 185 | - or (NULL,0) |
| 186 | set_chistics() makes sure this. |
| 187 | */ |
| 188 | Sp_chistics m_chistics; |
| 189 | public: |
| 190 | sql_mode_t m_sql_mode; ///< For SHOW CREATE and execution |
| 191 | bool m_explicit_name; /**< Prepend the db name? */ |
| 192 | LEX_CSTRING m_qname; ///< db.name |
| 193 | LEX_CSTRING m_params; |
| 194 | LEX_CSTRING m_body; |
| 195 | LEX_CSTRING m_body_utf8; |
| 196 | LEX_CSTRING m_defstr; |
| 197 | AUTHID m_definer; |
| 198 | |
| 199 | const st_sp_chistics &chistics() const { return m_chistics; } |
| 200 | const LEX_CSTRING &() const { return m_chistics.comment; } |
| 201 | void set_suid(enum_sp_suid_behaviour suid) { m_chistics.suid= suid; } |
| 202 | enum_sp_suid_behaviour suid() const { return m_chistics.suid; } |
| 203 | bool detistic() const { return m_chistics.detistic; } |
| 204 | enum_sp_data_access daccess() const { return m_chistics.daccess; } |
| 205 | enum_sp_aggregate_type agg_type() const { return m_chistics.agg_type; } |
| 206 | /** |
| 207 | Is this routine being executed? |
| 208 | */ |
| 209 | virtual bool is_invoked() const { return m_flags & IS_INVOKED; } |
| 210 | |
| 211 | /** |
| 212 | Get the value of the SP cache version, as remembered |
| 213 | when the routine was inserted into the cache. |
| 214 | */ |
| 215 | ulong sp_cache_version() const; |
| 216 | |
| 217 | /** Set the value of the SP cache version. */ |
| 218 | void set_sp_cache_version(ulong version_arg) const |
| 219 | { |
| 220 | m_sp_cache_version= version_arg; |
| 221 | } |
| 222 | |
| 223 | sp_rcontext *rcontext_create(THD *thd, Field *retval, List<Item> *args); |
| 224 | sp_rcontext *rcontext_create(THD *thd, Field *retval, |
| 225 | Item **args, uint arg_count); |
| 226 | sp_rcontext *rcontext_create(THD *thd, Field *retval, |
| 227 | Row_definition_list *list, |
| 228 | bool switch_security_ctx); |
| 229 | bool eq_routine_spec(const sp_head *) const; |
| 230 | private: |
| 231 | /** |
| 232 | Version of the stored routine cache at the moment when the |
| 233 | routine was added to it. Is used only for functions and |
| 234 | procedures, not used for triggers or events. When sp_head is |
| 235 | created, its version is 0. When it's added to the cache, the |
| 236 | version is assigned the global value 'Cversion'. |
| 237 | If later on Cversion is incremented, we know that the routine |
| 238 | is obsolete and should not be used -- |
| 239 | sp_cache_flush_obsolete() will purge it. |
| 240 | */ |
| 241 | mutable ulong m_sp_cache_version; |
| 242 | Stored_program_creation_ctx *m_creation_ctx; |
| 243 | /** |
| 244 | Boolean combination of (1<<flag), where flag is a member of |
| 245 | LEX::enum_binlog_stmt_unsafe. |
| 246 | */ |
| 247 | uint32 unsafe_flags; |
| 248 | |
| 249 | public: |
| 250 | inline Stored_program_creation_ctx *get_creation_ctx() |
| 251 | { |
| 252 | return m_creation_ctx; |
| 253 | } |
| 254 | |
| 255 | inline void set_creation_ctx(Stored_program_creation_ctx *creation_ctx) |
| 256 | { |
| 257 | m_creation_ctx= creation_ctx->clone(mem_root); |
| 258 | } |
| 259 | |
| 260 | longlong m_created; |
| 261 | longlong m_modified; |
| 262 | /** Recursion level of the current SP instance. The levels are numbered from 0 */ |
| 263 | ulong m_recursion_level; |
| 264 | /** |
| 265 | A list of diferent recursion level instances for the same procedure. |
| 266 | For every recursion level we have a sp_head instance. This instances |
| 267 | connected in the list. The list ordered by increasing recursion level |
| 268 | (m_recursion_level). |
| 269 | */ |
| 270 | sp_head *m_next_cached_sp; |
| 271 | /** |
| 272 | Pointer to the first element of the above list |
| 273 | */ |
| 274 | sp_head *m_first_instance; |
| 275 | /** |
| 276 | Pointer to the first free (non-INVOKED) routine in the list of |
| 277 | cached instances for this SP. This pointer is set only for the first |
| 278 | SP in the list of instences (see above m_first_cached_sp pointer). |
| 279 | The pointer equal to 0 if we have no free instances. |
| 280 | For non-first instance value of this pointer meanless (point to itself); |
| 281 | */ |
| 282 | sp_head *m_first_free_instance; |
| 283 | /** |
| 284 | Pointer to the last element in the list of instances of the SP. |
| 285 | For non-first instance value of this pointer meanless (point to itself); |
| 286 | */ |
| 287 | sp_head *m_last_cached_sp; |
| 288 | /** |
| 289 | Set containing names of stored routines used by this routine. |
| 290 | Note that unlike elements of similar set for statement elements of this |
| 291 | set are not linked in one list. Because of this we are able save memory |
| 292 | by using for this set same objects that are used in 'sroutines' sets |
| 293 | for statements of which this stored routine consists. |
| 294 | */ |
| 295 | HASH m_sroutines; |
| 296 | // Pointers set during parsing |
| 297 | const char *m_param_begin; |
| 298 | const char *m_param_end; |
| 299 | |
| 300 | private: |
| 301 | const char *m_body_begin; |
| 302 | |
| 303 | public: |
| 304 | /* |
| 305 | Security context for stored routine which should be run under |
| 306 | definer privileges. |
| 307 | */ |
| 308 | Security_context m_security_ctx; |
| 309 | |
| 310 | /** |
| 311 | List of all items (Item_trigger_field objects) representing fields in |
| 312 | old/new version of row in trigger. We use this list for checking whenever |
| 313 | all such fields are valid at trigger creation time and for binding these |
| 314 | fields to TABLE object at table open (although for latter pointer to table |
| 315 | being opened is probably enough). |
| 316 | */ |
| 317 | SQL_I_List<Item_trigger_field> m_trg_table_fields; |
| 318 | |
| 319 | static void * |
| 320 | operator new(size_t size) throw (); |
| 321 | |
| 322 | static void |
| 323 | operator delete(void *ptr, size_t size) throw (); |
| 324 | |
| 325 | sp_head(sp_package *parent, const Sp_handler *handler); |
| 326 | |
| 327 | /// Initialize after we have reset mem_root |
| 328 | void |
| 329 | init(LEX *lex); |
| 330 | |
| 331 | /** Copy sp name from parser. */ |
| 332 | void |
| 333 | init_sp_name(const sp_name *spname); |
| 334 | |
| 335 | /** Set the body-definition start position. */ |
| 336 | void |
| 337 | set_body_start(THD *thd, const char *begin_ptr); |
| 338 | |
| 339 | /** Set the statement-definition (body-definition) end position. */ |
| 340 | void |
| 341 | set_stmt_end(THD *thd); |
| 342 | |
| 343 | virtual ~sp_head(); |
| 344 | |
| 345 | bool |
| 346 | execute_trigger(THD *thd, |
| 347 | const LEX_CSTRING *db_name, |
| 348 | const LEX_CSTRING *table_name, |
| 349 | GRANT_INFO *grant_info); |
| 350 | |
| 351 | bool |
| 352 | execute_function(THD *thd, Item **args, uint argcount, Field *return_fld, |
| 353 | sp_rcontext **nctx, Query_arena *call_arena); |
| 354 | |
| 355 | bool |
| 356 | execute_procedure(THD *thd, List<Item> *args); |
| 357 | |
| 358 | static void |
| 359 | show_create_routine_get_fields(THD *thd, const Sp_handler *sph, |
| 360 | List<Item> *fields); |
| 361 | |
| 362 | bool |
| 363 | show_create_routine(THD *thd, const Sp_handler *sph); |
| 364 | |
| 365 | MEM_ROOT *get_main_mem_root() { return &main_mem_root; } |
| 366 | |
| 367 | int |
| 368 | add_instr(sp_instr *instr); |
| 369 | |
| 370 | bool |
| 371 | add_instr_jump(THD *thd, sp_pcontext *spcont); |
| 372 | |
| 373 | bool |
| 374 | add_instr_jump(THD *thd, sp_pcontext *spcont, uint dest); |
| 375 | |
| 376 | bool |
| 377 | add_instr_jump_forward_with_backpatch(THD *thd, sp_pcontext *spcont, |
| 378 | sp_label *lab); |
| 379 | bool |
| 380 | add_instr_jump_forward_with_backpatch(THD *thd, sp_pcontext *spcont) |
| 381 | { |
| 382 | return add_instr_jump_forward_with_backpatch(thd, spcont, |
| 383 | spcont->last_label()); |
| 384 | } |
| 385 | |
| 386 | bool |
| 387 | add_instr_freturn(THD *thd, sp_pcontext *spcont, Item *item, LEX *lex); |
| 388 | |
| 389 | bool |
| 390 | add_instr_preturn(THD *thd, sp_pcontext *spcont); |
| 391 | |
| 392 | Item *adjust_assignment_source(THD *thd, Item *val, Item *val2); |
| 393 | /** |
| 394 | @param thd - the current thd |
| 395 | @param spcont - the current parse context |
| 396 | @param spv - the SP variable |
| 397 | @param val - the value to be assigned to the variable |
| 398 | @param lex - the LEX that was used to create "val" |
| 399 | @param responsible_to_free_lex - if the generated sp_instr_set should |
| 400 | free "lex". |
| 401 | @retval true - on error |
| 402 | @retval false - on success |
| 403 | */ |
| 404 | bool set_local_variable(THD *thd, sp_pcontext *spcont, |
| 405 | const Sp_rcontext_handler *rh, |
| 406 | sp_variable *spv, Item *val, LEX *lex, |
| 407 | bool responsible_to_free_lex); |
| 408 | bool set_local_variable_row_field(THD *thd, sp_pcontext *spcont, |
| 409 | const Sp_rcontext_handler *rh, |
| 410 | sp_variable *spv, uint field_idx, |
| 411 | Item *val, LEX *lex); |
| 412 | bool set_local_variable_row_field_by_name(THD *thd, sp_pcontext *spcont, |
| 413 | const Sp_rcontext_handler *rh, |
| 414 | sp_variable *spv, |
| 415 | const LEX_CSTRING *field_name, |
| 416 | Item *val, LEX *lex); |
| 417 | bool check_package_routine_end_name(const LEX_CSTRING &end_name) const; |
| 418 | private: |
| 419 | /** |
| 420 | Generate a code to set a single cursor parameter variable. |
| 421 | @param thd - current thd, for mem_root allocations. |
| 422 | @param param_spcont - the context of the parameter block |
| 423 | @param idx - the index of the parameter |
| 424 | @param prm - the actual parameter (contains information about |
| 425 | the assignment source expression Item, |
| 426 | its free list, and its LEX) |
| 427 | */ |
| 428 | bool add_set_cursor_param_variable(THD *thd, |
| 429 | sp_pcontext *param_spcont, uint idx, |
| 430 | sp_assignment_lex *prm) |
| 431 | { |
| 432 | DBUG_ASSERT(idx < param_spcont->context_var_count()); |
| 433 | sp_variable *spvar= param_spcont->get_context_variable(idx); |
| 434 | /* |
| 435 | add_instr() gets free_list from m_thd->free_list. |
| 436 | Initialize it before the set_local_variable() call. |
| 437 | */ |
| 438 | DBUG_ASSERT(m_thd->free_list == NULL); |
| 439 | m_thd->free_list= prm->get_free_list(); |
| 440 | if (set_local_variable(thd, param_spcont, |
| 441 | &sp_rcontext_handler_local, |
| 442 | spvar, prm->get_item(), prm, true)) |
| 443 | return true; |
| 444 | /* |
| 445 | Safety: |
| 446 | The item and its free_list are now fully owned by the sp_instr_set |
| 447 | instance, created by set_local_variable(). The sp_instr_set instance |
| 448 | is now responsible for freeing the item and the free_list. |
| 449 | Reset the "item" and the "free_list" members of "prm", |
| 450 | to avoid double pointers to the same objects from "prm" and |
| 451 | from the sp_instr_set instance. |
| 452 | */ |
| 453 | prm->set_item_and_free_list(NULL, NULL); |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | Generate a code to set all cursor parameter variables. |
| 459 | This method is called only when parameters exists, |
| 460 | and the number of formal parameters matches the number of actual |
| 461 | parameters. See also comments to add_open_cursor(). |
| 462 | */ |
| 463 | bool add_set_cursor_param_variables(THD *thd, sp_pcontext *param_spcont, |
| 464 | List<sp_assignment_lex> *parameters) |
| 465 | { |
| 466 | DBUG_ASSERT(param_spcont->context_var_count() == parameters->elements); |
| 467 | sp_assignment_lex *prm; |
| 468 | List_iterator<sp_assignment_lex> li(*parameters); |
| 469 | for (uint idx= 0; (prm= li++); idx++) |
| 470 | { |
| 471 | if (add_set_cursor_param_variable(thd, param_spcont, idx, prm)) |
| 472 | return true; |
| 473 | } |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | Generate a code to set all cursor parameter variables for a FOR LOOP, e.g.: |
| 479 | FOR index IN cursor(1,2,3) |
| 480 | @param |
| 481 | */ |
| 482 | bool add_set_for_loop_cursor_param_variables(THD *thd, |
| 483 | sp_pcontext *param_spcont, |
| 484 | sp_assignment_lex *param_lex, |
| 485 | Item_args *parameters); |
| 486 | |
| 487 | public: |
| 488 | /** |
| 489 | Generate a code for an "OPEN cursor" statement. |
| 490 | @param thd - current thd, for mem_root allocations |
| 491 | @param spcont - the context of the cursor |
| 492 | @param offset - the offset of the cursor |
| 493 | @param param_spcont - the context of the cursor parameter block |
| 494 | @param parameters - the list of the OPEN actual parameters |
| 495 | |
| 496 | The caller must make sure that the number of local variables |
| 497 | in "param_spcont" (formal parameters) matches the number of list elements |
| 498 | in "parameters" (actual parameters). |
| 499 | NULL in either of them means 0 parameters. |
| 500 | */ |
| 501 | bool add_open_cursor(THD *thd, sp_pcontext *spcont, |
| 502 | uint offset, |
| 503 | sp_pcontext *param_spcont, |
| 504 | List<sp_assignment_lex> *parameters); |
| 505 | |
| 506 | /** |
| 507 | Generate an initiation code for a CURSOR FOR LOOP, e.g.: |
| 508 | FOR index IN cursor -- cursor without parameters |
| 509 | FOR index IN cursor(1,2,3) -- cursor with parameters |
| 510 | |
| 511 | The code generated by this method does the following during SP run-time: |
| 512 | - Sets all cursor parameter vartiables from "parameters" |
| 513 | - Initializes the index ROW-type variable from the cursor |
| 514 | (the structure is copied from the cursor to the index variable) |
| 515 | - The cursor gets opened |
| 516 | - The first records is fetched from the cursor to the variable "index". |
| 517 | |
| 518 | @param thd - the current thread (for mem_root and error reporting) |
| 519 | @param spcont - the current parse context |
| 520 | @param index - the loop "index" ROW-type variable |
| 521 | @param pcursor - the cursor |
| 522 | @param coffset - the cursor offset |
| 523 | @param param_lex - the LEX that owns Items in "parameters" |
| 524 | @param parameters - the cursor parameters Item array |
| 525 | @retval true - on error (EOM) |
| 526 | @retval false - on success |
| 527 | */ |
| 528 | bool add_for_loop_open_cursor(THD *thd, sp_pcontext *spcont, |
| 529 | sp_variable *index, |
| 530 | const sp_pcursor *pcursor, uint coffset, |
| 531 | sp_assignment_lex *param_lex, |
| 532 | Item_args *parameters); |
| 533 | /** |
| 534 | Returns true if any substatement in the routine directly |
| 535 | (not through another routine) modifies data/changes table. |
| 536 | |
| 537 | @sa Comment for MODIFIES_DATA flag. |
| 538 | */ |
| 539 | bool modifies_data() const |
| 540 | { return m_flags & MODIFIES_DATA; } |
| 541 | |
| 542 | inline uint instructions() |
| 543 | { return m_instr.elements; } |
| 544 | |
| 545 | inline sp_instr * |
| 546 | last_instruction() |
| 547 | { |
| 548 | sp_instr *i; |
| 549 | |
| 550 | get_dynamic(&m_instr, (uchar*)&i, m_instr.elements-1); |
| 551 | return i; |
| 552 | } |
| 553 | |
| 554 | bool replace_instr_to_nop(THD *thd, uint ip); |
| 555 | |
| 556 | /* |
| 557 | Resets lex in 'thd' and keeps a copy of the old one. |
| 558 | |
| 559 | @todo Conflicting comment in sp_head.cc |
| 560 | */ |
| 561 | bool |
| 562 | reset_lex(THD *thd); |
| 563 | |
| 564 | bool |
| 565 | reset_lex(THD *thd, sp_lex_local *sublex); |
| 566 | |
| 567 | /** |
| 568 | Merge two LEX instances. |
| 569 | @param oldlex - the upper level LEX we're going to restore to. |
| 570 | @param sublex - the local lex that have just parsed some substatement. |
| 571 | @returns - false on success, true on error (e.g. failed to |
| 572 | merge the routine list or the table list). |
| 573 | This method is shared by: |
| 574 | - restore_lex(), when the old LEX is popped by sp_head::m_lex.pop() |
| 575 | - THD::restore_from_local_lex_to_old_lex(), when the old LEX |
| 576 | is stored in the caller's local variable. |
| 577 | */ |
| 578 | bool |
| 579 | merge_lex(THD *thd, LEX *oldlex, LEX *sublex); |
| 580 | |
| 581 | /** |
| 582 | Restores lex in 'thd' from our copy, but keeps some status from the |
| 583 | one in 'thd', like ptr, tables, fields, etc. |
| 584 | |
| 585 | @todo Conflicting comment in sp_head.cc |
| 586 | */ |
| 587 | bool |
| 588 | restore_lex(THD *thd) |
| 589 | { |
| 590 | DBUG_ENTER("sp_head::restore_lex" ); |
| 591 | LEX *oldlex= (LEX *) m_lex.pop(); |
| 592 | if (!oldlex) |
| 593 | DBUG_RETURN(false); // Nothing to restore |
| 594 | LEX *sublex= thd->lex; |
| 595 | if (thd->restore_from_local_lex_to_old_lex(oldlex))// This restores thd->lex |
| 596 | DBUG_RETURN(true); |
| 597 | if (!sublex->sp_lex_in_use) |
| 598 | { |
| 599 | sublex->sphead= NULL; |
| 600 | lex_end(sublex); |
| 601 | delete sublex; |
| 602 | } |
| 603 | DBUG_RETURN(false); |
| 604 | } |
| 605 | |
| 606 | /// Put the instruction on the backpatch list, associated with the label. |
| 607 | int |
| 608 | push_backpatch(THD *thd, sp_instr *, sp_label *); |
| 609 | int |
| 610 | push_backpatch_goto(THD *thd, sp_pcontext *ctx, sp_label *lab); |
| 611 | |
| 612 | /// Update all instruction with this label in the backpatch list to |
| 613 | /// the current position. |
| 614 | void |
| 615 | backpatch(sp_label *); |
| 616 | void |
| 617 | backpatch_goto(THD *thd, sp_label *, sp_label *); |
| 618 | |
| 619 | /// Check for unresolved goto label |
| 620 | bool |
| 621 | check_unresolved_goto(); |
| 622 | |
| 623 | /// Start a new cont. backpatch level. If 'i' is NULL, the level is just incr. |
| 624 | int |
| 625 | new_cont_backpatch(sp_instr_opt_meta *i); |
| 626 | |
| 627 | /// Add an instruction to the current level |
| 628 | int |
| 629 | add_cont_backpatch(sp_instr_opt_meta *i); |
| 630 | |
| 631 | /// Backpatch (and pop) the current level to the current position. |
| 632 | void |
| 633 | do_cont_backpatch(); |
| 634 | |
| 635 | /// Add cpush instructions for all cursors declared in the current frame |
| 636 | bool sp_add_instr_cpush_for_cursors(THD *thd, sp_pcontext *pcontext); |
| 637 | |
| 638 | const LEX_CSTRING *name() const |
| 639 | { return &m_name; } |
| 640 | |
| 641 | char *create_string(THD *thd, ulong *lenp); |
| 642 | |
| 643 | Field *create_result_field(uint field_max_length, const LEX_CSTRING *field_name, |
| 644 | TABLE *table) const; |
| 645 | |
| 646 | |
| 647 | /** |
| 648 | Check and prepare an instance of Column_definition for field creation |
| 649 | (fill all necessary attributes), for variables, parameters and |
| 650 | function return values. |
| 651 | |
| 652 | @param[in] thd Thread handle |
| 653 | @param[in] lex Yacc parsing context |
| 654 | @param[out] field_def An instance of create_field to be filled |
| 655 | |
| 656 | @retval false on success |
| 657 | @retval true on error |
| 658 | */ |
| 659 | bool fill_field_definition(THD *thd, Column_definition *field_def) |
| 660 | { |
| 661 | const Type_handler *h= field_def->type_handler(); |
| 662 | return h->Column_definition_fix_attributes(field_def) || |
| 663 | field_def->sp_prepare_create_field(thd, mem_root); |
| 664 | } |
| 665 | bool row_fill_field_definitions(THD *thd, Row_definition_list *row) |
| 666 | { |
| 667 | /* |
| 668 | Prepare all row fields. This will (among other things) |
| 669 | - convert VARCHAR lengths from character length to octet length |
| 670 | - calculate interval lengths for SET and ENUM |
| 671 | */ |
| 672 | List_iterator<Spvar_definition> it(*row); |
| 673 | for (Spvar_definition *def= it++; def; def= it++) |
| 674 | { |
| 675 | if (fill_spvar_definition(thd, def)) |
| 676 | return true; |
| 677 | } |
| 678 | return false; |
| 679 | } |
| 680 | /** |
| 681 | Check and prepare a Column_definition for a variable or a parameter. |
| 682 | */ |
| 683 | bool fill_spvar_definition(THD *thd, Column_definition *def) |
| 684 | { |
| 685 | if (fill_field_definition(thd, def)) |
| 686 | return true; |
| 687 | def->pack_flag|= FIELDFLAG_MAYBE_NULL; |
| 688 | return false; |
| 689 | } |
| 690 | bool fill_spvar_definition(THD *thd, Column_definition *def, |
| 691 | LEX_CSTRING *name) |
| 692 | { |
| 693 | def->field_name= *name; |
| 694 | return fill_spvar_definition(thd, def); |
| 695 | } |
| 696 | |
| 697 | private: |
| 698 | /** |
| 699 | Set a column type reference for a parameter definition |
| 700 | */ |
| 701 | void fill_spvar_using_type_reference(sp_variable *spvar, |
| 702 | Qualified_column_ident *ref) |
| 703 | { |
| 704 | spvar->field_def.set_column_type_ref(ref); |
| 705 | spvar->field_def.field_name= spvar->name; |
| 706 | m_flags|= sp_head::HAS_COLUMN_TYPE_REFS; |
| 707 | } |
| 708 | |
| 709 | void fill_spvar_using_table_rowtype_reference(THD *thd, |
| 710 | sp_variable *spvar, |
| 711 | Table_ident *ref) |
| 712 | { |
| 713 | spvar->field_def.set_table_rowtype_ref(ref); |
| 714 | spvar->field_def.field_name= spvar->name; |
| 715 | fill_spvar_definition(thd, &spvar->field_def); |
| 716 | m_flags|= sp_head::HAS_COLUMN_TYPE_REFS; |
| 717 | } |
| 718 | |
| 719 | public: |
| 720 | bool spvar_fill_row(THD *thd, sp_variable *spvar, Row_definition_list *def); |
| 721 | bool spvar_fill_type_reference(THD *thd, sp_variable *spvar, |
| 722 | const LEX_CSTRING &table, |
| 723 | const LEX_CSTRING &column); |
| 724 | bool spvar_fill_type_reference(THD *thd, sp_variable *spvar, |
| 725 | const LEX_CSTRING &db, |
| 726 | const LEX_CSTRING &table, |
| 727 | const LEX_CSTRING &column); |
| 728 | bool spvar_fill_table_rowtype_reference(THD *thd, sp_variable *spvar, |
| 729 | const LEX_CSTRING &table); |
| 730 | bool spvar_fill_table_rowtype_reference(THD *thd, sp_variable *spvar, |
| 731 | const LEX_CSTRING &db, |
| 732 | const LEX_CSTRING &table); |
| 733 | |
| 734 | void set_chistics(const st_sp_chistics &chistics); |
| 735 | inline void set_chistics_agg_type(enum enum_sp_aggregate_type type) |
| 736 | { |
| 737 | m_chistics.agg_type= type; |
| 738 | } |
| 739 | void set_info(longlong created, longlong modified, |
| 740 | const st_sp_chistics &chistics, sql_mode_t sql_mode); |
| 741 | |
| 742 | void set_definer(const char *definer, size_t definerlen) |
| 743 | { |
| 744 | AUTHID tmp; |
| 745 | tmp.parse(definer, definerlen); |
| 746 | m_definer.copy(mem_root, &tmp.user, &tmp.host); |
| 747 | } |
| 748 | void set_definer(const LEX_CSTRING *user_name, const LEX_CSTRING *host_name) |
| 749 | { |
| 750 | m_definer.copy(mem_root, user_name, host_name); |
| 751 | } |
| 752 | |
| 753 | void reset_thd_mem_root(THD *thd); |
| 754 | |
| 755 | void restore_thd_mem_root(THD *thd); |
| 756 | |
| 757 | /** |
| 758 | Optimize the code. |
| 759 | */ |
| 760 | void optimize(); |
| 761 | |
| 762 | /** |
| 763 | Helper used during flow analysis during code optimization. |
| 764 | See the implementation of <code>opt_mark()</code>. |
| 765 | @param ip the instruction to add to the leads list |
| 766 | @param leads the list of remaining paths to explore in the graph that |
| 767 | represents the code, during flow analysis. |
| 768 | */ |
| 769 | void add_mark_lead(uint ip, List<sp_instr> *leads); |
| 770 | |
| 771 | inline sp_instr * |
| 772 | get_instr(uint i) |
| 773 | { |
| 774 | sp_instr *ip; |
| 775 | |
| 776 | if (i < m_instr.elements) |
| 777 | get_dynamic(&m_instr, (uchar*)&ip, i); |
| 778 | else |
| 779 | ip= NULL; |
| 780 | return ip; |
| 781 | } |
| 782 | |
| 783 | /* Add tables used by routine to the table list. */ |
| 784 | bool add_used_tables_to_table_list(THD *thd, |
| 785 | TABLE_LIST ***query_tables_last_ptr, |
| 786 | TABLE_LIST *belong_to_view); |
| 787 | |
| 788 | /** |
| 789 | Check if this stored routine contains statements disallowed |
| 790 | in a stored function or trigger, and set an appropriate error message |
| 791 | if this is the case. |
| 792 | */ |
| 793 | bool is_not_allowed_in_function(const char *where) |
| 794 | { |
| 795 | if (m_flags & CONTAINS_DYNAMIC_SQL) |
| 796 | my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "Dynamic SQL" ); |
| 797 | else if (m_flags & MULTI_RESULTS) |
| 798 | my_error(ER_SP_NO_RETSET, MYF(0), where); |
| 799 | else if (m_flags & HAS_SET_AUTOCOMMIT_STMT) |
| 800 | my_error(ER_SP_CANT_SET_AUTOCOMMIT, MYF(0)); |
| 801 | else if (m_flags & HAS_COMMIT_OR_ROLLBACK) |
| 802 | my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0)); |
| 803 | else if (m_flags & HAS_SQLCOM_RESET) |
| 804 | my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "RESET" ); |
| 805 | else if (m_flags & HAS_SQLCOM_FLUSH) |
| 806 | my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "FLUSH" ); |
| 807 | |
| 808 | return MY_TEST(m_flags & |
| 809 | (CONTAINS_DYNAMIC_SQL | MULTI_RESULTS | |
| 810 | HAS_SET_AUTOCOMMIT_STMT | HAS_COMMIT_OR_ROLLBACK | |
| 811 | HAS_SQLCOM_RESET | HAS_SQLCOM_FLUSH)); |
| 812 | } |
| 813 | |
| 814 | #ifndef DBUG_OFF |
| 815 | int show_routine_code(THD *thd); |
| 816 | #endif |
| 817 | |
| 818 | /* |
| 819 | This method is intended for attributes of a routine which need |
| 820 | to propagate upwards to the Query_tables_list of the caller (when |
| 821 | a property of a sp_head needs to "taint" the calling statement). |
| 822 | */ |
| 823 | void propagate_attributes(Query_tables_list *prelocking_ctx) |
| 824 | { |
| 825 | DBUG_ENTER("sp_head::propagate_attributes" ); |
| 826 | /* |
| 827 | If this routine needs row-based binary logging, the entire top statement |
| 828 | too (we cannot switch from statement-based to row-based only for this |
| 829 | routine, as in statement-based the top-statement may be binlogged and |
| 830 | the substatements not). |
| 831 | */ |
| 832 | DBUG_PRINT("info" , ("lex->get_stmt_unsafe_flags(): 0x%x" , |
| 833 | prelocking_ctx->get_stmt_unsafe_flags())); |
| 834 | DBUG_PRINT("info" , ("sp_head(%p=%s)->unsafe_flags: 0x%x" , |
| 835 | this, name()->str, unsafe_flags)); |
| 836 | prelocking_ctx->set_stmt_unsafe_flags(unsafe_flags); |
| 837 | DBUG_VOID_RETURN; |
| 838 | } |
| 839 | |
| 840 | sp_pcontext *get_parse_context() { return m_pcont; } |
| 841 | |
| 842 | /* |
| 843 | Check EXECUTE access: |
| 844 | - in case of a standalone rotuine, for the routine itself |
| 845 | - in case of a package routine, for the owner package body |
| 846 | */ |
| 847 | bool check_execute_access(THD *thd) const; |
| 848 | |
| 849 | virtual sp_package *get_package() |
| 850 | { |
| 851 | return NULL; |
| 852 | } |
| 853 | |
| 854 | protected: |
| 855 | |
| 856 | MEM_ROOT *m_thd_root; ///< Temp. store for thd's mem_root |
| 857 | THD *m_thd; ///< Set if we have reset mem_root |
| 858 | |
| 859 | sp_pcontext *m_pcont; ///< Parse context |
| 860 | List<LEX> m_lex; ///< Temp. store for the other lex |
| 861 | DYNAMIC_ARRAY m_instr; ///< The "instructions" |
| 862 | |
| 863 | enum backpatch_instr_type { GOTO, CPOP, HPOP }; |
| 864 | typedef struct |
| 865 | { |
| 866 | sp_label *lab; |
| 867 | sp_instr *instr; |
| 868 | backpatch_instr_type instr_type; |
| 869 | } bp_t; |
| 870 | List<bp_t> m_backpatch; ///< Instructions needing backpatching |
| 871 | List<bp_t> m_backpatch_goto; // Instructions needing backpatching (for goto) |
| 872 | |
| 873 | /** |
| 874 | We need a special list for backpatching of instructions with a continue |
| 875 | destination (in the case of a continue handler catching an error in |
| 876 | the test), since it would otherwise interfere with the normal backpatch |
| 877 | mechanism - e.g. jump_if_not instructions have two different destinations |
| 878 | which are to be patched differently. |
| 879 | Since these occur in a more restricted way (always the same "level" in |
| 880 | the code), we don't need the label. |
| 881 | */ |
| 882 | List<sp_instr_opt_meta> m_cont_backpatch; |
| 883 | uint m_cont_level; // The current cont. backpatch level |
| 884 | |
| 885 | /** |
| 886 | Multi-set representing optimized list of tables to be locked by this |
| 887 | routine. Does not include tables which are used by invoked routines. |
| 888 | |
| 889 | @note |
| 890 | For prelocking-free SPs this multiset is constructed too. |
| 891 | We do so because the same instance of sp_head may be called both |
| 892 | in prelocked mode and in non-prelocked mode. |
| 893 | */ |
| 894 | HASH m_sptabs; |
| 895 | |
| 896 | bool |
| 897 | execute(THD *thd, bool merge_da_on_success); |
| 898 | |
| 899 | /** |
| 900 | Perform a forward flow analysis in the generated code. |
| 901 | Mark reachable instructions, for the optimizer. |
| 902 | */ |
| 903 | void opt_mark(); |
| 904 | |
| 905 | /** |
| 906 | Merge the list of tables used by query into the multi-set of tables used |
| 907 | by routine. |
| 908 | */ |
| 909 | bool merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check); |
| 910 | |
| 911 | /// Put the instruction on the a backpatch list, associated with the label. |
| 912 | int |
| 913 | push_backpatch(THD *thd, sp_instr *, sp_label *, List<bp_t> *list, |
| 914 | backpatch_instr_type itype); |
| 915 | |
| 916 | }; // class sp_head : public Sql_alloc |
| 917 | |
| 918 | |
| 919 | class sp_package: public sp_head |
| 920 | { |
| 921 | bool validate_public_routines(THD *thd, sp_package *spec); |
| 922 | bool validate_private_routines(THD *thd); |
| 923 | public: |
| 924 | class LexList: public List<LEX> |
| 925 | { |
| 926 | public: |
| 927 | LexList() { elements= 0; } |
| 928 | // Find a package routine by a non qualified name |
| 929 | LEX *find(const LEX_CSTRING &name, stored_procedure_type type); |
| 930 | // Find a package routine by a package-qualified name, e.g. 'pkg.proc' |
| 931 | LEX *find_qualified(const LEX_CSTRING &name, stored_procedure_type type); |
| 932 | // Check if a routine with the given qualified name already exists |
| 933 | bool check_dup_qualified(const LEX_CSTRING &name, const Sp_handler *sph) |
| 934 | { |
| 935 | if (!find_qualified(name, sph->type())) |
| 936 | return false; |
| 937 | my_error(ER_SP_ALREADY_EXISTS, MYF(0), sph->type_str(), name.str); |
| 938 | return true; |
| 939 | } |
| 940 | bool check_dup_qualified(const sp_head *sp) |
| 941 | { |
| 942 | return check_dup_qualified(sp->m_name, sp->m_handler); |
| 943 | } |
| 944 | void cleanup(); |
| 945 | }; |
| 946 | /* |
| 947 | The LEX for a new package subroutine is initially assigned to |
| 948 | m_current_routine. After scanning parameters, return type and chistics, |
| 949 | the parser detects if we have a declaration or a definition, e.g.: |
| 950 | PROCEDURE p1(a INT); |
| 951 | vs |
| 952 | PROCEDURE p1(a INT) AS BEGIN NULL; END; |
| 953 | (i.e. either semicolon or the "AS" keyword) |
| 954 | m_current_routine is then added either to m_routine_implementations, |
| 955 | or m_routine_declarations, and then m_current_routine is set to NULL. |
| 956 | */ |
| 957 | LEX *m_current_routine; |
| 958 | LexList m_routine_implementations; |
| 959 | LexList m_routine_declarations; |
| 960 | |
| 961 | LEX *m_top_level_lex; |
| 962 | sp_rcontext *m_rcontext; |
| 963 | uint m_invoked_subroutine_count; |
| 964 | bool m_is_instantiated; |
| 965 | bool m_is_cloning_routine; |
| 966 | |
| 967 | sp_package(LEX *top_level_lex, |
| 968 | const sp_name *name, |
| 969 | const Sp_handler *sph); |
| 970 | ~sp_package(); |
| 971 | bool add_routine_declaration(LEX *lex) |
| 972 | { |
| 973 | return m_routine_declarations.check_dup_qualified(lex->sphead) || |
| 974 | m_routine_declarations.push_back(lex, &main_mem_root); |
| 975 | } |
| 976 | bool add_routine_implementation(LEX *lex) |
| 977 | { |
| 978 | return m_routine_implementations.check_dup_qualified(lex->sphead) || |
| 979 | m_routine_implementations.push_back(lex, &main_mem_root); |
| 980 | } |
| 981 | sp_package *get_package() { return this; } |
| 982 | bool is_invoked() const |
| 983 | { |
| 984 | /* |
| 985 | Cannot flush a package out of the SP cache when: |
| 986 | - its initialization block is running |
| 987 | - one of its subroutine is running |
| 988 | */ |
| 989 | return sp_head::is_invoked() || m_invoked_subroutine_count > 0; |
| 990 | } |
| 991 | sp_variable *find_package_variable(const LEX_CSTRING *name) const |
| 992 | { |
| 993 | /* |
| 994 | sp_head::m_pcont is a special level for routine parameters. |
| 995 | Variables declared inside CREATE PACKAGE BODY reside in m_children.at(0). |
| 996 | */ |
| 997 | sp_pcontext *ctx= m_pcont->child_context(0); |
| 998 | return ctx ? ctx->find_variable(name, true) : NULL; |
| 999 | } |
| 1000 | bool validate_after_parser(THD *thd); |
| 1001 | bool instantiate_if_needed(THD *thd); |
| 1002 | }; |
| 1003 | |
| 1004 | |
| 1005 | class sp_lex_cursor: public sp_lex_local, public Query_arena |
| 1006 | { |
| 1007 | LEX_CSTRING m_cursor_name; |
| 1008 | public: |
| 1009 | sp_lex_cursor(THD *thd, const LEX *oldlex, MEM_ROOT *mem_root_arg) |
| 1010 | :sp_lex_local(thd, oldlex), |
| 1011 | Query_arena(mem_root_arg, STMT_INITIALIZED_FOR_SP), |
| 1012 | m_cursor_name(null_clex_str) |
| 1013 | { } |
| 1014 | sp_lex_cursor(THD *thd, const LEX *oldlex) |
| 1015 | :sp_lex_local(thd, oldlex), |
| 1016 | Query_arena(thd->lex->sphead->get_main_mem_root(), STMT_INITIALIZED_FOR_SP) |
| 1017 | { } |
| 1018 | ~sp_lex_cursor() { free_items(); } |
| 1019 | void cleanup_stmt() { } |
| 1020 | Query_arena *query_arena() { return this; } |
| 1021 | bool validate() |
| 1022 | { |
| 1023 | DBUG_ASSERT(sql_command == SQLCOM_SELECT); |
| 1024 | if (result) |
| 1025 | { |
| 1026 | my_error(ER_SP_BAD_CURSOR_SELECT, MYF(0)); |
| 1027 | return true; |
| 1028 | } |
| 1029 | return false; |
| 1030 | } |
| 1031 | bool stmt_finalize(THD *thd) |
| 1032 | { |
| 1033 | if (validate()) |
| 1034 | return true; |
| 1035 | sp_lex_in_use= true; |
| 1036 | free_list= thd->free_list; |
| 1037 | thd->free_list= NULL; |
| 1038 | return false; |
| 1039 | } |
| 1040 | const LEX_CSTRING *cursor_name() const { return &m_cursor_name; } |
| 1041 | void set_cursor_name(const LEX_CSTRING *name) { m_cursor_name= *name; } |
| 1042 | }; |
| 1043 | |
| 1044 | |
| 1045 | // |
| 1046 | // "Instructions"... |
| 1047 | // |
| 1048 | |
| 1049 | class sp_instr :public Query_arena, public Sql_alloc |
| 1050 | { |
| 1051 | sp_instr(const sp_instr &); /**< Prevent use of these */ |
| 1052 | void operator=(sp_instr &); |
| 1053 | |
| 1054 | public: |
| 1055 | |
| 1056 | uint marked; |
| 1057 | uint m_ip; ///< My index |
| 1058 | sp_pcontext *m_ctx; ///< My parse context |
| 1059 | uint m_lineno; |
| 1060 | |
| 1061 | /// Should give each a name or type code for debugging purposes? |
| 1062 | sp_instr(uint ip, sp_pcontext *ctx) |
| 1063 | :Query_arena(0, STMT_INITIALIZED_FOR_SP), marked(0), m_ip(ip), m_ctx(ctx) |
| 1064 | {} |
| 1065 | |
| 1066 | virtual ~sp_instr() |
| 1067 | { free_items(); } |
| 1068 | |
| 1069 | |
| 1070 | /** |
| 1071 | Execute this instruction |
| 1072 | |
| 1073 | |
| 1074 | @param thd Thread handle |
| 1075 | @param[out] nextp index of the next instruction to execute. (For most |
| 1076 | instructions this will be the instruction following this |
| 1077 | one). Note that this parameter is undefined in case of |
| 1078 | errors, use get_cont_dest() to find the continuation |
| 1079 | instruction for CONTINUE error handlers. |
| 1080 | |
| 1081 | @retval 0 on success, |
| 1082 | @retval other if some error occurred |
| 1083 | */ |
| 1084 | |
| 1085 | virtual int execute(THD *thd, uint *nextp) = 0; |
| 1086 | |
| 1087 | /** |
| 1088 | Execute <code>open_and_lock_tables()</code> for this statement. |
| 1089 | Open and lock the tables used by this statement, as a pre-requisite |
| 1090 | to execute the core logic of this instruction with |
| 1091 | <code>exec_core()</code>. |
| 1092 | @param thd the current thread |
| 1093 | @param tables the list of tables to open and lock |
| 1094 | @return zero on success, non zero on failure. |
| 1095 | */ |
| 1096 | int exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables); |
| 1097 | |
| 1098 | /** |
| 1099 | Get the continuation destination of this instruction. |
| 1100 | @return the continuation destination |
| 1101 | */ |
| 1102 | virtual uint get_cont_dest() const; |
| 1103 | |
| 1104 | /* |
| 1105 | Execute core function of instruction after all preparations (e.g. |
| 1106 | setting of proper LEX, saving part of the thread context have been |
| 1107 | done). |
| 1108 | |
| 1109 | Should be implemented for instructions using expressions or whole |
| 1110 | statements (thus having to have own LEX). Used in concert with |
| 1111 | sp_lex_keeper class and its descendants (there are none currently). |
| 1112 | */ |
| 1113 | virtual int exec_core(THD *thd, uint *nextp); |
| 1114 | |
| 1115 | virtual void print(String *str) = 0; |
| 1116 | |
| 1117 | virtual void backpatch(uint dest, sp_pcontext *dst_ctx) |
| 1118 | {} |
| 1119 | |
| 1120 | /** |
| 1121 | Mark this instruction as reachable during optimization and return the |
| 1122 | index to the next instruction. Jump instruction will add their |
| 1123 | destination to the leads list. |
| 1124 | */ |
| 1125 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 1126 | { |
| 1127 | marked= 1; |
| 1128 | return m_ip+1; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | Short-cut jumps to jumps during optimization. This is used by the |
| 1133 | jump instructions' opt_mark() methods. 'start' is the starting point, |
| 1134 | used to prevent the mark sweep from looping for ever. Return the |
| 1135 | end destination. |
| 1136 | */ |
| 1137 | virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start) |
| 1138 | { |
| 1139 | return m_ip; |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | Inform the instruction that it has been moved during optimization. |
| 1144 | Most instructions will simply update its index, but jump instructions |
| 1145 | must also take care of their destination pointers. Forward jumps get |
| 1146 | pushed to the backpatch list 'ibp'. |
| 1147 | */ |
| 1148 | virtual void opt_move(uint dst, List<sp_instr> *ibp) |
| 1149 | { |
| 1150 | m_ip= dst; |
| 1151 | } |
| 1152 | |
| 1153 | }; // class sp_instr : public Sql_alloc |
| 1154 | |
| 1155 | |
| 1156 | /** |
| 1157 | Auxilary class to which instructions delegate responsibility |
| 1158 | for handling LEX and preparations before executing statement |
| 1159 | or calculating complex expression. |
| 1160 | |
| 1161 | Exist mainly to avoid having double hierarchy between instruction |
| 1162 | classes. |
| 1163 | |
| 1164 | @todo |
| 1165 | Add ability to not store LEX and do any preparations if |
| 1166 | expression used is simple. |
| 1167 | */ |
| 1168 | |
| 1169 | class sp_lex_keeper |
| 1170 | { |
| 1171 | /** Prevent use of these */ |
| 1172 | sp_lex_keeper(const sp_lex_keeper &); |
| 1173 | void operator=(sp_lex_keeper &); |
| 1174 | public: |
| 1175 | |
| 1176 | sp_lex_keeper(LEX *lex, bool lex_resp) |
| 1177 | : m_lex(lex), m_lex_resp(lex_resp), |
| 1178 | lex_query_tables_own_last(NULL) |
| 1179 | { |
| 1180 | lex->sp_lex_in_use= TRUE; |
| 1181 | } |
| 1182 | virtual ~sp_lex_keeper() |
| 1183 | { |
| 1184 | if (m_lex_resp) |
| 1185 | { |
| 1186 | /* Prevent endless recursion. */ |
| 1187 | m_lex->sphead= NULL; |
| 1188 | lex_end(m_lex); |
| 1189 | delete m_lex; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | Prepare execution of instruction using LEX, if requested check whenever |
| 1195 | we have read access to tables used and open/lock them, call instruction's |
| 1196 | exec_core() method, perform cleanup afterwards. |
| 1197 | |
| 1198 | @todo Conflicting comment in sp_head.cc |
| 1199 | */ |
| 1200 | int reset_lex_and_exec_core(THD *thd, uint *nextp, bool open_tables, |
| 1201 | sp_instr* instr); |
| 1202 | |
| 1203 | int cursor_reset_lex_and_exec_core(THD *thd, uint *nextp, bool open_tables, |
| 1204 | sp_instr *instr); |
| 1205 | |
| 1206 | inline uint sql_command() const |
| 1207 | { |
| 1208 | return (uint)m_lex->sql_command; |
| 1209 | } |
| 1210 | |
| 1211 | void disable_query_cache() |
| 1212 | { |
| 1213 | m_lex->safe_to_cache_query= 0; |
| 1214 | } |
| 1215 | |
| 1216 | const LEX_CSTRING *cursor_name() const |
| 1217 | { |
| 1218 | return m_lex->cursor_name(); |
| 1219 | } |
| 1220 | private: |
| 1221 | |
| 1222 | LEX *m_lex; |
| 1223 | /** |
| 1224 | Indicates whenever this sp_lex_keeper instance responsible |
| 1225 | for LEX deletion. |
| 1226 | */ |
| 1227 | bool m_lex_resp; |
| 1228 | |
| 1229 | /* |
| 1230 | Support for being able to execute this statement in two modes: |
| 1231 | a) inside prelocked mode set by the calling procedure or its ancestor. |
| 1232 | b) outside of prelocked mode, when this statement enters/leaves |
| 1233 | prelocked mode itself. |
| 1234 | */ |
| 1235 | |
| 1236 | /** |
| 1237 | List of additional tables this statement needs to lock when it |
| 1238 | enters/leaves prelocked mode on its own. |
| 1239 | */ |
| 1240 | TABLE_LIST *prelocking_tables; |
| 1241 | |
| 1242 | /** |
| 1243 | The value m_lex->query_tables_own_last should be set to this when the |
| 1244 | statement enters/leaves prelocked mode on its own. |
| 1245 | */ |
| 1246 | TABLE_LIST **lex_query_tables_own_last; |
| 1247 | }; |
| 1248 | |
| 1249 | |
| 1250 | /** |
| 1251 | Call out to some prepared SQL statement. |
| 1252 | */ |
| 1253 | class sp_instr_stmt : public sp_instr |
| 1254 | { |
| 1255 | sp_instr_stmt(const sp_instr_stmt &); /**< Prevent use of these */ |
| 1256 | void operator=(sp_instr_stmt &); |
| 1257 | |
| 1258 | public: |
| 1259 | |
| 1260 | LEX_STRING m_query; ///< For thd->query |
| 1261 | |
| 1262 | sp_instr_stmt(uint ip, sp_pcontext *ctx, LEX *lex) |
| 1263 | : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE) |
| 1264 | { |
| 1265 | m_query.str= 0; |
| 1266 | m_query.length= 0; |
| 1267 | } |
| 1268 | |
| 1269 | virtual ~sp_instr_stmt() |
| 1270 | {}; |
| 1271 | |
| 1272 | virtual int execute(THD *thd, uint *nextp); |
| 1273 | |
| 1274 | virtual int exec_core(THD *thd, uint *nextp); |
| 1275 | |
| 1276 | virtual void print(String *str); |
| 1277 | |
| 1278 | private: |
| 1279 | |
| 1280 | sp_lex_keeper m_lex_keeper; |
| 1281 | |
| 1282 | }; // class sp_instr_stmt : public sp_instr |
| 1283 | |
| 1284 | |
| 1285 | class sp_instr_set : public sp_instr |
| 1286 | { |
| 1287 | sp_instr_set(const sp_instr_set &); /**< Prevent use of these */ |
| 1288 | void operator=(sp_instr_set &); |
| 1289 | |
| 1290 | public: |
| 1291 | |
| 1292 | sp_instr_set(uint ip, sp_pcontext *ctx, |
| 1293 | const Sp_rcontext_handler *rh, |
| 1294 | uint offset, Item *val, |
| 1295 | LEX *lex, bool lex_resp) |
| 1296 | : sp_instr(ip, ctx), |
| 1297 | m_rcontext_handler(rh), m_offset(offset), m_value(val), |
| 1298 | m_lex_keeper(lex, lex_resp) |
| 1299 | {} |
| 1300 | |
| 1301 | virtual ~sp_instr_set() |
| 1302 | {} |
| 1303 | |
| 1304 | virtual int execute(THD *thd, uint *nextp); |
| 1305 | |
| 1306 | virtual int exec_core(THD *thd, uint *nextp); |
| 1307 | |
| 1308 | virtual void print(String *str); |
| 1309 | |
| 1310 | protected: |
| 1311 | sp_rcontext *get_rcontext(THD *thd) const; |
| 1312 | const Sp_rcontext_handler *m_rcontext_handler; |
| 1313 | uint m_offset; ///< Frame offset |
| 1314 | Item *m_value; |
| 1315 | sp_lex_keeper m_lex_keeper; |
| 1316 | }; // class sp_instr_set : public sp_instr |
| 1317 | |
| 1318 | |
| 1319 | /* |
| 1320 | This class handles assignments of a ROW fields: |
| 1321 | DECLARE rec ROW (a INT,b INT); |
| 1322 | SET rec.a= 10; |
| 1323 | */ |
| 1324 | class sp_instr_set_row_field : public sp_instr_set |
| 1325 | { |
| 1326 | sp_instr_set_row_field(const sp_instr_set_row_field &); // Prevent use of this |
| 1327 | void operator=(sp_instr_set_row_field &); |
| 1328 | uint m_field_offset; |
| 1329 | |
| 1330 | public: |
| 1331 | |
| 1332 | sp_instr_set_row_field(uint ip, sp_pcontext *ctx, |
| 1333 | const Sp_rcontext_handler *rh, |
| 1334 | uint offset, uint field_offset, |
| 1335 | Item *val, |
| 1336 | LEX *lex, bool lex_resp) |
| 1337 | : sp_instr_set(ip, ctx, rh, offset, val, lex, lex_resp), |
| 1338 | m_field_offset(field_offset) |
| 1339 | {} |
| 1340 | |
| 1341 | virtual ~sp_instr_set_row_field() |
| 1342 | {} |
| 1343 | |
| 1344 | virtual int exec_core(THD *thd, uint *nextp); |
| 1345 | |
| 1346 | virtual void print(String *str); |
| 1347 | }; // class sp_instr_set_field : public sp_instr_set |
| 1348 | |
| 1349 | |
| 1350 | /** |
| 1351 | This class handles assignment instructions like this: |
| 1352 | DECLARE |
| 1353 | CURSOR cur IS SELECT * FROM t1; |
| 1354 | rec cur%ROWTYPE; |
| 1355 | BEGIN |
| 1356 | rec.column1:= 10; -- This instruction |
| 1357 | END; |
| 1358 | |
| 1359 | The idea is that during sp_rcontext::create() we do not know the extact |
| 1360 | structure of "rec". It gets resolved at run time, during the corresponding |
| 1361 | sp_instr_cursor_copy_struct::exec_core(). |
| 1362 | |
| 1363 | So sp_instr_set_row_field_by_name searches for ROW fields by name, |
| 1364 | while sp_instr_set_row_field (see above) searches for ROW fields by index. |
| 1365 | */ |
| 1366 | class sp_instr_set_row_field_by_name : public sp_instr_set |
| 1367 | { |
| 1368 | // Prevent use of this |
| 1369 | sp_instr_set_row_field_by_name(const sp_instr_set_row_field &); |
| 1370 | void operator=(sp_instr_set_row_field_by_name &); |
| 1371 | const LEX_CSTRING m_field_name; |
| 1372 | |
| 1373 | public: |
| 1374 | |
| 1375 | sp_instr_set_row_field_by_name(uint ip, sp_pcontext *ctx, |
| 1376 | const Sp_rcontext_handler *rh, |
| 1377 | uint offset, const LEX_CSTRING &field_name, |
| 1378 | Item *val, |
| 1379 | LEX *lex, bool lex_resp) |
| 1380 | : sp_instr_set(ip, ctx, rh, offset, val, lex, lex_resp), |
| 1381 | m_field_name(field_name) |
| 1382 | {} |
| 1383 | |
| 1384 | virtual ~sp_instr_set_row_field_by_name() |
| 1385 | {} |
| 1386 | |
| 1387 | virtual int exec_core(THD *thd, uint *nextp); |
| 1388 | |
| 1389 | virtual void print(String *str); |
| 1390 | }; // class sp_instr_set_field_by_name : public sp_instr_set |
| 1391 | |
| 1392 | |
| 1393 | /** |
| 1394 | Set NEW/OLD row field value instruction. Used in triggers. |
| 1395 | */ |
| 1396 | class sp_instr_set_trigger_field : public sp_instr |
| 1397 | { |
| 1398 | sp_instr_set_trigger_field(const sp_instr_set_trigger_field &); |
| 1399 | void operator=(sp_instr_set_trigger_field &); |
| 1400 | |
| 1401 | public: |
| 1402 | |
| 1403 | sp_instr_set_trigger_field(uint ip, sp_pcontext *ctx, |
| 1404 | Item_trigger_field *trg_fld, |
| 1405 | Item *val, LEX *lex) |
| 1406 | : sp_instr(ip, ctx), |
| 1407 | trigger_field(trg_fld), |
| 1408 | value(val), m_lex_keeper(lex, TRUE) |
| 1409 | {} |
| 1410 | |
| 1411 | virtual ~sp_instr_set_trigger_field() |
| 1412 | {} |
| 1413 | |
| 1414 | virtual int execute(THD *thd, uint *nextp); |
| 1415 | |
| 1416 | virtual int exec_core(THD *thd, uint *nextp); |
| 1417 | |
| 1418 | virtual void print(String *str); |
| 1419 | |
| 1420 | private: |
| 1421 | Item_trigger_field *trigger_field; |
| 1422 | Item *value; |
| 1423 | sp_lex_keeper m_lex_keeper; |
| 1424 | }; // class sp_instr_trigger_field : public sp_instr |
| 1425 | |
| 1426 | |
| 1427 | /** |
| 1428 | An abstract class for all instructions with destinations that |
| 1429 | needs to be updated by the optimizer. |
| 1430 | |
| 1431 | Even if not all subclasses will use both the normal destination and |
| 1432 | the continuation destination, we put them both here for simplicity. |
| 1433 | */ |
| 1434 | class sp_instr_opt_meta : public sp_instr |
| 1435 | { |
| 1436 | public: |
| 1437 | |
| 1438 | uint m_dest; ///< Where we will go |
| 1439 | uint m_cont_dest; ///< Where continue handlers will go |
| 1440 | |
| 1441 | sp_instr_opt_meta(uint ip, sp_pcontext *ctx) |
| 1442 | : sp_instr(ip, ctx), |
| 1443 | m_dest(0), m_cont_dest(0), m_optdest(0), m_cont_optdest(0) |
| 1444 | {} |
| 1445 | |
| 1446 | sp_instr_opt_meta(uint ip, sp_pcontext *ctx, uint dest) |
| 1447 | : sp_instr(ip, ctx), |
| 1448 | m_dest(dest), m_cont_dest(0), m_optdest(0), m_cont_optdest(0) |
| 1449 | {} |
| 1450 | |
| 1451 | virtual ~sp_instr_opt_meta() |
| 1452 | {} |
| 1453 | |
| 1454 | virtual void set_destination(uint old_dest, uint new_dest) |
| 1455 | = 0; |
| 1456 | |
| 1457 | virtual uint get_cont_dest() const; |
| 1458 | |
| 1459 | protected: |
| 1460 | |
| 1461 | sp_instr *m_optdest; ///< Used during optimization |
| 1462 | sp_instr *m_cont_optdest; ///< Used during optimization |
| 1463 | |
| 1464 | }; // class sp_instr_opt_meta : public sp_instr |
| 1465 | |
| 1466 | class sp_instr_jump : public sp_instr_opt_meta |
| 1467 | { |
| 1468 | sp_instr_jump(const sp_instr_jump &); /**< Prevent use of these */ |
| 1469 | void operator=(sp_instr_jump &); |
| 1470 | |
| 1471 | public: |
| 1472 | |
| 1473 | sp_instr_jump(uint ip, sp_pcontext *ctx) |
| 1474 | : sp_instr_opt_meta(ip, ctx) |
| 1475 | {} |
| 1476 | |
| 1477 | sp_instr_jump(uint ip, sp_pcontext *ctx, uint dest) |
| 1478 | : sp_instr_opt_meta(ip, ctx, dest) |
| 1479 | {} |
| 1480 | |
| 1481 | virtual ~sp_instr_jump() |
| 1482 | {} |
| 1483 | |
| 1484 | virtual int execute(THD *thd, uint *nextp); |
| 1485 | |
| 1486 | virtual void print(String *str); |
| 1487 | |
| 1488 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads); |
| 1489 | |
| 1490 | virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start); |
| 1491 | |
| 1492 | virtual void opt_move(uint dst, List<sp_instr> *ibp); |
| 1493 | |
| 1494 | virtual void backpatch(uint dest, sp_pcontext *dst_ctx) |
| 1495 | { |
| 1496 | /* Calling backpatch twice is a logic flaw in jump resolution. */ |
| 1497 | DBUG_ASSERT(m_dest == 0); |
| 1498 | m_dest= dest; |
| 1499 | } |
| 1500 | |
| 1501 | /** |
| 1502 | Update the destination; used by the optimizer. |
| 1503 | */ |
| 1504 | virtual void set_destination(uint old_dest, uint new_dest) |
| 1505 | { |
| 1506 | if (m_dest == old_dest) |
| 1507 | m_dest= new_dest; |
| 1508 | } |
| 1509 | |
| 1510 | }; // class sp_instr_jump : public sp_instr_opt_meta |
| 1511 | |
| 1512 | |
| 1513 | class sp_instr_jump_if_not : public sp_instr_jump |
| 1514 | { |
| 1515 | sp_instr_jump_if_not(const sp_instr_jump_if_not &); /**< Prevent use of these */ |
| 1516 | void operator=(sp_instr_jump_if_not &); |
| 1517 | |
| 1518 | public: |
| 1519 | |
| 1520 | sp_instr_jump_if_not(uint ip, sp_pcontext *ctx, Item *i, LEX *lex) |
| 1521 | : sp_instr_jump(ip, ctx), m_expr(i), |
| 1522 | m_lex_keeper(lex, TRUE) |
| 1523 | {} |
| 1524 | |
| 1525 | sp_instr_jump_if_not(uint ip, sp_pcontext *ctx, Item *i, uint dest, LEX *lex) |
| 1526 | : sp_instr_jump(ip, ctx, dest), m_expr(i), |
| 1527 | m_lex_keeper(lex, TRUE) |
| 1528 | {} |
| 1529 | |
| 1530 | virtual ~sp_instr_jump_if_not() |
| 1531 | {} |
| 1532 | |
| 1533 | virtual int execute(THD *thd, uint *nextp); |
| 1534 | |
| 1535 | virtual int exec_core(THD *thd, uint *nextp); |
| 1536 | |
| 1537 | virtual void print(String *str); |
| 1538 | |
| 1539 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads); |
| 1540 | |
| 1541 | /** Override sp_instr_jump's shortcut; we stop here */ |
| 1542 | virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start) |
| 1543 | { |
| 1544 | return m_ip; |
| 1545 | } |
| 1546 | |
| 1547 | virtual void opt_move(uint dst, List<sp_instr> *ibp); |
| 1548 | |
| 1549 | virtual void set_destination(uint old_dest, uint new_dest) |
| 1550 | { |
| 1551 | sp_instr_jump::set_destination(old_dest, new_dest); |
| 1552 | if (m_cont_dest == old_dest) |
| 1553 | m_cont_dest= new_dest; |
| 1554 | } |
| 1555 | |
| 1556 | private: |
| 1557 | |
| 1558 | Item *m_expr; ///< The condition |
| 1559 | sp_lex_keeper m_lex_keeper; |
| 1560 | |
| 1561 | }; // class sp_instr_jump_if_not : public sp_instr_jump |
| 1562 | |
| 1563 | |
| 1564 | class sp_instr_preturn : public sp_instr |
| 1565 | { |
| 1566 | sp_instr_preturn(const sp_instr_preturn &); /**< Prevent use of these */ |
| 1567 | void operator=(sp_instr_preturn &); |
| 1568 | |
| 1569 | public: |
| 1570 | |
| 1571 | sp_instr_preturn(uint ip, sp_pcontext *ctx) |
| 1572 | : sp_instr(ip, ctx) |
| 1573 | {} |
| 1574 | |
| 1575 | virtual ~sp_instr_preturn() |
| 1576 | {} |
| 1577 | |
| 1578 | virtual int execute(THD *thd, uint *nextp) |
| 1579 | { |
| 1580 | DBUG_ENTER("sp_instr_preturn::execute" ); |
| 1581 | *nextp= UINT_MAX; |
| 1582 | DBUG_RETURN(0); |
| 1583 | } |
| 1584 | |
| 1585 | virtual void print(String *str) |
| 1586 | { |
| 1587 | str->append(STRING_WITH_LEN("preturn" )); |
| 1588 | } |
| 1589 | |
| 1590 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 1591 | { |
| 1592 | marked= 1; |
| 1593 | return UINT_MAX; |
| 1594 | } |
| 1595 | |
| 1596 | }; // class sp_instr_preturn : public sp_instr |
| 1597 | |
| 1598 | |
| 1599 | class sp_instr_freturn : public sp_instr |
| 1600 | { |
| 1601 | sp_instr_freturn(const sp_instr_freturn &); /**< Prevent use of these */ |
| 1602 | void operator=(sp_instr_freturn &); |
| 1603 | |
| 1604 | public: |
| 1605 | |
| 1606 | sp_instr_freturn(uint ip, sp_pcontext *ctx, |
| 1607 | Item *val, const Type_handler *handler, LEX *lex) |
| 1608 | : sp_instr(ip, ctx), m_value(val), m_type_handler(handler), |
| 1609 | m_lex_keeper(lex, TRUE) |
| 1610 | {} |
| 1611 | |
| 1612 | virtual ~sp_instr_freturn() |
| 1613 | {} |
| 1614 | |
| 1615 | virtual int execute(THD *thd, uint *nextp); |
| 1616 | |
| 1617 | virtual int exec_core(THD *thd, uint *nextp); |
| 1618 | |
| 1619 | virtual void print(String *str); |
| 1620 | |
| 1621 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 1622 | { |
| 1623 | marked= 1; |
| 1624 | return UINT_MAX; |
| 1625 | } |
| 1626 | |
| 1627 | protected: |
| 1628 | |
| 1629 | Item *m_value; |
| 1630 | const Type_handler *m_type_handler; |
| 1631 | sp_lex_keeper m_lex_keeper; |
| 1632 | |
| 1633 | }; // class sp_instr_freturn : public sp_instr |
| 1634 | |
| 1635 | |
| 1636 | class sp_instr_hpush_jump : public sp_instr_jump |
| 1637 | { |
| 1638 | sp_instr_hpush_jump(const sp_instr_hpush_jump &); /**< Prevent use of these */ |
| 1639 | void operator=(sp_instr_hpush_jump &); |
| 1640 | |
| 1641 | public: |
| 1642 | |
| 1643 | sp_instr_hpush_jump(uint ip, |
| 1644 | sp_pcontext *ctx, |
| 1645 | sp_handler *handler) |
| 1646 | :sp_instr_jump(ip, ctx), |
| 1647 | m_handler(handler), |
| 1648 | m_opt_hpop(0), |
| 1649 | m_frame(ctx->current_var_count()) |
| 1650 | { |
| 1651 | DBUG_ASSERT(m_handler->condition_values.elements == 0); |
| 1652 | } |
| 1653 | |
| 1654 | virtual ~sp_instr_hpush_jump() |
| 1655 | { |
| 1656 | m_handler->condition_values.empty(); |
| 1657 | m_handler= NULL; |
| 1658 | } |
| 1659 | |
| 1660 | virtual int execute(THD *thd, uint *nextp); |
| 1661 | |
| 1662 | virtual void print(String *str); |
| 1663 | |
| 1664 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads); |
| 1665 | |
| 1666 | /** Override sp_instr_jump's shortcut; we stop here. */ |
| 1667 | virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start) |
| 1668 | { |
| 1669 | return m_ip; |
| 1670 | } |
| 1671 | |
| 1672 | virtual void backpatch(uint dest, sp_pcontext *dst_ctx) |
| 1673 | { |
| 1674 | DBUG_ASSERT(!m_dest || !m_opt_hpop); |
| 1675 | if (!m_dest) |
| 1676 | m_dest= dest; |
| 1677 | else |
| 1678 | m_opt_hpop= dest; |
| 1679 | } |
| 1680 | |
| 1681 | void add_condition(sp_condition_value *condition_value) |
| 1682 | { m_handler->condition_values.push_back(condition_value); } |
| 1683 | |
| 1684 | sp_handler *get_handler() |
| 1685 | { return m_handler; } |
| 1686 | |
| 1687 | private: |
| 1688 | |
| 1689 | private: |
| 1690 | /// Handler. |
| 1691 | sp_handler *m_handler; |
| 1692 | |
| 1693 | /// hpop marking end of handler scope. |
| 1694 | uint m_opt_hpop; |
| 1695 | |
| 1696 | // This attribute is needed for SHOW PROCEDURE CODE only (i.e. it's needed in |
| 1697 | // debug version only). It's used in print(). |
| 1698 | uint m_frame; |
| 1699 | |
| 1700 | }; // class sp_instr_hpush_jump : public sp_instr_jump |
| 1701 | |
| 1702 | |
| 1703 | class sp_instr_hpop : public sp_instr |
| 1704 | { |
| 1705 | sp_instr_hpop(const sp_instr_hpop &); /**< Prevent use of these */ |
| 1706 | void operator=(sp_instr_hpop &); |
| 1707 | |
| 1708 | public: |
| 1709 | |
| 1710 | sp_instr_hpop(uint ip, sp_pcontext *ctx, uint count) |
| 1711 | : sp_instr(ip, ctx), m_count(count) |
| 1712 | {} |
| 1713 | |
| 1714 | virtual ~sp_instr_hpop() |
| 1715 | {} |
| 1716 | |
| 1717 | void update_count(uint count) |
| 1718 | { |
| 1719 | m_count= count; |
| 1720 | } |
| 1721 | |
| 1722 | virtual int execute(THD *thd, uint *nextp); |
| 1723 | |
| 1724 | virtual void print(String *str); |
| 1725 | |
| 1726 | private: |
| 1727 | |
| 1728 | uint m_count; |
| 1729 | |
| 1730 | }; // class sp_instr_hpop : public sp_instr |
| 1731 | |
| 1732 | |
| 1733 | class sp_instr_hreturn : public sp_instr_jump |
| 1734 | { |
| 1735 | sp_instr_hreturn(const sp_instr_hreturn &); /**< Prevent use of these */ |
| 1736 | void operator=(sp_instr_hreturn &); |
| 1737 | |
| 1738 | public: |
| 1739 | |
| 1740 | sp_instr_hreturn(uint ip, sp_pcontext *ctx) |
| 1741 | :sp_instr_jump(ip, ctx), |
| 1742 | m_frame(ctx->current_var_count()) |
| 1743 | {} |
| 1744 | |
| 1745 | virtual ~sp_instr_hreturn() |
| 1746 | {} |
| 1747 | |
| 1748 | virtual int execute(THD *thd, uint *nextp); |
| 1749 | |
| 1750 | virtual void print(String *str); |
| 1751 | |
| 1752 | /* This instruction will not be short cut optimized. */ |
| 1753 | virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start) |
| 1754 | { |
| 1755 | return m_ip; |
| 1756 | } |
| 1757 | |
| 1758 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads); |
| 1759 | |
| 1760 | private: |
| 1761 | |
| 1762 | uint m_frame; |
| 1763 | |
| 1764 | }; // class sp_instr_hreturn : public sp_instr_jump |
| 1765 | |
| 1766 | |
| 1767 | /** This is DECLARE CURSOR */ |
| 1768 | class sp_instr_cpush : public sp_instr |
| 1769 | { |
| 1770 | sp_instr_cpush(const sp_instr_cpush &); /**< Prevent use of these */ |
| 1771 | void operator=(sp_instr_cpush &); |
| 1772 | |
| 1773 | public: |
| 1774 | |
| 1775 | sp_instr_cpush(uint ip, sp_pcontext *ctx, LEX *lex, uint offset) |
| 1776 | : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE), m_cursor(offset) |
| 1777 | {} |
| 1778 | |
| 1779 | virtual ~sp_instr_cpush() |
| 1780 | {} |
| 1781 | |
| 1782 | virtual int execute(THD *thd, uint *nextp); |
| 1783 | |
| 1784 | virtual void print(String *str); |
| 1785 | |
| 1786 | /** |
| 1787 | This call is used to cleanup the instruction when a sensitive |
| 1788 | cursor is closed. For now stored procedures always use materialized |
| 1789 | cursors and the call is not used. |
| 1790 | */ |
| 1791 | virtual void cleanup_stmt() { /* no op */ } |
| 1792 | private: |
| 1793 | |
| 1794 | sp_lex_keeper m_lex_keeper; |
| 1795 | uint m_cursor; /**< Frame offset (for debugging) */ |
| 1796 | |
| 1797 | }; // class sp_instr_cpush : public sp_instr |
| 1798 | |
| 1799 | |
| 1800 | class sp_instr_cpop : public sp_instr |
| 1801 | { |
| 1802 | sp_instr_cpop(const sp_instr_cpop &); /**< Prevent use of these */ |
| 1803 | void operator=(sp_instr_cpop &); |
| 1804 | |
| 1805 | public: |
| 1806 | |
| 1807 | sp_instr_cpop(uint ip, sp_pcontext *ctx, uint count) |
| 1808 | : sp_instr(ip, ctx), m_count(count) |
| 1809 | {} |
| 1810 | |
| 1811 | virtual ~sp_instr_cpop() |
| 1812 | {} |
| 1813 | |
| 1814 | void update_count(uint count) |
| 1815 | { |
| 1816 | m_count= count; |
| 1817 | } |
| 1818 | |
| 1819 | virtual int execute(THD *thd, uint *nextp); |
| 1820 | |
| 1821 | virtual void print(String *str); |
| 1822 | |
| 1823 | private: |
| 1824 | |
| 1825 | uint m_count; |
| 1826 | |
| 1827 | }; // class sp_instr_cpop : public sp_instr |
| 1828 | |
| 1829 | |
| 1830 | class sp_instr_copen : public sp_instr |
| 1831 | { |
| 1832 | sp_instr_copen(const sp_instr_copen &); /**< Prevent use of these */ |
| 1833 | void operator=(sp_instr_copen &); |
| 1834 | |
| 1835 | public: |
| 1836 | |
| 1837 | sp_instr_copen(uint ip, sp_pcontext *ctx, uint c) |
| 1838 | : sp_instr(ip, ctx), m_cursor(c) |
| 1839 | {} |
| 1840 | |
| 1841 | virtual ~sp_instr_copen() |
| 1842 | {} |
| 1843 | |
| 1844 | virtual int execute(THD *thd, uint *nextp); |
| 1845 | |
| 1846 | virtual int exec_core(THD *thd, uint *nextp); |
| 1847 | |
| 1848 | virtual void print(String *str); |
| 1849 | |
| 1850 | private: |
| 1851 | |
| 1852 | uint m_cursor; ///< Stack index |
| 1853 | |
| 1854 | }; // class sp_instr_copen : public sp_instr_stmt |
| 1855 | |
| 1856 | |
| 1857 | /** |
| 1858 | Initialize the structure of a cursor%ROWTYPE variable |
| 1859 | from the LEX containing the cursor SELECT statement. |
| 1860 | */ |
| 1861 | class sp_instr_cursor_copy_struct: public sp_instr |
| 1862 | { |
| 1863 | /**< Prevent use of these */ |
| 1864 | sp_instr_cursor_copy_struct(const sp_instr_cursor_copy_struct &); |
| 1865 | void operator=(sp_instr_cursor_copy_struct &); |
| 1866 | sp_lex_keeper m_lex_keeper; |
| 1867 | uint m_var; |
| 1868 | public: |
| 1869 | sp_instr_cursor_copy_struct(uint ip, sp_pcontext *ctx, |
| 1870 | sp_lex_cursor *lex, uint voffs) |
| 1871 | : sp_instr(ip, ctx), m_lex_keeper(lex, FALSE), m_var(voffs) |
| 1872 | {} |
| 1873 | virtual ~sp_instr_cursor_copy_struct() |
| 1874 | {} |
| 1875 | virtual int execute(THD *thd, uint *nextp); |
| 1876 | virtual int exec_core(THD *thd, uint *nextp); |
| 1877 | virtual void print(String *str); |
| 1878 | }; |
| 1879 | |
| 1880 | |
| 1881 | class sp_instr_cclose : public sp_instr |
| 1882 | { |
| 1883 | sp_instr_cclose(const sp_instr_cclose &); /**< Prevent use of these */ |
| 1884 | void operator=(sp_instr_cclose &); |
| 1885 | |
| 1886 | public: |
| 1887 | |
| 1888 | sp_instr_cclose(uint ip, sp_pcontext *ctx, uint c) |
| 1889 | : sp_instr(ip, ctx), m_cursor(c) |
| 1890 | {} |
| 1891 | |
| 1892 | virtual ~sp_instr_cclose() |
| 1893 | {} |
| 1894 | |
| 1895 | virtual int execute(THD *thd, uint *nextp); |
| 1896 | |
| 1897 | virtual void print(String *str); |
| 1898 | |
| 1899 | private: |
| 1900 | |
| 1901 | uint m_cursor; |
| 1902 | |
| 1903 | }; // class sp_instr_cclose : public sp_instr |
| 1904 | |
| 1905 | |
| 1906 | class sp_instr_cfetch : public sp_instr |
| 1907 | { |
| 1908 | sp_instr_cfetch(const sp_instr_cfetch &); /**< Prevent use of these */ |
| 1909 | void operator=(sp_instr_cfetch &); |
| 1910 | |
| 1911 | public: |
| 1912 | |
| 1913 | sp_instr_cfetch(uint ip, sp_pcontext *ctx, uint c, bool error_on_no_data) |
| 1914 | : sp_instr(ip, ctx), m_cursor(c), m_error_on_no_data(error_on_no_data) |
| 1915 | { |
| 1916 | m_varlist.empty(); |
| 1917 | } |
| 1918 | |
| 1919 | virtual ~sp_instr_cfetch() |
| 1920 | {} |
| 1921 | |
| 1922 | virtual int execute(THD *thd, uint *nextp); |
| 1923 | |
| 1924 | virtual void print(String *str); |
| 1925 | |
| 1926 | void add_to_varlist(sp_variable *var) |
| 1927 | { |
| 1928 | m_varlist.push_back(var); |
| 1929 | } |
| 1930 | |
| 1931 | private: |
| 1932 | |
| 1933 | uint m_cursor; |
| 1934 | List<sp_variable> m_varlist; |
| 1935 | bool m_error_on_no_data; |
| 1936 | |
| 1937 | }; // class sp_instr_cfetch : public sp_instr |
| 1938 | |
| 1939 | /* |
| 1940 | This class is created for the special fetch instruction |
| 1941 | FETCH GROUP NEXT ROW, used in the user-defined aggregate |
| 1942 | functions |
| 1943 | */ |
| 1944 | |
| 1945 | class sp_instr_agg_cfetch : public sp_instr |
| 1946 | { |
| 1947 | sp_instr_agg_cfetch(const sp_instr_cfetch &); /**< Prevent use of these */ |
| 1948 | void operator=(sp_instr_cfetch &); |
| 1949 | |
| 1950 | public: |
| 1951 | |
| 1952 | sp_instr_agg_cfetch(uint ip, sp_pcontext *ctx) |
| 1953 | : sp_instr(ip, ctx){} |
| 1954 | |
| 1955 | virtual ~sp_instr_agg_cfetch() |
| 1956 | {} |
| 1957 | |
| 1958 | virtual int execute(THD *thd, uint *nextp); |
| 1959 | |
| 1960 | virtual void print(String *str); |
| 1961 | }; // class sp_instr_agg_cfetch : public sp_instr |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | |
| 1966 | class sp_instr_error : public sp_instr |
| 1967 | { |
| 1968 | sp_instr_error(const sp_instr_error &); /**< Prevent use of these */ |
| 1969 | void operator=(sp_instr_error &); |
| 1970 | |
| 1971 | public: |
| 1972 | |
| 1973 | sp_instr_error(uint ip, sp_pcontext *ctx, int errcode) |
| 1974 | : sp_instr(ip, ctx), m_errcode(errcode) |
| 1975 | {} |
| 1976 | |
| 1977 | virtual ~sp_instr_error() |
| 1978 | {} |
| 1979 | |
| 1980 | virtual int execute(THD *thd, uint *nextp); |
| 1981 | |
| 1982 | virtual void print(String *str); |
| 1983 | |
| 1984 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 1985 | { |
| 1986 | marked= 1; |
| 1987 | return UINT_MAX; |
| 1988 | } |
| 1989 | |
| 1990 | private: |
| 1991 | |
| 1992 | int m_errcode; |
| 1993 | |
| 1994 | }; // class sp_instr_error : public sp_instr |
| 1995 | |
| 1996 | |
| 1997 | class sp_instr_set_case_expr : public sp_instr_opt_meta |
| 1998 | { |
| 1999 | public: |
| 2000 | |
| 2001 | sp_instr_set_case_expr(uint ip, sp_pcontext *ctx, uint case_expr_id, |
| 2002 | Item *case_expr, LEX *lex) |
| 2003 | : sp_instr_opt_meta(ip, ctx), |
| 2004 | m_case_expr_id(case_expr_id), m_case_expr(case_expr), |
| 2005 | m_lex_keeper(lex, TRUE) |
| 2006 | {} |
| 2007 | |
| 2008 | virtual ~sp_instr_set_case_expr() |
| 2009 | {} |
| 2010 | |
| 2011 | virtual int execute(THD *thd, uint *nextp); |
| 2012 | |
| 2013 | virtual int exec_core(THD *thd, uint *nextp); |
| 2014 | |
| 2015 | virtual void print(String *str); |
| 2016 | |
| 2017 | virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads); |
| 2018 | |
| 2019 | virtual void opt_move(uint dst, List<sp_instr> *ibp); |
| 2020 | |
| 2021 | virtual void set_destination(uint old_dest, uint new_dest) |
| 2022 | { |
| 2023 | if (m_cont_dest == old_dest) |
| 2024 | m_cont_dest= new_dest; |
| 2025 | } |
| 2026 | |
| 2027 | private: |
| 2028 | |
| 2029 | uint m_case_expr_id; |
| 2030 | Item *m_case_expr; |
| 2031 | sp_lex_keeper m_lex_keeper; |
| 2032 | |
| 2033 | }; // class sp_instr_set_case_expr : public sp_instr_opt_meta |
| 2034 | |
| 2035 | |
| 2036 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 2037 | bool |
| 2038 | sp_change_security_context(THD *thd, sp_head *sp, |
| 2039 | Security_context **backup); |
| 2040 | void |
| 2041 | sp_restore_security_context(THD *thd, Security_context *backup); |
| 2042 | |
| 2043 | bool |
| 2044 | set_routine_security_ctx(THD *thd, sp_head *sp, Security_context **save_ctx); |
| 2045 | #endif /* NO_EMBEDDED_ACCESS_CHECKS */ |
| 2046 | |
| 2047 | TABLE_LIST * |
| 2048 | sp_add_to_query_tables(THD *thd, LEX *lex, |
| 2049 | const LEX_CSTRING *db, const LEX_CSTRING *name, |
| 2050 | thr_lock_type locktype, |
| 2051 | enum_mdl_type mdl_type); |
| 2052 | |
| 2053 | /** |
| 2054 | @} (end of group Stored_Routines) |
| 2055 | */ |
| 2056 | |
| 2057 | #endif /* _SP_HEAD_H_ */ |
| 2058 | |