| 1 | /* |
| 2 | Copyright (c) 2002, 2016, Oracle and/or its affiliates. |
| 3 | Copyright (c) 2011, 2017, MariaDB |
| 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 | #include "mariadb.h" /* NO_EMBEDDED_ACCESS_CHECKS */ |
| 19 | #include "sql_priv.h" |
| 20 | #include "unireg.h" |
| 21 | #include "sql_prepare.h" |
| 22 | #include "sql_cache.h" // query_cache_* |
| 23 | #include "probes_mysql.h" |
| 24 | #include "sql_show.h" // append_identifier |
| 25 | #include "sql_db.h" // mysql_opt_change_db, mysql_change_db |
| 26 | #include "sql_acl.h" // *_ACL |
| 27 | #include "sql_array.h" // Dynamic_array |
| 28 | #include "log_event.h" // Query_log_event |
| 29 | #include "sql_derived.h" // mysql_handle_derived |
| 30 | #include "sql_select.h" // Virtual_tmp_table |
| 31 | |
| 32 | #ifdef USE_PRAGMA_IMPLEMENTATION |
| 33 | #pragma implementation |
| 34 | #endif |
| 35 | #include "sp_head.h" |
| 36 | #include "sp.h" |
| 37 | #include "sp_pcontext.h" |
| 38 | #include "sp_rcontext.h" |
| 39 | #include "sp_cache.h" |
| 40 | #include "set_var.h" |
| 41 | #include "sql_parse.h" // cleanup_items |
| 42 | #include "sql_base.h" // close_thread_tables |
| 43 | #include "transaction.h" // trans_commit_stmt |
| 44 | #include "sql_audit.h" |
| 45 | #include "debug_sync.h" |
| 46 | |
| 47 | /* |
| 48 | Sufficient max length of printed destinations and frame offsets (all uints). |
| 49 | */ |
| 50 | #define SP_INSTR_UINT_MAXLEN 8 |
| 51 | #define SP_STMT_PRINT_MAXLEN 40 |
| 52 | |
| 53 | |
| 54 | #include <my_user.h> |
| 55 | |
| 56 | extern "C" uchar *sp_table_key(const uchar *ptr, size_t *plen, my_bool first); |
| 57 | |
| 58 | /** |
| 59 | Helper function which operates on a THD object to set the query start_time to |
| 60 | the current time. |
| 61 | |
| 62 | @param[in, out] thd The session object |
| 63 | |
| 64 | */ |
| 65 | |
| 66 | static void reset_start_time_for_sp(THD *thd) |
| 67 | { |
| 68 | if (!thd->in_sub_stmt) |
| 69 | thd->set_start_time(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | Item::Type |
| 74 | sp_map_item_type(const Type_handler *handler) |
| 75 | { |
| 76 | if (handler == &type_handler_row) |
| 77 | return Item::ROW_ITEM; |
| 78 | enum_field_types type= real_type_to_type(handler->real_field_type()); |
| 79 | |
| 80 | switch (type) { |
| 81 | case MYSQL_TYPE_BIT: |
| 82 | case MYSQL_TYPE_TINY: |
| 83 | case MYSQL_TYPE_SHORT: |
| 84 | case MYSQL_TYPE_LONG: |
| 85 | case MYSQL_TYPE_LONGLONG: |
| 86 | case MYSQL_TYPE_INT24: |
| 87 | return Item::INT_ITEM; |
| 88 | case MYSQL_TYPE_DECIMAL: |
| 89 | case MYSQL_TYPE_NEWDECIMAL: |
| 90 | return Item::DECIMAL_ITEM; |
| 91 | case MYSQL_TYPE_FLOAT: |
| 92 | case MYSQL_TYPE_DOUBLE: |
| 93 | return Item::REAL_ITEM; |
| 94 | default: |
| 95 | return Item::STRING_ITEM; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | bool Item_splocal::append_for_log(THD *thd, String *str) |
| 101 | { |
| 102 | if (fix_fields(thd, NULL)) |
| 103 | return true; |
| 104 | |
| 105 | if (limit_clause_param) |
| 106 | return str->append_ulonglong(val_uint()); |
| 107 | |
| 108 | /* |
| 109 | ROW variables are currently not allowed in select_list, e.g.: |
| 110 | SELECT row_variable; |
| 111 | ROW variables can appear in query parts where name is not important, e.g.: |
| 112 | SELECT ROW(1,2)=row_variable FROM t1; |
| 113 | So we can skip using NAME_CONST() and use ROW() constants directly. |
| 114 | */ |
| 115 | if (type_handler() == &type_handler_row) |
| 116 | return append_value_for_log(thd, str); |
| 117 | |
| 118 | if (str->append(STRING_WITH_LEN(" NAME_CONST('" )) || |
| 119 | str->append(&m_name) || |
| 120 | str->append(STRING_WITH_LEN("'," ))) |
| 121 | return true; |
| 122 | return append_value_for_log(thd, str) || str->append(')'); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | bool Item_splocal::append_value_for_log(THD *thd, String *str) |
| 127 | { |
| 128 | StringBuffer<STRING_BUFFER_USUAL_SIZE> str_value_holder(&my_charset_latin1); |
| 129 | Item *item= this_item(); |
| 130 | String *str_value= item->type_handler()->print_item_value(thd, item, |
| 131 | &str_value_holder); |
| 132 | return str_value ? |
| 133 | str->append(*str_value) : |
| 134 | str->append(STRING_WITH_LEN("NULL" )); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | bool Item_splocal_row_field::append_for_log(THD *thd, String *str) |
| 139 | { |
| 140 | if (fix_fields(thd, NULL)) |
| 141 | return true; |
| 142 | |
| 143 | if (limit_clause_param) |
| 144 | return str->append_ulonglong(val_uint()); |
| 145 | |
| 146 | if (str->append(STRING_WITH_LEN(" NAME_CONST('" )) || |
| 147 | str->append(&m_name) || |
| 148 | str->append("." ) || |
| 149 | str->append(&m_field_name) || |
| 150 | str->append(STRING_WITH_LEN("'," ))) |
| 151 | return true; |
| 152 | return append_value_for_log(thd, str) || str->append(')'); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /** |
| 157 | Returns a combination of: |
| 158 | - sp_head::MULTI_RESULTS: added if the 'cmd' is a command that might |
| 159 | result in multiple result sets being sent back. |
| 160 | - sp_head::CONTAINS_DYNAMIC_SQL: added if 'cmd' is one of PREPARE, |
| 161 | EXECUTE, DEALLOCATE. |
| 162 | */ |
| 163 | |
| 164 | uint |
| 165 | sp_get_flags_for_command(LEX *lex) |
| 166 | { |
| 167 | uint flags; |
| 168 | |
| 169 | switch (lex->sql_command) { |
| 170 | case SQLCOM_SELECT: |
| 171 | if (lex->result) |
| 172 | { |
| 173 | flags= 0; /* This is a SELECT with INTO clause */ |
| 174 | break; |
| 175 | } |
| 176 | /* fallthrough */ |
| 177 | case SQLCOM_ANALYZE: |
| 178 | case SQLCOM_OPTIMIZE: |
| 179 | case SQLCOM_PRELOAD_KEYS: |
| 180 | case SQLCOM_ASSIGN_TO_KEYCACHE: |
| 181 | case SQLCOM_CHECKSUM: |
| 182 | case SQLCOM_CHECK: |
| 183 | case SQLCOM_HA_READ: |
| 184 | case SQLCOM_SHOW_AUTHORS: |
| 185 | case SQLCOM_SHOW_BINLOGS: |
| 186 | case SQLCOM_SHOW_BINLOG_EVENTS: |
| 187 | case SQLCOM_SHOW_RELAYLOG_EVENTS: |
| 188 | case SQLCOM_SHOW_CHARSETS: |
| 189 | case SQLCOM_SHOW_COLLATIONS: |
| 190 | case SQLCOM_SHOW_CONTRIBUTORS: |
| 191 | case SQLCOM_SHOW_CREATE: |
| 192 | case SQLCOM_SHOW_CREATE_DB: |
| 193 | case SQLCOM_SHOW_CREATE_FUNC: |
| 194 | case SQLCOM_SHOW_CREATE_PROC: |
| 195 | case SQLCOM_SHOW_CREATE_PACKAGE: |
| 196 | case SQLCOM_SHOW_CREATE_PACKAGE_BODY: |
| 197 | case SQLCOM_SHOW_CREATE_EVENT: |
| 198 | case SQLCOM_SHOW_CREATE_TRIGGER: |
| 199 | case SQLCOM_SHOW_CREATE_USER: |
| 200 | case SQLCOM_SHOW_DATABASES: |
| 201 | case SQLCOM_SHOW_ERRORS: |
| 202 | case SQLCOM_SHOW_EXPLAIN: |
| 203 | case SQLCOM_SHOW_FIELDS: |
| 204 | case SQLCOM_SHOW_FUNC_CODE: |
| 205 | case SQLCOM_SHOW_GRANTS: |
| 206 | case SQLCOM_SHOW_ENGINE_STATUS: |
| 207 | case SQLCOM_SHOW_ENGINE_LOGS: |
| 208 | case SQLCOM_SHOW_ENGINE_MUTEX: |
| 209 | case SQLCOM_SHOW_EVENTS: |
| 210 | case SQLCOM_SHOW_KEYS: |
| 211 | case SQLCOM_SHOW_MASTER_STAT: |
| 212 | case SQLCOM_SHOW_OPEN_TABLES: |
| 213 | case SQLCOM_SHOW_PRIVILEGES: |
| 214 | case SQLCOM_SHOW_PROCESSLIST: |
| 215 | case SQLCOM_SHOW_PROC_CODE: |
| 216 | case SQLCOM_SHOW_PACKAGE_BODY_CODE: |
| 217 | case SQLCOM_SHOW_SLAVE_HOSTS: |
| 218 | case SQLCOM_SHOW_SLAVE_STAT: |
| 219 | case SQLCOM_SHOW_STATUS: |
| 220 | case SQLCOM_SHOW_STATUS_FUNC: |
| 221 | case SQLCOM_SHOW_STATUS_PROC: |
| 222 | case SQLCOM_SHOW_STATUS_PACKAGE: |
| 223 | case SQLCOM_SHOW_STATUS_PACKAGE_BODY: |
| 224 | case SQLCOM_SHOW_STORAGE_ENGINES: |
| 225 | case SQLCOM_SHOW_TABLES: |
| 226 | case SQLCOM_SHOW_TABLE_STATUS: |
| 227 | case SQLCOM_SHOW_VARIABLES: |
| 228 | case SQLCOM_SHOW_WARNS: |
| 229 | case SQLCOM_REPAIR: |
| 230 | flags= sp_head::MULTI_RESULTS; |
| 231 | break; |
| 232 | /* |
| 233 | EXECUTE statement may return a result set, but doesn't have to. |
| 234 | We can't, however, know it in advance, and therefore must add |
| 235 | this statement here. This is ok, as is equivalent to a result-set |
| 236 | statement within an IF condition. |
| 237 | */ |
| 238 | case SQLCOM_EXECUTE: |
| 239 | case SQLCOM_EXECUTE_IMMEDIATE: |
| 240 | flags= sp_head::MULTI_RESULTS | sp_head::CONTAINS_DYNAMIC_SQL; |
| 241 | break; |
| 242 | case SQLCOM_PREPARE: |
| 243 | case SQLCOM_DEALLOCATE_PREPARE: |
| 244 | flags= sp_head::CONTAINS_DYNAMIC_SQL; |
| 245 | break; |
| 246 | case SQLCOM_CREATE_TABLE: |
| 247 | case SQLCOM_CREATE_SEQUENCE: |
| 248 | if (lex->tmp_table()) |
| 249 | flags= 0; |
| 250 | else |
| 251 | flags= sp_head::HAS_COMMIT_OR_ROLLBACK; |
| 252 | break; |
| 253 | case SQLCOM_DROP_TABLE: |
| 254 | case SQLCOM_DROP_SEQUENCE: |
| 255 | if (lex->tmp_table()) |
| 256 | flags= 0; |
| 257 | else |
| 258 | flags= sp_head::HAS_COMMIT_OR_ROLLBACK; |
| 259 | break; |
| 260 | case SQLCOM_FLUSH: |
| 261 | flags= sp_head::HAS_SQLCOM_FLUSH; |
| 262 | break; |
| 263 | case SQLCOM_RESET: |
| 264 | flags= sp_head::HAS_SQLCOM_RESET; |
| 265 | break; |
| 266 | case SQLCOM_CREATE_INDEX: |
| 267 | case SQLCOM_CREATE_DB: |
| 268 | case SQLCOM_CREATE_PACKAGE: |
| 269 | case SQLCOM_CREATE_PACKAGE_BODY: |
| 270 | case SQLCOM_CREATE_VIEW: |
| 271 | case SQLCOM_CREATE_TRIGGER: |
| 272 | case SQLCOM_CREATE_USER: |
| 273 | case SQLCOM_CREATE_ROLE: |
| 274 | case SQLCOM_ALTER_TABLE: |
| 275 | case SQLCOM_ALTER_SEQUENCE: |
| 276 | case SQLCOM_ALTER_USER: |
| 277 | case SQLCOM_GRANT: |
| 278 | case SQLCOM_GRANT_ROLE: |
| 279 | case SQLCOM_REVOKE: |
| 280 | case SQLCOM_REVOKE_ROLE: |
| 281 | case SQLCOM_BEGIN: |
| 282 | case SQLCOM_RENAME_TABLE: |
| 283 | case SQLCOM_RENAME_USER: |
| 284 | case SQLCOM_DROP_INDEX: |
| 285 | case SQLCOM_DROP_DB: |
| 286 | case SQLCOM_DROP_PACKAGE: |
| 287 | case SQLCOM_DROP_PACKAGE_BODY: |
| 288 | case SQLCOM_REVOKE_ALL: |
| 289 | case SQLCOM_DROP_USER: |
| 290 | case SQLCOM_DROP_ROLE: |
| 291 | case SQLCOM_DROP_VIEW: |
| 292 | case SQLCOM_DROP_TRIGGER: |
| 293 | case SQLCOM_TRUNCATE: |
| 294 | case SQLCOM_COMMIT: |
| 295 | case SQLCOM_ROLLBACK: |
| 296 | case SQLCOM_LOAD: |
| 297 | case SQLCOM_LOCK_TABLES: |
| 298 | case SQLCOM_CREATE_PROCEDURE: |
| 299 | case SQLCOM_CREATE_SPFUNCTION: |
| 300 | case SQLCOM_ALTER_PROCEDURE: |
| 301 | case SQLCOM_ALTER_FUNCTION: |
| 302 | case SQLCOM_DROP_PROCEDURE: |
| 303 | case SQLCOM_DROP_FUNCTION: |
| 304 | case SQLCOM_CREATE_EVENT: |
| 305 | case SQLCOM_ALTER_EVENT: |
| 306 | case SQLCOM_DROP_EVENT: |
| 307 | case SQLCOM_INSTALL_PLUGIN: |
| 308 | case SQLCOM_UNINSTALL_PLUGIN: |
| 309 | flags= sp_head::HAS_COMMIT_OR_ROLLBACK; |
| 310 | break; |
| 311 | case SQLCOM_DELETE: |
| 312 | case SQLCOM_DELETE_MULTI: |
| 313 | { |
| 314 | /* |
| 315 | DELETE normally doesn't return resultset, but there are 3 exceptions: |
| 316 | - DELETE ... RETURNING |
| 317 | - EXPLAIN DELETE ... |
| 318 | - ANALYZE DELETE ... |
| 319 | */ |
| 320 | if (lex->select_lex.item_list.is_empty() && |
| 321 | !lex->describe && !lex->analyze_stmt) |
| 322 | flags= 0; |
| 323 | else |
| 324 | flags= sp_head::MULTI_RESULTS; |
| 325 | break; |
| 326 | } |
| 327 | case SQLCOM_UPDATE: |
| 328 | case SQLCOM_UPDATE_MULTI: |
| 329 | case SQLCOM_INSERT: |
| 330 | case SQLCOM_REPLACE: |
| 331 | case SQLCOM_REPLACE_SELECT: |
| 332 | case SQLCOM_INSERT_SELECT: |
| 333 | { |
| 334 | if (!lex->describe && !lex->analyze_stmt) |
| 335 | flags= 0; |
| 336 | else |
| 337 | flags= sp_head::MULTI_RESULTS; |
| 338 | break; |
| 339 | } |
| 340 | default: |
| 341 | flags= 0; |
| 342 | break; |
| 343 | } |
| 344 | return flags; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | Prepare an Item for evaluation (call of fix_fields). |
| 349 | |
| 350 | @param it_addr pointer on item refernce |
| 351 | @param cols expected number of elements (1 for scalar, >=1 for ROWs) |
| 352 | |
| 353 | @retval |
| 354 | NULL error |
| 355 | @retval |
| 356 | non-NULL prepared item |
| 357 | */ |
| 358 | |
| 359 | Item *THD::sp_prepare_func_item(Item **it_addr, uint cols) |
| 360 | { |
| 361 | DBUG_ENTER("THD::sp_prepare_func_item" ); |
| 362 | Item *res= sp_fix_func_item(it_addr); |
| 363 | if (res && res->check_cols(cols)) |
| 364 | DBUG_RETURN(NULL); |
| 365 | DBUG_RETURN(res); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | /** |
| 370 | Fix an Item for evaluation for SP. |
| 371 | */ |
| 372 | |
| 373 | Item *THD::sp_fix_func_item(Item **it_addr) |
| 374 | { |
| 375 | DBUG_ENTER("THD::sp_fix_func_item" ); |
| 376 | if (!(*it_addr)->fixed && |
| 377 | (*it_addr)->fix_fields(this, it_addr)) |
| 378 | { |
| 379 | DBUG_PRINT("info" , ("fix_fields() failed" )); |
| 380 | DBUG_RETURN(NULL); |
| 381 | } |
| 382 | it_addr= (*it_addr)->this_item_addr(this, it_addr); |
| 383 | |
| 384 | if (!(*it_addr)->fixed && |
| 385 | (*it_addr)->fix_fields(this, it_addr)) |
| 386 | { |
| 387 | DBUG_PRINT("info" , ("fix_fields() failed" )); |
| 388 | DBUG_RETURN(NULL); |
| 389 | } |
| 390 | DBUG_RETURN(*it_addr); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /** |
| 395 | Evaluate an expression and store the result in the field. |
| 396 | |
| 397 | @param result_field the field to store the result |
| 398 | @param expr_item_ptr the root item of the expression |
| 399 | |
| 400 | @retval |
| 401 | FALSE on success |
| 402 | @retval |
| 403 | TRUE on error |
| 404 | */ |
| 405 | |
| 406 | bool THD::sp_eval_expr(Field *result_field, Item **expr_item_ptr) |
| 407 | { |
| 408 | DBUG_ENTER("THD::sp_eval_expr" ); |
| 409 | DBUG_ASSERT(*expr_item_ptr); |
| 410 | Sp_eval_expr_state state(this); |
| 411 | /* Save the value in the field. Convert the value if needed. */ |
| 412 | DBUG_RETURN(result_field->sp_prepare_and_store_item(this, expr_item_ptr)); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /** |
| 417 | Create temporary sp_name object from MDL key. |
| 418 | |
| 419 | @note The lifetime of this object is bound to the lifetime of the MDL_key. |
| 420 | This should be fine as sp_name objects created by this constructor |
| 421 | are mainly used for SP-cache lookups. |
| 422 | |
| 423 | @param key MDL key containing database and routine name. |
| 424 | @param qname_buff Buffer to be used for storing quoted routine name |
| 425 | (should be at least 2*NAME_LEN+1+1 bytes). |
| 426 | */ |
| 427 | |
| 428 | sp_name::sp_name(const MDL_key *key, char *qname_buff) |
| 429 | :Database_qualified_name(key->db_name(), key->db_name_length(), |
| 430 | key->name(), key->name_length()), |
| 431 | m_explicit_name(false) |
| 432 | { |
| 433 | if (m_db.length) |
| 434 | strxmov(qname_buff, m_db.str, "." , m_name.str, NullS); |
| 435 | else |
| 436 | strmov(qname_buff, m_name.str); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
| 441 | Check that the name 'ident' is ok. It's assumed to be an 'ident' |
| 442 | from the parser, so we only have to check length and trailing spaces. |
| 443 | The former is a standard requirement (and 'show status' assumes a |
| 444 | non-empty name), the latter is a mysql:ism as trailing spaces are |
| 445 | removed by get_field(). |
| 446 | |
| 447 | @retval |
| 448 | TRUE bad name |
| 449 | @retval |
| 450 | FALSE name is ok |
| 451 | */ |
| 452 | |
| 453 | bool |
| 454 | check_routine_name(const LEX_CSTRING *ident) |
| 455 | { |
| 456 | DBUG_ASSERT(ident); |
| 457 | DBUG_ASSERT(ident->str); |
| 458 | |
| 459 | if (!ident->str[0] || ident->str[ident->length-1] == ' ') |
| 460 | { |
| 461 | my_error(ER_SP_WRONG_NAME, MYF(0), ident->str); |
| 462 | return TRUE; |
| 463 | } |
| 464 | if (check_ident_length(ident)) |
| 465 | return TRUE; |
| 466 | |
| 467 | return FALSE; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /* |
| 472 | * |
| 473 | * sp_head |
| 474 | * |
| 475 | */ |
| 476 | |
| 477 | void * |
| 478 | sp_head::operator new(size_t size) throw() |
| 479 | { |
| 480 | DBUG_ENTER("sp_head::operator new" ); |
| 481 | MEM_ROOT own_root; |
| 482 | sp_head *sp; |
| 483 | |
| 484 | init_sql_alloc(&own_root, "sp_head" , |
| 485 | MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC, MYF(0)); |
| 486 | sp= (sp_head *) alloc_root(&own_root, size); |
| 487 | if (sp == NULL) |
| 488 | DBUG_RETURN(NULL); |
| 489 | sp->main_mem_root= own_root; |
| 490 | DBUG_PRINT("info" , ("mem_root %p" , &sp->mem_root)); |
| 491 | DBUG_RETURN(sp); |
| 492 | } |
| 493 | |
| 494 | void |
| 495 | sp_head::operator delete(void *ptr, size_t size) throw() |
| 496 | { |
| 497 | DBUG_ENTER("sp_head::operator delete" ); |
| 498 | MEM_ROOT own_root; |
| 499 | |
| 500 | if (ptr == NULL) |
| 501 | DBUG_VOID_RETURN; |
| 502 | |
| 503 | sp_head *sp= (sp_head *) ptr; |
| 504 | |
| 505 | /* Make a copy of main_mem_root as free_root will free the sp */ |
| 506 | own_root= sp->main_mem_root; |
| 507 | DBUG_PRINT("info" , ("mem_root %p moved to %p" , |
| 508 | &sp->mem_root, &own_root)); |
| 509 | free_root(&own_root, MYF(0)); |
| 510 | |
| 511 | DBUG_VOID_RETURN; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | sp_head::sp_head(sp_package *parent, const Sp_handler *sph) |
| 516 | :Query_arena(&main_mem_root, STMT_INITIALIZED_FOR_SP), |
| 517 | Database_qualified_name(&null_clex_str, &null_clex_str), |
| 518 | m_parent(parent), |
| 519 | m_handler(sph), |
| 520 | m_flags(0), |
| 521 | m_tmp_query(NULL), |
| 522 | m_explicit_name(false), |
| 523 | /* |
| 524 | FIXME: the only use case when name is NULL is events, and it should |
| 525 | be rewritten soon. Remove the else part and replace 'if' with |
| 526 | an assert when this is done. |
| 527 | */ |
| 528 | m_qname(null_clex_str), |
| 529 | m_params(null_clex_str), |
| 530 | m_body(null_clex_str), |
| 531 | m_body_utf8(null_clex_str), |
| 532 | m_defstr(null_clex_str), |
| 533 | m_sp_cache_version(0), |
| 534 | m_creation_ctx(0), |
| 535 | unsafe_flags(0), |
| 536 | m_created(0), |
| 537 | m_modified(0), |
| 538 | m_recursion_level(0), |
| 539 | m_next_cached_sp(0), |
| 540 | m_param_begin(NULL), |
| 541 | m_param_end(NULL), |
| 542 | m_body_begin(NULL), |
| 543 | m_thd_root(NULL), |
| 544 | m_thd(NULL), |
| 545 | m_pcont(new (&main_mem_root) sp_pcontext()), |
| 546 | m_cont_level(0) |
| 547 | { |
| 548 | m_first_instance= this; |
| 549 | m_first_free_instance= this; |
| 550 | m_last_cached_sp= this; |
| 551 | |
| 552 | m_return_field_def.charset = NULL; |
| 553 | |
| 554 | DBUG_ENTER("sp_head::sp_head" ); |
| 555 | |
| 556 | m_backpatch.empty(); |
| 557 | m_backpatch_goto.empty(); |
| 558 | m_cont_backpatch.empty(); |
| 559 | m_lex.empty(); |
| 560 | my_init_dynamic_array(&m_instr, sizeof(sp_instr *), 16, 8, MYF(0)); |
| 561 | my_hash_init(&m_sptabs, system_charset_info, 0, 0, 0, sp_table_key, 0, 0); |
| 562 | my_hash_init(&m_sroutines, system_charset_info, 0, 0, 0, sp_sroutine_key, |
| 563 | 0, 0); |
| 564 | |
| 565 | DBUG_VOID_RETURN; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | sp_package::sp_package(LEX *top_level_lex, |
| 570 | const sp_name *name, |
| 571 | const Sp_handler *sph) |
| 572 | :sp_head(NULL, sph), |
| 573 | m_current_routine(NULL), |
| 574 | m_top_level_lex(top_level_lex), |
| 575 | m_rcontext(NULL), |
| 576 | m_invoked_subroutine_count(0), |
| 577 | m_is_instantiated(false), |
| 578 | m_is_cloning_routine(false) |
| 579 | { |
| 580 | init_sp_name(name); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | sp_package::~sp_package() |
| 585 | { |
| 586 | m_routine_implementations.cleanup(); |
| 587 | m_routine_declarations.cleanup(); |
| 588 | m_body= null_clex_str; |
| 589 | if (m_current_routine) |
| 590 | delete m_current_routine->sphead; |
| 591 | delete m_rcontext; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /* |
| 596 | Test if two routines have equal specifications |
| 597 | */ |
| 598 | |
| 599 | bool sp_head::eq_routine_spec(const sp_head *sp) const |
| 600 | { |
| 601 | // TODO: Add tests for equal return data types (in case of FUNCTION) |
| 602 | // TODO: Add tests for equal argument data types |
| 603 | return |
| 604 | m_handler->type() == sp->m_handler->type() && |
| 605 | m_pcont->context_var_count() == sp->m_pcont->context_var_count(); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | bool sp_package::validate_after_parser(THD *thd) |
| 610 | { |
| 611 | if (m_handler->type() != TYPE_ENUM_PACKAGE_BODY) |
| 612 | return false; |
| 613 | sp_head *sp= sp_cache_lookup(&thd->sp_package_spec_cache, this); |
| 614 | sp_package *spec= sp ? sp->get_package() : NULL; |
| 615 | DBUG_ASSERT(spec); // CREATE PACKAGE must already be cached |
| 616 | return validate_public_routines(thd, spec) || |
| 617 | validate_private_routines(thd); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | bool sp_package::validate_public_routines(THD *thd, sp_package *spec) |
| 622 | { |
| 623 | /* |
| 624 | Check that all routines declared in CREATE PACKAGE |
| 625 | have implementations in CREATE PACKAGE BODY. |
| 626 | */ |
| 627 | List_iterator<LEX> it(spec->m_routine_declarations); |
| 628 | for (LEX *lex; (lex= it++); ) |
| 629 | { |
| 630 | bool found= false; |
| 631 | DBUG_ASSERT(lex->sphead); |
| 632 | List_iterator<LEX> it2(m_routine_implementations); |
| 633 | for (LEX *lex2; (lex2= it2++); ) |
| 634 | { |
| 635 | DBUG_ASSERT(lex2->sphead); |
| 636 | if (Sp_handler::eq_routine_name(lex2->sphead->m_name, |
| 637 | lex->sphead->m_name) && |
| 638 | lex2->sphead->eq_routine_spec(lex->sphead)) |
| 639 | { |
| 640 | found= true; |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | if (!found) |
| 645 | { |
| 646 | my_error(ER_PACKAGE_ROUTINE_IN_SPEC_NOT_DEFINED_IN_BODY, MYF(0), |
| 647 | ErrConvDQName(lex->sphead).ptr()); |
| 648 | return true; |
| 649 | } |
| 650 | } |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | |
| 655 | bool sp_package::validate_private_routines(THD *thd) |
| 656 | { |
| 657 | /* |
| 658 | Check that all forwad declarations in |
| 659 | CREATE PACKAGE BODY have implementations. |
| 660 | */ |
| 661 | List_iterator<LEX> it(m_routine_declarations); |
| 662 | for (LEX *lex; (lex= it++); ) |
| 663 | { |
| 664 | bool found= false; |
| 665 | DBUG_ASSERT(lex->sphead); |
| 666 | List_iterator<LEX> it2(m_routine_implementations); |
| 667 | for (LEX *lex2; (lex2= it2++); ) |
| 668 | { |
| 669 | DBUG_ASSERT(lex2->sphead); |
| 670 | if (Sp_handler::eq_routine_name(lex2->sphead->m_name, |
| 671 | lex->sphead->m_name) && |
| 672 | lex2->sphead->eq_routine_spec(lex->sphead)) |
| 673 | { |
| 674 | found= true; |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | if (!found) |
| 679 | { |
| 680 | my_error(ER_PACKAGE_ROUTINE_FORWARD_DECLARATION_NOT_DEFINED, MYF(0), |
| 681 | ErrConvDQName(lex->sphead).ptr()); |
| 682 | return true; |
| 683 | } |
| 684 | } |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | |
| 689 | LEX *sp_package::LexList::find(const LEX_CSTRING &name, |
| 690 | stored_procedure_type type) |
| 691 | { |
| 692 | List_iterator<LEX> it(*this); |
| 693 | for (LEX *lex; (lex= it++); ) |
| 694 | { |
| 695 | DBUG_ASSERT(lex->sphead); |
| 696 | const char *dot; |
| 697 | if (lex->sphead->m_handler->type() == type && |
| 698 | (dot= strrchr(lex->sphead->m_name.str, '.'))) |
| 699 | { |
| 700 | size_t ofs= dot + 1 - lex->sphead->m_name.str; |
| 701 | LEX_CSTRING non_qualified_sphead_name= lex->sphead->m_name; |
| 702 | non_qualified_sphead_name.str+= ofs; |
| 703 | non_qualified_sphead_name.length-= ofs; |
| 704 | if (Sp_handler::eq_routine_name(non_qualified_sphead_name, name)) |
| 705 | return lex; |
| 706 | } |
| 707 | } |
| 708 | return NULL; |
| 709 | } |
| 710 | |
| 711 | |
| 712 | LEX *sp_package::LexList::find_qualified(const LEX_CSTRING &name, |
| 713 | stored_procedure_type type) |
| 714 | { |
| 715 | List_iterator<LEX> it(*this); |
| 716 | for (LEX *lex; (lex= it++); ) |
| 717 | { |
| 718 | DBUG_ASSERT(lex->sphead); |
| 719 | if (lex->sphead->m_handler->type() == type && |
| 720 | Sp_handler::eq_routine_name(lex->sphead->m_name, name)) |
| 721 | return lex; |
| 722 | } |
| 723 | return NULL; |
| 724 | } |
| 725 | |
| 726 | |
| 727 | void |
| 728 | sp_head::init(LEX *lex) |
| 729 | { |
| 730 | DBUG_ENTER("sp_head::init" ); |
| 731 | |
| 732 | lex->spcont= m_pcont; |
| 733 | |
| 734 | if (!lex->spcont) |
| 735 | DBUG_VOID_RETURN; |
| 736 | |
| 737 | /* |
| 738 | Altough trg_table_fields list is used only in triggers we init for all |
| 739 | types of stored procedures to simplify reset_lex()/restore_lex() code. |
| 740 | */ |
| 741 | lex->trg_table_fields.empty(); |
| 742 | |
| 743 | DBUG_VOID_RETURN; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | void |
| 748 | sp_head::init_sp_name(const sp_name *spname) |
| 749 | { |
| 750 | DBUG_ENTER("sp_head::init_sp_name" ); |
| 751 | |
| 752 | /* Must be initialized in the parser. */ |
| 753 | |
| 754 | DBUG_ASSERT(spname && spname->m_db.str && spname->m_db.length); |
| 755 | |
| 756 | /* We have to copy strings to get them into the right memroot. */ |
| 757 | Database_qualified_name::copy(&main_mem_root, spname->m_db, spname->m_name); |
| 758 | m_explicit_name= spname->m_explicit_name; |
| 759 | DBUG_VOID_RETURN; |
| 760 | } |
| 761 | |
| 762 | |
| 763 | void |
| 764 | sp_head::set_body_start(THD *thd, const char *begin_ptr) |
| 765 | { |
| 766 | m_body_begin= begin_ptr; |
| 767 | thd->m_parser_state->m_lip.body_utf8_start(thd, begin_ptr); |
| 768 | } |
| 769 | |
| 770 | |
| 771 | void |
| 772 | sp_head::set_stmt_end(THD *thd) |
| 773 | { |
| 774 | Lex_input_stream *lip= & thd->m_parser_state->m_lip; /* shortcut */ |
| 775 | const char *end_ptr= lip->get_cpp_ptr(); /* shortcut */ |
| 776 | |
| 777 | /* Make the string of parameters. */ |
| 778 | |
| 779 | if (m_param_begin && m_param_end) |
| 780 | { |
| 781 | m_params.length= m_param_end - m_param_begin; |
| 782 | m_params.str= thd->strmake(m_param_begin, m_params.length); |
| 783 | } |
| 784 | |
| 785 | /* Remember end pointer for further dumping of whole statement. */ |
| 786 | |
| 787 | thd->lex->stmt_definition_end= end_ptr; |
| 788 | |
| 789 | /* Make the string of body (in the original character set). */ |
| 790 | |
| 791 | m_body.length= end_ptr - m_body_begin; |
| 792 | m_body.str= thd->strmake(m_body_begin, m_body.length); |
| 793 | trim_whitespace(thd->charset(), &m_body); |
| 794 | |
| 795 | /* Make the string of UTF-body. */ |
| 796 | |
| 797 | lip->body_utf8_append(end_ptr); |
| 798 | |
| 799 | m_body_utf8.length= lip->get_body_utf8_length(); |
| 800 | m_body_utf8.str= thd->strmake(lip->get_body_utf8_str(), m_body_utf8.length); |
| 801 | trim_whitespace(thd->charset(), &m_body_utf8); |
| 802 | |
| 803 | /* |
| 804 | Make the string of whole stored-program-definition query (in the |
| 805 | original character set). |
| 806 | */ |
| 807 | |
| 808 | m_defstr.length= end_ptr - lip->get_cpp_buf(); |
| 809 | m_defstr.str= thd->strmake(lip->get_cpp_buf(), m_defstr.length); |
| 810 | trim_whitespace(thd->charset(), &m_defstr); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | sp_head::~sp_head() |
| 815 | { |
| 816 | LEX *lex; |
| 817 | sp_instr *i; |
| 818 | DBUG_ENTER("sp_head::~sp_head" ); |
| 819 | |
| 820 | /* sp_head::restore_thd_mem_root() must already have been called. */ |
| 821 | DBUG_ASSERT(m_thd == NULL); |
| 822 | |
| 823 | for (uint ip = 0 ; (i = get_instr(ip)) ; ip++) |
| 824 | delete i; |
| 825 | delete_dynamic(&m_instr); |
| 826 | delete m_pcont; |
| 827 | free_items(); |
| 828 | |
| 829 | /* |
| 830 | If we have non-empty LEX stack then we just came out of parser with |
| 831 | error. Now we should delete all auxilary LEXes and restore original |
| 832 | THD::lex. It is safe to not update LEX::ptr because further query |
| 833 | string parsing and execution will be stopped anyway. |
| 834 | */ |
| 835 | while ((lex= (LEX *)m_lex.pop())) |
| 836 | { |
| 837 | THD *thd= lex->thd; |
| 838 | thd->lex->sphead= NULL; |
| 839 | lex_end(thd->lex); |
| 840 | delete thd->lex; |
| 841 | thd->lex= lex; |
| 842 | } |
| 843 | |
| 844 | my_hash_free(&m_sptabs); |
| 845 | my_hash_free(&m_sroutines); |
| 846 | |
| 847 | delete m_next_cached_sp; |
| 848 | |
| 849 | DBUG_VOID_RETURN; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | void sp_package::LexList::cleanup() |
| 854 | { |
| 855 | List_iterator<LEX> it(*this); |
| 856 | for (LEX *lex; (lex= it++); ) |
| 857 | { |
| 858 | lex_end(lex); |
| 859 | delete lex; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /** |
| 865 | This is only used for result fields from functions (both during |
| 866 | fix_length_and_dec() and evaluation). |
| 867 | */ |
| 868 | |
| 869 | Field * |
| 870 | sp_head::create_result_field(uint field_max_length, const LEX_CSTRING *field_name, |
| 871 | TABLE *table) const |
| 872 | { |
| 873 | Field *field; |
| 874 | LEX_CSTRING name; |
| 875 | |
| 876 | DBUG_ENTER("sp_head::create_result_field" ); |
| 877 | |
| 878 | /* |
| 879 | m_return_field_def.length is always set to the field length calculated |
| 880 | by the parser, according to the RETURNS clause. See prepare_create_field() |
| 881 | in sql_table.cc. Value examples, depending on data type: |
| 882 | - 11 for INT (character representation length) |
| 883 | - 20 for BIGINT (character representation length) |
| 884 | - 22 for DOUBLE (character representation length) |
| 885 | - N for CHAR(N) CHARACTER SET latin1 (octet length) |
| 886 | - 3*N for CHAR(N) CHARACTER SET utf8 (octet length) |
| 887 | - 8 for blob-alike data types (packed length !!!) |
| 888 | |
| 889 | field_max_length is also set according to the data type in the RETURNS |
| 890 | clause but can have different values depending on the execution stage: |
| 891 | |
| 892 | 1. During direct execution: |
| 893 | field_max_length is 0, because Item_func_sp::fix_length_and_dec() has |
| 894 | not been called yet, so Item_func_sp::max_length is 0 by default. |
| 895 | |
| 896 | 2a. During PREPARE: |
| 897 | field_max_length is 0, because Item_func_sp::fix_length_and_dec() |
| 898 | has not been called yet. It's called after create_result_field(). |
| 899 | |
| 900 | 2b. During EXEC: |
| 901 | field_max_length is set to the maximum possible octet length of the |
| 902 | RETURNS data type. |
| 903 | - N for CHAR(N) CHARACTER SET latin1 (octet length) |
| 904 | - 3*N for CHAR(N) CHARACTER SET utf8 (octet length) |
| 905 | - 255 for TINYBLOB (octet length, not packed length !!!) |
| 906 | |
| 907 | Perhaps we should refactor prepare_create_field() to set |
| 908 | Create_field::length to maximum octet length for BLOBs, |
| 909 | instead of packed length). |
| 910 | |
| 911 | Note, for integer data types, field_max_length can be bigger |
| 912 | than the user specified length, e.g. a field of the INT(1) data type |
| 913 | is translated to the item with max_length=11. |
| 914 | */ |
| 915 | DBUG_ASSERT(field_max_length <= m_return_field_def.length || |
| 916 | m_return_field_def.type_handler()->cmp_type() == INT_RESULT || |
| 917 | (current_thd->stmt_arena->is_stmt_execute() && |
| 918 | m_return_field_def.length == 8 && |
| 919 | (m_return_field_def.pack_flag & |
| 920 | (FIELDFLAG_BLOB|FIELDFLAG_GEOM)))); |
| 921 | |
| 922 | if (field_name) |
| 923 | name= *field_name; |
| 924 | else |
| 925 | name= m_name; |
| 926 | field= m_return_field_def.make_field(table->s, /* TABLE_SHARE ptr */ |
| 927 | table->in_use->mem_root, |
| 928 | &name); |
| 929 | |
| 930 | field->vcol_info= m_return_field_def.vcol_info; |
| 931 | if (field) |
| 932 | field->init(table); |
| 933 | |
| 934 | DBUG_RETURN(field); |
| 935 | } |
| 936 | |
| 937 | |
| 938 | int cmp_rqp_locations(Rewritable_query_parameter * const *a, |
| 939 | Rewritable_query_parameter * const *b) |
| 940 | { |
| 941 | return (int)((*a)->pos_in_query - (*b)->pos_in_query); |
| 942 | } |
| 943 | |
| 944 | |
| 945 | /* |
| 946 | StoredRoutinesBinlogging |
| 947 | This paragraph applies only to statement-based binlogging. Row-based |
| 948 | binlogging does not need anything special like this. |
| 949 | |
| 950 | Top-down overview: |
| 951 | |
| 952 | 1. Statements |
| 953 | |
| 954 | Statements that have is_update_query(stmt) == TRUE are written into the |
| 955 | binary log verbatim. |
| 956 | Examples: |
| 957 | UPDATE tbl SET tbl.x = spfunc_w_side_effects() |
| 958 | UPDATE tbl SET tbl.x=1 WHERE spfunc_w_side_effect_that_returns_false(tbl.y) |
| 959 | |
| 960 | Statements that have is_update_query(stmt) == FALSE (e.g. SELECTs) are not |
| 961 | written into binary log. Instead we catch function calls the statement |
| 962 | makes and write it into binary log separately (see #3). |
| 963 | |
| 964 | 2. PROCEDURE calls |
| 965 | |
| 966 | CALL statements are not written into binary log. Instead |
| 967 | * Any FUNCTION invocation (in SET, IF, WHILE, OPEN CURSOR and other SP |
| 968 | instructions) is written into binlog separately. |
| 969 | |
| 970 | * Each statement executed in SP is binlogged separately, according to rules |
| 971 | in #1, with the exception that we modify query string: we replace uses |
| 972 | of SP local variables with NAME_CONST('spvar_name', <spvar-value>) calls. |
| 973 | This substitution is done in subst_spvars(). |
| 974 | |
| 975 | 3. FUNCTION calls |
| 976 | |
| 977 | In sp_head::execute_function(), we check |
| 978 | * If this function invocation is done from a statement that is written |
| 979 | into the binary log. |
| 980 | * If there were any attempts to write events to the binary log during |
| 981 | function execution (grep for start_union_events and stop_union_events) |
| 982 | |
| 983 | If the answers are No and Yes, we write the function call into the binary |
| 984 | log as "SELECT spfunc(<param1value>, <param2value>, ...)" |
| 985 | |
| 986 | |
| 987 | 4. Miscellaneous issues. |
| 988 | |
| 989 | 4.1 User variables. |
| 990 | |
| 991 | When we call mysql_bin_log.write() for an SP statement, thd->user_var_events |
| 992 | must hold set<{var_name, value}> pairs for all user variables used during |
| 993 | the statement execution. |
| 994 | This set is produced by tracking user variable reads during statement |
| 995 | execution. |
| 996 | |
| 997 | For SPs, this has the following implications: |
| 998 | 1) thd->user_var_events may contain events from several SP statements and |
| 999 | needs to be valid after exection of these statements was finished. In |
| 1000 | order to achieve that, we |
| 1001 | * Allocate user_var_events array elements on appropriate mem_root (grep |
| 1002 | for user_var_events_alloc). |
| 1003 | * Use is_query_in_union() to determine if user_var_event is created. |
| 1004 | |
| 1005 | 2) We need to empty thd->user_var_events after we have wrote a function |
| 1006 | call. This is currently done by making |
| 1007 | reset_dynamic(&thd->user_var_events); |
| 1008 | calls in several different places. (TODO cosider moving this into |
| 1009 | mysql_bin_log.write() function) |
| 1010 | |
| 1011 | 4.2 Auto_increment storage in binlog |
| 1012 | |
| 1013 | As we may write two statements to binlog from one single logical statement |
| 1014 | (case of "SELECT func1(),func2()": it is binlogged as "SELECT func1()" and |
| 1015 | then "SELECT func2()"), we need to reset auto_increment binlog variables |
| 1016 | after each binlogged SELECT. Otherwise, the auto_increment value of the |
| 1017 | first SELECT would be used for the second too. |
| 1018 | */ |
| 1019 | |
| 1020 | |
| 1021 | /** |
| 1022 | Replace thd->query{_length} with a string that one can write to |
| 1023 | the binlog. |
| 1024 | |
| 1025 | The binlog-suitable string is produced by replacing references to SP local |
| 1026 | variables with NAME_CONST('sp_var_name', value) calls. |
| 1027 | |
| 1028 | @param thd Current thread. |
| 1029 | @param instr Instruction (we look for Item_splocal instances in |
| 1030 | instr->free_list) |
| 1031 | @param query_str Original query string |
| 1032 | |
| 1033 | @return |
| 1034 | - FALSE on success. |
| 1035 | thd->query{_length} either has been appropriately replaced or there |
| 1036 | is no need for replacements. |
| 1037 | - TRUE out of memory error. |
| 1038 | */ |
| 1039 | |
| 1040 | static bool |
| 1041 | subst_spvars(THD *thd, sp_instr *instr, LEX_STRING *query_str) |
| 1042 | { |
| 1043 | DBUG_ENTER("subst_spvars" ); |
| 1044 | |
| 1045 | Dynamic_array<Rewritable_query_parameter*> rewritables; |
| 1046 | char *pbuf; |
| 1047 | StringBuffer<512> qbuf; |
| 1048 | Copy_query_with_rewrite acc(thd, query_str->str, query_str->length, &qbuf); |
| 1049 | |
| 1050 | /* Find rewritable Items used in this statement */ |
| 1051 | for (Item *item= instr->free_list; item; item= item->next) |
| 1052 | { |
| 1053 | Rewritable_query_parameter *rqp= item->get_rewritable_query_parameter(); |
| 1054 | if (rqp && rqp->pos_in_query) |
| 1055 | rewritables.append(rqp); |
| 1056 | } |
| 1057 | if (!rewritables.elements()) |
| 1058 | DBUG_RETURN(FALSE); |
| 1059 | |
| 1060 | rewritables.sort(cmp_rqp_locations); |
| 1061 | |
| 1062 | thd->query_name_consts= (uint)rewritables.elements(); |
| 1063 | |
| 1064 | for (Rewritable_query_parameter **rqp= rewritables.front(); |
| 1065 | rqp <= rewritables.back(); rqp++) |
| 1066 | { |
| 1067 | if (acc.append(*rqp)) |
| 1068 | DBUG_RETURN(TRUE); |
| 1069 | } |
| 1070 | if (acc.finalize()) |
| 1071 | DBUG_RETURN(TRUE); |
| 1072 | |
| 1073 | /* |
| 1074 | Allocate additional space at the end of the new query string for the |
| 1075 | query_cache_send_result_to_client function. |
| 1076 | |
| 1077 | The query buffer layout is: |
| 1078 | buffer :== |
| 1079 | <statement> The input statement(s) |
| 1080 | '\0' Terminating null char |
| 1081 | <length> Length of following current database name 2 |
| 1082 | <db_name> Name of current database |
| 1083 | <flags> Flags struct |
| 1084 | */ |
| 1085 | size_t buf_len= (qbuf.length() + 1 + QUERY_CACHE_DB_LENGTH_SIZE + |
| 1086 | thd->db.length + QUERY_CACHE_FLAGS_SIZE + 1); |
| 1087 | if ((pbuf= (char *) alloc_root(thd->mem_root, buf_len))) |
| 1088 | { |
| 1089 | char *ptr= pbuf + qbuf.length(); |
| 1090 | memcpy(pbuf, qbuf.ptr(), qbuf.length()); |
| 1091 | *ptr= 0; |
| 1092 | int2store(ptr+1, thd->db.length); |
| 1093 | } |
| 1094 | else |
| 1095 | DBUG_RETURN(TRUE); |
| 1096 | |
| 1097 | thd->set_query(pbuf, qbuf.length()); |
| 1098 | |
| 1099 | DBUG_RETURN(FALSE); |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | void Sp_handler_procedure::recursion_level_error(THD *thd, |
| 1104 | const sp_head *sp) const |
| 1105 | { |
| 1106 | my_error(ER_SP_RECURSION_LIMIT, MYF(0), |
| 1107 | static_cast<int>(thd->variables.max_sp_recursion_depth), |
| 1108 | sp->m_name.str); |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | /** |
| 1113 | Execute the routine. The main instruction jump loop is there. |
| 1114 | Assume the parameters already set. |
| 1115 | |
| 1116 | @param thd Thread context. |
| 1117 | @param merge_da_on_success Flag specifying if Warning Info should be |
| 1118 | propagated to the caller on Completion |
| 1119 | Condition or not. |
| 1120 | |
| 1121 | @todo |
| 1122 | - Will write this SP statement into binlog separately |
| 1123 | (TODO: consider changing the condition to "not inside event union") |
| 1124 | |
| 1125 | @return Error status. |
| 1126 | @retval |
| 1127 | FALSE on success |
| 1128 | @retval |
| 1129 | TRUE on error |
| 1130 | */ |
| 1131 | |
| 1132 | bool |
| 1133 | sp_head::execute(THD *thd, bool merge_da_on_success) |
| 1134 | { |
| 1135 | DBUG_ENTER("sp_head::execute" ); |
| 1136 | char saved_cur_db_name_buf[SAFE_NAME_LEN+1]; |
| 1137 | LEX_STRING saved_cur_db_name= |
| 1138 | { saved_cur_db_name_buf, sizeof(saved_cur_db_name_buf) }; |
| 1139 | bool cur_db_changed= FALSE; |
| 1140 | sp_rcontext *ctx= thd->spcont; |
| 1141 | bool err_status= FALSE; |
| 1142 | uint ip= 0; |
| 1143 | sql_mode_t save_sql_mode; |
| 1144 | |
| 1145 | // TODO(cvicentiu) See if you can drop this bit. This is used to resume |
| 1146 | // execution from where we left off. |
| 1147 | if (m_chistics.agg_type == GROUP_AGGREGATE) |
| 1148 | ip= thd->spcont->instr_ptr; |
| 1149 | |
| 1150 | bool save_abort_on_warning; |
| 1151 | Query_arena *old_arena; |
| 1152 | /* per-instruction arena */ |
| 1153 | MEM_ROOT execute_mem_root; |
| 1154 | Query_arena execute_arena(&execute_mem_root, STMT_INITIALIZED_FOR_SP), |
| 1155 | backup_arena; |
| 1156 | query_id_t old_query_id; |
| 1157 | TABLE *old_derived_tables; |
| 1158 | LEX *old_lex; |
| 1159 | Item_change_list old_change_list; |
| 1160 | String old_packet; |
| 1161 | uint old_server_status; |
| 1162 | const uint status_backup_mask= SERVER_STATUS_CURSOR_EXISTS | |
| 1163 | SERVER_STATUS_LAST_ROW_SENT; |
| 1164 | MEM_ROOT *user_var_events_alloc_saved= 0; |
| 1165 | Reprepare_observer *save_reprepare_observer= thd->m_reprepare_observer; |
| 1166 | Object_creation_ctx *UNINIT_VAR(saved_creation_ctx); |
| 1167 | Diagnostics_area *da= thd->get_stmt_da(); |
| 1168 | Warning_info sp_wi(da->warning_info_id(), false, true); |
| 1169 | |
| 1170 | /* this 7*STACK_MIN_SIZE is a complex matter with a long history (see it!) */ |
| 1171 | if (check_stack_overrun(thd, 7 * STACK_MIN_SIZE, (uchar*)&old_packet)) |
| 1172 | DBUG_RETURN(TRUE); |
| 1173 | |
| 1174 | /* init per-instruction memroot */ |
| 1175 | init_sql_alloc(&execute_mem_root, "per_instruction_memroot" , |
| 1176 | MEM_ROOT_BLOCK_SIZE, 0, MYF(0)); |
| 1177 | |
| 1178 | DBUG_ASSERT(!(m_flags & IS_INVOKED)); |
| 1179 | m_flags|= IS_INVOKED; |
| 1180 | if (m_parent) |
| 1181 | m_parent->m_invoked_subroutine_count++; |
| 1182 | m_first_instance->m_first_free_instance= m_next_cached_sp; |
| 1183 | if (m_next_cached_sp) |
| 1184 | { |
| 1185 | DBUG_PRINT("info" , |
| 1186 | ("first free for %p ++: %p->%p level: %lu flags %x" , |
| 1187 | m_first_instance, this, |
| 1188 | m_next_cached_sp, |
| 1189 | m_next_cached_sp->m_recursion_level, |
| 1190 | m_next_cached_sp->m_flags)); |
| 1191 | } |
| 1192 | /* |
| 1193 | Check that if there are not any instances after this one then |
| 1194 | pointer to the last instance points on this instance or if there are |
| 1195 | some instances after this one then recursion level of next instance |
| 1196 | greater then recursion level of current instance on 1 |
| 1197 | */ |
| 1198 | DBUG_ASSERT((m_next_cached_sp == 0 && |
| 1199 | m_first_instance->m_last_cached_sp == this) || |
| 1200 | (m_recursion_level + 1 == m_next_cached_sp->m_recursion_level)); |
| 1201 | |
| 1202 | /* |
| 1203 | NOTE: The SQL Standard does not specify the context that should be |
| 1204 | preserved for stored routines. However, at SAP/Walldorf meeting it was |
| 1205 | decided that current database should be preserved. |
| 1206 | */ |
| 1207 | |
| 1208 | if (m_db.length && |
| 1209 | (err_status= mysql_opt_change_db(thd, &m_db, &saved_cur_db_name, FALSE, |
| 1210 | &cur_db_changed))) |
| 1211 | { |
| 1212 | goto done; |
| 1213 | } |
| 1214 | |
| 1215 | thd->is_slave_error= 0; |
| 1216 | old_arena= thd->stmt_arena; |
| 1217 | |
| 1218 | /* Push a new warning information area. */ |
| 1219 | da->copy_sql_conditions_to_wi(thd, &sp_wi); |
| 1220 | da->push_warning_info(&sp_wi); |
| 1221 | |
| 1222 | /* |
| 1223 | Switch query context. This has to be done early as this is sometimes |
| 1224 | allocated on THD::mem_root |
| 1225 | */ |
| 1226 | if (m_creation_ctx) |
| 1227 | saved_creation_ctx= m_creation_ctx->set_n_backup(thd); |
| 1228 | |
| 1229 | /* |
| 1230 | We have to save/restore this info when we are changing call level to |
| 1231 | be able properly do close_thread_tables() in instructions. |
| 1232 | */ |
| 1233 | old_query_id= thd->query_id; |
| 1234 | old_derived_tables= thd->derived_tables; |
| 1235 | thd->derived_tables= 0; |
| 1236 | save_sql_mode= thd->variables.sql_mode; |
| 1237 | thd->variables.sql_mode= m_sql_mode; |
| 1238 | save_abort_on_warning= thd->abort_on_warning; |
| 1239 | thd->abort_on_warning= 0; |
| 1240 | /** |
| 1241 | When inside a substatement (a stored function or trigger |
| 1242 | statement), clear the metadata observer in THD, if any. |
| 1243 | Remember the value of the observer here, to be able |
| 1244 | to restore it when leaving the substatement. |
| 1245 | |
| 1246 | We reset the observer to suppress errors when a substatement |
| 1247 | uses temporary tables. If a temporary table does not exist |
| 1248 | at start of the main statement, it's not prelocked |
| 1249 | and thus is not validated with other prelocked tables. |
| 1250 | |
| 1251 | Later on, when the temporary table is opened, metadata |
| 1252 | versions mismatch, expectedly. |
| 1253 | |
| 1254 | The proper solution for the problem is to re-validate tables |
| 1255 | of substatements (Bug#12257, Bug#27011, Bug#32868, Bug#33000), |
| 1256 | but it's not implemented yet. |
| 1257 | */ |
| 1258 | thd->m_reprepare_observer= 0; |
| 1259 | |
| 1260 | /* |
| 1261 | It is also more efficient to save/restore current thd->lex once when |
| 1262 | do it in each instruction |
| 1263 | */ |
| 1264 | old_lex= thd->lex; |
| 1265 | /* |
| 1266 | We should also save Item tree change list to avoid rollback something |
| 1267 | too early in the calling query. |
| 1268 | */ |
| 1269 | thd->Item_change_list::move_elements_to(&old_change_list); |
| 1270 | /* |
| 1271 | Cursors will use thd->packet, so they may corrupt data which was prepared |
| 1272 | for sending by upper level. OTOH cursors in the same routine can share this |
| 1273 | buffer safely so let use use routine-local packet instead of having own |
| 1274 | packet buffer for each cursor. |
| 1275 | |
| 1276 | It is probably safe to use same thd->convert_buff everywhere. |
| 1277 | */ |
| 1278 | old_packet.swap(thd->packet); |
| 1279 | old_server_status= thd->server_status & status_backup_mask; |
| 1280 | |
| 1281 | /* |
| 1282 | Switch to per-instruction arena here. We can do it since we cleanup |
| 1283 | arena after every instruction. |
| 1284 | */ |
| 1285 | thd->set_n_backup_active_arena(&execute_arena, &backup_arena); |
| 1286 | |
| 1287 | /* |
| 1288 | Save callers arena in order to store instruction results and out |
| 1289 | parameters in it later during sp_eval_func_item() |
| 1290 | */ |
| 1291 | thd->spcont->callers_arena= &backup_arena; |
| 1292 | |
| 1293 | #if defined(ENABLED_PROFILING) |
| 1294 | /* Discard the initial part of executing routines. */ |
| 1295 | thd->profiling.discard_current_query(); |
| 1296 | #endif |
| 1297 | sp_instr *i; |
| 1298 | DEBUG_SYNC(thd, "sp_head_execute_before_loop" ); |
| 1299 | do |
| 1300 | { |
| 1301 | #if defined(ENABLED_PROFILING) |
| 1302 | /* |
| 1303 | Treat each "instr" of a routine as discrete unit that could be profiled. |
| 1304 | Profiling only records information for segments of code that set the |
| 1305 | source of the query, and almost all kinds of instructions in s-p do not. |
| 1306 | */ |
| 1307 | thd->profiling.finish_current_query(); |
| 1308 | thd->profiling.start_new_query("continuing inside routine" ); |
| 1309 | #endif |
| 1310 | |
| 1311 | /* get_instr returns NULL when we're done. */ |
| 1312 | i = get_instr(ip); |
| 1313 | if (i == NULL) |
| 1314 | { |
| 1315 | #if defined(ENABLED_PROFILING) |
| 1316 | thd->profiling.discard_current_query(); |
| 1317 | #endif |
| 1318 | thd->spcont->quit_func= TRUE; |
| 1319 | break; |
| 1320 | } |
| 1321 | |
| 1322 | /* Reset number of warnings for this query. */ |
| 1323 | thd->get_stmt_da()->reset_for_next_command(); |
| 1324 | |
| 1325 | DBUG_PRINT("execute" , ("Instruction %u" , ip)); |
| 1326 | |
| 1327 | /* |
| 1328 | We need to reset start_time to allow for time to flow inside a stored |
| 1329 | procedure. This is only done for SP since time is suppose to be constant |
| 1330 | during execution of triggers and functions. |
| 1331 | */ |
| 1332 | reset_start_time_for_sp(thd); |
| 1333 | |
| 1334 | /* |
| 1335 | We have to set thd->stmt_arena before executing the instruction |
| 1336 | to store in the instruction free_list all new items, created |
| 1337 | during the first execution (for example expanding of '*' or the |
| 1338 | items made during other permanent subquery transformations). |
| 1339 | */ |
| 1340 | thd->stmt_arena= i; |
| 1341 | |
| 1342 | /* |
| 1343 | Will write this SP statement into binlog separately. |
| 1344 | TODO: consider changing the condition to "not inside event union". |
| 1345 | */ |
| 1346 | if (thd->locked_tables_mode <= LTM_LOCK_TABLES) |
| 1347 | { |
| 1348 | user_var_events_alloc_saved= thd->user_var_events_alloc; |
| 1349 | thd->user_var_events_alloc= thd->mem_root; |
| 1350 | } |
| 1351 | |
| 1352 | sql_digest_state *parent_digest= thd->m_digest; |
| 1353 | thd->m_digest= NULL; |
| 1354 | |
| 1355 | err_status= i->execute(thd, &ip); |
| 1356 | |
| 1357 | thd->m_digest= parent_digest; |
| 1358 | |
| 1359 | if (i->free_list) |
| 1360 | cleanup_items(i->free_list); |
| 1361 | |
| 1362 | /* |
| 1363 | If we've set thd->user_var_events_alloc to mem_root of this SP |
| 1364 | statement, clean all the events allocated in it. |
| 1365 | */ |
| 1366 | if (thd->locked_tables_mode <= LTM_LOCK_TABLES) |
| 1367 | { |
| 1368 | reset_dynamic(&thd->user_var_events); |
| 1369 | thd->user_var_events_alloc= user_var_events_alloc_saved; |
| 1370 | } |
| 1371 | |
| 1372 | /* we should cleanup free_list and memroot, used by instruction */ |
| 1373 | thd->cleanup_after_query(); |
| 1374 | free_root(&execute_mem_root, MYF(0)); |
| 1375 | |
| 1376 | /* |
| 1377 | Find and process SQL handlers unless it is a fatal error (fatal |
| 1378 | errors are not catchable by SQL handlers) or the connection has been |
| 1379 | killed during execution. |
| 1380 | */ |
| 1381 | if (likely(!thd->is_fatal_error) && likely(!thd->killed_errno()) && |
| 1382 | ctx->handle_sql_condition(thd, &ip, i)) |
| 1383 | { |
| 1384 | err_status= FALSE; |
| 1385 | } |
| 1386 | |
| 1387 | /* Reset sp_rcontext::end_partial_result_set flag. */ |
| 1388 | ctx->end_partial_result_set= FALSE; |
| 1389 | |
| 1390 | } while (!err_status && likely(!thd->killed) && |
| 1391 | likely(!thd->is_fatal_error) && |
| 1392 | !thd->spcont->pause_state); |
| 1393 | |
| 1394 | #if defined(ENABLED_PROFILING) |
| 1395 | thd->profiling.finish_current_query(); |
| 1396 | thd->profiling.start_new_query("tail end of routine" ); |
| 1397 | #endif |
| 1398 | |
| 1399 | /* Restore query context. */ |
| 1400 | |
| 1401 | if (m_creation_ctx) |
| 1402 | m_creation_ctx->restore_env(thd, saved_creation_ctx); |
| 1403 | |
| 1404 | /* Restore arena. */ |
| 1405 | |
| 1406 | thd->restore_active_arena(&execute_arena, &backup_arena); |
| 1407 | |
| 1408 | /* Only pop cursors when we're done with group aggregate running. */ |
| 1409 | if (m_chistics.agg_type != GROUP_AGGREGATE || |
| 1410 | (m_chistics.agg_type == GROUP_AGGREGATE && thd->spcont->quit_func)) |
| 1411 | thd->spcont->pop_all_cursors(); // To avoid memory leaks after an error |
| 1412 | |
| 1413 | /* Restore all saved */ |
| 1414 | if (m_chistics.agg_type == GROUP_AGGREGATE) |
| 1415 | thd->spcont->instr_ptr= ip; |
| 1416 | thd->server_status= (thd->server_status & ~status_backup_mask) | old_server_status; |
| 1417 | old_packet.swap(thd->packet); |
| 1418 | DBUG_ASSERT(thd->Item_change_list::is_empty()); |
| 1419 | old_change_list.move_elements_to(thd); |
| 1420 | thd->lex= old_lex; |
| 1421 | thd->set_query_id(old_query_id); |
| 1422 | DBUG_ASSERT(!thd->derived_tables); |
| 1423 | thd->derived_tables= old_derived_tables; |
| 1424 | thd->variables.sql_mode= save_sql_mode; |
| 1425 | thd->abort_on_warning= save_abort_on_warning; |
| 1426 | thd->m_reprepare_observer= save_reprepare_observer; |
| 1427 | |
| 1428 | thd->stmt_arena= old_arena; |
| 1429 | state= STMT_EXECUTED; |
| 1430 | |
| 1431 | /* |
| 1432 | Restore the caller's original warning information area: |
| 1433 | - warnings generated during trigger execution should not be |
| 1434 | propagated to the caller on success; |
| 1435 | - if there was an exception during execution, warning info should be |
| 1436 | propagated to the caller in any case. |
| 1437 | */ |
| 1438 | da->pop_warning_info(); |
| 1439 | |
| 1440 | if (err_status || merge_da_on_success) |
| 1441 | { |
| 1442 | /* |
| 1443 | If a routine body is empty or if a routine did not generate any warnings, |
| 1444 | do not duplicate our own contents by appending the contents of the called |
| 1445 | routine. We know that the called routine did not change its warning info. |
| 1446 | |
| 1447 | On the other hand, if the routine body is not empty and some statement in |
| 1448 | the routine generates a warning or uses tables, warning info is guaranteed |
| 1449 | to have changed. In this case we know that the routine warning info |
| 1450 | contains only new warnings, and thus we perform a copy. |
| 1451 | */ |
| 1452 | if (da->warning_info_changed(&sp_wi)) |
| 1453 | { |
| 1454 | /* |
| 1455 | If the invocation of the routine was a standalone statement, |
| 1456 | rather than a sub-statement, in other words, if it's a CALL |
| 1457 | of a procedure, rather than invocation of a function or a |
| 1458 | trigger, we need to clear the current contents of the caller's |
| 1459 | warning info. |
| 1460 | |
| 1461 | This is per MySQL rules: if a statement generates a warning, |
| 1462 | warnings from the previous statement are flushed. Normally |
| 1463 | it's done in push_warning(). However, here we don't use |
| 1464 | push_warning() to avoid invocation of condition handlers or |
| 1465 | escalation of warnings to errors. |
| 1466 | */ |
| 1467 | da->opt_clear_warning_info(thd->query_id); |
| 1468 | da->copy_sql_conditions_from_wi(thd, &sp_wi); |
| 1469 | da->remove_marked_sql_conditions(); |
| 1470 | if (i != NULL) |
| 1471 | push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE, |
| 1472 | ER_SP_STACK_TRACE, |
| 1473 | ER_THD(thd, ER_SP_STACK_TRACE), |
| 1474 | i->m_lineno, |
| 1475 | m_qname.str != NULL ? m_qname.str : |
| 1476 | "anonymous block" ); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | done: |
| 1481 | DBUG_PRINT("info" , ("err_status: %d killed: %d is_slave_error: %d report_error: %d" , |
| 1482 | err_status, thd->killed, thd->is_slave_error, |
| 1483 | thd->is_error())); |
| 1484 | |
| 1485 | if (thd->killed) |
| 1486 | err_status= TRUE; |
| 1487 | /* |
| 1488 | If the DB has changed, the pointer has changed too, but the |
| 1489 | original thd->db will then have been freed |
| 1490 | */ |
| 1491 | if (cur_db_changed && thd->killed != KILL_CONNECTION) |
| 1492 | { |
| 1493 | /* |
| 1494 | Force switching back to the saved current database, because it may be |
| 1495 | NULL. In this case, mysql_change_db() would generate an error. |
| 1496 | */ |
| 1497 | |
| 1498 | err_status|= mysql_change_db(thd, (LEX_CSTRING*) &saved_cur_db_name, TRUE); |
| 1499 | } |
| 1500 | m_flags&= ~IS_INVOKED; |
| 1501 | if (m_parent) |
| 1502 | m_parent->m_invoked_subroutine_count--; |
| 1503 | DBUG_PRINT("info" , |
| 1504 | ("first free for %p --: %p->%p, level: %lu, flags %x" , |
| 1505 | m_first_instance, |
| 1506 | m_first_instance->m_first_free_instance, |
| 1507 | this, m_recursion_level, m_flags)); |
| 1508 | /* |
| 1509 | Check that we have one of following: |
| 1510 | |
| 1511 | 1) there are not free instances which means that this instance is last |
| 1512 | in the list of instances (pointer to the last instance point on it and |
| 1513 | ther are not other instances after this one in the list) |
| 1514 | |
| 1515 | 2) There are some free instances which mean that first free instance |
| 1516 | should go just after this one and recursion level of that free instance |
| 1517 | should be on 1 more then recursion level of this instance. |
| 1518 | */ |
| 1519 | DBUG_ASSERT((m_first_instance->m_first_free_instance == 0 && |
| 1520 | this == m_first_instance->m_last_cached_sp && |
| 1521 | m_next_cached_sp == 0) || |
| 1522 | (m_first_instance->m_first_free_instance != 0 && |
| 1523 | m_first_instance->m_first_free_instance == m_next_cached_sp && |
| 1524 | m_first_instance->m_first_free_instance->m_recursion_level == |
| 1525 | m_recursion_level + 1)); |
| 1526 | m_first_instance->m_first_free_instance= this; |
| 1527 | |
| 1528 | DBUG_RETURN(err_status); |
| 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1533 | /** |
| 1534 | set_routine_security_ctx() changes routine security context, and |
| 1535 | checks if there is an EXECUTE privilege in new context. If there is |
| 1536 | no EXECUTE privilege, it changes the context back and returns a |
| 1537 | error. |
| 1538 | |
| 1539 | @param thd thread handle |
| 1540 | @param sp stored routine to change the context for |
| 1541 | @param save_ctx pointer to an old security context |
| 1542 | |
| 1543 | @todo |
| 1544 | - Cache if the definer has the right to use the object on the |
| 1545 | first usage and only reset the cache if someone does a GRANT |
| 1546 | statement that 'may' affect this. |
| 1547 | |
| 1548 | @retval |
| 1549 | TRUE if there was a error, and the context wasn't changed. |
| 1550 | @retval |
| 1551 | FALSE if the context was changed. |
| 1552 | */ |
| 1553 | |
| 1554 | bool |
| 1555 | set_routine_security_ctx(THD *thd, sp_head *sp, Security_context **save_ctx) |
| 1556 | { |
| 1557 | *save_ctx= 0; |
| 1558 | if (sp->suid() != SP_IS_NOT_SUID && |
| 1559 | sp->m_security_ctx.change_security_context(thd, &sp->m_definer.user, |
| 1560 | &sp->m_definer.host, |
| 1561 | &sp->m_db, |
| 1562 | save_ctx)) |
| 1563 | return TRUE; |
| 1564 | |
| 1565 | /* |
| 1566 | If we changed context to run as another user, we need to check the |
| 1567 | access right for the new context again as someone may have revoked |
| 1568 | the right to use the procedure from this user. |
| 1569 | |
| 1570 | TODO: |
| 1571 | Cache if the definer has the right to use the object on the |
| 1572 | first usage and only reset the cache if someone does a GRANT |
| 1573 | statement that 'may' affect this. |
| 1574 | */ |
| 1575 | if (*save_ctx && |
| 1576 | sp->check_execute_access(thd)) |
| 1577 | { |
| 1578 | sp->m_security_ctx.restore_security_context(thd, *save_ctx); |
| 1579 | *save_ctx= 0; |
| 1580 | return TRUE; |
| 1581 | } |
| 1582 | |
| 1583 | return FALSE; |
| 1584 | } |
| 1585 | #endif // ! NO_EMBEDDED_ACCESS_CHECKS |
| 1586 | |
| 1587 | |
| 1588 | bool sp_head::check_execute_access(THD *thd) const |
| 1589 | { |
| 1590 | return m_parent ? m_parent->check_execute_access(thd) : |
| 1591 | check_routine_access(thd, EXECUTE_ACL, |
| 1592 | &m_db, &m_name, |
| 1593 | m_handler, false); |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | /** |
| 1598 | Create rcontext optionally using the routine security. |
| 1599 | This is important for sql_mode=ORACLE to make sure that the invoker has |
| 1600 | access to the tables mentioned in the %TYPE references. |
| 1601 | |
| 1602 | In non-Oracle sql_modes we do not need access to any tables, |
| 1603 | so we can omit the security context switch for performance purposes. |
| 1604 | |
| 1605 | @param thd |
| 1606 | @param ret_value |
| 1607 | @retval NULL - error (access denided or EOM) |
| 1608 | @retval !NULL - success (the invoker has rights to all %TYPE tables) |
| 1609 | */ |
| 1610 | |
| 1611 | sp_rcontext *sp_head::rcontext_create(THD *thd, Field *ret_value, |
| 1612 | Row_definition_list *defs, |
| 1613 | bool switch_security_ctx) |
| 1614 | { |
| 1615 | if (!(m_flags & HAS_COLUMN_TYPE_REFS)) |
| 1616 | return sp_rcontext::create(thd, this, m_pcont, ret_value, *defs); |
| 1617 | sp_rcontext *res= NULL; |
| 1618 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1619 | Security_context *save_security_ctx; |
| 1620 | if (switch_security_ctx && |
| 1621 | set_routine_security_ctx(thd, this, &save_security_ctx)) |
| 1622 | return NULL; |
| 1623 | #endif |
| 1624 | if (!defs->resolve_type_refs(thd)) |
| 1625 | res= sp_rcontext::create(thd, this, m_pcont, ret_value, *defs); |
| 1626 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1627 | if (switch_security_ctx) |
| 1628 | m_security_ctx.restore_security_context(thd, save_security_ctx); |
| 1629 | #endif |
| 1630 | return res; |
| 1631 | } |
| 1632 | |
| 1633 | |
| 1634 | sp_rcontext *sp_head::rcontext_create(THD *thd, Field *ret_value, |
| 1635 | List<Item> *args) |
| 1636 | { |
| 1637 | DBUG_ASSERT(args); |
| 1638 | Row_definition_list defs; |
| 1639 | m_pcont->retrieve_field_definitions(&defs); |
| 1640 | if (defs.adjust_formal_params_to_actual_params(thd, args)) |
| 1641 | return NULL; |
| 1642 | return rcontext_create(thd, ret_value, &defs, true); |
| 1643 | } |
| 1644 | |
| 1645 | |
| 1646 | sp_rcontext *sp_head::rcontext_create(THD *thd, Field *ret_value, |
| 1647 | Item **args, uint arg_count) |
| 1648 | { |
| 1649 | Row_definition_list defs; |
| 1650 | m_pcont->retrieve_field_definitions(&defs); |
| 1651 | if (defs.adjust_formal_params_to_actual_params(thd, args, arg_count)) |
| 1652 | return NULL; |
| 1653 | return rcontext_create(thd, ret_value, &defs, true); |
| 1654 | } |
| 1655 | |
| 1656 | |
| 1657 | /** |
| 1658 | Execute trigger stored program. |
| 1659 | |
| 1660 | - changes security context for triggers |
| 1661 | - switch to new memroot |
| 1662 | - call sp_head::execute |
| 1663 | - restore old memroot |
| 1664 | - restores security context |
| 1665 | |
| 1666 | @param thd Thread handle |
| 1667 | @param db database name |
| 1668 | @param table table name |
| 1669 | @param grant_info GRANT_INFO structure to be filled with |
| 1670 | information about definer's privileges |
| 1671 | on subject table |
| 1672 | |
| 1673 | @todo |
| 1674 | - TODO: we should create sp_rcontext once per command and reuse it |
| 1675 | on subsequent executions of a trigger. |
| 1676 | |
| 1677 | @retval |
| 1678 | FALSE on success |
| 1679 | @retval |
| 1680 | TRUE on error |
| 1681 | */ |
| 1682 | |
| 1683 | bool |
| 1684 | sp_head::execute_trigger(THD *thd, |
| 1685 | const LEX_CSTRING *db_name, |
| 1686 | const LEX_CSTRING *table_name, |
| 1687 | GRANT_INFO *grant_info) |
| 1688 | { |
| 1689 | sp_rcontext *octx = thd->spcont; |
| 1690 | sp_rcontext *nctx = NULL; |
| 1691 | bool err_status= FALSE; |
| 1692 | MEM_ROOT call_mem_root; |
| 1693 | Query_arena call_arena(&call_mem_root, Query_arena::STMT_INITIALIZED_FOR_SP); |
| 1694 | Query_arena backup_arena; |
| 1695 | DBUG_ENTER("sp_head::execute_trigger" ); |
| 1696 | DBUG_PRINT("info" , ("trigger %s" , m_name.str)); |
| 1697 | |
| 1698 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1699 | Security_context *save_ctx= NULL; |
| 1700 | |
| 1701 | |
| 1702 | if (suid() != SP_IS_NOT_SUID && |
| 1703 | m_security_ctx.change_security_context(thd, |
| 1704 | &m_definer.user, |
| 1705 | &m_definer.host, |
| 1706 | &m_db, |
| 1707 | &save_ctx)) |
| 1708 | DBUG_RETURN(TRUE); |
| 1709 | |
| 1710 | /* |
| 1711 | Fetch information about table-level privileges for subject table into |
| 1712 | GRANT_INFO instance. The access check itself will happen in |
| 1713 | Item_trigger_field, where this information will be used along with |
| 1714 | information about column-level privileges. |
| 1715 | */ |
| 1716 | |
| 1717 | fill_effective_table_privileges(thd, |
| 1718 | grant_info, |
| 1719 | db_name->str, |
| 1720 | table_name->str); |
| 1721 | |
| 1722 | /* Check that the definer has TRIGGER privilege on the subject table. */ |
| 1723 | |
| 1724 | if (!(grant_info->privilege & TRIGGER_ACL)) |
| 1725 | { |
| 1726 | char priv_desc[128]; |
| 1727 | get_privilege_desc(priv_desc, sizeof(priv_desc), TRIGGER_ACL); |
| 1728 | |
| 1729 | my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0), priv_desc, |
| 1730 | thd->security_ctx->priv_user, thd->security_ctx->host_or_ip, |
| 1731 | table_name->str); |
| 1732 | |
| 1733 | m_security_ctx.restore_security_context(thd, save_ctx); |
| 1734 | DBUG_RETURN(TRUE); |
| 1735 | } |
| 1736 | #endif // NO_EMBEDDED_ACCESS_CHECKS |
| 1737 | |
| 1738 | /* |
| 1739 | Prepare arena and memroot for objects which lifetime is whole |
| 1740 | duration of trigger call (sp_rcontext, it's tables and items, |
| 1741 | sp_cursor and Item_cache holders for case expressions). We can't |
| 1742 | use caller's arena/memroot for those objects because in this case |
| 1743 | some fixed amount of memory will be consumed for each trigger |
| 1744 | invocation and so statements which involve lot of them will hog |
| 1745 | memory. |
| 1746 | |
| 1747 | TODO: we should create sp_rcontext once per command and reuse it |
| 1748 | on subsequent executions of a trigger. |
| 1749 | */ |
| 1750 | init_sql_alloc(&call_mem_root, "execute_trigger" , MEM_ROOT_BLOCK_SIZE, 0, |
| 1751 | MYF(0)); |
| 1752 | thd->set_n_backup_active_arena(&call_arena, &backup_arena); |
| 1753 | |
| 1754 | Row_definition_list defs; |
| 1755 | m_pcont->retrieve_field_definitions(&defs); |
| 1756 | if (!(nctx= rcontext_create(thd, NULL, &defs, false))) |
| 1757 | { |
| 1758 | err_status= TRUE; |
| 1759 | goto err_with_cleanup; |
| 1760 | } |
| 1761 | |
| 1762 | thd->spcont= nctx; |
| 1763 | |
| 1764 | err_status= execute(thd, FALSE); |
| 1765 | |
| 1766 | err_with_cleanup: |
| 1767 | thd->restore_active_arena(&call_arena, &backup_arena); |
| 1768 | |
| 1769 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1770 | m_security_ctx.restore_security_context(thd, save_ctx); |
| 1771 | #endif // NO_EMBEDDED_ACCESS_CHECKS |
| 1772 | |
| 1773 | delete nctx; |
| 1774 | call_arena.free_items(); |
| 1775 | free_root(&call_mem_root, MYF(0)); |
| 1776 | thd->spcont= octx; |
| 1777 | |
| 1778 | if (thd->killed) |
| 1779 | thd->send_kill_message(); |
| 1780 | |
| 1781 | DBUG_RETURN(err_status); |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | /* |
| 1786 | Execute the package initialization section. |
| 1787 | */ |
| 1788 | |
| 1789 | bool sp_package::instantiate_if_needed(THD *thd) |
| 1790 | { |
| 1791 | List<Item> args; |
| 1792 | if (m_is_instantiated) |
| 1793 | return false; |
| 1794 | /* |
| 1795 | Set m_is_instantiated to true early, to avoid recursion in case if |
| 1796 | the package initialization section calls routines from the same package. |
| 1797 | */ |
| 1798 | m_is_instantiated= true; |
| 1799 | /* |
| 1800 | Check that the initialization section doesn't contain Dynamic SQL |
| 1801 | and doesn't return result sets: such stored procedures can't |
| 1802 | be called from a function or trigger. |
| 1803 | */ |
| 1804 | if (thd->in_sub_stmt) |
| 1805 | { |
| 1806 | const char *where= (thd->in_sub_stmt & SUB_STMT_TRIGGER ? |
| 1807 | "trigger" : "function" ); |
| 1808 | if (is_not_allowed_in_function(where)) |
| 1809 | goto err; |
| 1810 | } |
| 1811 | |
| 1812 | args.elements= 0; |
| 1813 | if (execute_procedure(thd, &args)) |
| 1814 | goto err; |
| 1815 | return false; |
| 1816 | err: |
| 1817 | m_is_instantiated= false; |
| 1818 | return true; |
| 1819 | } |
| 1820 | |
| 1821 | |
| 1822 | /** |
| 1823 | Execute a function. |
| 1824 | |
| 1825 | - evaluate parameters |
| 1826 | - changes security context for SUID routines |
| 1827 | - switch to new memroot |
| 1828 | - call sp_head::execute |
| 1829 | - restore old memroot |
| 1830 | - evaluate the return value |
| 1831 | - restores security context |
| 1832 | |
| 1833 | @param thd Thread handle |
| 1834 | @param argp Passed arguments (these are items from containing |
| 1835 | statement?) |
| 1836 | @param argcount Number of passed arguments. We need to check if |
| 1837 | this is correct. |
| 1838 | @param return_value_fld Save result here. |
| 1839 | |
| 1840 | @todo |
| 1841 | We should create sp_rcontext once per command and reuse |
| 1842 | it on subsequent executions of a function/trigger. |
| 1843 | |
| 1844 | @todo |
| 1845 | In future we should associate call arena/mem_root with |
| 1846 | sp_rcontext and allocate all these objects (and sp_rcontext |
| 1847 | itself) on it directly rather than juggle with arenas. |
| 1848 | |
| 1849 | @retval |
| 1850 | FALSE on success |
| 1851 | @retval |
| 1852 | TRUE on error |
| 1853 | */ |
| 1854 | |
| 1855 | bool |
| 1856 | sp_head::execute_function(THD *thd, Item **argp, uint argcount, |
| 1857 | Field *return_value_fld, sp_rcontext **func_ctx, |
| 1858 | Query_arena *call_arena) |
| 1859 | { |
| 1860 | ulonglong UNINIT_VAR(binlog_save_options); |
| 1861 | bool need_binlog_call= FALSE; |
| 1862 | uint arg_no; |
| 1863 | sp_rcontext *octx = thd->spcont; |
| 1864 | char buf[STRING_BUFFER_USUAL_SIZE]; |
| 1865 | String binlog_buf(buf, sizeof(buf), &my_charset_bin); |
| 1866 | bool err_status= FALSE; |
| 1867 | Query_arena backup_arena; |
| 1868 | DBUG_ENTER("sp_head::execute_function" ); |
| 1869 | DBUG_PRINT("info" , ("function %s" , m_name.str)); |
| 1870 | |
| 1871 | if (m_parent && m_parent->instantiate_if_needed(thd)) |
| 1872 | DBUG_RETURN(true); |
| 1873 | |
| 1874 | /* |
| 1875 | Check that the function is called with all specified arguments. |
| 1876 | |
| 1877 | If it is not, use my_error() to report an error, or it will not terminate |
| 1878 | the invoking query properly. |
| 1879 | */ |
| 1880 | if (argcount != m_pcont->context_var_count()) |
| 1881 | { |
| 1882 | /* |
| 1883 | Need to use my_error here, or it will not terminate the |
| 1884 | invoking query properly. |
| 1885 | */ |
| 1886 | my_error(ER_SP_WRONG_NO_OF_ARGS, MYF(0), |
| 1887 | "FUNCTION" , ErrConvDQName(this).ptr(), |
| 1888 | m_pcont->context_var_count(), argcount); |
| 1889 | DBUG_RETURN(TRUE); |
| 1890 | } |
| 1891 | /* |
| 1892 | Prepare arena and memroot for objects which lifetime is whole |
| 1893 | duration of function call (sp_rcontext, it's tables and items, |
| 1894 | sp_cursor and Item_cache holders for case expressions). |
| 1895 | We can't use caller's arena/memroot for those objects because |
| 1896 | in this case some fixed amount of memory will be consumed for |
| 1897 | each function/trigger invocation and so statements which involve |
| 1898 | lot of them will hog memory. |
| 1899 | TODO: we should create sp_rcontext once per command and reuse |
| 1900 | it on subsequent executions of a function/trigger. |
| 1901 | */ |
| 1902 | if (!(*func_ctx)) |
| 1903 | { |
| 1904 | thd->set_n_backup_active_arena(call_arena, &backup_arena); |
| 1905 | |
| 1906 | if (!(*func_ctx= rcontext_create(thd, return_value_fld, argp, argcount))) |
| 1907 | { |
| 1908 | thd->restore_active_arena(call_arena, &backup_arena); |
| 1909 | err_status= TRUE; |
| 1910 | goto err_with_cleanup; |
| 1911 | } |
| 1912 | |
| 1913 | /* |
| 1914 | We have to switch temporarily back to callers arena/memroot. |
| 1915 | Function arguments belong to the caller and so the may reference |
| 1916 | memory which they will allocate during calculation long after |
| 1917 | this function call will be finished (e.g. in Item::cleanup()). |
| 1918 | */ |
| 1919 | thd->restore_active_arena(call_arena, &backup_arena); |
| 1920 | } |
| 1921 | |
| 1922 | /* Pass arguments. */ |
| 1923 | for (arg_no= 0; arg_no < argcount; arg_no++) |
| 1924 | { |
| 1925 | /* Arguments must be fixed in Item_func_sp::fix_fields */ |
| 1926 | DBUG_ASSERT(argp[arg_no]->fixed); |
| 1927 | |
| 1928 | if ((err_status= (*func_ctx)->set_parameter(thd, arg_no, &(argp[arg_no])))) |
| 1929 | goto err_with_cleanup; |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | If row-based binlogging, we don't need to binlog the function's call, let |
| 1934 | each substatement be binlogged its way. |
| 1935 | */ |
| 1936 | need_binlog_call= mysql_bin_log.is_open() && |
| 1937 | (thd->variables.option_bits & OPTION_BIN_LOG) && |
| 1938 | !thd->is_current_stmt_binlog_format_row(); |
| 1939 | |
| 1940 | /* |
| 1941 | Remember the original arguments for unrolled replication of functions |
| 1942 | before they are changed by execution. |
| 1943 | */ |
| 1944 | if (need_binlog_call) |
| 1945 | { |
| 1946 | binlog_buf.length(0); |
| 1947 | binlog_buf.append(STRING_WITH_LEN("SELECT " )); |
| 1948 | append_identifier(thd, &binlog_buf, &m_db); |
| 1949 | binlog_buf.append('.'); |
| 1950 | append_identifier(thd, &binlog_buf, &m_name); |
| 1951 | binlog_buf.append('('); |
| 1952 | for (arg_no= 0; arg_no < argcount; arg_no++) |
| 1953 | { |
| 1954 | String str_value_holder; |
| 1955 | String *str_value; |
| 1956 | |
| 1957 | if (arg_no) |
| 1958 | binlog_buf.append(','); |
| 1959 | |
| 1960 | Item_field *item= (*func_ctx)->get_parameter(arg_no); |
| 1961 | str_value= item->type_handler()->print_item_value(thd, item, |
| 1962 | &str_value_holder); |
| 1963 | if (str_value) |
| 1964 | binlog_buf.append(*str_value); |
| 1965 | else |
| 1966 | binlog_buf.append(STRING_WITH_LEN("NULL" )); |
| 1967 | } |
| 1968 | binlog_buf.append(')'); |
| 1969 | } |
| 1970 | thd->spcont= *func_ctx; |
| 1971 | |
| 1972 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 1973 | Security_context *save_security_ctx; |
| 1974 | if (set_routine_security_ctx(thd, this, &save_security_ctx)) |
| 1975 | { |
| 1976 | err_status= TRUE; |
| 1977 | goto err_with_cleanup; |
| 1978 | } |
| 1979 | #endif |
| 1980 | |
| 1981 | if (need_binlog_call) |
| 1982 | { |
| 1983 | query_id_t q; |
| 1984 | reset_dynamic(&thd->user_var_events); |
| 1985 | /* |
| 1986 | In case of artificially constructed events for function calls |
| 1987 | we have separate union for each such event and hence can't use |
| 1988 | query_id of real calling statement as the start of all these |
| 1989 | unions (this will break logic of replication of user-defined |
| 1990 | variables). So we use artifical value which is guaranteed to |
| 1991 | be greater than all query_id's of all statements belonging |
| 1992 | to previous events/unions. |
| 1993 | Possible alternative to this is logging of all function invocations |
| 1994 | as one select and not resetting THD::user_var_events before |
| 1995 | each invocation. |
| 1996 | */ |
| 1997 | q= get_query_id(); |
| 1998 | mysql_bin_log.start_union_events(thd, q + 1); |
| 1999 | binlog_save_options= thd->variables.option_bits; |
| 2000 | thd->variables.option_bits&= ~OPTION_BIN_LOG; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | Switch to call arena/mem_root so objects like sp_cursor or |
| 2005 | Item_cache holders for case expressions can be allocated on it. |
| 2006 | |
| 2007 | TODO: In future we should associate call arena/mem_root with |
| 2008 | sp_rcontext and allocate all these objects (and sp_rcontext |
| 2009 | itself) on it directly rather than juggle with arenas. |
| 2010 | */ |
| 2011 | thd->set_n_backup_active_arena(call_arena, &backup_arena); |
| 2012 | |
| 2013 | err_status= execute(thd, TRUE); |
| 2014 | |
| 2015 | thd->restore_active_arena(call_arena, &backup_arena); |
| 2016 | |
| 2017 | if (need_binlog_call) |
| 2018 | { |
| 2019 | mysql_bin_log.stop_union_events(thd); |
| 2020 | thd->variables.option_bits= binlog_save_options; |
| 2021 | if (thd->binlog_evt_union.unioned_events) |
| 2022 | { |
| 2023 | int errcode = query_error_code(thd, thd->killed == NOT_KILLED); |
| 2024 | Query_log_event qinfo(thd, binlog_buf.ptr(), binlog_buf.length(), |
| 2025 | thd->binlog_evt_union.unioned_events_trans, FALSE, FALSE, errcode); |
| 2026 | if (mysql_bin_log.write(&qinfo) && |
| 2027 | thd->binlog_evt_union.unioned_events_trans) |
| 2028 | { |
| 2029 | push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR, |
| 2030 | "Invoked ROUTINE modified a transactional table but MySQL " |
| 2031 | "failed to reflect this change in the binary log" ); |
| 2032 | err_status= TRUE; |
| 2033 | } |
| 2034 | reset_dynamic(&thd->user_var_events); |
| 2035 | /* Forget those values, in case more function calls are binlogged: */ |
| 2036 | thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0; |
| 2037 | thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty(); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | if (!err_status && thd->spcont->quit_func) |
| 2042 | { |
| 2043 | /* We need result only in function but not in trigger */ |
| 2044 | |
| 2045 | if (!(*func_ctx)->is_return_value_set()) |
| 2046 | { |
| 2047 | my_error(ER_SP_NORETURNEND, MYF(0), m_name.str); |
| 2048 | err_status= TRUE; |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 2053 | m_security_ctx.restore_security_context(thd, save_security_ctx); |
| 2054 | #endif |
| 2055 | |
| 2056 | err_with_cleanup: |
| 2057 | thd->spcont= octx; |
| 2058 | |
| 2059 | /* |
| 2060 | If not insided a procedure and a function printing warning |
| 2061 | messsages. |
| 2062 | */ |
| 2063 | if (need_binlog_call && |
| 2064 | thd->spcont == NULL && !thd->binlog_evt_union.do_union) |
| 2065 | thd->issue_unsafe_warnings(); |
| 2066 | |
| 2067 | DBUG_RETURN(err_status); |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | /** |
| 2072 | Execute a procedure. |
| 2073 | |
| 2074 | The function does the following steps: |
| 2075 | - Set all parameters |
| 2076 | - changes security context for SUID routines |
| 2077 | - call sp_head::execute |
| 2078 | - copy back values of INOUT and OUT parameters |
| 2079 | - restores security context |
| 2080 | |
| 2081 | @param thd Thread handle |
| 2082 | @param args List of values passed as arguments. |
| 2083 | |
| 2084 | @retval |
| 2085 | FALSE on success |
| 2086 | @retval |
| 2087 | TRUE on error |
| 2088 | */ |
| 2089 | |
| 2090 | bool |
| 2091 | sp_head::execute_procedure(THD *thd, List<Item> *args) |
| 2092 | { |
| 2093 | bool err_status= FALSE; |
| 2094 | uint params = m_pcont->context_var_count(); |
| 2095 | /* Query start time may be reset in a multi-stmt SP; keep this for later. */ |
| 2096 | ulonglong utime_before_sp_exec= thd->utime_after_lock; |
| 2097 | sp_rcontext *save_spcont, *octx; |
| 2098 | sp_rcontext *nctx = NULL; |
| 2099 | bool save_enable_slow_log; |
| 2100 | bool save_log_general= false; |
| 2101 | sp_package *pkg= get_package(); |
| 2102 | DBUG_ENTER("sp_head::execute_procedure" ); |
| 2103 | DBUG_PRINT("info" , ("procedure %s" , m_name.str)); |
| 2104 | |
| 2105 | if (m_parent && m_parent->instantiate_if_needed(thd)) |
| 2106 | DBUG_RETURN(true); |
| 2107 | |
| 2108 | if (args->elements != params) |
| 2109 | { |
| 2110 | my_error(ER_SP_WRONG_NO_OF_ARGS, MYF(0), "PROCEDURE" , |
| 2111 | ErrConvDQName(this).ptr(), params, args->elements); |
| 2112 | DBUG_RETURN(TRUE); |
| 2113 | } |
| 2114 | |
| 2115 | save_spcont= octx= thd->spcont; |
| 2116 | if (! octx) |
| 2117 | { |
| 2118 | /* Create a temporary old context. */ |
| 2119 | if (!(octx= rcontext_create(thd, NULL, args))) |
| 2120 | { |
| 2121 | DBUG_PRINT("error" , ("Could not create octx" )); |
| 2122 | DBUG_RETURN(TRUE); |
| 2123 | } |
| 2124 | |
| 2125 | thd->spcont= octx; |
| 2126 | |
| 2127 | /* set callers_arena to thd, for upper-level function to work */ |
| 2128 | thd->spcont->callers_arena= thd; |
| 2129 | } |
| 2130 | |
| 2131 | if (!pkg) |
| 2132 | { |
| 2133 | if (!(nctx= rcontext_create(thd, NULL, args))) |
| 2134 | { |
| 2135 | delete nctx; /* Delete nctx if it was init() that failed. */ |
| 2136 | thd->spcont= save_spcont; |
| 2137 | DBUG_RETURN(TRUE); |
| 2138 | } |
| 2139 | } |
| 2140 | else |
| 2141 | { |
| 2142 | if (!pkg->m_rcontext) |
| 2143 | { |
| 2144 | Query_arena backup_arena; |
| 2145 | thd->set_n_backup_active_arena(this, &backup_arena); |
| 2146 | nctx= pkg->rcontext_create(thd, NULL, args); |
| 2147 | thd->restore_active_arena(this, &backup_arena); |
| 2148 | if (!nctx) |
| 2149 | { |
| 2150 | thd->spcont= save_spcont; |
| 2151 | DBUG_RETURN(TRUE); |
| 2152 | } |
| 2153 | pkg->m_rcontext= nctx; |
| 2154 | } |
| 2155 | else |
| 2156 | nctx= pkg->m_rcontext; |
| 2157 | } |
| 2158 | |
| 2159 | if (params > 0) |
| 2160 | { |
| 2161 | List_iterator<Item> it_args(*args); |
| 2162 | |
| 2163 | DBUG_PRINT("info" ,(" %.*s: eval args" , (int) m_name.length, m_name.str)); |
| 2164 | |
| 2165 | for (uint i= 0 ; i < params ; i++) |
| 2166 | { |
| 2167 | Item *arg_item= it_args++; |
| 2168 | |
| 2169 | if (!arg_item) |
| 2170 | break; |
| 2171 | |
| 2172 | sp_variable *spvar= m_pcont->find_variable(i); |
| 2173 | |
| 2174 | if (!spvar) |
| 2175 | continue; |
| 2176 | |
| 2177 | if (spvar->mode != sp_variable::MODE_IN) |
| 2178 | { |
| 2179 | Settable_routine_parameter *srp= |
| 2180 | arg_item->get_settable_routine_parameter(); |
| 2181 | |
| 2182 | if (!srp) |
| 2183 | { |
| 2184 | my_error(ER_SP_NOT_VAR_ARG, MYF(0), i+1, ErrConvDQName(this).ptr()); |
| 2185 | err_status= TRUE; |
| 2186 | break; |
| 2187 | } |
| 2188 | |
| 2189 | srp->set_required_privilege(spvar->mode == sp_variable::MODE_INOUT); |
| 2190 | } |
| 2191 | |
| 2192 | if (spvar->mode == sp_variable::MODE_OUT) |
| 2193 | { |
| 2194 | Item_null *null_item= new (thd->mem_root) Item_null(thd); |
| 2195 | Item *tmp_item= null_item; |
| 2196 | |
| 2197 | if (!null_item || |
| 2198 | nctx->set_parameter(thd, i, &tmp_item)) |
| 2199 | { |
| 2200 | DBUG_PRINT("error" , ("set variable failed" )); |
| 2201 | err_status= TRUE; |
| 2202 | break; |
| 2203 | } |
| 2204 | } |
| 2205 | else |
| 2206 | { |
| 2207 | if (nctx->set_parameter(thd, i, it_args.ref())) |
| 2208 | { |
| 2209 | DBUG_PRINT("error" , ("set variable 2 failed" )); |
| 2210 | err_status= TRUE; |
| 2211 | break; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | TRANSACT_TRACKER(add_trx_state_from_thd(thd)); |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | Okay, got values for all arguments. Close tables that might be used by |
| 2220 | arguments evaluation. If arguments evaluation required prelocking mode, |
| 2221 | we'll leave it here. |
| 2222 | */ |
| 2223 | thd->lex->unit.cleanup(); |
| 2224 | |
| 2225 | if (!thd->in_sub_stmt) |
| 2226 | { |
| 2227 | thd->get_stmt_da()->set_overwrite_status(true); |
| 2228 | thd->is_error() ? trans_rollback_stmt(thd) : trans_commit_stmt(thd); |
| 2229 | thd->get_stmt_da()->set_overwrite_status(false); |
| 2230 | } |
| 2231 | |
| 2232 | close_thread_tables(thd); |
| 2233 | thd_proc_info(thd, 0); |
| 2234 | |
| 2235 | if (! thd->in_sub_stmt) |
| 2236 | { |
| 2237 | if (thd->transaction_rollback_request) |
| 2238 | { |
| 2239 | trans_rollback_implicit(thd); |
| 2240 | thd->mdl_context.release_transactional_locks(); |
| 2241 | } |
| 2242 | else if (! thd->in_multi_stmt_transaction_mode()) |
| 2243 | thd->mdl_context.release_transactional_locks(); |
| 2244 | else |
| 2245 | thd->mdl_context.release_statement_locks(); |
| 2246 | } |
| 2247 | |
| 2248 | thd->rollback_item_tree_changes(); |
| 2249 | |
| 2250 | DBUG_PRINT("info" ,(" %.*s: eval args done" , (int) m_name.length, |
| 2251 | m_name.str)); |
| 2252 | } |
| 2253 | |
| 2254 | save_enable_slow_log= thd->enable_slow_log; |
| 2255 | |
| 2256 | /* |
| 2257 | Disable slow log if: |
| 2258 | - Slow logging is enabled (no change needed) |
| 2259 | - This is a normal SP (not event log) |
| 2260 | - If we have not explicitely disabled logging of SP |
| 2261 | */ |
| 2262 | if (save_enable_slow_log && |
| 2263 | ((!(m_flags & LOG_SLOW_STATEMENTS) && |
| 2264 | (thd->variables.log_slow_disabled_statements & LOG_SLOW_DISABLE_SP)))) |
| 2265 | { |
| 2266 | DBUG_PRINT("info" , ("Disabling slow log for the execution" )); |
| 2267 | thd->enable_slow_log= FALSE; |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | Disable general log if: |
| 2272 | - If general log is enabled (no change needed) |
| 2273 | - This is a normal SP (not event log) |
| 2274 | - If we have not explicitely disabled logging of SP |
| 2275 | */ |
| 2276 | if (!(thd->variables.option_bits & OPTION_LOG_OFF) && |
| 2277 | (!(m_flags & LOG_GENERAL_LOG) && |
| 2278 | (thd->variables.log_disabled_statements & LOG_DISABLE_SP))) |
| 2279 | { |
| 2280 | DBUG_PRINT("info" , ("Disabling general log for the execution" )); |
| 2281 | save_log_general= true; |
| 2282 | /* disable this bit */ |
| 2283 | thd->variables.option_bits |= OPTION_LOG_OFF; |
| 2284 | } |
| 2285 | thd->spcont= nctx; |
| 2286 | |
| 2287 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 2288 | Security_context *save_security_ctx= 0; |
| 2289 | if (!err_status) |
| 2290 | err_status= set_routine_security_ctx(thd, this, &save_security_ctx); |
| 2291 | #endif |
| 2292 | |
| 2293 | if (!err_status) |
| 2294 | { |
| 2295 | err_status= execute(thd, TRUE); |
| 2296 | } |
| 2297 | |
| 2298 | if (save_log_general) |
| 2299 | thd->variables.option_bits &= ~OPTION_LOG_OFF; |
| 2300 | thd->enable_slow_log= save_enable_slow_log; |
| 2301 | |
| 2302 | /* |
| 2303 | In the case when we weren't able to employ reuse mechanism for |
| 2304 | OUT/INOUT paranmeters, we should reallocate memory. This |
| 2305 | allocation should be done on the arena which will live through |
| 2306 | all execution of calling routine. |
| 2307 | */ |
| 2308 | thd->spcont->callers_arena= octx->callers_arena; |
| 2309 | |
| 2310 | if (!err_status && params > 0) |
| 2311 | { |
| 2312 | List_iterator<Item> it_args(*args); |
| 2313 | |
| 2314 | /* |
| 2315 | Copy back all OUT or INOUT values to the previous frame, or |
| 2316 | set global user variables |
| 2317 | */ |
| 2318 | for (uint i= 0 ; i < params ; i++) |
| 2319 | { |
| 2320 | Item *arg_item= it_args++; |
| 2321 | |
| 2322 | if (!arg_item) |
| 2323 | break; |
| 2324 | |
| 2325 | sp_variable *spvar= m_pcont->find_variable(i); |
| 2326 | |
| 2327 | if (spvar->mode == sp_variable::MODE_IN) |
| 2328 | continue; |
| 2329 | |
| 2330 | Settable_routine_parameter *srp= |
| 2331 | arg_item->get_settable_routine_parameter(); |
| 2332 | |
| 2333 | DBUG_ASSERT(srp); |
| 2334 | |
| 2335 | if (srp->set_value(thd, octx, nctx->get_variable_addr(i))) |
| 2336 | { |
| 2337 | DBUG_PRINT("error" , ("set value failed" )); |
| 2338 | err_status= TRUE; |
| 2339 | break; |
| 2340 | } |
| 2341 | |
| 2342 | Send_field *out_param_info= new (thd->mem_root) Send_field(); |
| 2343 | nctx->get_parameter(i)->make_send_field(thd, out_param_info); |
| 2344 | out_param_info->db_name= m_db.str; |
| 2345 | out_param_info->table_name= m_name.str; |
| 2346 | out_param_info->org_table_name= m_name.str; |
| 2347 | out_param_info->col_name= spvar->name; |
| 2348 | out_param_info->org_col_name= spvar->name; |
| 2349 | |
| 2350 | srp->set_out_param_info(out_param_info); |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
| 2355 | if (save_security_ctx) |
| 2356 | m_security_ctx.restore_security_context(thd, save_security_ctx); |
| 2357 | #endif |
| 2358 | |
| 2359 | if (!save_spcont) |
| 2360 | delete octx; |
| 2361 | |
| 2362 | if (!pkg) |
| 2363 | delete nctx; |
| 2364 | thd->spcont= save_spcont; |
| 2365 | thd->utime_after_lock= utime_before_sp_exec; |
| 2366 | |
| 2367 | /* |
| 2368 | If not insided a procedure and a function printing warning |
| 2369 | messsages. |
| 2370 | */ |
| 2371 | bool need_binlog_call= mysql_bin_log.is_open() && |
| 2372 | (thd->variables.option_bits & OPTION_BIN_LOG) && |
| 2373 | !thd->is_current_stmt_binlog_format_row(); |
| 2374 | if (need_binlog_call && thd->spcont == NULL && |
| 2375 | !thd->binlog_evt_union.do_union) |
| 2376 | thd->issue_unsafe_warnings(); |
| 2377 | |
| 2378 | DBUG_RETURN(err_status); |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | /** |
| 2383 | Reset lex during parsing, before we parse a sub statement. |
| 2384 | |
| 2385 | @param thd Thread handler. |
| 2386 | |
| 2387 | @return Error state |
| 2388 | @retval true An error occurred. |
| 2389 | @retval false Success. |
| 2390 | */ |
| 2391 | |
| 2392 | bool |
| 2393 | sp_head::reset_lex(THD *thd, sp_lex_local *sublex) |
| 2394 | { |
| 2395 | DBUG_ENTER("sp_head::reset_lex" ); |
| 2396 | LEX *oldlex= thd->lex; |
| 2397 | |
| 2398 | thd->set_local_lex(sublex); |
| 2399 | |
| 2400 | DBUG_RETURN(m_lex.push_front(oldlex)); |
| 2401 | } |
| 2402 | |
| 2403 | |
| 2404 | bool |
| 2405 | sp_head::reset_lex(THD *thd) |
| 2406 | { |
| 2407 | DBUG_ENTER("sp_head::reset_lex" ); |
| 2408 | sp_lex_local *sublex= new (thd->mem_root) sp_lex_local(thd, thd->lex); |
| 2409 | DBUG_RETURN(sublex ? reset_lex(thd, sublex) : true); |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | /** |
| 2414 | Restore lex during parsing, after we have parsed a sub statement. |
| 2415 | |
| 2416 | @param thd Thread handle |
| 2417 | @param oldlex The upper level lex we're near to restore to |
| 2418 | @param sublex The local lex we're near to restore from |
| 2419 | |
| 2420 | @return |
| 2421 | @retval TRUE failure |
| 2422 | @retval FALSE success |
| 2423 | */ |
| 2424 | |
| 2425 | bool |
| 2426 | sp_head::merge_lex(THD *thd, LEX *oldlex, LEX *sublex) |
| 2427 | { |
| 2428 | DBUG_ENTER("sp_head::merge_lex" ); |
| 2429 | |
| 2430 | sublex->set_trg_event_type_for_tables(); |
| 2431 | |
| 2432 | oldlex->trg_table_fields.push_back(&sublex->trg_table_fields); |
| 2433 | |
| 2434 | /* If this substatement is unsafe, the entire routine is too. */ |
| 2435 | DBUG_PRINT("info" , ("sublex->get_stmt_unsafe_flags: 0x%x" , |
| 2436 | sublex->get_stmt_unsafe_flags())); |
| 2437 | unsafe_flags|= sublex->get_stmt_unsafe_flags(); |
| 2438 | |
| 2439 | /* |
| 2440 | Add routines which are used by statement to respective set for |
| 2441 | this routine. |
| 2442 | */ |
| 2443 | if (sp_update_sp_used_routines(&m_sroutines, &sublex->sroutines)) |
| 2444 | DBUG_RETURN(TRUE); |
| 2445 | |
| 2446 | /* If this substatement is a update query, then mark MODIFIES_DATA */ |
| 2447 | if (is_update_query(sublex->sql_command)) |
| 2448 | m_flags|= MODIFIES_DATA; |
| 2449 | |
| 2450 | /* |
| 2451 | Merge tables used by this statement (but not by its functions or |
| 2452 | procedures) to multiset of tables used by this routine. |
| 2453 | */ |
| 2454 | merge_table_list(thd, sublex->query_tables, sublex); |
| 2455 | /* Merge lists of PS parameters. */ |
| 2456 | oldlex->param_list.append(&sublex->param_list); |
| 2457 | |
| 2458 | DBUG_RETURN(FALSE); |
| 2459 | } |
| 2460 | |
| 2461 | /** |
| 2462 | Put the instruction on the backpatch list, associated with the label. |
| 2463 | */ |
| 2464 | |
| 2465 | int |
| 2466 | sp_head::push_backpatch(THD *thd, sp_instr *i, sp_label *lab, |
| 2467 | List<bp_t> *list, backpatch_instr_type itype) |
| 2468 | { |
| 2469 | bp_t *bp= (bp_t *) thd->alloc(sizeof(bp_t)); |
| 2470 | |
| 2471 | if (!bp) |
| 2472 | return 1; |
| 2473 | bp->lab= lab; |
| 2474 | bp->instr= i; |
| 2475 | bp->instr_type= itype; |
| 2476 | return list->push_front(bp); |
| 2477 | } |
| 2478 | |
| 2479 | int |
| 2480 | sp_head::push_backpatch(THD *thd, sp_instr *i, sp_label *lab) |
| 2481 | { |
| 2482 | return push_backpatch(thd, i, lab, &m_backpatch, GOTO); |
| 2483 | } |
| 2484 | |
| 2485 | int |
| 2486 | sp_head::push_backpatch_goto(THD *thd, sp_pcontext *ctx, sp_label *lab) |
| 2487 | { |
| 2488 | uint ip= instructions(); |
| 2489 | |
| 2490 | /* |
| 2491 | Add cpop/hpop : they will be removed or updated later if target is in |
| 2492 | the same block or not |
| 2493 | */ |
| 2494 | sp_instr_hpop *hpop= new (thd->mem_root) sp_instr_hpop(ip++, ctx, 0); |
| 2495 | if (hpop == NULL || add_instr(hpop)) |
| 2496 | return true; |
| 2497 | if (push_backpatch(thd, hpop, lab, &m_backpatch_goto, HPOP)) |
| 2498 | return true; |
| 2499 | |
| 2500 | sp_instr_cpop *cpop= new (thd->mem_root) sp_instr_cpop(ip++, ctx, 0); |
| 2501 | if (cpop == NULL || add_instr(cpop)) |
| 2502 | return true; |
| 2503 | if (push_backpatch(thd, cpop, lab, &m_backpatch_goto, CPOP)) |
| 2504 | return true; |
| 2505 | |
| 2506 | // Add jump with ip=0. IP will be updated when label is found. |
| 2507 | sp_instr_jump *i= new (thd->mem_root) sp_instr_jump(ip, ctx); |
| 2508 | if (i == NULL || add_instr(i)) |
| 2509 | return true; |
| 2510 | if (push_backpatch(thd, i, lab, &m_backpatch_goto, GOTO)) |
| 2511 | return true; |
| 2512 | |
| 2513 | return false; |
| 2514 | } |
| 2515 | |
| 2516 | /** |
| 2517 | Update all instruction with this label in the backpatch list to |
| 2518 | the current position. |
| 2519 | */ |
| 2520 | |
| 2521 | void |
| 2522 | sp_head::backpatch(sp_label *lab) |
| 2523 | { |
| 2524 | bp_t *bp; |
| 2525 | uint dest= instructions(); |
| 2526 | List_iterator_fast<bp_t> li(m_backpatch); |
| 2527 | |
| 2528 | DBUG_ENTER("sp_head::backpatch" ); |
| 2529 | while ((bp= li++)) |
| 2530 | { |
| 2531 | if (bp->lab == lab) |
| 2532 | { |
| 2533 | DBUG_PRINT("info" , ("backpatch: (m_ip %d, label %p <%s>) to dest %d" , |
| 2534 | bp->instr->m_ip, lab, lab->name.str, dest)); |
| 2535 | bp->instr->backpatch(dest, lab->ctx); |
| 2536 | } |
| 2537 | } |
| 2538 | DBUG_VOID_RETURN; |
| 2539 | } |
| 2540 | |
| 2541 | void |
| 2542 | sp_head::backpatch_goto(THD *thd, sp_label *lab,sp_label *lab_begin_block) |
| 2543 | { |
| 2544 | bp_t *bp; |
| 2545 | uint dest= instructions(); |
| 2546 | List_iterator<bp_t> li(m_backpatch_goto); |
| 2547 | |
| 2548 | DBUG_ENTER("sp_head::backpatch_goto" ); |
| 2549 | while ((bp= li++)) |
| 2550 | { |
| 2551 | if (bp->instr->m_ip < lab_begin_block->ip || bp->instr->m_ip > lab->ip) |
| 2552 | { |
| 2553 | /* |
| 2554 | Update only jump target from the beginning of the block where the |
| 2555 | label is defined. |
| 2556 | */ |
| 2557 | continue; |
| 2558 | } |
| 2559 | if (lex_string_cmp(system_charset_info, &bp->lab->name, &lab->name) == 0) |
| 2560 | { |
| 2561 | if (bp->instr_type == GOTO) |
| 2562 | { |
| 2563 | DBUG_PRINT("info" , |
| 2564 | ("backpatch_goto: (m_ip %d, label %p <%s>) to dest %d" , |
| 2565 | bp->instr->m_ip, lab, lab->name.str, dest)); |
| 2566 | bp->instr->backpatch(dest, lab->ctx); |
| 2567 | // Jump resolved, remove from the list |
| 2568 | li.remove(); |
| 2569 | continue; |
| 2570 | } |
| 2571 | if (bp->instr_type == CPOP) |
| 2572 | { |
| 2573 | uint n= lab->ctx->diff_cursors(lab_begin_block->ctx, true); |
| 2574 | if (n == 0) |
| 2575 | { |
| 2576 | // Remove cpop instr |
| 2577 | replace_instr_to_nop(thd,bp->instr->m_ip); |
| 2578 | } |
| 2579 | else |
| 2580 | { |
| 2581 | // update count of cpop |
| 2582 | static_cast<sp_instr_cpop*>(bp->instr)->update_count(n); |
| 2583 | n= 1; |
| 2584 | } |
| 2585 | li.remove(); |
| 2586 | continue; |
| 2587 | } |
| 2588 | if (bp->instr_type == HPOP) |
| 2589 | { |
| 2590 | uint n= lab->ctx->diff_handlers(lab_begin_block->ctx, true); |
| 2591 | if (n == 0) |
| 2592 | { |
| 2593 | // Remove hpop instr |
| 2594 | replace_instr_to_nop(thd,bp->instr->m_ip); |
| 2595 | } |
| 2596 | else |
| 2597 | { |
| 2598 | // update count of cpop |
| 2599 | static_cast<sp_instr_hpop*>(bp->instr)->update_count(n); |
| 2600 | n= 1; |
| 2601 | } |
| 2602 | li.remove(); |
| 2603 | continue; |
| 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | DBUG_VOID_RETURN; |
| 2608 | } |
| 2609 | |
| 2610 | bool |
| 2611 | sp_head::check_unresolved_goto() |
| 2612 | { |
| 2613 | DBUG_ENTER("sp_head::check_unresolved_goto" ); |
| 2614 | bool has_unresolved_label=false; |
| 2615 | if (m_backpatch_goto.elements > 0) |
| 2616 | { |
| 2617 | List_iterator_fast<bp_t> li(m_backpatch_goto); |
| 2618 | while (bp_t* bp= li++) |
| 2619 | { |
| 2620 | if (bp->instr_type == GOTO) |
| 2621 | { |
| 2622 | my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "GOTO" , bp->lab->name.str); |
| 2623 | has_unresolved_label=true; |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | DBUG_RETURN(has_unresolved_label); |
| 2628 | } |
| 2629 | |
| 2630 | int |
| 2631 | sp_head::new_cont_backpatch(sp_instr_opt_meta *i) |
| 2632 | { |
| 2633 | m_cont_level+= 1; |
| 2634 | if (i) |
| 2635 | { |
| 2636 | /* Use the cont. destination slot to store the level */ |
| 2637 | i->m_cont_dest= m_cont_level; |
| 2638 | if (m_cont_backpatch.push_front(i)) |
| 2639 | return 1; |
| 2640 | } |
| 2641 | return 0; |
| 2642 | } |
| 2643 | |
| 2644 | int |
| 2645 | sp_head::add_cont_backpatch(sp_instr_opt_meta *i) |
| 2646 | { |
| 2647 | i->m_cont_dest= m_cont_level; |
| 2648 | return m_cont_backpatch.push_front(i); |
| 2649 | } |
| 2650 | |
| 2651 | void |
| 2652 | sp_head::do_cont_backpatch() |
| 2653 | { |
| 2654 | uint dest= instructions(); |
| 2655 | uint lev= m_cont_level--; |
| 2656 | sp_instr_opt_meta *i; |
| 2657 | |
| 2658 | while ((i= m_cont_backpatch.head()) && i->m_cont_dest == lev) |
| 2659 | { |
| 2660 | i->m_cont_dest= dest; |
| 2661 | (void)m_cont_backpatch.pop(); |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | |
| 2666 | bool |
| 2667 | sp_head::sp_add_instr_cpush_for_cursors(THD *thd, sp_pcontext *pcontext) |
| 2668 | { |
| 2669 | for (uint i= 0; i < pcontext->frame_cursor_count(); i++) |
| 2670 | { |
| 2671 | const sp_pcursor *c= pcontext->get_cursor_by_local_frame_offset(i); |
| 2672 | sp_instr_cpush *instr= new (thd->mem_root) |
| 2673 | sp_instr_cpush(instructions(), pcontext, c->lex(), |
| 2674 | pcontext->cursor_offset() + i); |
| 2675 | if (instr == NULL || add_instr(instr)) |
| 2676 | return true; |
| 2677 | } |
| 2678 | return false; |
| 2679 | } |
| 2680 | |
| 2681 | |
| 2682 | void |
| 2683 | sp_head::set_chistics(const st_sp_chistics &chistics) |
| 2684 | { |
| 2685 | m_chistics.set(chistics); |
| 2686 | if (m_chistics.comment.length == 0) |
| 2687 | m_chistics.comment.str= 0; |
| 2688 | else |
| 2689 | m_chistics.comment.str= strmake_root(mem_root, |
| 2690 | m_chistics.comment.str, |
| 2691 | m_chistics.comment.length); |
| 2692 | } |
| 2693 | |
| 2694 | void |
| 2695 | sp_head::set_info(longlong created, longlong modified, |
| 2696 | const st_sp_chistics &chistics, sql_mode_t sql_mode) |
| 2697 | { |
| 2698 | m_created= created; |
| 2699 | m_modified= modified; |
| 2700 | set_chistics(chistics); |
| 2701 | m_sql_mode= sql_mode; |
| 2702 | } |
| 2703 | |
| 2704 | |
| 2705 | void |
| 2706 | sp_head::reset_thd_mem_root(THD *thd) |
| 2707 | { |
| 2708 | DBUG_ENTER("sp_head::reset_thd_mem_root" ); |
| 2709 | m_thd_root= thd->mem_root; |
| 2710 | thd->mem_root= &main_mem_root; |
| 2711 | DBUG_PRINT("info" , ("mem_root %p moved to thd mem root %p" , |
| 2712 | &mem_root, &thd->mem_root)); |
| 2713 | free_list= thd->free_list; // Keep the old list |
| 2714 | thd->free_list= NULL; // Start a new one |
| 2715 | m_thd= thd; |
| 2716 | DBUG_VOID_RETURN; |
| 2717 | } |
| 2718 | |
| 2719 | void |
| 2720 | sp_head::restore_thd_mem_root(THD *thd) |
| 2721 | { |
| 2722 | DBUG_ENTER("sp_head::restore_thd_mem_root" ); |
| 2723 | |
| 2724 | /* |
| 2725 | In some cases our parser detects a syntax error and calls |
| 2726 | LEX::cleanup_lex_after_parse_error() method only after |
| 2727 | finishing parsing the whole routine. In such a situation |
| 2728 | sp_head::restore_thd_mem_root() will be called twice - the |
| 2729 | first time as part of normal parsing process and the second |
| 2730 | time by cleanup_lex_after_parse_error(). |
| 2731 | To avoid ruining active arena/mem_root state in this case we |
| 2732 | skip restoration of old arena/mem_root if this method has been |
| 2733 | already called for this routine. |
| 2734 | */ |
| 2735 | if (!m_thd) |
| 2736 | DBUG_VOID_RETURN; |
| 2737 | |
| 2738 | Item *flist= free_list; // The old list |
| 2739 | set_query_arena(thd); // Get new free_list and mem_root |
| 2740 | state= STMT_INITIALIZED_FOR_SP; |
| 2741 | is_stored_procedure= true; |
| 2742 | |
| 2743 | DBUG_PRINT("info" , ("mem_root %p returned from thd mem root %p" , |
| 2744 | &mem_root, &thd->mem_root)); |
| 2745 | thd->free_list= flist; // Restore the old one |
| 2746 | thd->mem_root= m_thd_root; |
| 2747 | m_thd= NULL; |
| 2748 | DBUG_VOID_RETURN; |
| 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | /** |
| 2753 | Check if a user has access right to a routine. |
| 2754 | |
| 2755 | @param thd Thread handler |
| 2756 | @param sp SP |
| 2757 | @param full_access Set to 1 if the user has SELECT right to the |
| 2758 | 'mysql.proc' able or is the owner of the routine |
| 2759 | @retval |
| 2760 | false ok |
| 2761 | @retval |
| 2762 | true error |
| 2763 | */ |
| 2764 | |
| 2765 | bool check_show_routine_access(THD *thd, sp_head *sp, bool *full_access) |
| 2766 | { |
| 2767 | TABLE_LIST tables; |
| 2768 | bzero((char*) &tables,sizeof(tables)); |
| 2769 | tables.db= MYSQL_SCHEMA_NAME; |
| 2770 | tables.table_name= MYSQL_PROC_NAME; |
| 2771 | tables.alias= MYSQL_PROC_NAME; |
| 2772 | |
| 2773 | *full_access= ((!check_table_access(thd, SELECT_ACL, &tables, FALSE, |
| 2774 | 1, TRUE) && |
| 2775 | (tables.grant.privilege & SELECT_ACL) != 0) || |
| 2776 | /* Check if user owns the routine. */ |
| 2777 | (!strcmp(sp->m_definer.user.str, |
| 2778 | thd->security_ctx->priv_user) && |
| 2779 | !strcmp(sp->m_definer.host.str, |
| 2780 | thd->security_ctx->priv_host)) || |
| 2781 | /* Check if current role or any of the sub-granted roles |
| 2782 | own the routine. */ |
| 2783 | (sp->m_definer.host.length == 0 && |
| 2784 | (!strcmp(sp->m_definer.user.str, |
| 2785 | thd->security_ctx->priv_role) || |
| 2786 | check_role_is_granted(thd->security_ctx->priv_role, NULL, |
| 2787 | sp->m_definer.user.str)))); |
| 2788 | if (!*full_access) |
| 2789 | return check_some_routine_access(thd, sp->m_db.str, sp->m_name.str, |
| 2790 | sp->m_handler); |
| 2791 | return 0; |
| 2792 | } |
| 2793 | |
| 2794 | |
| 2795 | /** |
| 2796 | Collect metadata for SHOW CREATE statement for stored routines. |
| 2797 | |
| 2798 | @param thd Thread context. |
| 2799 | @param sph Stored routine handler |
| 2800 | @param fields Item list to populate |
| 2801 | |
| 2802 | @return Error status. |
| 2803 | @retval FALSE on success |
| 2804 | @retval TRUE on error |
| 2805 | */ |
| 2806 | |
| 2807 | void |
| 2808 | sp_head::show_create_routine_get_fields(THD *thd, const Sp_handler *sph, |
| 2809 | List<Item> *fields) |
| 2810 | { |
| 2811 | const char *col1_caption= sph->show_create_routine_col1_caption(); |
| 2812 | const char *col3_caption= sph->show_create_routine_col3_caption(); |
| 2813 | |
| 2814 | MEM_ROOT *mem_root= thd->mem_root; |
| 2815 | |
| 2816 | /* Send header. */ |
| 2817 | |
| 2818 | fields->push_back(new (mem_root) |
| 2819 | Item_empty_string(thd, col1_caption, NAME_CHAR_LEN), |
| 2820 | mem_root); |
| 2821 | fields->push_back(new (mem_root) |
| 2822 | Item_empty_string(thd, "sql_mode" , 256), |
| 2823 | mem_root); |
| 2824 | |
| 2825 | { |
| 2826 | /* |
| 2827 | NOTE: SQL statement field must be not less than 1024 in order not to |
| 2828 | confuse old clients. |
| 2829 | */ |
| 2830 | |
| 2831 | Item_empty_string *stmt_fld= |
| 2832 | new (mem_root) Item_empty_string(thd, col3_caption, 1024); |
| 2833 | stmt_fld->maybe_null= TRUE; |
| 2834 | |
| 2835 | fields->push_back(stmt_fld, mem_root); |
| 2836 | } |
| 2837 | |
| 2838 | fields->push_back(new (mem_root) |
| 2839 | Item_empty_string(thd, "character_set_client" , |
| 2840 | MY_CS_NAME_SIZE), |
| 2841 | mem_root); |
| 2842 | |
| 2843 | fields->push_back(new (mem_root) |
| 2844 | Item_empty_string(thd, "collation_connection" , |
| 2845 | MY_CS_NAME_SIZE), |
| 2846 | mem_root); |
| 2847 | |
| 2848 | fields->push_back(new (mem_root) |
| 2849 | Item_empty_string(thd, "Database Collation" , |
| 2850 | MY_CS_NAME_SIZE), |
| 2851 | mem_root); |
| 2852 | } |
| 2853 | |
| 2854 | |
| 2855 | /** |
| 2856 | Implement SHOW CREATE statement for stored routines. |
| 2857 | |
| 2858 | @param thd Thread context. |
| 2859 | @param sph Stored routine handler |
| 2860 | |
| 2861 | @return Error status. |
| 2862 | @retval FALSE on success |
| 2863 | @retval TRUE on error |
| 2864 | */ |
| 2865 | |
| 2866 | bool |
| 2867 | sp_head::show_create_routine(THD *thd, const Sp_handler *sph) |
| 2868 | { |
| 2869 | const char *col1_caption= sph->show_create_routine_col1_caption(); |
| 2870 | const char *col3_caption= sph->show_create_routine_col3_caption(); |
| 2871 | |
| 2872 | bool err_status; |
| 2873 | |
| 2874 | Protocol *protocol= thd->protocol; |
| 2875 | List<Item> fields; |
| 2876 | |
| 2877 | LEX_CSTRING sql_mode; |
| 2878 | |
| 2879 | bool full_access; |
| 2880 | MEM_ROOT *mem_root= thd->mem_root; |
| 2881 | |
| 2882 | DBUG_ENTER("sp_head::show_create_routine" ); |
| 2883 | DBUG_PRINT("info" , ("routine %s" , m_name.str)); |
| 2884 | |
| 2885 | if (check_show_routine_access(thd, this, &full_access)) |
| 2886 | DBUG_RETURN(TRUE); |
| 2887 | |
| 2888 | sql_mode_string_representation(thd, m_sql_mode, &sql_mode); |
| 2889 | |
| 2890 | /* Send header. */ |
| 2891 | |
| 2892 | fields.push_back(new (mem_root) |
| 2893 | Item_empty_string(thd, col1_caption, NAME_CHAR_LEN), |
| 2894 | thd->mem_root); |
| 2895 | fields.push_back(new (mem_root) |
| 2896 | Item_empty_string(thd, "sql_mode" , (uint)sql_mode.length), |
| 2897 | thd->mem_root); |
| 2898 | |
| 2899 | { |
| 2900 | /* |
| 2901 | NOTE: SQL statement field must be not less than 1024 in order not to |
| 2902 | confuse old clients. |
| 2903 | */ |
| 2904 | |
| 2905 | Item_empty_string *stmt_fld= |
| 2906 | new (mem_root) Item_empty_string(thd, col3_caption, |
| 2907 | (uint)MY_MAX(m_defstr.length, 1024)); |
| 2908 | |
| 2909 | stmt_fld->maybe_null= TRUE; |
| 2910 | |
| 2911 | fields.push_back(stmt_fld, thd->mem_root); |
| 2912 | } |
| 2913 | |
| 2914 | fields.push_back(new (mem_root) |
| 2915 | Item_empty_string(thd, "character_set_client" , |
| 2916 | MY_CS_NAME_SIZE), |
| 2917 | thd->mem_root); |
| 2918 | |
| 2919 | fields.push_back(new (mem_root) |
| 2920 | Item_empty_string(thd, "collation_connection" , |
| 2921 | MY_CS_NAME_SIZE), |
| 2922 | thd->mem_root); |
| 2923 | |
| 2924 | fields.push_back(new (mem_root) |
| 2925 | Item_empty_string(thd, "Database Collation" , |
| 2926 | MY_CS_NAME_SIZE), |
| 2927 | thd->mem_root); |
| 2928 | |
| 2929 | if (protocol->send_result_set_metadata(&fields, |
| 2930 | Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)) |
| 2931 | { |
| 2932 | DBUG_RETURN(TRUE); |
| 2933 | } |
| 2934 | |
| 2935 | /* Send data. */ |
| 2936 | |
| 2937 | protocol->prepare_for_resend(); |
| 2938 | |
| 2939 | protocol->store(m_name.str, m_name.length, system_charset_info); |
| 2940 | protocol->store(sql_mode.str, sql_mode.length, system_charset_info); |
| 2941 | |
| 2942 | if (full_access) |
| 2943 | protocol->store(m_defstr.str, m_defstr.length, |
| 2944 | m_creation_ctx->get_client_cs()); |
| 2945 | else |
| 2946 | protocol->store_null(); |
| 2947 | |
| 2948 | |
| 2949 | protocol->store(m_creation_ctx->get_client_cs()->csname, system_charset_info); |
| 2950 | protocol->store(m_creation_ctx->get_connection_cl()->name, system_charset_info); |
| 2951 | protocol->store(m_creation_ctx->get_db_cl()->name, system_charset_info); |
| 2952 | |
| 2953 | err_status= protocol->write(); |
| 2954 | |
| 2955 | if (!err_status) |
| 2956 | my_eof(thd); |
| 2957 | |
| 2958 | DBUG_RETURN(err_status); |
| 2959 | } |
| 2960 | |
| 2961 | |
| 2962 | /** |
| 2963 | Add instruction to SP. |
| 2964 | |
| 2965 | @param instr Instruction |
| 2966 | */ |
| 2967 | |
| 2968 | int sp_head::add_instr(sp_instr *instr) |
| 2969 | { |
| 2970 | instr->free_list= m_thd->free_list; |
| 2971 | m_thd->free_list= 0; |
| 2972 | /* |
| 2973 | Memory root of every instruction is designated for permanent |
| 2974 | transformations (optimizations) made on the parsed tree during |
| 2975 | the first execution. It points to the memory root of the |
| 2976 | entire stored procedure, as their life span is equal. |
| 2977 | */ |
| 2978 | instr->mem_root= &main_mem_root; |
| 2979 | instr->m_lineno= m_thd->m_parser_state->m_lip.yylineno; |
| 2980 | return insert_dynamic(&m_instr, (uchar*)&instr); |
| 2981 | } |
| 2982 | |
| 2983 | |
| 2984 | bool sp_head::add_instr_jump(THD *thd, sp_pcontext *spcont) |
| 2985 | { |
| 2986 | sp_instr_jump *i= new (thd->mem_root) sp_instr_jump(instructions(), spcont); |
| 2987 | return i == NULL || add_instr(i); |
| 2988 | } |
| 2989 | |
| 2990 | |
| 2991 | bool sp_head::add_instr_jump(THD *thd, sp_pcontext *spcont, uint dest) |
| 2992 | { |
| 2993 | sp_instr_jump *i= new (thd->mem_root) sp_instr_jump(instructions(), |
| 2994 | spcont, dest); |
| 2995 | return i == NULL || add_instr(i); |
| 2996 | } |
| 2997 | |
| 2998 | |
| 2999 | bool sp_head::add_instr_jump_forward_with_backpatch(THD *thd, |
| 3000 | sp_pcontext *spcont, |
| 3001 | sp_label *lab) |
| 3002 | { |
| 3003 | sp_instr_jump *i= new (thd->mem_root) sp_instr_jump(instructions(), spcont); |
| 3004 | if (i == NULL || add_instr(i)) |
| 3005 | return true; |
| 3006 | push_backpatch(thd, i, lab); |
| 3007 | return false; |
| 3008 | } |
| 3009 | |
| 3010 | |
| 3011 | bool sp_head::add_instr_freturn(THD *thd, sp_pcontext *spcont, |
| 3012 | Item *item, LEX *lex) |
| 3013 | { |
| 3014 | sp_instr_freturn *i= new (thd->mem_root) |
| 3015 | sp_instr_freturn(instructions(), spcont, item, |
| 3016 | m_return_field_def.type_handler(), thd->lex); |
| 3017 | if (i == NULL || add_instr(i)) |
| 3018 | return true; |
| 3019 | m_flags|= sp_head::HAS_RETURN; |
| 3020 | return false; |
| 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | bool sp_head::add_instr_preturn(THD *thd, sp_pcontext *spcont) |
| 3025 | { |
| 3026 | sp_instr_preturn *i= new (thd->mem_root) |
| 3027 | sp_instr_preturn(instructions(), spcont); |
| 3028 | if (i == NULL || add_instr(i)) |
| 3029 | return true; |
| 3030 | return false; |
| 3031 | } |
| 3032 | |
| 3033 | |
| 3034 | /* |
| 3035 | Replace an instruction at position to "no operation". |
| 3036 | |
| 3037 | @param thd - use mem_root of this THD for "new". |
| 3038 | @param ip - position of the operation |
| 3039 | @returns - true on error, false on success |
| 3040 | |
| 3041 | When we need to remove an instruction that during compilation |
| 3042 | appeared to be useless (typically as useless jump), we replace |
| 3043 | it to a jump to exactly the next instruction. |
| 3044 | Such jumps are later removed during sp_head::optimize(). |
| 3045 | |
| 3046 | QQ: Perhaps we need a dedicated sp_instr_nop for this purpose. |
| 3047 | */ |
| 3048 | |
| 3049 | bool sp_head::replace_instr_to_nop(THD *thd, uint ip) |
| 3050 | { |
| 3051 | sp_instr *instr= get_instr(ip); |
| 3052 | sp_instr_jump *nop= new (thd->mem_root) sp_instr_jump(instr->m_ip, |
| 3053 | instr->m_ctx, |
| 3054 | instr->m_ip + 1); |
| 3055 | if (!nop) |
| 3056 | return true; |
| 3057 | delete instr; |
| 3058 | set_dynamic(&m_instr, (uchar *) &nop, ip); |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
| 3062 | |
| 3063 | /** |
| 3064 | Do some minimal optimization of the code: |
| 3065 | -# Mark used instructions |
| 3066 | -# While doing this, shortcut jumps to jump instructions |
| 3067 | -# Compact the code, removing unused instructions. |
| 3068 | |
| 3069 | This is the main mark and move loop; it relies on the following methods |
| 3070 | in sp_instr and its subclasses: |
| 3071 | |
| 3072 | - opt_mark() : Mark instruction as reachable |
| 3073 | - opt_shortcut_jump(): Shortcut jumps to the final destination; |
| 3074 | used by opt_mark(). |
| 3075 | - opt_move() : Update moved instruction |
| 3076 | - set_destination() : Set the new destination (jump instructions only) |
| 3077 | */ |
| 3078 | |
| 3079 | void sp_head::optimize() |
| 3080 | { |
| 3081 | List<sp_instr> bp; |
| 3082 | sp_instr *i; |
| 3083 | uint src, dst; |
| 3084 | |
| 3085 | opt_mark(); |
| 3086 | |
| 3087 | bp.empty(); |
| 3088 | src= dst= 0; |
| 3089 | while ((i= get_instr(src))) |
| 3090 | { |
| 3091 | if (! i->marked) |
| 3092 | { |
| 3093 | delete i; |
| 3094 | src+= 1; |
| 3095 | } |
| 3096 | else |
| 3097 | { |
| 3098 | if (src != dst) |
| 3099 | { |
| 3100 | /* Move the instruction and update prev. jumps */ |
| 3101 | sp_instr *ibp; |
| 3102 | List_iterator_fast<sp_instr> li(bp); |
| 3103 | |
| 3104 | set_dynamic(&m_instr, (uchar*)&i, dst); |
| 3105 | while ((ibp= li++)) |
| 3106 | { |
| 3107 | sp_instr_opt_meta *im= static_cast<sp_instr_opt_meta *>(ibp); |
| 3108 | im->set_destination(src, dst); |
| 3109 | } |
| 3110 | } |
| 3111 | i->opt_move(dst, &bp); |
| 3112 | src+= 1; |
| 3113 | dst+= 1; |
| 3114 | } |
| 3115 | } |
| 3116 | m_instr.elements= dst; |
| 3117 | bp.empty(); |
| 3118 | } |
| 3119 | |
| 3120 | void sp_head::add_mark_lead(uint ip, List<sp_instr> *leads) |
| 3121 | { |
| 3122 | sp_instr *i= get_instr(ip); |
| 3123 | |
| 3124 | if (i && ! i->marked) |
| 3125 | leads->push_front(i); |
| 3126 | } |
| 3127 | |
| 3128 | void |
| 3129 | sp_head::opt_mark() |
| 3130 | { |
| 3131 | uint ip; |
| 3132 | sp_instr *i; |
| 3133 | List<sp_instr> leads; |
| 3134 | |
| 3135 | /* |
| 3136 | Forward flow analysis algorithm in the instruction graph: |
| 3137 | - first, add the entry point in the graph (the first instruction) to the |
| 3138 | 'leads' list of paths to explore. |
| 3139 | - while there are still leads to explore: |
| 3140 | - pick one lead, and follow the path forward. Mark instruction reached. |
| 3141 | Stop only if the end of the routine is reached, or the path converge |
| 3142 | to code already explored (marked). |
| 3143 | - while following a path, collect in the 'leads' list any fork to |
| 3144 | another path (caused by conditional jumps instructions), so that these |
| 3145 | paths can be explored as well. |
| 3146 | */ |
| 3147 | |
| 3148 | /* Add the entry point */ |
| 3149 | i= get_instr(0); |
| 3150 | leads.push_front(i); |
| 3151 | |
| 3152 | /* For each path of code ... */ |
| 3153 | while (leads.elements != 0) |
| 3154 | { |
| 3155 | i= leads.pop(); |
| 3156 | |
| 3157 | /* Mark the entire path, collecting new leads. */ |
| 3158 | while (i && ! i->marked) |
| 3159 | { |
| 3160 | ip= i->opt_mark(this, & leads); |
| 3161 | i= get_instr(ip); |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | |
| 3167 | #ifndef DBUG_OFF |
| 3168 | /** |
| 3169 | Return the routine instructions as a result set. |
| 3170 | @return |
| 3171 | 0 if ok, !=0 on error. |
| 3172 | */ |
| 3173 | |
| 3174 | int |
| 3175 | sp_head::show_routine_code(THD *thd) |
| 3176 | { |
| 3177 | Protocol *protocol= thd->protocol; |
| 3178 | char buff[2048]; |
| 3179 | String buffer(buff, sizeof(buff), system_charset_info); |
| 3180 | List<Item> field_list; |
| 3181 | sp_instr *i; |
| 3182 | bool full_access; |
| 3183 | int res= 0; |
| 3184 | uint ip; |
| 3185 | DBUG_ENTER("sp_head::show_routine_code" ); |
| 3186 | DBUG_PRINT("info" , ("procedure: %s" , m_name.str)); |
| 3187 | |
| 3188 | if (check_show_routine_access(thd, this, &full_access) || !full_access) |
| 3189 | DBUG_RETURN(1); |
| 3190 | |
| 3191 | field_list.push_back(new (thd->mem_root) Item_uint(thd, "Pos" , 9), |
| 3192 | thd->mem_root); |
| 3193 | // 1024 is for not to confuse old clients |
| 3194 | field_list.push_back(new (thd->mem_root) |
| 3195 | Item_empty_string(thd, "Instruction" , |
| 3196 | MY_MAX(buffer.length(), 1024)), |
| 3197 | thd->mem_root); |
| 3198 | if (protocol->send_result_set_metadata(&field_list, Protocol::SEND_NUM_ROWS | |
| 3199 | Protocol::SEND_EOF)) |
| 3200 | DBUG_RETURN(1); |
| 3201 | |
| 3202 | for (ip= 0; (i = get_instr(ip)) ; ip++) |
| 3203 | { |
| 3204 | /* |
| 3205 | Consistency check. If these are different something went wrong |
| 3206 | during optimization. |
| 3207 | */ |
| 3208 | if (ip != i->m_ip) |
| 3209 | { |
| 3210 | const char *format= "Instruction at position %u has m_ip=%u" ; |
| 3211 | char tmp[sizeof(format) + 2*SP_INSTR_UINT_MAXLEN + 1]; |
| 3212 | |
| 3213 | sprintf(tmp, format, ip, i->m_ip); |
| 3214 | /* |
| 3215 | Since this is for debugging purposes only, we don't bother to |
| 3216 | introduce a special error code for it. |
| 3217 | */ |
| 3218 | push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR, tmp); |
| 3219 | } |
| 3220 | protocol->prepare_for_resend(); |
| 3221 | protocol->store_long(ip); |
| 3222 | |
| 3223 | buffer.set("" , 0, system_charset_info); |
| 3224 | i->print(&buffer); |
| 3225 | protocol->store(buffer.ptr(), buffer.length(), system_charset_info); |
| 3226 | if ((res= protocol->write())) |
| 3227 | break; |
| 3228 | } |
| 3229 | |
| 3230 | if (!res) |
| 3231 | my_eof(thd); |
| 3232 | |
| 3233 | DBUG_RETURN(res); |
| 3234 | } |
| 3235 | #endif // ifndef DBUG_OFF |
| 3236 | |
| 3237 | |
| 3238 | /** |
| 3239 | Prepare LEX and thread for execution of instruction, if requested open |
| 3240 | and lock LEX's tables, execute instruction's core function, perform |
| 3241 | cleanup afterwards. |
| 3242 | |
| 3243 | @param thd thread context |
| 3244 | @param nextp out - next instruction |
| 3245 | @param open_tables if TRUE then check read access to tables in LEX's table |
| 3246 | list and open and lock them (used in instructions which |
| 3247 | need to calculate some expression and don't execute |
| 3248 | complete statement). |
| 3249 | @param sp_instr instruction for which we prepare context, and which core |
| 3250 | function execute by calling its exec_core() method. |
| 3251 | |
| 3252 | @note |
| 3253 | We are not saving/restoring some parts of THD which may need this because |
| 3254 | we do this once for whole routine execution in sp_head::execute(). |
| 3255 | |
| 3256 | @return |
| 3257 | 0/non-0 - Success/Failure |
| 3258 | */ |
| 3259 | |
| 3260 | int |
| 3261 | sp_lex_keeper::reset_lex_and_exec_core(THD *thd, uint *nextp, |
| 3262 | bool open_tables, sp_instr* instr) |
| 3263 | { |
| 3264 | int res= 0; |
| 3265 | DBUG_ENTER("reset_lex_and_exec_core" ); |
| 3266 | |
| 3267 | /* |
| 3268 | The flag is saved at the entry to the following substatement. |
| 3269 | It's reset further in the common code part. |
| 3270 | It's merged with the saved parent's value at the exit of this func. |
| 3271 | */ |
| 3272 | bool parent_modified_non_trans_table= thd->transaction.stmt.modified_non_trans_table; |
| 3273 | thd->transaction.stmt.modified_non_trans_table= FALSE; |
| 3274 | DBUG_ASSERT(!thd->derived_tables); |
| 3275 | DBUG_ASSERT(thd->Item_change_list::is_empty()); |
| 3276 | /* |
| 3277 | Use our own lex. |
| 3278 | We should not save old value since it is saved/restored in |
| 3279 | sp_head::execute() when we are entering/leaving routine. |
| 3280 | */ |
| 3281 | thd->lex= m_lex; |
| 3282 | |
| 3283 | thd->set_query_id(next_query_id()); |
| 3284 | |
| 3285 | if (thd->locked_tables_mode <= LTM_LOCK_TABLES) |
| 3286 | { |
| 3287 | /* |
| 3288 | This statement will enter/leave prelocked mode on its own. |
| 3289 | Entering prelocked mode changes table list and related members |
| 3290 | of LEX, so we'll need to restore them. |
| 3291 | */ |
| 3292 | if (lex_query_tables_own_last) |
| 3293 | { |
| 3294 | /* |
| 3295 | We've already entered/left prelocked mode with this statement. |
| 3296 | Attach the list of tables that need to be prelocked and mark m_lex |
| 3297 | as having such list attached. |
| 3298 | */ |
| 3299 | *lex_query_tables_own_last= prelocking_tables; |
| 3300 | m_lex->mark_as_requiring_prelocking(lex_query_tables_own_last); |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | reinit_stmt_before_use(thd, m_lex); |
| 3305 | |
| 3306 | #ifndef EMBEDDED_LIBRARY |
| 3307 | /* |
| 3308 | If there was instruction which changed tracking state, |
| 3309 | the result of changed tracking state send to client in OK packed. |
| 3310 | So it changes result sent to client and probably can be different |
| 3311 | independent on query text. So we can't cache such results. |
| 3312 | */ |
| 3313 | if ((thd->client_capabilities & CLIENT_SESSION_TRACK) && |
| 3314 | (thd->server_status & SERVER_SESSION_STATE_CHANGED)) |
| 3315 | thd->lex->safe_to_cache_query= 0; |
| 3316 | #endif |
| 3317 | |
| 3318 | if (open_tables) |
| 3319 | res= instr->exec_open_and_lock_tables(thd, m_lex->query_tables); |
| 3320 | |
| 3321 | if (likely(!res)) |
| 3322 | { |
| 3323 | res= instr->exec_core(thd, nextp); |
| 3324 | DBUG_PRINT("info" ,("exec_core returned: %d" , res)); |
| 3325 | } |
| 3326 | |
| 3327 | /* |
| 3328 | Call after unit->cleanup() to close open table |
| 3329 | key read. |
| 3330 | */ |
| 3331 | if (open_tables) |
| 3332 | { |
| 3333 | m_lex->unit.cleanup(); |
| 3334 | /* Here we also commit or rollback the current statement. */ |
| 3335 | if (! thd->in_sub_stmt) |
| 3336 | { |
| 3337 | thd->get_stmt_da()->set_overwrite_status(true); |
| 3338 | thd->is_error() ? trans_rollback_stmt(thd) : trans_commit_stmt(thd); |
| 3339 | thd->get_stmt_da()->set_overwrite_status(false); |
| 3340 | } |
| 3341 | close_thread_tables(thd); |
| 3342 | thd_proc_info(thd, 0); |
| 3343 | |
| 3344 | if (! thd->in_sub_stmt) |
| 3345 | { |
| 3346 | if (thd->transaction_rollback_request) |
| 3347 | { |
| 3348 | trans_rollback_implicit(thd); |
| 3349 | thd->mdl_context.release_transactional_locks(); |
| 3350 | } |
| 3351 | else if (! thd->in_multi_stmt_transaction_mode()) |
| 3352 | thd->mdl_context.release_transactional_locks(); |
| 3353 | else |
| 3354 | thd->mdl_context.release_statement_locks(); |
| 3355 | } |
| 3356 | } |
| 3357 | //TODO: why is this here if log_slow_query is in sp_instr_stmt_execute? |
| 3358 | delete_explain_query(m_lex); |
| 3359 | |
| 3360 | if (m_lex->query_tables_own_last) |
| 3361 | { |
| 3362 | /* |
| 3363 | We've entered and left prelocking mode when executing statement |
| 3364 | stored in m_lex. |
| 3365 | m_lex->query_tables(->next_global)* list now has a 'tail' - a list |
| 3366 | of tables that are added for prelocking. (If this is the first |
| 3367 | execution, the 'tail' was added by open_tables(), otherwise we've |
| 3368 | attached it above in this function). |
| 3369 | Now we'll save the 'tail', and detach it. |
| 3370 | */ |
| 3371 | lex_query_tables_own_last= m_lex->query_tables_own_last; |
| 3372 | prelocking_tables= *lex_query_tables_own_last; |
| 3373 | *lex_query_tables_own_last= NULL; |
| 3374 | m_lex->mark_as_requiring_prelocking(NULL); |
| 3375 | } |
| 3376 | thd->rollback_item_tree_changes(); |
| 3377 | /* |
| 3378 | Update the state of the active arena if no errors on |
| 3379 | open_tables stage. |
| 3380 | */ |
| 3381 | if (likely(!res) || likely(!thd->is_error()) || |
| 3382 | (thd->get_stmt_da()->sql_errno() != ER_CANT_REOPEN_TABLE && |
| 3383 | thd->get_stmt_da()->sql_errno() != ER_NO_SUCH_TABLE && |
| 3384 | thd->get_stmt_da()->sql_errno() != ER_NO_SUCH_TABLE_IN_ENGINE && |
| 3385 | thd->get_stmt_da()->sql_errno() != ER_UPDATE_TABLE_USED)) |
| 3386 | thd->stmt_arena->state= Query_arena::STMT_EXECUTED; |
| 3387 | |
| 3388 | /* |
| 3389 | Merge here with the saved parent's values |
| 3390 | what is needed from the substatement gained |
| 3391 | */ |
| 3392 | thd->transaction.stmt.modified_non_trans_table |= parent_modified_non_trans_table; |
| 3393 | |
| 3394 | TRANSACT_TRACKER(add_trx_state_from_thd(thd)); |
| 3395 | |
| 3396 | /* |
| 3397 | Unlike for PS we should not call Item's destructors for newly created |
| 3398 | items after execution of each instruction in stored routine. This is |
| 3399 | because SP often create Item (like Item_int, Item_string etc...) when |
| 3400 | they want to store some value in local variable, pass return value and |
| 3401 | etc... So their life time should be longer than one instruction. |
| 3402 | |
| 3403 | cleanup_items() is called in sp_head::execute() |
| 3404 | */ |
| 3405 | thd->lex->restore_set_statement_var(); |
| 3406 | DBUG_RETURN(res || thd->is_error()); |
| 3407 | } |
| 3408 | |
| 3409 | |
| 3410 | int sp_lex_keeper::cursor_reset_lex_and_exec_core(THD *thd, uint *nextp, |
| 3411 | bool open_tables, |
| 3412 | sp_instr *instr) |
| 3413 | { |
| 3414 | Query_arena *old_arena= thd->stmt_arena; |
| 3415 | /* |
| 3416 | Get the Query_arena from the cursor statement LEX, which contains |
| 3417 | the free_list of the query, so new items (if any) are stored in |
| 3418 | the right free_list, and we can cleanup after each cursor operation, |
| 3419 | e.g. open or cursor_copy_struct (for cursor%ROWTYPE variables). |
| 3420 | */ |
| 3421 | thd->stmt_arena= m_lex->query_arena(); |
| 3422 | int res= reset_lex_and_exec_core(thd, nextp, open_tables, instr); |
| 3423 | cleanup_items(thd->stmt_arena->free_list); |
| 3424 | thd->stmt_arena= old_arena; |
| 3425 | return res; |
| 3426 | } |
| 3427 | |
| 3428 | |
| 3429 | /* |
| 3430 | sp_instr class functions |
| 3431 | */ |
| 3432 | |
| 3433 | int sp_instr::exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables) |
| 3434 | { |
| 3435 | int result; |
| 3436 | |
| 3437 | /* |
| 3438 | Check whenever we have access to tables for this statement |
| 3439 | and open and lock them before executing instructions core function. |
| 3440 | */ |
| 3441 | if (thd->open_temporary_tables(tables) || |
| 3442 | check_table_access(thd, SELECT_ACL, tables, FALSE, UINT_MAX, FALSE) |
| 3443 | || open_and_lock_tables(thd, tables, TRUE, 0)) |
| 3444 | result= -1; |
| 3445 | else |
| 3446 | result= 0; |
| 3447 | /* Prepare all derived tables/views to catch possible errors. */ |
| 3448 | if (!result) |
| 3449 | result= mysql_handle_derived(thd->lex, DT_PREPARE) ? -1 : 0; |
| 3450 | |
| 3451 | return result; |
| 3452 | } |
| 3453 | |
| 3454 | uint sp_instr::get_cont_dest() const |
| 3455 | { |
| 3456 | return (m_ip+1); |
| 3457 | } |
| 3458 | |
| 3459 | |
| 3460 | int sp_instr::exec_core(THD *thd, uint *nextp) |
| 3461 | { |
| 3462 | DBUG_ASSERT(0); |
| 3463 | return 0; |
| 3464 | } |
| 3465 | |
| 3466 | /* |
| 3467 | sp_instr_stmt class functions |
| 3468 | */ |
| 3469 | |
| 3470 | int |
| 3471 | sp_instr_stmt::execute(THD *thd, uint *nextp) |
| 3472 | { |
| 3473 | int res; |
| 3474 | bool save_enable_slow_log; |
| 3475 | const CSET_STRING query_backup= thd->query_string; |
| 3476 | QUERY_START_TIME_INFO time_info; |
| 3477 | Sub_statement_state backup_state; |
| 3478 | DBUG_ENTER("sp_instr_stmt::execute" ); |
| 3479 | DBUG_PRINT("info" , ("command: %d" , m_lex_keeper.sql_command())); |
| 3480 | |
| 3481 | #if defined(ENABLED_PROFILING) |
| 3482 | /* This s-p instr is profilable and will be captured. */ |
| 3483 | thd->profiling.set_query_source(m_query.str, m_query.length); |
| 3484 | #endif |
| 3485 | |
| 3486 | if ((save_enable_slow_log= thd->enable_slow_log)) |
| 3487 | { |
| 3488 | /* |
| 3489 | Save start time info for the CALL statement and overwrite it with the |
| 3490 | current time for log_slow_statement() to log the individual query timing. |
| 3491 | */ |
| 3492 | thd->backup_query_start_time(&time_info); |
| 3493 | thd->set_time(); |
| 3494 | } |
| 3495 | thd->store_slow_query_state(&backup_state); |
| 3496 | |
| 3497 | if (!(res= alloc_query(thd, m_query.str, m_query.length)) && |
| 3498 | !(res=subst_spvars(thd, this, &m_query))) |
| 3499 | { |
| 3500 | /* |
| 3501 | (the order of query cache and subst_spvars calls is irrelevant because |
| 3502 | queries with SP vars can't be cached) |
| 3503 | */ |
| 3504 | general_log_write(thd, COM_QUERY, thd->query(), thd->query_length()); |
| 3505 | |
| 3506 | if (query_cache_send_result_to_client(thd, thd->query(), |
| 3507 | thd->query_length()) <= 0) |
| 3508 | { |
| 3509 | thd->reset_slow_query_state(); |
| 3510 | res= m_lex_keeper.reset_lex_and_exec_core(thd, nextp, FALSE, this); |
| 3511 | bool log_slow= !res && thd->enable_slow_log; |
| 3512 | |
| 3513 | /* Finalize server status flags after executing a statement. */ |
| 3514 | if (log_slow || thd->get_stmt_da()->is_eof()) |
| 3515 | thd->update_server_status(); |
| 3516 | |
| 3517 | if (thd->get_stmt_da()->is_eof()) |
| 3518 | thd->protocol->end_statement(); |
| 3519 | |
| 3520 | query_cache_end_of_result(thd); |
| 3521 | |
| 3522 | mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_STATUS, |
| 3523 | thd->get_stmt_da()->is_error() ? |
| 3524 | thd->get_stmt_da()->sql_errno() : 0, |
| 3525 | command_name[COM_QUERY].str); |
| 3526 | |
| 3527 | if (log_slow) |
| 3528 | log_slow_statement(thd); |
| 3529 | |
| 3530 | /* |
| 3531 | Restore enable_slow_log, that can be changed by a admin or call |
| 3532 | command |
| 3533 | */ |
| 3534 | thd->enable_slow_log= save_enable_slow_log; |
| 3535 | |
| 3536 | /* Add the number of rows to thd for the 'call' statistics */ |
| 3537 | thd->add_slow_query_state(&backup_state); |
| 3538 | } |
| 3539 | else |
| 3540 | { |
| 3541 | /* change statistics */ |
| 3542 | enum_sql_command save_sql_command= thd->lex->sql_command; |
| 3543 | thd->lex->sql_command= SQLCOM_SELECT; |
| 3544 | status_var_increment(thd->status_var.com_stat[SQLCOM_SELECT]); |
| 3545 | thd->update_stats(); |
| 3546 | thd->lex->sql_command= save_sql_command; |
| 3547 | *nextp= m_ip+1; |
| 3548 | } |
| 3549 | thd->set_query(query_backup); |
| 3550 | thd->query_name_consts= 0; |
| 3551 | |
| 3552 | if (likely(!thd->is_error())) |
| 3553 | { |
| 3554 | res= 0; |
| 3555 | thd->get_stmt_da()->reset_diagnostics_area(); |
| 3556 | } |
| 3557 | } |
| 3558 | /* Restore the original query start time */ |
| 3559 | if (thd->enable_slow_log) |
| 3560 | thd->restore_query_start_time(&time_info); |
| 3561 | |
| 3562 | DBUG_RETURN(res || thd->is_error()); |
| 3563 | } |
| 3564 | |
| 3565 | |
| 3566 | void |
| 3567 | sp_instr_stmt::print(String *str) |
| 3568 | { |
| 3569 | size_t i, len; |
| 3570 | |
| 3571 | /* stmt CMD "..." */ |
| 3572 | if (str->reserve(SP_STMT_PRINT_MAXLEN+SP_INSTR_UINT_MAXLEN+8)) |
| 3573 | return; |
| 3574 | str->qs_append(STRING_WITH_LEN("stmt " )); |
| 3575 | str->qs_append((uint)m_lex_keeper.sql_command()); |
| 3576 | str->qs_append(STRING_WITH_LEN(" \"" )); |
| 3577 | len= m_query.length; |
| 3578 | /* |
| 3579 | Print the query string (but not too much of it), just to indicate which |
| 3580 | statement it is. |
| 3581 | */ |
| 3582 | if (len > SP_STMT_PRINT_MAXLEN) |
| 3583 | len= SP_STMT_PRINT_MAXLEN-3; |
| 3584 | /* Copy the query string and replace '\n' with ' ' in the process */ |
| 3585 | for (i= 0 ; i < len ; i++) |
| 3586 | { |
| 3587 | char c= m_query.str[i]; |
| 3588 | if (c == '\n') |
| 3589 | c= ' '; |
| 3590 | str->qs_append(c); |
| 3591 | } |
| 3592 | if (m_query.length > SP_STMT_PRINT_MAXLEN) |
| 3593 | str->qs_append(STRING_WITH_LEN("..." )); /* Indicate truncated string */ |
| 3594 | str->qs_append('"'); |
| 3595 | } |
| 3596 | |
| 3597 | |
| 3598 | int |
| 3599 | sp_instr_stmt::exec_core(THD *thd, uint *nextp) |
| 3600 | { |
| 3601 | MYSQL_QUERY_EXEC_START(thd->query(), |
| 3602 | thd->thread_id, |
| 3603 | thd->get_db(), |
| 3604 | &thd->security_ctx->priv_user[0], |
| 3605 | (char *)thd->security_ctx->host_or_ip, |
| 3606 | 3); |
| 3607 | int res= mysql_execute_command(thd); |
| 3608 | MYSQL_QUERY_EXEC_DONE(res); |
| 3609 | *nextp= m_ip+1; |
| 3610 | return res; |
| 3611 | } |
| 3612 | |
| 3613 | |
| 3614 | /* |
| 3615 | sp_instr_set class functions |
| 3616 | */ |
| 3617 | |
| 3618 | int |
| 3619 | sp_instr_set::execute(THD *thd, uint *nextp) |
| 3620 | { |
| 3621 | DBUG_ENTER("sp_instr_set::execute" ); |
| 3622 | DBUG_PRINT("info" , ("offset: %u" , m_offset)); |
| 3623 | |
| 3624 | DBUG_RETURN(m_lex_keeper.reset_lex_and_exec_core(thd, nextp, TRUE, this)); |
| 3625 | } |
| 3626 | |
| 3627 | |
| 3628 | sp_rcontext *sp_instr_set::get_rcontext(THD *thd) const |
| 3629 | { |
| 3630 | return m_rcontext_handler->get_rcontext(thd->spcont); |
| 3631 | } |
| 3632 | |
| 3633 | |
| 3634 | int |
| 3635 | sp_instr_set::exec_core(THD *thd, uint *nextp) |
| 3636 | { |
| 3637 | int res= get_rcontext(thd)->set_variable(thd, m_offset, &m_value); |
| 3638 | delete_explain_query(thd->lex); |
| 3639 | *nextp = m_ip+1; |
| 3640 | return res; |
| 3641 | } |
| 3642 | |
| 3643 | void |
| 3644 | sp_instr_set::print(String *str) |
| 3645 | { |
| 3646 | /* set name@offset ... */ |
| 3647 | size_t rsrv = SP_INSTR_UINT_MAXLEN+6; |
| 3648 | sp_variable *var = m_ctx->find_variable(m_offset); |
| 3649 | const LEX_CSTRING *prefix= m_rcontext_handler->get_name_prefix(); |
| 3650 | |
| 3651 | /* 'var' should always be non-null, but just in case... */ |
| 3652 | if (var) |
| 3653 | rsrv+= var->name.length + prefix->length; |
| 3654 | if (str->reserve(rsrv)) |
| 3655 | return; |
| 3656 | str->qs_append(STRING_WITH_LEN("set " )); |
| 3657 | str->qs_append(prefix->str, prefix->length); |
| 3658 | if (var) |
| 3659 | { |
| 3660 | str->qs_append(&var->name); |
| 3661 | str->qs_append('@'); |
| 3662 | } |
| 3663 | str->qs_append(m_offset); |
| 3664 | str->qs_append(' '); |
| 3665 | m_value->print(str, enum_query_type(QT_ORDINARY | |
| 3666 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 3667 | } |
| 3668 | |
| 3669 | |
| 3670 | /* |
| 3671 | sp_instr_set_field class functions |
| 3672 | */ |
| 3673 | |
| 3674 | int |
| 3675 | sp_instr_set_row_field::exec_core(THD *thd, uint *nextp) |
| 3676 | { |
| 3677 | int res= get_rcontext(thd)->set_variable_row_field(thd, m_offset, |
| 3678 | m_field_offset, |
| 3679 | &m_value); |
| 3680 | delete_explain_query(thd->lex); |
| 3681 | *nextp= m_ip + 1; |
| 3682 | return res; |
| 3683 | } |
| 3684 | |
| 3685 | |
| 3686 | void |
| 3687 | sp_instr_set_row_field::print(String *str) |
| 3688 | { |
| 3689 | /* set name@offset[field_offset] ... */ |
| 3690 | size_t rsrv= SP_INSTR_UINT_MAXLEN + 6 + 6 + 3; |
| 3691 | sp_variable *var= m_ctx->find_variable(m_offset); |
| 3692 | const LEX_CSTRING *prefix= m_rcontext_handler->get_name_prefix(); |
| 3693 | DBUG_ASSERT(var); |
| 3694 | DBUG_ASSERT(var->field_def.is_row()); |
| 3695 | const Column_definition *def= |
| 3696 | var->field_def.row_field_definitions()->elem(m_field_offset); |
| 3697 | DBUG_ASSERT(def); |
| 3698 | |
| 3699 | rsrv+= var->name.length + def->field_name.length + prefix->length; |
| 3700 | if (str->reserve(rsrv)) |
| 3701 | return; |
| 3702 | str->qs_append(STRING_WITH_LEN("set " )); |
| 3703 | str->qs_append(prefix); |
| 3704 | str->qs_append(&var->name); |
| 3705 | str->qs_append('.'); |
| 3706 | str->qs_append(&def->field_name); |
| 3707 | str->qs_append('@'); |
| 3708 | str->qs_append(m_offset); |
| 3709 | str->qs_append('['); |
| 3710 | str->qs_append(m_field_offset); |
| 3711 | str->qs_append(']'); |
| 3712 | str->qs_append(' '); |
| 3713 | m_value->print(str, enum_query_type(QT_ORDINARY | |
| 3714 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 3715 | } |
| 3716 | |
| 3717 | |
| 3718 | /* |
| 3719 | sp_instr_set_field_by_name class functions |
| 3720 | */ |
| 3721 | |
| 3722 | int |
| 3723 | sp_instr_set_row_field_by_name::exec_core(THD *thd, uint *nextp) |
| 3724 | { |
| 3725 | int res= get_rcontext(thd)->set_variable_row_field_by_name(thd, m_offset, |
| 3726 | m_field_name, |
| 3727 | &m_value); |
| 3728 | delete_explain_query(thd->lex); |
| 3729 | *nextp= m_ip + 1; |
| 3730 | return res; |
| 3731 | } |
| 3732 | |
| 3733 | |
| 3734 | void |
| 3735 | sp_instr_set_row_field_by_name::print(String *str) |
| 3736 | { |
| 3737 | /* set name.field@offset["field"] ... */ |
| 3738 | size_t rsrv= SP_INSTR_UINT_MAXLEN + 6 + 6 + 3 + 2; |
| 3739 | sp_variable *var= m_ctx->find_variable(m_offset); |
| 3740 | const LEX_CSTRING *prefix= m_rcontext_handler->get_name_prefix(); |
| 3741 | DBUG_ASSERT(var); |
| 3742 | DBUG_ASSERT(var->field_def.is_table_rowtype_ref() || |
| 3743 | var->field_def.is_cursor_rowtype_ref()); |
| 3744 | |
| 3745 | rsrv+= var->name.length + 2 * m_field_name.length + prefix->length; |
| 3746 | if (str->reserve(rsrv)) |
| 3747 | return; |
| 3748 | str->qs_append(STRING_WITH_LEN("set " )); |
| 3749 | str->qs_append(prefix); |
| 3750 | str->qs_append(&var->name); |
| 3751 | str->qs_append('.'); |
| 3752 | str->qs_append(&m_field_name); |
| 3753 | str->qs_append('@'); |
| 3754 | str->qs_append(m_offset); |
| 3755 | str->qs_append("[\"" ,2); |
| 3756 | str->qs_append(&m_field_name); |
| 3757 | str->qs_append("\"]" ,2); |
| 3758 | str->qs_append(' '); |
| 3759 | m_value->print(str, enum_query_type(QT_ORDINARY | |
| 3760 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 3761 | } |
| 3762 | |
| 3763 | |
| 3764 | /* |
| 3765 | sp_instr_set_trigger_field class functions |
| 3766 | */ |
| 3767 | |
| 3768 | int |
| 3769 | sp_instr_set_trigger_field::execute(THD *thd, uint *nextp) |
| 3770 | { |
| 3771 | DBUG_ENTER("sp_instr_set_trigger_field::execute" ); |
| 3772 | thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL; |
| 3773 | DBUG_RETURN(m_lex_keeper.reset_lex_and_exec_core(thd, nextp, TRUE, this)); |
| 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | int |
| 3778 | sp_instr_set_trigger_field::exec_core(THD *thd, uint *nextp) |
| 3779 | { |
| 3780 | bool sav_abort_on_warning= thd->abort_on_warning; |
| 3781 | thd->abort_on_warning= thd->is_strict_mode() && !thd->lex->ignore; |
| 3782 | const int res= (trigger_field->set_value(thd, &value) ? -1 : 0); |
| 3783 | thd->abort_on_warning= sav_abort_on_warning; |
| 3784 | *nextp = m_ip+1; |
| 3785 | return res; |
| 3786 | } |
| 3787 | |
| 3788 | void |
| 3789 | sp_instr_set_trigger_field::print(String *str) |
| 3790 | { |
| 3791 | str->append(STRING_WITH_LEN("set_trigger_field " )); |
| 3792 | trigger_field->print(str, enum_query_type(QT_ORDINARY | |
| 3793 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 3794 | str->append(STRING_WITH_LEN(":=" )); |
| 3795 | value->print(str, enum_query_type(QT_ORDINARY | |
| 3796 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 3797 | } |
| 3798 | |
| 3799 | /* |
| 3800 | sp_instr_opt_meta |
| 3801 | */ |
| 3802 | |
| 3803 | uint sp_instr_opt_meta::get_cont_dest() const |
| 3804 | { |
| 3805 | return m_cont_dest; |
| 3806 | } |
| 3807 | |
| 3808 | |
| 3809 | /* |
| 3810 | sp_instr_jump class functions |
| 3811 | */ |
| 3812 | |
| 3813 | int |
| 3814 | sp_instr_jump::execute(THD *thd, uint *nextp) |
| 3815 | { |
| 3816 | DBUG_ENTER("sp_instr_jump::execute" ); |
| 3817 | DBUG_PRINT("info" , ("destination: %u" , m_dest)); |
| 3818 | |
| 3819 | *nextp= m_dest; |
| 3820 | DBUG_RETURN(0); |
| 3821 | } |
| 3822 | |
| 3823 | void |
| 3824 | sp_instr_jump::print(String *str) |
| 3825 | { |
| 3826 | /* jump dest */ |
| 3827 | if (str->reserve(SP_INSTR_UINT_MAXLEN+5)) |
| 3828 | return; |
| 3829 | str->qs_append(STRING_WITH_LEN("jump " )); |
| 3830 | str->qs_append(m_dest); |
| 3831 | } |
| 3832 | |
| 3833 | uint |
| 3834 | sp_instr_jump::opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 3835 | { |
| 3836 | m_dest= opt_shortcut_jump(sp, this); |
| 3837 | if (m_dest != m_ip+1) /* Jumping to following instruction? */ |
| 3838 | marked= 1; |
| 3839 | m_optdest= sp->get_instr(m_dest); |
| 3840 | return m_dest; |
| 3841 | } |
| 3842 | |
| 3843 | uint |
| 3844 | sp_instr_jump::opt_shortcut_jump(sp_head *sp, sp_instr *start) |
| 3845 | { |
| 3846 | uint dest= m_dest; |
| 3847 | sp_instr *i; |
| 3848 | |
| 3849 | while ((i= sp->get_instr(dest))) |
| 3850 | { |
| 3851 | uint ndest; |
| 3852 | |
| 3853 | if (start == i || this == i) |
| 3854 | break; |
| 3855 | ndest= i->opt_shortcut_jump(sp, start); |
| 3856 | if (ndest == dest) |
| 3857 | break; |
| 3858 | dest= ndest; |
| 3859 | } |
| 3860 | return dest; |
| 3861 | } |
| 3862 | |
| 3863 | void |
| 3864 | sp_instr_jump::opt_move(uint dst, List<sp_instr> *bp) |
| 3865 | { |
| 3866 | if (m_dest > m_ip) |
| 3867 | bp->push_back(this); // Forward |
| 3868 | else if (m_optdest) |
| 3869 | m_dest= m_optdest->m_ip; // Backward |
| 3870 | m_ip= dst; |
| 3871 | } |
| 3872 | |
| 3873 | |
| 3874 | /* |
| 3875 | sp_instr_jump_if_not class functions |
| 3876 | */ |
| 3877 | |
| 3878 | int |
| 3879 | sp_instr_jump_if_not::execute(THD *thd, uint *nextp) |
| 3880 | { |
| 3881 | DBUG_ENTER("sp_instr_jump_if_not::execute" ); |
| 3882 | DBUG_PRINT("info" , ("destination: %u" , m_dest)); |
| 3883 | DBUG_RETURN(m_lex_keeper.reset_lex_and_exec_core(thd, nextp, TRUE, this)); |
| 3884 | } |
| 3885 | |
| 3886 | |
| 3887 | int |
| 3888 | sp_instr_jump_if_not::exec_core(THD *thd, uint *nextp) |
| 3889 | { |
| 3890 | Item *it; |
| 3891 | int res; |
| 3892 | |
| 3893 | it= thd->sp_prepare_func_item(&m_expr); |
| 3894 | if (! it) |
| 3895 | { |
| 3896 | res= -1; |
| 3897 | } |
| 3898 | else |
| 3899 | { |
| 3900 | res= 0; |
| 3901 | if (! it->val_bool()) |
| 3902 | *nextp = m_dest; |
| 3903 | else |
| 3904 | *nextp = m_ip+1; |
| 3905 | } |
| 3906 | |
| 3907 | return res; |
| 3908 | } |
| 3909 | |
| 3910 | |
| 3911 | void |
| 3912 | sp_instr_jump_if_not::print(String *str) |
| 3913 | { |
| 3914 | /* jump_if_not dest(cont) ... */ |
| 3915 | if (str->reserve(2*SP_INSTR_UINT_MAXLEN+14+32)) // Add some for the expr. too |
| 3916 | return; |
| 3917 | str->qs_append(STRING_WITH_LEN("jump_if_not " )); |
| 3918 | str->qs_append(m_dest); |
| 3919 | str->qs_append('('); |
| 3920 | str->qs_append(m_cont_dest); |
| 3921 | str->qs_append(STRING_WITH_LEN(") " )); |
| 3922 | m_expr->print(str, enum_query_type(QT_ORDINARY | |
| 3923 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 3924 | } |
| 3925 | |
| 3926 | |
| 3927 | uint |
| 3928 | sp_instr_jump_if_not::opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 3929 | { |
| 3930 | sp_instr *i; |
| 3931 | |
| 3932 | marked= 1; |
| 3933 | if ((i= sp->get_instr(m_dest))) |
| 3934 | { |
| 3935 | m_dest= i->opt_shortcut_jump(sp, this); |
| 3936 | m_optdest= sp->get_instr(m_dest); |
| 3937 | } |
| 3938 | sp->add_mark_lead(m_dest, leads); |
| 3939 | if ((i= sp->get_instr(m_cont_dest))) |
| 3940 | { |
| 3941 | m_cont_dest= i->opt_shortcut_jump(sp, this); |
| 3942 | m_cont_optdest= sp->get_instr(m_cont_dest); |
| 3943 | } |
| 3944 | sp->add_mark_lead(m_cont_dest, leads); |
| 3945 | return m_ip+1; |
| 3946 | } |
| 3947 | |
| 3948 | void |
| 3949 | sp_instr_jump_if_not::opt_move(uint dst, List<sp_instr> *bp) |
| 3950 | { |
| 3951 | /* |
| 3952 | cont. destinations may point backwards after shortcutting jumps |
| 3953 | during the mark phase. If it's still pointing forwards, only |
| 3954 | push this for backpatching if sp_instr_jump::opt_move() will not |
| 3955 | do it (i.e. if the m_dest points backwards). |
| 3956 | */ |
| 3957 | if (m_cont_dest > m_ip) |
| 3958 | { // Forward |
| 3959 | if (m_dest < m_ip) |
| 3960 | bp->push_back(this); |
| 3961 | } |
| 3962 | else if (m_cont_optdest) |
| 3963 | m_cont_dest= m_cont_optdest->m_ip; // Backward |
| 3964 | /* This will take care of m_dest and m_ip */ |
| 3965 | sp_instr_jump::opt_move(dst, bp); |
| 3966 | } |
| 3967 | |
| 3968 | |
| 3969 | /* |
| 3970 | sp_instr_freturn class functions |
| 3971 | */ |
| 3972 | |
| 3973 | int |
| 3974 | sp_instr_freturn::execute(THD *thd, uint *nextp) |
| 3975 | { |
| 3976 | DBUG_ENTER("sp_instr_freturn::execute" ); |
| 3977 | DBUG_RETURN(m_lex_keeper.reset_lex_and_exec_core(thd, nextp, TRUE, this)); |
| 3978 | } |
| 3979 | |
| 3980 | |
| 3981 | int |
| 3982 | sp_instr_freturn::exec_core(THD *thd, uint *nextp) |
| 3983 | { |
| 3984 | /* |
| 3985 | RETURN is a "procedure statement" (in terms of the SQL standard). |
| 3986 | That means, Diagnostics Area should be clean before its execution. |
| 3987 | */ |
| 3988 | |
| 3989 | if (!(thd->variables.sql_mode & MODE_ORACLE)) |
| 3990 | { |
| 3991 | /* |
| 3992 | Don't clean warnings in ORACLE mode, |
| 3993 | as they are needed for SQLCODE and SQLERRM: |
| 3994 | BEGIN |
| 3995 | SELECT a INTO a FROM t1; |
| 3996 | RETURN 'No exception ' || SQLCODE || ' ' || SQLERRM; |
| 3997 | EXCEPTION WHEN NO_DATA_FOUND THEN |
| 3998 | RETURN 'Exception ' || SQLCODE || ' ' || SQLERRM; |
| 3999 | END; |
| 4000 | */ |
| 4001 | Diagnostics_area *da= thd->get_stmt_da(); |
| 4002 | da->clear_warning_info(da->warning_info_id()); |
| 4003 | } |
| 4004 | |
| 4005 | /* |
| 4006 | Change <next instruction pointer>, so that this will be the last |
| 4007 | instruction in the stored function. |
| 4008 | */ |
| 4009 | |
| 4010 | *nextp= UINT_MAX; |
| 4011 | |
| 4012 | /* |
| 4013 | Evaluate the value of return expression and store it in current runtime |
| 4014 | context. |
| 4015 | |
| 4016 | NOTE: It's necessary to evaluate result item right here, because we must |
| 4017 | do it in scope of execution the current context/block. |
| 4018 | */ |
| 4019 | |
| 4020 | return thd->spcont->set_return_value(thd, &m_value); |
| 4021 | } |
| 4022 | |
| 4023 | void |
| 4024 | sp_instr_freturn::print(String *str) |
| 4025 | { |
| 4026 | /* freturn type expr... */ |
| 4027 | if (str->reserve(1024+8+32)) // Add some for the expr. too |
| 4028 | return; |
| 4029 | str->qs_append(STRING_WITH_LEN("freturn " )); |
| 4030 | str->qs_append(m_type_handler->name().ptr()); |
| 4031 | str->qs_append(' '); |
| 4032 | m_value->print(str, enum_query_type(QT_ORDINARY | |
| 4033 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 4034 | } |
| 4035 | |
| 4036 | /* |
| 4037 | sp_instr_hpush_jump class functions |
| 4038 | */ |
| 4039 | |
| 4040 | int |
| 4041 | sp_instr_hpush_jump::execute(THD *thd, uint *nextp) |
| 4042 | { |
| 4043 | DBUG_ENTER("sp_instr_hpush_jump::execute" ); |
| 4044 | |
| 4045 | int ret= thd->spcont->push_handler(m_handler, m_ip + 1); |
| 4046 | |
| 4047 | *nextp= m_dest; |
| 4048 | |
| 4049 | DBUG_RETURN(ret); |
| 4050 | } |
| 4051 | |
| 4052 | |
| 4053 | void |
| 4054 | sp_instr_hpush_jump::print(String *str) |
| 4055 | { |
| 4056 | /* hpush_jump dest fsize type */ |
| 4057 | if (str->reserve(SP_INSTR_UINT_MAXLEN*2 + 21)) |
| 4058 | return; |
| 4059 | |
| 4060 | str->qs_append(STRING_WITH_LEN("hpush_jump " )); |
| 4061 | str->qs_append(m_dest); |
| 4062 | str->qs_append(' '); |
| 4063 | str->qs_append(m_frame); |
| 4064 | |
| 4065 | switch (m_handler->type) { |
| 4066 | case sp_handler::EXIT: |
| 4067 | str->qs_append(STRING_WITH_LEN(" EXIT" )); |
| 4068 | break; |
| 4069 | case sp_handler::CONTINUE: |
| 4070 | str->qs_append(STRING_WITH_LEN(" CONTINUE" )); |
| 4071 | break; |
| 4072 | default: |
| 4073 | // The handler type must be either CONTINUE or EXIT. |
| 4074 | DBUG_ASSERT(0); |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | |
| 4079 | uint |
| 4080 | sp_instr_hpush_jump::opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 4081 | { |
| 4082 | sp_instr *i; |
| 4083 | |
| 4084 | marked= 1; |
| 4085 | if ((i= sp->get_instr(m_dest))) |
| 4086 | { |
| 4087 | m_dest= i->opt_shortcut_jump(sp, this); |
| 4088 | m_optdest= sp->get_instr(m_dest); |
| 4089 | } |
| 4090 | sp->add_mark_lead(m_dest, leads); |
| 4091 | |
| 4092 | /* |
| 4093 | For continue handlers, all instructions in the scope of the handler |
| 4094 | are possible leads. For example, the instruction after freturn might |
| 4095 | be executed if the freturn triggers the condition handled by the |
| 4096 | continue handler. |
| 4097 | |
| 4098 | m_dest marks the start of the handler scope. It's added as a lead |
| 4099 | above, so we start on m_dest+1 here. |
| 4100 | m_opt_hpop is the hpop marking the end of the handler scope. |
| 4101 | */ |
| 4102 | if (m_handler->type == sp_handler::CONTINUE) |
| 4103 | { |
| 4104 | for (uint scope_ip= m_dest+1; scope_ip <= m_opt_hpop; scope_ip++) |
| 4105 | sp->add_mark_lead(scope_ip, leads); |
| 4106 | } |
| 4107 | |
| 4108 | return m_ip+1; |
| 4109 | } |
| 4110 | |
| 4111 | |
| 4112 | /* |
| 4113 | sp_instr_hpop class functions |
| 4114 | */ |
| 4115 | |
| 4116 | int |
| 4117 | sp_instr_hpop::execute(THD *thd, uint *nextp) |
| 4118 | { |
| 4119 | DBUG_ENTER("sp_instr_hpop::execute" ); |
| 4120 | thd->spcont->pop_handlers(m_count); |
| 4121 | *nextp= m_ip+1; |
| 4122 | DBUG_RETURN(0); |
| 4123 | } |
| 4124 | |
| 4125 | void |
| 4126 | sp_instr_hpop::print(String *str) |
| 4127 | { |
| 4128 | /* hpop count */ |
| 4129 | if (str->reserve(SP_INSTR_UINT_MAXLEN+5)) |
| 4130 | return; |
| 4131 | str->qs_append(STRING_WITH_LEN("hpop " )); |
| 4132 | str->qs_append(m_count); |
| 4133 | } |
| 4134 | |
| 4135 | |
| 4136 | /* |
| 4137 | sp_instr_hreturn class functions |
| 4138 | */ |
| 4139 | |
| 4140 | int |
| 4141 | sp_instr_hreturn::execute(THD *thd, uint *nextp) |
| 4142 | { |
| 4143 | DBUG_ENTER("sp_instr_hreturn::execute" ); |
| 4144 | |
| 4145 | uint continue_ip= thd->spcont->exit_handler(thd->get_stmt_da()); |
| 4146 | |
| 4147 | *nextp= m_dest ? m_dest : continue_ip; |
| 4148 | |
| 4149 | DBUG_RETURN(0); |
| 4150 | } |
| 4151 | |
| 4152 | |
| 4153 | void |
| 4154 | sp_instr_hreturn::print(String *str) |
| 4155 | { |
| 4156 | /* hreturn framesize dest */ |
| 4157 | if (str->reserve(SP_INSTR_UINT_MAXLEN*2 + 9)) |
| 4158 | return; |
| 4159 | str->qs_append(STRING_WITH_LEN("hreturn " )); |
| 4160 | if (m_dest) |
| 4161 | { |
| 4162 | // NOTE: this is legacy: hreturn instruction for EXIT handler |
| 4163 | // should print out 0 as frame index. |
| 4164 | str->qs_append(STRING_WITH_LEN("0 " )); |
| 4165 | str->qs_append(m_dest); |
| 4166 | } |
| 4167 | else |
| 4168 | { |
| 4169 | str->qs_append(m_frame); |
| 4170 | } |
| 4171 | } |
| 4172 | |
| 4173 | |
| 4174 | uint |
| 4175 | sp_instr_hreturn::opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 4176 | { |
| 4177 | marked= 1; |
| 4178 | |
| 4179 | if (m_dest) |
| 4180 | { |
| 4181 | /* |
| 4182 | This is an EXIT handler; next instruction step is in m_dest. |
| 4183 | */ |
| 4184 | return m_dest; |
| 4185 | } |
| 4186 | |
| 4187 | /* |
| 4188 | This is a CONTINUE handler; next instruction step will come from |
| 4189 | the handler stack and not from opt_mark. |
| 4190 | */ |
| 4191 | return UINT_MAX; |
| 4192 | } |
| 4193 | |
| 4194 | |
| 4195 | /* |
| 4196 | sp_instr_cpush class functions |
| 4197 | */ |
| 4198 | |
| 4199 | int |
| 4200 | sp_instr_cpush::execute(THD *thd, uint *nextp) |
| 4201 | { |
| 4202 | DBUG_ENTER("sp_instr_cpush::execute" ); |
| 4203 | |
| 4204 | int ret= thd->spcont->push_cursor(thd, &m_lex_keeper); |
| 4205 | |
| 4206 | *nextp= m_ip+1; |
| 4207 | |
| 4208 | DBUG_RETURN(ret); |
| 4209 | } |
| 4210 | |
| 4211 | |
| 4212 | void |
| 4213 | sp_instr_cpush::print(String *str) |
| 4214 | { |
| 4215 | const LEX_CSTRING *cursor_name= m_ctx->find_cursor(m_cursor); |
| 4216 | |
| 4217 | /* cpush name@offset */ |
| 4218 | size_t rsrv= SP_INSTR_UINT_MAXLEN+7; |
| 4219 | |
| 4220 | if (cursor_name) |
| 4221 | rsrv+= cursor_name->length; |
| 4222 | if (str->reserve(rsrv)) |
| 4223 | return; |
| 4224 | str->qs_append(STRING_WITH_LEN("cpush " )); |
| 4225 | if (cursor_name) |
| 4226 | { |
| 4227 | str->qs_append(cursor_name->str, cursor_name->length); |
| 4228 | str->qs_append('@'); |
| 4229 | } |
| 4230 | str->qs_append(m_cursor); |
| 4231 | } |
| 4232 | |
| 4233 | |
| 4234 | /* |
| 4235 | sp_instr_cpop class functions |
| 4236 | */ |
| 4237 | |
| 4238 | int |
| 4239 | sp_instr_cpop::execute(THD *thd, uint *nextp) |
| 4240 | { |
| 4241 | DBUG_ENTER("sp_instr_cpop::execute" ); |
| 4242 | thd->spcont->pop_cursors(m_count); |
| 4243 | *nextp= m_ip+1; |
| 4244 | DBUG_RETURN(0); |
| 4245 | } |
| 4246 | |
| 4247 | |
| 4248 | void |
| 4249 | sp_instr_cpop::print(String *str) |
| 4250 | { |
| 4251 | /* cpop count */ |
| 4252 | if (str->reserve(SP_INSTR_UINT_MAXLEN+5)) |
| 4253 | return; |
| 4254 | str->qs_append(STRING_WITH_LEN("cpop " )); |
| 4255 | str->qs_append(m_count); |
| 4256 | } |
| 4257 | |
| 4258 | |
| 4259 | /* |
| 4260 | sp_instr_copen class functions |
| 4261 | */ |
| 4262 | |
| 4263 | /** |
| 4264 | @todo |
| 4265 | Assert that we either have an error or a cursor |
| 4266 | */ |
| 4267 | |
| 4268 | int |
| 4269 | sp_instr_copen::execute(THD *thd, uint *nextp) |
| 4270 | { |
| 4271 | /* |
| 4272 | We don't store a pointer to the cursor in the instruction to be |
| 4273 | able to reuse the same instruction among different threads in future. |
| 4274 | */ |
| 4275 | sp_cursor *c= thd->spcont->get_cursor(m_cursor); |
| 4276 | int res; |
| 4277 | DBUG_ENTER("sp_instr_copen::execute" ); |
| 4278 | |
| 4279 | if (! c) |
| 4280 | res= -1; |
| 4281 | else |
| 4282 | { |
| 4283 | sp_lex_keeper *lex_keeper= c->get_lex_keeper(); |
| 4284 | res= lex_keeper->cursor_reset_lex_and_exec_core(thd, nextp, FALSE, this); |
| 4285 | /* TODO: Assert here that we either have an error or a cursor */ |
| 4286 | } |
| 4287 | DBUG_RETURN(res); |
| 4288 | } |
| 4289 | |
| 4290 | |
| 4291 | int |
| 4292 | sp_instr_copen::exec_core(THD *thd, uint *nextp) |
| 4293 | { |
| 4294 | sp_cursor *c= thd->spcont->get_cursor(m_cursor); |
| 4295 | int res= c->open(thd); |
| 4296 | *nextp= m_ip+1; |
| 4297 | return res; |
| 4298 | } |
| 4299 | |
| 4300 | void |
| 4301 | sp_instr_copen::print(String *str) |
| 4302 | { |
| 4303 | const LEX_CSTRING *cursor_name= m_ctx->find_cursor(m_cursor); |
| 4304 | |
| 4305 | /* copen name@offset */ |
| 4306 | size_t rsrv= SP_INSTR_UINT_MAXLEN+7; |
| 4307 | |
| 4308 | if (cursor_name) |
| 4309 | rsrv+= cursor_name->length; |
| 4310 | if (str->reserve(rsrv)) |
| 4311 | return; |
| 4312 | str->qs_append(STRING_WITH_LEN("copen " )); |
| 4313 | if (cursor_name) |
| 4314 | { |
| 4315 | str->qs_append(cursor_name->str, cursor_name->length); |
| 4316 | str->qs_append('@'); |
| 4317 | } |
| 4318 | str->qs_append(m_cursor); |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | /* |
| 4323 | sp_instr_cclose class functions |
| 4324 | */ |
| 4325 | |
| 4326 | int |
| 4327 | sp_instr_cclose::execute(THD *thd, uint *nextp) |
| 4328 | { |
| 4329 | sp_cursor *c= thd->spcont->get_cursor(m_cursor); |
| 4330 | int res; |
| 4331 | DBUG_ENTER("sp_instr_cclose::execute" ); |
| 4332 | |
| 4333 | if (! c) |
| 4334 | res= -1; |
| 4335 | else |
| 4336 | res= c->close(thd); |
| 4337 | *nextp= m_ip+1; |
| 4338 | DBUG_RETURN(res); |
| 4339 | } |
| 4340 | |
| 4341 | |
| 4342 | void |
| 4343 | sp_instr_cclose::print(String *str) |
| 4344 | { |
| 4345 | const LEX_CSTRING *cursor_name= m_ctx->find_cursor(m_cursor); |
| 4346 | |
| 4347 | /* cclose name@offset */ |
| 4348 | size_t rsrv= SP_INSTR_UINT_MAXLEN+8; |
| 4349 | |
| 4350 | if (cursor_name) |
| 4351 | rsrv+= cursor_name->length; |
| 4352 | if (str->reserve(rsrv)) |
| 4353 | return; |
| 4354 | str->qs_append(STRING_WITH_LEN("cclose " )); |
| 4355 | if (cursor_name) |
| 4356 | { |
| 4357 | str->qs_append(cursor_name->str, cursor_name->length); |
| 4358 | str->qs_append('@'); |
| 4359 | } |
| 4360 | str->qs_append(m_cursor); |
| 4361 | } |
| 4362 | |
| 4363 | |
| 4364 | /* |
| 4365 | sp_instr_cfetch class functions |
| 4366 | */ |
| 4367 | |
| 4368 | int |
| 4369 | sp_instr_cfetch::execute(THD *thd, uint *nextp) |
| 4370 | { |
| 4371 | sp_cursor *c= thd->spcont->get_cursor(m_cursor); |
| 4372 | int res; |
| 4373 | Query_arena backup_arena; |
| 4374 | DBUG_ENTER("sp_instr_cfetch::execute" ); |
| 4375 | |
| 4376 | res= c ? c->fetch(thd, &m_varlist, m_error_on_no_data) : -1; |
| 4377 | |
| 4378 | *nextp= m_ip+1; |
| 4379 | DBUG_RETURN(res); |
| 4380 | } |
| 4381 | |
| 4382 | |
| 4383 | void |
| 4384 | sp_instr_cfetch::print(String *str) |
| 4385 | { |
| 4386 | List_iterator_fast<sp_variable> li(m_varlist); |
| 4387 | sp_variable *pv; |
| 4388 | const LEX_CSTRING *cursor_name= m_ctx->find_cursor(m_cursor); |
| 4389 | |
| 4390 | /* cfetch name@offset vars... */ |
| 4391 | size_t rsrv= SP_INSTR_UINT_MAXLEN+8; |
| 4392 | |
| 4393 | if (cursor_name) |
| 4394 | rsrv+= cursor_name->length; |
| 4395 | if (str->reserve(rsrv)) |
| 4396 | return; |
| 4397 | str->qs_append(STRING_WITH_LEN("cfetch " )); |
| 4398 | if (cursor_name) |
| 4399 | { |
| 4400 | str->qs_append(cursor_name->str, cursor_name->length); |
| 4401 | str->qs_append('@'); |
| 4402 | } |
| 4403 | str->qs_append(m_cursor); |
| 4404 | while ((pv= li++)) |
| 4405 | { |
| 4406 | if (str->reserve(pv->name.length+SP_INSTR_UINT_MAXLEN+2)) |
| 4407 | return; |
| 4408 | str->qs_append(' '); |
| 4409 | str->qs_append(&pv->name); |
| 4410 | str->qs_append('@'); |
| 4411 | str->qs_append(pv->offset); |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | int |
| 4416 | sp_instr_agg_cfetch::execute(THD *thd, uint *nextp) |
| 4417 | { |
| 4418 | DBUG_ENTER("sp_instr_agg_cfetch::execute" ); |
| 4419 | int res= 0; |
| 4420 | if (!thd->spcont->instr_ptr) |
| 4421 | { |
| 4422 | *nextp= m_ip+1; |
| 4423 | thd->spcont->instr_ptr= m_ip + 1; |
| 4424 | } |
| 4425 | else if (!thd->spcont->pause_state) |
| 4426 | thd->spcont->pause_state= TRUE; |
| 4427 | else |
| 4428 | { |
| 4429 | thd->spcont->pause_state= FALSE; |
| 4430 | if (thd->server_status == SERVER_STATUS_LAST_ROW_SENT) |
| 4431 | { |
| 4432 | my_message(ER_SP_FETCH_NO_DATA, |
| 4433 | ER_THD(thd, ER_SP_FETCH_NO_DATA), MYF(0)); |
| 4434 | res= -1; |
| 4435 | thd->spcont->quit_func= TRUE; |
| 4436 | } |
| 4437 | else |
| 4438 | *nextp= m_ip + 1; |
| 4439 | } |
| 4440 | DBUG_RETURN(res); |
| 4441 | } |
| 4442 | |
| 4443 | void |
| 4444 | sp_instr_agg_cfetch::print(String *str) |
| 4445 | { |
| 4446 | |
| 4447 | uint rsrv= SP_INSTR_UINT_MAXLEN+11; |
| 4448 | |
| 4449 | if (str->reserve(rsrv)) |
| 4450 | return; |
| 4451 | str->qs_append(STRING_WITH_LEN("agg_cfetch" )); |
| 4452 | } |
| 4453 | |
| 4454 | /* |
| 4455 | sp_instr_cursor_copy_struct class functions |
| 4456 | */ |
| 4457 | |
| 4458 | /** |
| 4459 | This methods processes cursor %ROWTYPE declarations, e.g.: |
| 4460 | CURSOR cur IS SELECT * FROM t1; |
| 4461 | rec cur%ROWTYPE; |
| 4462 | and does the following: |
| 4463 | - opens the cursor without copying data (materialization). |
| 4464 | - copies the cursor structure to the associated %ROWTYPE variable. |
| 4465 | */ |
| 4466 | |
| 4467 | int |
| 4468 | sp_instr_cursor_copy_struct::exec_core(THD *thd, uint *nextp) |
| 4469 | { |
| 4470 | DBUG_ENTER("sp_instr_cursor_copy_struct::exec_core" ); |
| 4471 | int ret= 0; |
| 4472 | Item_field_row *row= (Item_field_row*) thd->spcont->get_variable(m_var); |
| 4473 | DBUG_ASSERT(row->type_handler() == &type_handler_row); |
| 4474 | |
| 4475 | /* |
| 4476 | Copy structure only once. If the cursor%ROWTYPE variable is declared |
| 4477 | inside a LOOP block, it gets its structure on the first loop interation |
| 4478 | and remembers the structure for all consequent loop iterations. |
| 4479 | It we recreated the structure on every iteration, we would get |
| 4480 | potential memory leaks, and it would be less efficient. |
| 4481 | */ |
| 4482 | if (!row->arguments()) |
| 4483 | { |
| 4484 | sp_cursor tmp(thd, &m_lex_keeper); |
| 4485 | // Open the cursor without copying data |
| 4486 | if (!(ret= tmp.open_view_structure_only(thd))) |
| 4487 | { |
| 4488 | Row_definition_list defs; |
| 4489 | if (!(ret= tmp.export_structure(thd, &defs))) |
| 4490 | { |
| 4491 | /* |
| 4492 | Create row elements on the caller arena. |
| 4493 | It's the same arena that was used during sp_rcontext::create(). |
| 4494 | This puts cursor%ROWTYPE elements on the same mem_root |
| 4495 | where explicit ROW elements and table%ROWTYPE reside. |
| 4496 | */ |
| 4497 | Query_arena current_arena; |
| 4498 | thd->set_n_backup_active_arena(thd->spcont->callers_arena, ¤t_arena); |
| 4499 | row->row_create_items(thd, &defs); |
| 4500 | thd->restore_active_arena(thd->spcont->callers_arena, ¤t_arena); |
| 4501 | } |
| 4502 | tmp.close(thd); |
| 4503 | } |
| 4504 | } |
| 4505 | *nextp= m_ip + 1; |
| 4506 | DBUG_RETURN(ret); |
| 4507 | } |
| 4508 | |
| 4509 | |
| 4510 | int |
| 4511 | sp_instr_cursor_copy_struct::execute(THD *thd, uint *nextp) |
| 4512 | { |
| 4513 | DBUG_ENTER("sp_instr_cursor_copy_struct::execute" ); |
| 4514 | int ret= m_lex_keeper.cursor_reset_lex_and_exec_core(thd, nextp, FALSE, this); |
| 4515 | DBUG_RETURN(ret); |
| 4516 | } |
| 4517 | |
| 4518 | |
| 4519 | void |
| 4520 | sp_instr_cursor_copy_struct::print(String *str) |
| 4521 | { |
| 4522 | sp_variable *var= m_ctx->find_variable(m_var); |
| 4523 | const LEX_CSTRING *name= m_lex_keeper.cursor_name(); |
| 4524 | str->append(STRING_WITH_LEN("cursor_copy_struct " )); |
| 4525 | str->append(name); |
| 4526 | str->append(' '); |
| 4527 | str->append(&var->name); |
| 4528 | str->append('@'); |
| 4529 | str->append_ulonglong(m_var); |
| 4530 | } |
| 4531 | |
| 4532 | |
| 4533 | /* |
| 4534 | sp_instr_error class functions |
| 4535 | */ |
| 4536 | |
| 4537 | int |
| 4538 | sp_instr_error::execute(THD *thd, uint *nextp) |
| 4539 | { |
| 4540 | DBUG_ENTER("sp_instr_error::execute" ); |
| 4541 | |
| 4542 | my_message(m_errcode, ER_THD(thd, m_errcode), MYF(0)); |
| 4543 | *nextp= m_ip+1; |
| 4544 | DBUG_RETURN(-1); |
| 4545 | } |
| 4546 | |
| 4547 | |
| 4548 | void |
| 4549 | sp_instr_error::print(String *str) |
| 4550 | { |
| 4551 | /* error code */ |
| 4552 | if (str->reserve(SP_INSTR_UINT_MAXLEN+6)) |
| 4553 | return; |
| 4554 | str->qs_append(STRING_WITH_LEN("error " )); |
| 4555 | str->qs_append(m_errcode); |
| 4556 | } |
| 4557 | |
| 4558 | |
| 4559 | /************************************************************************** |
| 4560 | sp_instr_set_case_expr class implementation |
| 4561 | **************************************************************************/ |
| 4562 | |
| 4563 | int |
| 4564 | sp_instr_set_case_expr::execute(THD *thd, uint *nextp) |
| 4565 | { |
| 4566 | DBUG_ENTER("sp_instr_set_case_expr::execute" ); |
| 4567 | |
| 4568 | DBUG_RETURN(m_lex_keeper.reset_lex_and_exec_core(thd, nextp, TRUE, this)); |
| 4569 | } |
| 4570 | |
| 4571 | |
| 4572 | int |
| 4573 | sp_instr_set_case_expr::exec_core(THD *thd, uint *nextp) |
| 4574 | { |
| 4575 | int res= thd->spcont->set_case_expr(thd, m_case_expr_id, &m_case_expr); |
| 4576 | |
| 4577 | if (res && !thd->spcont->get_case_expr(m_case_expr_id)) |
| 4578 | { |
| 4579 | /* |
| 4580 | Failed to evaluate the value, the case expression is still not |
| 4581 | initialized. Set to NULL so we can continue. |
| 4582 | */ |
| 4583 | |
| 4584 | Item *null_item= new (thd->mem_root) Item_null(thd); |
| 4585 | |
| 4586 | if (!null_item || |
| 4587 | thd->spcont->set_case_expr(thd, m_case_expr_id, &null_item)) |
| 4588 | { |
| 4589 | /* If this also failed, we have to abort. */ |
| 4590 | my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR)); |
| 4591 | } |
| 4592 | } |
| 4593 | else |
| 4594 | *nextp= m_ip+1; |
| 4595 | |
| 4596 | return res; |
| 4597 | } |
| 4598 | |
| 4599 | |
| 4600 | void |
| 4601 | sp_instr_set_case_expr::print(String *str) |
| 4602 | { |
| 4603 | /* set_case_expr (cont) id ... */ |
| 4604 | str->reserve(2*SP_INSTR_UINT_MAXLEN+18+32); // Add some extra for expr too |
| 4605 | str->qs_append(STRING_WITH_LEN("set_case_expr (" )); |
| 4606 | str->qs_append(m_cont_dest); |
| 4607 | str->qs_append(STRING_WITH_LEN(") " )); |
| 4608 | str->qs_append(m_case_expr_id); |
| 4609 | str->qs_append(' '); |
| 4610 | m_case_expr->print(str, enum_query_type(QT_ORDINARY | |
| 4611 | QT_ITEM_ORIGINAL_FUNC_NULLIF)); |
| 4612 | } |
| 4613 | |
| 4614 | uint |
| 4615 | sp_instr_set_case_expr::opt_mark(sp_head *sp, List<sp_instr> *leads) |
| 4616 | { |
| 4617 | sp_instr *i; |
| 4618 | |
| 4619 | marked= 1; |
| 4620 | if ((i= sp->get_instr(m_cont_dest))) |
| 4621 | { |
| 4622 | m_cont_dest= i->opt_shortcut_jump(sp, this); |
| 4623 | m_cont_optdest= sp->get_instr(m_cont_dest); |
| 4624 | } |
| 4625 | sp->add_mark_lead(m_cont_dest, leads); |
| 4626 | return m_ip+1; |
| 4627 | } |
| 4628 | |
| 4629 | void |
| 4630 | sp_instr_set_case_expr::opt_move(uint dst, List<sp_instr> *bp) |
| 4631 | { |
| 4632 | if (m_cont_dest > m_ip) |
| 4633 | bp->push_back(this); // Forward |
| 4634 | else if (m_cont_optdest) |
| 4635 | m_cont_dest= m_cont_optdest->m_ip; // Backward |
| 4636 | m_ip= dst; |
| 4637 | } |
| 4638 | |
| 4639 | |
| 4640 | /* ------------------------------------------------------------------ */ |
| 4641 | |
| 4642 | |
| 4643 | /* |
| 4644 | Structure that represent all instances of one table |
| 4645 | in optimized multi-set of tables used by routine. |
| 4646 | */ |
| 4647 | |
| 4648 | typedef struct st_sp_table |
| 4649 | { |
| 4650 | /* |
| 4651 | Multi-set key: |
| 4652 | db_name\0table_name\0alias\0 - for normal tables |
| 4653 | db_name\0table_name\0 - for temporary tables |
| 4654 | */ |
| 4655 | LEX_STRING qname; |
| 4656 | size_t db_length, table_name_length; |
| 4657 | bool temp; /* true if corresponds to a temporary table */ |
| 4658 | thr_lock_type lock_type; /* lock type used for prelocking */ |
| 4659 | uint lock_count; |
| 4660 | uint query_lock_count; |
| 4661 | uint8 trg_event_map; |
| 4662 | } SP_TABLE; |
| 4663 | |
| 4664 | |
| 4665 | uchar *sp_table_key(const uchar *ptr, size_t *plen, my_bool first) |
| 4666 | { |
| 4667 | SP_TABLE *tab= (SP_TABLE *)ptr; |
| 4668 | *plen= tab->qname.length; |
| 4669 | return (uchar *)tab->qname.str; |
| 4670 | } |
| 4671 | |
| 4672 | |
| 4673 | /** |
| 4674 | Merge the list of tables used by some query into the multi-set of |
| 4675 | tables used by routine. |
| 4676 | |
| 4677 | @param thd thread context |
| 4678 | @param table table list |
| 4679 | @param lex_for_tmp_check LEX of the query for which we are merging |
| 4680 | table list. |
| 4681 | |
| 4682 | @note |
| 4683 | This method will use LEX provided to check whenever we are creating |
| 4684 | temporary table and mark it as such in target multi-set. |
| 4685 | |
| 4686 | @retval |
| 4687 | TRUE Success |
| 4688 | @retval |
| 4689 | FALSE Error |
| 4690 | */ |
| 4691 | |
| 4692 | bool |
| 4693 | sp_head::merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check) |
| 4694 | { |
| 4695 | SP_TABLE *tab; |
| 4696 | |
| 4697 | if ((lex_for_tmp_check->sql_command == SQLCOM_DROP_TABLE || |
| 4698 | lex_for_tmp_check->sql_command == SQLCOM_DROP_SEQUENCE) && |
| 4699 | lex_for_tmp_check->tmp_table()) |
| 4700 | return TRUE; |
| 4701 | |
| 4702 | for (uint i= 0 ; i < m_sptabs.records ; i++) |
| 4703 | { |
| 4704 | tab= (SP_TABLE*) my_hash_element(&m_sptabs, i); |
| 4705 | tab->query_lock_count= 0; |
| 4706 | } |
| 4707 | |
| 4708 | for (; table ; table= table->next_global) |
| 4709 | if (!table->derived && !table->schema_table) |
| 4710 | { |
| 4711 | /* |
| 4712 | Structure of key for the multi-set is "db\0table\0alias\0". |
| 4713 | Since "alias" part can have arbitrary length we use String |
| 4714 | object to construct the key. By default String will use |
| 4715 | buffer allocated on stack with NAME_LEN bytes reserved for |
| 4716 | alias, since in most cases it is going to be smaller than |
| 4717 | NAME_LEN bytes. |
| 4718 | */ |
| 4719 | char tname_buff[(SAFE_NAME_LEN + 1) * 3]; |
| 4720 | String tname(tname_buff, sizeof(tname_buff), &my_charset_bin); |
| 4721 | uint temp_table_key_length; |
| 4722 | |
| 4723 | tname.length(0); |
| 4724 | tname.append(&table->db); |
| 4725 | tname.append('\0'); |
| 4726 | tname.append(&table->table_name); |
| 4727 | tname.append('\0'); |
| 4728 | temp_table_key_length= tname.length(); |
| 4729 | tname.append(&table->alias); |
| 4730 | tname.append('\0'); |
| 4731 | |
| 4732 | /* |
| 4733 | Upgrade the lock type because this table list will be used |
| 4734 | only in pre-locked mode, in which DELAYED inserts are always |
| 4735 | converted to normal inserts. |
| 4736 | */ |
| 4737 | if (table->lock_type == TL_WRITE_DELAYED) |
| 4738 | table->lock_type= TL_WRITE; |
| 4739 | |
| 4740 | /* |
| 4741 | We ignore alias when we check if table was already marked as temporary |
| 4742 | (and therefore should not be prelocked). Otherwise we will erroneously |
| 4743 | treat table with same name but with different alias as non-temporary. |
| 4744 | */ |
| 4745 | if ((tab= (SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname.ptr(), |
| 4746 | tname.length())) || |
| 4747 | ((tab= (SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname.ptr(), |
| 4748 | temp_table_key_length)) && |
| 4749 | tab->temp)) |
| 4750 | { |
| 4751 | if (tab->lock_type < table->lock_type) |
| 4752 | tab->lock_type= table->lock_type; // Use the table with the highest lock type |
| 4753 | tab->query_lock_count++; |
| 4754 | if (tab->query_lock_count > tab->lock_count) |
| 4755 | tab->lock_count++; |
| 4756 | tab->trg_event_map|= table->trg_event_map; |
| 4757 | } |
| 4758 | else |
| 4759 | { |
| 4760 | if (!(tab= (SP_TABLE *)thd->calloc(sizeof(SP_TABLE)))) |
| 4761 | return FALSE; |
| 4762 | if ((lex_for_tmp_check->sql_command == SQLCOM_CREATE_TABLE || |
| 4763 | lex_for_tmp_check->sql_command == SQLCOM_CREATE_SEQUENCE) && |
| 4764 | lex_for_tmp_check->query_tables == table && |
| 4765 | lex_for_tmp_check->tmp_table()) |
| 4766 | { |
| 4767 | tab->temp= TRUE; |
| 4768 | tab->qname.length= temp_table_key_length; |
| 4769 | } |
| 4770 | else |
| 4771 | tab->qname.length= tname.length(); |
| 4772 | tab->qname.str= (char*) thd->memdup(tname.ptr(), tab->qname.length); |
| 4773 | if (!tab->qname.str) |
| 4774 | return FALSE; |
| 4775 | tab->table_name_length= table->table_name.length; |
| 4776 | tab->db_length= table->db.length; |
| 4777 | tab->lock_type= table->lock_type; |
| 4778 | tab->lock_count= tab->query_lock_count= 1; |
| 4779 | tab->trg_event_map= table->trg_event_map; |
| 4780 | if (my_hash_insert(&m_sptabs, (uchar *)tab)) |
| 4781 | return FALSE; |
| 4782 | } |
| 4783 | } |
| 4784 | return TRUE; |
| 4785 | } |
| 4786 | |
| 4787 | |
| 4788 | /** |
| 4789 | Add tables used by routine to the table list. |
| 4790 | |
| 4791 | Converts multi-set of tables used by this routine to table list and adds |
| 4792 | this list to the end of table list specified by 'query_tables_last_ptr'. |
| 4793 | |
| 4794 | Elements of list will be allocated in PS memroot, so this list will be |
| 4795 | persistent between PS executions. |
| 4796 | |
| 4797 | @param[in] thd Thread context |
| 4798 | @param[in,out] query_tables_last_ptr Pointer to the next_global member of |
| 4799 | last element of the list where tables |
| 4800 | will be added (or to its root). |
| 4801 | @param[in] belong_to_view Uppermost view which uses this routine, |
| 4802 | 0 if none. |
| 4803 | |
| 4804 | @retval |
| 4805 | TRUE if some elements were added |
| 4806 | @retval |
| 4807 | FALSE otherwise. |
| 4808 | */ |
| 4809 | |
| 4810 | bool |
| 4811 | sp_head::add_used_tables_to_table_list(THD *thd, |
| 4812 | TABLE_LIST ***query_tables_last_ptr, |
| 4813 | TABLE_LIST *belong_to_view) |
| 4814 | { |
| 4815 | uint i; |
| 4816 | Query_arena *arena, backup; |
| 4817 | bool result= FALSE; |
| 4818 | DBUG_ENTER("sp_head::add_used_tables_to_table_list" ); |
| 4819 | |
| 4820 | /* |
| 4821 | Use persistent arena for table list allocation to be PS/SP friendly. |
| 4822 | Note that we also have to copy database/table names and alias to PS/SP |
| 4823 | memory since current instance of sp_head object can pass away before |
| 4824 | next execution of PS/SP for which tables are added to prelocking list. |
| 4825 | This will be fixed by introducing of proper invalidation mechanism |
| 4826 | once new TDC is ready. |
| 4827 | */ |
| 4828 | arena= thd->activate_stmt_arena_if_needed(&backup); |
| 4829 | |
| 4830 | for (i=0 ; i < m_sptabs.records ; i++) |
| 4831 | { |
| 4832 | char *tab_buff, *key_buff; |
| 4833 | SP_TABLE *stab= (SP_TABLE*) my_hash_element(&m_sptabs, i); |
| 4834 | LEX_CSTRING db_name; |
| 4835 | if (stab->temp) |
| 4836 | continue; |
| 4837 | |
| 4838 | if (!(tab_buff= (char *)thd->alloc(ALIGN_SIZE(sizeof(TABLE_LIST)) * |
| 4839 | stab->lock_count)) || |
| 4840 | !(key_buff= (char*)thd->memdup(stab->qname.str, |
| 4841 | stab->qname.length))) |
| 4842 | DBUG_RETURN(FALSE); |
| 4843 | |
| 4844 | db_name.str= key_buff; |
| 4845 | db_name.length= stab->db_length; |
| 4846 | |
| 4847 | |
| 4848 | for (uint j= 0; j < stab->lock_count; j++) |
| 4849 | { |
| 4850 | TABLE_LIST *table= (TABLE_LIST *)tab_buff; |
| 4851 | LEX_CSTRING table_name= { key_buff + stab->db_length + 1, |
| 4852 | stab->table_name_length }; |
| 4853 | LEX_CSTRING alias= { table_name.str + table_name.length + 1, |
| 4854 | strlen(table_name.str + table_name.length + 1) }; |
| 4855 | |
| 4856 | table->init_one_table_for_prelocking(&db_name, |
| 4857 | &table_name, |
| 4858 | &alias, |
| 4859 | stab->lock_type, |
| 4860 | TABLE_LIST::PRELOCK_ROUTINE, |
| 4861 | belong_to_view, |
| 4862 | stab->trg_event_map, |
| 4863 | query_tables_last_ptr); |
| 4864 | tab_buff+= ALIGN_SIZE(sizeof(TABLE_LIST)); |
| 4865 | result= TRUE; |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | if (arena) |
| 4870 | thd->restore_active_arena(arena, &backup); |
| 4871 | |
| 4872 | DBUG_RETURN(result); |
| 4873 | } |
| 4874 | |
| 4875 | |
| 4876 | /** |
| 4877 | Simple function for adding an explicitly named (systems) table to |
| 4878 | the global table list, e.g. "mysql", "proc". |
| 4879 | */ |
| 4880 | |
| 4881 | TABLE_LIST * |
| 4882 | sp_add_to_query_tables(THD *thd, LEX *lex, |
| 4883 | const LEX_CSTRING *db, const LEX_CSTRING *name, |
| 4884 | thr_lock_type locktype, |
| 4885 | enum_mdl_type mdl_type) |
| 4886 | { |
| 4887 | TABLE_LIST *table; |
| 4888 | |
| 4889 | if (!(table= (TABLE_LIST *)thd->calloc(sizeof(TABLE_LIST)))) |
| 4890 | return NULL; |
| 4891 | if (!thd->make_lex_string(&table->db, db->str, db->length) || |
| 4892 | !thd->make_lex_string(&table->table_name, name->str, name->length) || |
| 4893 | !thd->make_lex_string(&table->alias, name->str, name->length)) |
| 4894 | return NULL; |
| 4895 | |
| 4896 | table->lock_type= locktype; |
| 4897 | table->select_lex= lex->current_select; |
| 4898 | table->cacheable_table= 1; |
| 4899 | table->mdl_request.init(MDL_key::TABLE, table->db.str, table->table_name.str, |
| 4900 | mdl_type, MDL_TRANSACTION); |
| 4901 | |
| 4902 | lex->add_to_query_tables(table); |
| 4903 | return table; |
| 4904 | } |
| 4905 | |
| 4906 | |
| 4907 | Item *sp_head::adjust_assignment_source(THD *thd, Item *val, Item *val2) |
| 4908 | { |
| 4909 | return val ? val : val2 ? val2 : new (thd->mem_root) Item_null(thd); |
| 4910 | } |
| 4911 | |
| 4912 | /** |
| 4913 | Helper action for a SET statement. |
| 4914 | Used to push a SP local variable into the assignment list. |
| 4915 | |
| 4916 | @param var_type the SP local variable |
| 4917 | @param val the value being assigned to the variable |
| 4918 | |
| 4919 | @return TRUE if error, FALSE otherwise. |
| 4920 | */ |
| 4921 | |
| 4922 | bool |
| 4923 | sp_head::set_local_variable(THD *thd, sp_pcontext *spcont, |
| 4924 | const Sp_rcontext_handler *rh, |
| 4925 | sp_variable *spv, Item *val, LEX *lex, |
| 4926 | bool responsible_to_free_lex) |
| 4927 | { |
| 4928 | if (!(val= adjust_assignment_source(thd, val, spv->default_value))) |
| 4929 | return true; |
| 4930 | |
| 4931 | sp_instr_set *sp_set= new (thd->mem_root) |
| 4932 | sp_instr_set(instructions(), spcont, rh, |
| 4933 | spv->offset, val, lex, |
| 4934 | responsible_to_free_lex); |
| 4935 | |
| 4936 | return sp_set == NULL || add_instr(sp_set); |
| 4937 | } |
| 4938 | |
| 4939 | |
| 4940 | /** |
| 4941 | Similar to set_local_variable(), but for ROW variable fields. |
| 4942 | */ |
| 4943 | |
| 4944 | bool |
| 4945 | sp_head::set_local_variable_row_field(THD *thd, sp_pcontext *spcont, |
| 4946 | const Sp_rcontext_handler *rh, |
| 4947 | sp_variable *spv, uint field_idx, |
| 4948 | Item *val, LEX *lex) |
| 4949 | { |
| 4950 | if (!(val= adjust_assignment_source(thd, val, NULL))) |
| 4951 | return true; |
| 4952 | |
| 4953 | sp_instr_set_row_field *sp_set= new (thd->mem_root) |
| 4954 | sp_instr_set_row_field(instructions(), |
| 4955 | spcont, rh, |
| 4956 | spv->offset, |
| 4957 | field_idx, val, |
| 4958 | lex, true); |
| 4959 | return sp_set == NULL || add_instr(sp_set); |
| 4960 | } |
| 4961 | |
| 4962 | |
| 4963 | bool |
| 4964 | sp_head::set_local_variable_row_field_by_name(THD *thd, sp_pcontext *spcont, |
| 4965 | const Sp_rcontext_handler *rh, |
| 4966 | sp_variable *spv, |
| 4967 | const LEX_CSTRING *field_name, |
| 4968 | Item *val, LEX *lex) |
| 4969 | { |
| 4970 | if (!(val= adjust_assignment_source(thd, val, NULL))) |
| 4971 | return true; |
| 4972 | |
| 4973 | sp_instr_set_row_field_by_name *sp_set= |
| 4974 | new (thd->mem_root) sp_instr_set_row_field_by_name(instructions(), |
| 4975 | spcont, rh, |
| 4976 | spv->offset, |
| 4977 | *field_name, |
| 4978 | val, |
| 4979 | lex, true); |
| 4980 | return sp_set == NULL || add_instr(sp_set); |
| 4981 | } |
| 4982 | |
| 4983 | |
| 4984 | bool sp_head::add_open_cursor(THD *thd, sp_pcontext *spcont, uint offset, |
| 4985 | sp_pcontext *param_spcont, |
| 4986 | List<sp_assignment_lex> *parameters) |
| 4987 | { |
| 4988 | /* |
| 4989 | The caller must make sure that the number of formal parameters matches |
| 4990 | the number of actual parameters. |
| 4991 | */ |
| 4992 | DBUG_ASSERT((param_spcont ? param_spcont->context_var_count() : 0) == |
| 4993 | (parameters ? parameters->elements : 0)); |
| 4994 | |
| 4995 | if (parameters && |
| 4996 | add_set_cursor_param_variables(thd, param_spcont, parameters)) |
| 4997 | return true; |
| 4998 | |
| 4999 | sp_instr_copen *i= new (thd->mem_root) |
| 5000 | sp_instr_copen(instructions(), spcont, offset); |
| 5001 | return i == NULL || add_instr(i); |
| 5002 | } |
| 5003 | |
| 5004 | |
| 5005 | bool sp_head::add_for_loop_open_cursor(THD *thd, sp_pcontext *spcont, |
| 5006 | sp_variable *index, |
| 5007 | const sp_pcursor *pcursor, uint coffset, |
| 5008 | sp_assignment_lex *param_lex, |
| 5009 | Item_args *parameters) |
| 5010 | { |
| 5011 | if (parameters && |
| 5012 | add_set_for_loop_cursor_param_variables(thd, pcursor->param_context(), |
| 5013 | param_lex, parameters)) |
| 5014 | return true; |
| 5015 | |
| 5016 | sp_instr *instr_copy_struct= |
| 5017 | new (thd->mem_root) sp_instr_cursor_copy_struct(instructions(), |
| 5018 | spcont, pcursor->lex(), |
| 5019 | index->offset); |
| 5020 | if (instr_copy_struct == NULL || add_instr(instr_copy_struct)) |
| 5021 | return true; |
| 5022 | |
| 5023 | sp_instr_copen *instr_copen= |
| 5024 | new (thd->mem_root) sp_instr_copen(instructions(), spcont, coffset); |
| 5025 | if (instr_copen == NULL || add_instr(instr_copen)) |
| 5026 | return true; |
| 5027 | |
| 5028 | sp_instr_cfetch *instr_cfetch= |
| 5029 | new (thd->mem_root) sp_instr_cfetch(instructions(), |
| 5030 | spcont, coffset, false); |
| 5031 | if (instr_cfetch == NULL || add_instr(instr_cfetch)) |
| 5032 | return true; |
| 5033 | instr_cfetch->add_to_varlist(index); |
| 5034 | return false; |
| 5035 | } |
| 5036 | |
| 5037 | |
| 5038 | bool |
| 5039 | sp_head::add_set_for_loop_cursor_param_variables(THD *thd, |
| 5040 | sp_pcontext *param_spcont, |
| 5041 | sp_assignment_lex *param_lex, |
| 5042 | Item_args *parameters) |
| 5043 | { |
| 5044 | DBUG_ASSERT(param_spcont->context_var_count() == parameters->argument_count()); |
| 5045 | for (uint idx= 0; idx < parameters->argument_count(); idx ++) |
| 5046 | { |
| 5047 | /* |
| 5048 | param_lex is shared between multiple items (cursor parameters). |
| 5049 | Only the last sp_instr_set is responsible for freeing param_lex. |
| 5050 | See more comments in LEX::sp_for_loop_cursor_declarations in sql_lex.cc. |
| 5051 | */ |
| 5052 | bool last= idx + 1 == parameters->argument_count(); |
| 5053 | sp_variable *spvar= param_spcont->get_context_variable(idx); |
| 5054 | if (set_local_variable(thd, param_spcont, |
| 5055 | &sp_rcontext_handler_local, |
| 5056 | spvar, parameters->arguments()[idx], |
| 5057 | param_lex, last)) |
| 5058 | return true; |
| 5059 | } |
| 5060 | return false; |
| 5061 | } |
| 5062 | |
| 5063 | |
| 5064 | bool sp_head::spvar_fill_row(THD *thd, |
| 5065 | sp_variable *spvar, |
| 5066 | Row_definition_list *defs) |
| 5067 | { |
| 5068 | spvar->field_def.set_row_field_definitions(defs); |
| 5069 | spvar->field_def.field_name= spvar->name; |
| 5070 | if (fill_spvar_definition(thd, &spvar->field_def)) |
| 5071 | return true; |
| 5072 | row_fill_field_definitions(thd, defs); |
| 5073 | return false; |
| 5074 | } |
| 5075 | |
| 5076 | |
| 5077 | bool sp_head::spvar_fill_type_reference(THD *thd, |
| 5078 | sp_variable *spvar, |
| 5079 | const LEX_CSTRING &table, |
| 5080 | const LEX_CSTRING &col) |
| 5081 | { |
| 5082 | Qualified_column_ident *ref; |
| 5083 | if (!(ref= new (thd->mem_root) Qualified_column_ident(&table, &col))) |
| 5084 | return true; |
| 5085 | fill_spvar_using_type_reference(spvar, ref); |
| 5086 | return false; |
| 5087 | } |
| 5088 | |
| 5089 | |
| 5090 | bool sp_head::spvar_fill_type_reference(THD *thd, |
| 5091 | sp_variable *spvar, |
| 5092 | const LEX_CSTRING &db, |
| 5093 | const LEX_CSTRING &table, |
| 5094 | const LEX_CSTRING &col) |
| 5095 | { |
| 5096 | Qualified_column_ident *ref; |
| 5097 | if (!(ref= new (thd->mem_root) Qualified_column_ident(thd, &db, &table, &col))) |
| 5098 | return true; |
| 5099 | fill_spvar_using_type_reference(spvar, ref); |
| 5100 | return false; |
| 5101 | } |
| 5102 | |
| 5103 | |
| 5104 | bool sp_head::spvar_fill_table_rowtype_reference(THD *thd, |
| 5105 | sp_variable *spvar, |
| 5106 | const LEX_CSTRING &table) |
| 5107 | { |
| 5108 | Table_ident *ref; |
| 5109 | if (!(ref= new (thd->mem_root) Table_ident(&table))) |
| 5110 | return true; |
| 5111 | fill_spvar_using_table_rowtype_reference(thd, spvar, ref); |
| 5112 | return false; |
| 5113 | } |
| 5114 | |
| 5115 | |
| 5116 | bool sp_head::spvar_fill_table_rowtype_reference(THD *thd, |
| 5117 | sp_variable *spvar, |
| 5118 | const LEX_CSTRING &db, |
| 5119 | const LEX_CSTRING &table) |
| 5120 | { |
| 5121 | Table_ident *ref; |
| 5122 | if (!(ref= new (thd->mem_root) Table_ident(thd, &db, &table, false))) |
| 5123 | return true; |
| 5124 | fill_spvar_using_table_rowtype_reference(thd, spvar, ref); |
| 5125 | return false; |
| 5126 | } |
| 5127 | |
| 5128 | |
| 5129 | /* |
| 5130 | In Oracle mode stored routines have an optional name |
| 5131 | at the end of a declaration: |
| 5132 | PROCEDURE p1 AS |
| 5133 | BEGIN |
| 5134 | NULL |
| 5135 | END p1; |
| 5136 | Check that the first p1 and the last p1 match. |
| 5137 | */ |
| 5138 | |
| 5139 | bool sp_head::check_package_routine_end_name(const LEX_CSTRING &end_name) const |
| 5140 | { |
| 5141 | LEX_CSTRING non_qualified_name= m_name; |
| 5142 | const char *errpos; |
| 5143 | size_t ofs; |
| 5144 | if (!end_name.length) |
| 5145 | return false; // No end name |
| 5146 | if (!(errpos= strrchr(m_name.str, '.'))) |
| 5147 | { |
| 5148 | errpos= m_name.str; |
| 5149 | goto err; |
| 5150 | } |
| 5151 | errpos++; |
| 5152 | ofs= errpos - m_name.str; |
| 5153 | non_qualified_name.str+= ofs; |
| 5154 | non_qualified_name.length-= ofs; |
| 5155 | if (Sp_handler::eq_routine_name(end_name, non_qualified_name)) |
| 5156 | return false; |
| 5157 | err: |
| 5158 | my_error(ER_END_IDENTIFIER_DOES_NOT_MATCH, MYF(0), end_name.str, errpos); |
| 5159 | return true; |
| 5160 | } |
| 5161 | |
| 5162 | |
| 5163 | ulong sp_head::sp_cache_version() const |
| 5164 | { |
| 5165 | return m_parent ? m_parent->sp_cache_version() : m_sp_cache_version; |
| 5166 | } |
| 5167 | |