17 #include "my_global.h"
20 #include "sql_prepare.h"
21 #include "sql_cache.h"
22 #include "probes_mysql.h"
25 #include "sql_table.h"
27 #include "sql_array.h"
33 #include "sp_pcontext.h"
34 #include "sp_rcontext.h"
37 #include "sql_parse.h"
39 #include "transaction.h"
41 #include "global_threads.h"
59 uint db_length, table_name_length;
61 thr_lock_type lock_type;
63 uint query_lock_count;
73 uchar *sp_table_key(
const uchar *ptr,
size_t *plen, my_bool first)
76 *plen= tab->qname.length;
77 return (uchar *)tab->qname.str;
87 static void reset_start_time_for_sp(THD *thd)
95 if (thd->user_time.tv_sec || thd->user_time.tv_usec)
96 thd->start_time= thd->user_time;
98 my_micro_time_to_timeval(my_micro_time(), &thd->start_time);
120 static bool sp_update_sp_used_routines(
HASH *dst,
HASH *src)
122 for (uint
i= 0 ;
i < src->records ;
i++)
128 if (my_hash_insert(dst, (uchar *)rt))
151 sp_name::sp_name(
const MDL_key *key,
char *qname_buff)
153 m_db.str= (
char*)key->db_name();
154 m_db.length= key->db_name_length();
155 m_name.str= (
char*)key->name();
156 m_name.length= key->name_length();
157 m_qname.str= qname_buff;
160 strxmov(qname_buff, m_db.str,
".", m_name.str, NullS);
161 m_qname.length= m_db.length + 1 + m_name.length;
165 strmov(qname_buff, m_name.str);
166 m_qname.length= m_name.length;
177 const uint dot= !!m_db.length;
179 m_qname.length= m_db.length + dot + m_name.length;
180 if (!(m_qname.str= (
char*) thd->alloc(m_qname.length + 1)))
182 sprintf(m_qname.str,
"%.*s%.*s%.*s",
183 (
int) m_db.length, (m_db.length ? m_db.str :
""),
185 (
int) m_name.length, m_name.str);
192 void *sp_head::operator
new(
size_t size)
throw()
196 init_sql_alloc(&own_root, MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC);
202 sp->main_mem_root= own_root;
203 DBUG_PRINT(
"info", (
"mem_root 0x%lx", (ulong) &sp->mem_root));
207 void sp_head::operator
delete(
void *ptr,
size_t size)
throw()
215 MEM_ROOT own_root= sp->main_mem_root;
216 DBUG_PRINT(
"info", (
"mem_root 0x%lx moved to 0x%lx",
217 (ulong) &sp->mem_root, (ulong) &own_root));
218 free_root(&own_root, MYF(0));
222 sp_head::sp_head(enum_sp_type
type)
223 :Query_arena(&main_mem_root, STMT_INITIALIZED_FOR_SP),
228 m_explicit_name(false),
231 m_recursion_level(0),
232 m_next_cached_sp(NULL),
233 m_first_instance(NULL),
234 m_first_free_instance(NULL),
235 m_last_cached_sp(NULL),
237 m_root_parsing_ctx(NULL),
238 m_sp_cache_version(0),
239 m_creation_ctx(NULL),
242 m_first_instance=
this;
243 m_first_free_instance=
this;
244 m_last_cached_sp=
this;
246 m_return_field_def.charset = NULL;
262 m_body_utf8= NULL_STR;
264 my_hash_init(&m_sptabs, system_charset_info, 0, 0, 0, sp_table_key, 0, 0);
265 my_hash_init(&m_sroutines, system_charset_info, 0, 0, 0, sp_sroutine_key,
270 void sp_head::init_sp_name(THD *thd,
sp_name *spname)
274 DBUG_ASSERT(spname && spname->m_db.str && spname->m_db.length);
278 m_db.length= spname->m_db.length;
279 m_db.str= strmake_root(thd->mem_root, spname->m_db.str, spname->m_db.length);
281 m_name.length= spname->m_name.length;
282 m_name.str= strmake_root(thd->mem_root, spname->m_name.str,
283 spname->m_name.length);
287 if (spname->m_qname.length == 0)
290 m_qname.length= spname->m_qname.length;
291 m_qname.str= (
char*) memdup_root(thd->mem_root,
293 spname->m_qname.length + 1);
301 thd->m_parser_state->m_lip.body_utf8_start(thd, begin_ptr);
307 Lex_input_stream *lip= & thd->m_parser_state->m_lip;
308 const char *end_ptr= lip->get_cpp_ptr();
313 const char *p_start=
m_parser_data.get_parameter_start_ptr();
316 if (p_start && p_end)
318 m_params.length= p_end - p_start;
319 m_params.str= thd->strmake(p_start, m_params.length);
325 thd->lex->stmt_definition_end= end_ptr;
330 m_body.str= thd->strmake(
m_parser_data.get_body_start_ptr(), m_body.length);
331 trim_whitespace(thd->charset(), & m_body);
335 lip->body_utf8_append(end_ptr);
337 m_body_utf8.length= lip->get_body_utf8_length();
338 m_body_utf8.str= thd->strmake(lip->get_body_utf8_str(), m_body_utf8.length);
339 trim_whitespace(thd->charset(), & m_body_utf8);
346 m_defstr.length= end_ptr - lip->get_cpp_buf();
347 m_defstr.str= thd->strmake(lip->get_cpp_buf(), m_defstr.length);
348 trim_whitespace(thd->charset(), & m_defstr);
360 for (uint ip = 0 ; (i =
get_instr(ip)) ; ip++)
363 delete m_root_parsing_ctx;
376 thd->lex->sphead= NULL;
382 my_hash_free(&m_sptabs);
390 const char *field_name,
397 ::make_field(table->s,
408 field_name ? field_name : (
const char *) m_name.str);
417 bool sp_head::execute(THD *thd,
bool merge_da_on_success)
419 char saved_cur_db_name_buf[NAME_LEN+1];
421 { saved_cur_db_name_buf,
sizeof(saved_cur_db_name_buf) };
422 bool cur_db_changed= FALSE;
423 bool err_status= FALSE;
425 sql_mode_t save_sql_mode;
426 bool save_abort_on_warning;
427 Query_arena *old_arena;
430 Query_arena execute_arena(&execute_mem_root, STMT_INITIALIZED_FOR_SP),
432 query_id_t old_query_id;
433 TABLE *old_derived_tables;
435 Item_change_list old_change_list;
464 #if defined(__sparcv9) && defined(__sun)
465 const int sp_stack_size= 10 * STACK_MIN_SIZE;
467 const int sp_stack_size= 8 * STACK_MIN_SIZE;
474 opt_trace_disable_if_no_security_context_access(thd);
477 init_sql_alloc(&execute_mem_root, MEM_ROOT_BLOCK_SIZE, 0);
479 DBUG_ASSERT(!(
m_flags & IS_INVOKED));
485 (
"first free for 0x%lx ++: 0x%lx->0x%lx level: %lu flags %x",
489 m_next_cached_sp->m_flags));
508 (err_status= mysql_opt_change_db(thd, &m_db, &saved_cur_db_name, FALSE,
514 thd->is_slave_error= 0;
515 old_arena= thd->stmt_arena;
518 da->copy_sql_conditions_to_wi(thd, &sp_wi);
519 da->push_warning_info(&sp_wi);
525 saved_creation_ctx= m_creation_ctx->set_n_backup(thd);
531 old_query_id= thd->query_id;
532 old_derived_tables= thd->derived_tables;
533 thd->derived_tables= 0;
534 save_sql_mode= thd->variables.sql_mode;
536 save_abort_on_warning= thd->abort_on_warning;
537 thd->abort_on_warning= 0;
556 thd->push_reprepare_observer(NULL);
567 thd->change_list.move_elements_to(&old_change_list);
576 old_packet.swap(thd->packet);
582 thd->set_n_backup_active_arena(&execute_arena, &backup_arena);
588 thd->sp_runtime_ctx->callers_arena= &backup_arena;
590 #if defined(ENABLED_PROFILING)
592 thd->profiling.discard_current_query();
598 #if defined(ENABLED_PROFILING)
604 thd->profiling.finish_current_query();
605 thd->profiling.start_new_query(
"continuing inside routine");
612 #if defined(ENABLED_PROFILING)
613 thd->profiling.discard_current_query();
619 thd->get_stmt_da()->reset_for_next_command();
621 DBUG_PRINT(
"execute", (
"Instruction %u", ip));
628 reset_start_time_for_sp(thd);
642 if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
643 thd->user_var_events_alloc= thd->mem_root;
645 err_status= i->
execute(thd, &ip);
654 if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
656 reset_dynamic(&thd->user_var_events);
657 thd->user_var_events_alloc= NULL;
661 thd->cleanup_after_query();
662 free_root(&execute_mem_root, MYF(0));
669 if (!thd->is_fatal_error && !thd->killed_errno() &&
670 thd->sp_runtime_ctx->handle_sql_condition(thd, &ip, i))
676 thd->sp_runtime_ctx->end_partial_result_set= FALSE;
678 }
while (!err_status && !thd->killed && !thd->is_fatal_error);
680 #if defined(ENABLED_PROFILING)
681 thd->profiling.finish_current_query();
682 thd->profiling.start_new_query(
"tail end of routine");
687 m_creation_ctx->restore_env(thd, saved_creation_ctx);
691 thd->restore_active_arena(&execute_arena, &backup_arena);
693 thd->sp_runtime_ctx->pop_all_cursors();
696 old_packet.swap(thd->packet);
697 DBUG_ASSERT(thd->change_list.is_empty());
698 old_change_list.move_elements_to(&thd->change_list);
700 thd->set_query_id(old_query_id);
701 DBUG_ASSERT(!thd->derived_tables);
702 thd->derived_tables= old_derived_tables;
703 thd->variables.sql_mode= save_sql_mode;
704 thd->abort_on_warning= save_abort_on_warning;
705 thd->pop_reprepare_observer();
707 thd->stmt_arena= old_arena;
708 state= STMT_EXECUTED;
717 da->pop_warning_info();
719 if (err_status || merge_da_on_success)
746 da->opt_clear_warning_info(thd->query_id);
747 da->copy_sql_conditions_from_wi(thd, &sp_wi);
748 da->remove_marked_sql_conditions();
753 DBUG_PRINT(
"info", (
"err_status: %d killed: %d is_slave_error: %d report_error: %d",
754 err_status, thd->killed, thd->is_slave_error,
763 if (cur_db_changed && thd->killed != THD::KILL_CONNECTION)
770 err_status|= mysql_change_db(thd, &saved_cur_db_name, TRUE);
774 (
"first free for 0x%lx --: 0x%lx->0x%lx, level: %lu, flags %x",
775 (ulong) m_first_instance,
776 (ulong) m_first_instance->m_first_free_instance,
789 DBUG_ASSERT((m_first_instance->m_first_free_instance == 0 &&
790 this == m_first_instance->m_last_cached_sp &&
792 (m_first_instance->m_first_free_instance != 0 &&
796 m_first_instance->m_first_free_instance=
this;
807 sp_rcontext *parent_sp_runtime_ctx = thd->sp_runtime_ctx;
808 bool err_status= FALSE;
810 Query_arena call_arena(&call_mem_root, Query_arena::STMT_INITIALIZED_FOR_SP);
811 Query_arena backup_arena;
813 DBUG_ENTER(
"sp_head::execute_trigger");
814 DBUG_PRINT(
"info", (
"trigger %s", m_name.str));
816 #ifndef NO_EMBEDDED_ACCESS_CHECKS
817 Security_context *save_ctx= NULL;
821 m_security_ctx.change_security_context(thd,
835 fill_effective_table_privileges(thd,
842 if (!(grant_info->
privilege & TRIGGER_ACL))
845 get_privilege_desc(priv_desc,
sizeof(priv_desc), TRIGGER_ACL);
847 my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0), priv_desc,
848 thd->security_ctx->priv_user, thd->security_ctx->host_or_ip,
851 m_security_ctx.restore_security_context(thd, save_ctx);
861 #endif // NO_EMBEDDED_ACCESS_CHECKS
875 init_sql_alloc(&call_mem_root, MEM_ROOT_BLOCK_SIZE, 0);
876 thd->set_n_backup_active_arena(&call_arena, &backup_arena);
881 if (!trigger_runtime_ctx)
884 goto err_with_cleanup;
887 trigger_runtime_ctx->
sp=
this;
888 thd->sp_runtime_ctx= trigger_runtime_ctx;
890 err_status= execute(thd, FALSE);
893 thd->restore_active_arena(&call_arena, &backup_arena);
895 #ifndef NO_EMBEDDED_ACCESS_CHECKS
896 m_security_ctx.restore_security_context(thd, save_ctx);
897 #endif // NO_EMBEDDED_ACCESS_CHECKS
899 delete trigger_runtime_ctx;
900 call_arena.free_items();
901 free_root(&call_mem_root, MYF(0));
902 thd->sp_runtime_ctx= parent_sp_runtime_ctx;
905 thd->send_kill_message();
907 DBUG_RETURN(err_status);
912 Field *return_value_fld)
914 ulonglong binlog_save_options;
915 bool need_binlog_call= FALSE;
917 sp_rcontext *parent_sp_runtime_ctx = thd->sp_runtime_ctx;
918 char buf[STRING_BUFFER_USUAL_SIZE];
919 String binlog_buf(buf,
sizeof(buf), &my_charset_bin);
920 bool err_status= FALSE;
922 Query_arena call_arena(&call_mem_root, Query_arena::STMT_INITIALIZED_FOR_SP);
923 Query_arena backup_arena;
925 DBUG_ENTER(
"sp_head::execute_function");
926 DBUG_PRINT(
"info", (
"function %s", m_name.str));
928 LINT_INIT(binlog_save_options);
930 thd->where= THD::DEFAULT_WHERE;
943 my_error(ER_SP_WRONG_NO_OF_ARGS, MYF(0),
959 init_sql_alloc(&call_mem_root, MEM_ROOT_BLOCK_SIZE, 0);
960 thd->set_n_backup_active_arena(&call_arena, &backup_arena);
965 if (!func_runtime_ctx)
967 thd->restore_active_arena(&call_arena, &backup_arena);
969 goto err_with_cleanup;
972 func_runtime_ctx->
sp=
this;
980 thd->restore_active_arena(&call_arena, &backup_arena);
989 for (arg_no= 0; arg_no < argcount; arg_no++)
992 DBUG_ASSERT(argp[arg_no]->fixed);
994 err_status= func_runtime_ctx->set_variable(thd, arg_no, &(argp[arg_no]));
997 goto err_with_cleanup;
1004 need_binlog_call= mysql_bin_log.is_open() &&
1005 (thd->variables.option_bits & OPTION_BIN_LOG) &&
1006 !thd->is_current_stmt_binlog_format_row();
1015 if (need_binlog_call)
1017 binlog_buf.length(0);
1018 binlog_buf.append(STRING_WITH_LEN(
"SELECT "));
1019 append_identifier(thd, &binlog_buf, m_db.str, m_db.length);
1020 binlog_buf.append(
'.');
1021 append_identifier(thd, &binlog_buf, m_name.str, m_name.length);
1022 binlog_buf.append(
'(');
1023 for (arg_no= 0; arg_no < argcount; arg_no++)
1029 binlog_buf.append(
',');
1031 str_value= sp_get_item_value(thd, func_runtime_ctx->get_item(arg_no),
1035 binlog_buf.append(*str_value);
1037 binlog_buf.append(STRING_WITH_LEN(
"NULL"));
1039 binlog_buf.append(
')');
1042 thd->sp_runtime_ctx= func_runtime_ctx;
1044 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1045 Security_context *save_security_ctx;
1049 goto err_with_cleanup;
1053 if (need_binlog_call)
1056 reset_dynamic(&thd->user_var_events);
1072 mysql_bin_log.start_union_events(thd, q + 1);
1073 binlog_save_options= thd->variables.option_bits;
1074 thd->variables.option_bits&= ~OPTION_BIN_LOG;
1077 opt_trace_disable_if_no_stored_proc_func_access(thd,
this);
1087 thd->set_n_backup_active_arena(&call_arena, &backup_arena);
1089 err_status= execute(thd, TRUE);
1091 thd->restore_active_arena(&call_arena, &backup_arena);
1093 if (need_binlog_call)
1095 mysql_bin_log.stop_union_events(thd);
1096 thd->variables.option_bits= binlog_save_options;
1097 if (thd->binlog_evt_union.unioned_events)
1099 int errcode = query_error_code(thd, thd->killed == THD::NOT_KILLED);
1101 thd->binlog_evt_union.unioned_events_trans, FALSE, FALSE, errcode);
1103 thd->binlog_evt_union.unioned_events_trans)
1105 push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
1106 "Invoked ROUTINE modified a transactional table but MySQL "
1107 "failed to reflect this change in the binary log");
1110 reset_dynamic(&thd->user_var_events);
1112 thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
1113 thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
1121 if (!thd->sp_runtime_ctx->is_return_value_set())
1123 my_error(ER_SP_NORETURNEND, MYF(0), m_name.str);
1128 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1129 m_security_ctx.restore_security_context(thd, save_security_ctx);
1133 delete func_runtime_ctx;
1134 call_arena.free_items();
1135 free_root(&call_mem_root, MYF(0));
1136 thd->sp_runtime_ctx= parent_sp_runtime_ctx;
1142 if (need_binlog_call &&
1143 thd->sp_runtime_ctx == NULL && !thd->binlog_evt_union.do_union)
1144 thd->issue_unsafe_warnings();
1146 DBUG_RETURN(err_status);
1152 bool err_status= FALSE;
1155 ulonglong utime_before_sp_exec= thd->utime_after_lock;
1156 sp_rcontext *parent_sp_runtime_ctx= thd->sp_runtime_ctx;
1157 sp_rcontext *sp_runtime_ctx_saved= thd->sp_runtime_ctx;
1158 bool save_enable_slow_log=
false;
1159 bool save_log_general=
false;
1161 DBUG_ENTER(
"sp_head::execute_procedure");
1162 DBUG_PRINT(
"info", (
"procedure %s", m_name.str));
1164 if (args->elements != params)
1166 my_error(ER_SP_WRONG_NO_OF_ARGS, MYF(0),
"PROCEDURE",
1167 m_qname.str, params, args->elements);
1171 if (!parent_sp_runtime_ctx)
1176 if (!parent_sp_runtime_ctx)
1179 parent_sp_runtime_ctx->
sp= 0;
1180 thd->sp_runtime_ctx= parent_sp_runtime_ctx;
1189 if (!proc_runtime_ctx)
1191 thd->sp_runtime_ctx= sp_runtime_ctx_saved;
1193 if (!sp_runtime_ctx_saved)
1194 delete parent_sp_runtime_ctx;
1199 proc_runtime_ctx->
sp=
this;
1205 DBUG_PRINT(
"info",(
" %.*s: eval args", (
int) m_name.length, m_name.str));
1207 for (uint i= 0 ; i < params ; i++)
1209 Item *arg_item= it_args++;
1219 if (spvar->
mode != sp_variable::MODE_IN)
1222 arg_item->get_settable_routine_parameter();
1226 my_error(ER_SP_NOT_VAR_ARG, MYF(0), i+1,
m_qname.str);
1231 srp->set_required_privilege(spvar->
mode == sp_variable::MODE_INOUT);
1234 if (spvar->
mode == sp_variable::MODE_OUT)
1239 proc_runtime_ctx->set_variable(thd, i, (
Item **)&null_item))
1247 if (proc_runtime_ctx->set_variable(thd, i, it_args.ref()))
1260 thd->lex->unit.cleanup();
1262 if (!thd->in_sub_stmt)
1264 thd->get_stmt_da()->set_overwrite_status(
true);
1265 thd->is_error() ? trans_rollback_stmt(thd) : trans_commit_stmt(thd);
1266 thd->get_stmt_da()->set_overwrite_status(
false);
1269 thd_proc_info(thd,
"closing tables");
1270 close_thread_tables(thd);
1271 thd_proc_info(thd, 0);
1273 if (! thd->in_sub_stmt)
1275 if (thd->transaction_rollback_request)
1277 trans_rollback_implicit(thd);
1278 thd->mdl_context.release_transactional_locks();
1280 else if (! thd->in_multi_stmt_transaction_mode())
1281 thd->mdl_context.release_transactional_locks();
1283 thd->mdl_context.release_statement_locks();
1286 thd->rollback_item_tree_changes();
1288 DBUG_PRINT(
"info",(
" %.*s: eval args done", (
int) m_name.length,
1291 if (!(
m_flags & LOG_SLOW_STATEMENTS) && thd->enable_slow_log)
1293 DBUG_PRINT(
"info", (
"Disabling slow log for the execution"));
1294 save_enable_slow_log=
true;
1295 thd->enable_slow_log= FALSE;
1297 if (!(
m_flags & LOG_GENERAL_LOG) && !(thd->variables.option_bits & OPTION_LOG_OFF))
1299 DBUG_PRINT(
"info", (
"Disabling general log for the execution"));
1300 save_log_general=
true;
1302 thd->variables.option_bits |= OPTION_LOG_OFF;
1304 thd->sp_runtime_ctx= proc_runtime_ctx;
1306 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1307 Security_context *save_security_ctx= 0;
1312 opt_trace_disable_if_no_stored_proc_func_access(thd,
this);
1315 err_status= execute(thd, TRUE);
1317 if (save_log_general)
1318 thd->variables.option_bits &= ~OPTION_LOG_OFF;
1319 if (save_enable_slow_log)
1320 thd->enable_slow_log=
true;
1327 thd->sp_runtime_ctx->callers_arena= parent_sp_runtime_ctx->
callers_arena;
1329 if (!err_status && params > 0)
1337 for (uint i= 0 ; i < params ; i++)
1339 Item *arg_item= it_args++;
1346 if (spvar->
mode == sp_variable::MODE_IN)
1350 arg_item->get_settable_routine_parameter();
1354 if (srp->set_value(thd, parent_sp_runtime_ctx, proc_runtime_ctx->get_item_addr(i)))
1361 proc_runtime_ctx->get_item(i)->make_field(out_param_info);
1362 out_param_info->db_name= m_db.str;
1363 out_param_info->table_name= m_name.str;
1364 out_param_info->org_table_name= m_name.str;
1365 out_param_info->col_name= spvar->
name.str;
1366 out_param_info->org_col_name= spvar->
name.str;
1368 srp->set_out_param_info(out_param_info);
1372 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1373 if (save_security_ctx)
1374 m_security_ctx.restore_security_context(thd, save_security_ctx);
1377 if (!sp_runtime_ctx_saved)
1378 delete parent_sp_runtime_ctx;
1380 delete proc_runtime_ctx;
1381 thd->sp_runtime_ctx= sp_runtime_ctx_saved;
1382 thd->utime_after_lock= utime_before_sp_exec;
1388 bool need_binlog_call= mysql_bin_log.is_open() &&
1389 (thd->variables.option_bits & OPTION_BIN_LOG) &&
1390 !thd->is_current_stmt_binlog_format_row();
1391 if (need_binlog_call && thd->sp_runtime_ctx == NULL &&
1392 !thd->binlog_evt_union.do_union)
1393 thd->issue_unsafe_warnings();
1395 DBUG_RETURN(err_status);
1401 LEX *oldlex= thd->lex;
1403 LEX *sublex=
new (thd->mem_root)st_lex_local;
1415 sublex->sphead= oldlex->sphead;
1416 sublex->set_sp_current_parsing_ctx(oldlex->get_sp_current_parsing_ctx());
1417 sublex->sp_lex_in_use= FALSE;
1421 sublex->charset= NULL;
1422 sublex->length= NULL;
1424 sublex->interval_list.empty();
1428 thd->m_parser_state->m_yacc.reset_before_substatement();
1436 LEX *sublex= thd->lex;
1438 sublex->set_trg_event_type_for_tables();
1446 DBUG_PRINT(
"info", (
"lex->get_stmt_unsafe_flags: 0x%x",
1447 thd->lex->get_stmt_unsafe_flags()));
1448 unsafe_flags|= sublex->get_stmt_unsafe_flags();
1454 if (sp_update_sp_used_routines(&
m_sroutines, &sublex->sroutines))
1458 if (is_update_query(sublex->sql_command))
1465 merge_table_list(thd, sublex->query_tables, sublex);
1467 if (!sublex->sp_lex_in_use)
1469 sublex->sphead= NULL;
1478 void sp_head::set_info(longlong created,
1480 st_sp_chistics *chistics,
1481 sql_mode_t sql_mode)
1484 m_modified= modified;
1485 m_chistics= (st_sp_chistics *) memdup_root(mem_root, (
char*) chistics,
1490 m_chistics->comment.str= strmake_root(mem_root,
1497 void sp_head::set_definer(
const char *definer, uint definerlen)
1499 char user_name_holder[USERNAME_LENGTH + 1];
1500 LEX_STRING user_name= { user_name_holder, USERNAME_LENGTH };
1502 char host_name_holder[HOSTNAME_LENGTH + 1];
1503 LEX_STRING host_name= { host_name_holder, HOSTNAME_LENGTH };
1505 parse_user(definer, definerlen, user_name.str, &user_name.length,
1506 host_name.str, &host_name.length);
1508 set_definer(&user_name, &host_name);
1512 void sp_head::set_definer(
const LEX_STRING *user_name,
1515 m_definer_user.str= strmake_root(mem_root, user_name->str, user_name->length);
1516 m_definer_user.length= user_name->length;
1518 m_definer_host.str= strmake_root(mem_root, host_name->str, host_name->length);
1519 m_definer_host.length= host_name->length;
1525 const char *col1_caption= (type == SP_TYPE_PROCEDURE) ?
1526 "Procedure" :
"Function";
1528 const char *col3_caption= (type == SP_TYPE_PROCEDURE) ?
1529 "Create Procedure" :
"Create Function";
1540 DBUG_ASSERT(type == SP_TYPE_PROCEDURE || type == SP_TYPE_FUNCTION);
1545 sql_mode_string_representation(thd,
m_sql_mode, &sql_mode);
1560 std::max<size_t>(m_defstr.length, 1024
U));
1562 stmt_fld->maybe_null= TRUE;
1564 fields.push_back(stmt_fld);
1577 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
1584 protocol->prepare_for_resend();
1586 protocol->
store(m_name.str, m_name.length, system_charset_info);
1587 protocol->
store(sql_mode.str, sql_mode.length, system_charset_info);
1590 protocol->
store(m_defstr.str, m_defstr.length,
1591 m_creation_ctx->get_client_cs());
1593 protocol->store_null();
1596 protocol->
store(m_creation_ctx->get_client_cs()->csname, system_charset_info);
1597 protocol->
store(m_creation_ctx->get_connection_cl()->name, system_charset_info);
1598 protocol->
store(m_creation_ctx->get_db_cl()->name, system_charset_info);
1600 err_status= protocol->write();
1621 return m_instructions.
append(instr);
1637 if (!i->opt_is_marked())
1646 m_instructions.set(dst, i);
1661 m_instructions.elements(dst);
1670 if (i && !i->opt_is_marked())
1671 leads->push_front(i);
1675 void sp_head::opt_mark()
1696 leads.push_front(i);
1699 while (leads.elements != 0)
1704 while (i && !i->opt_is_marked())
1718 String buffer(buff,
sizeof(buff), system_charset_info);
1728 field_list.push_back(
new Item_uint(NAME_STRING(
"Pos"), 0, 9));
1731 std::max(buffer.length(), 1024
U)));
1733 Protocol::SEND_EOF))
1742 if (ip != i->get_ip())
1744 const char *format=
"Instruction at position %u has m_ip=%u";
1745 char tmp[
sizeof(format) + 2 *
sizeof(uint) + 1];
1747 sprintf(tmp, format, ip, i->get_ip());
1752 push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR, tmp);
1754 protocol->prepare_for_resend();
1755 protocol->
store((longlong)ip);
1757 buffer.set(
"", 0, system_charset_info);
1759 protocol->
store(buffer.ptr(), buffer.length(), system_charset_info);
1760 if ((res= protocol->write()))
1769 #endif // ifndef DBUG_OFF
1772 bool sp_head::merge_table_list(THD *thd,
1774 LEX *lex_for_tmp_check)
1776 if (lex_for_tmp_check->sql_command == SQLCOM_DROP_TABLE &&
1777 lex_for_tmp_check->drop_temporary)
1780 for (uint i= 0 ; i < m_sptabs.records ; i++)
1783 tab->query_lock_count= 0;
1786 for (;
table ; table= table->next_global)
1787 if (!table->
derived && !table->schema_table)
1797 char tname_buff[(NAME_LEN + 1) * 3];
1798 String tname(tname_buff,
sizeof(tname_buff), &my_charset_bin);
1799 uint temp_table_key_length;
1802 tname.append(table->db, table->db_length);
1804 tname.append(table->table_name, table->table_name_length);
1806 temp_table_key_length= tname.length();
1807 tname.append(table->alias);
1815 if (table->lock_type == TL_WRITE_DELAYED)
1816 table->lock_type= TL_WRITE;
1826 if ((tab= (
SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname.ptr(),
1828 ((tab= (
SP_TABLE*) my_hash_search(&m_sptabs, (uchar *)tname.ptr(),
1829 temp_table_key_length)) &&
1832 if (tab->lock_type < table->lock_type)
1833 tab->lock_type= table->lock_type;
1834 tab->query_lock_count++;
1835 if (tab->query_lock_count > tab->lock_count)
1843 if (lex_for_tmp_check->sql_command == SQLCOM_CREATE_TABLE &&
1844 lex_for_tmp_check->query_tables == table &&
1845 lex_for_tmp_check->create_info.options & HA_LEX_CREATE_TMP_TABLE)
1848 tab->qname.length= temp_table_key_length;
1851 tab->qname.length= tname.length();
1852 tab->qname.str= (
char*) thd->memdup(tname.ptr(), tab->qname.length);
1853 if (!tab->qname.str)
1855 tab->table_name_length= table->table_name_length;
1856 tab->db_length= table->db_length;
1857 tab->lock_type= table->lock_type;
1858 tab->lock_count= tab->query_lock_count= 1;
1860 if (my_hash_insert(&m_sptabs, (uchar *)tab))
1882 Prepared_stmt_arena_holder ps_arena_holder(thd);
1884 for (uint i= 0; i < m_sptabs.records; i++)
1886 char *tab_buff, *key_buff;
1891 if (!(tab_buff= (
char *)thd->calloc(ALIGN_SIZE(
sizeof(
TABLE_LIST)) *
1892 stab->lock_count)) ||
1893 !(key_buff= (
char*)thd->memdup(stab->qname.str,
1894 stab->qname.length)))
1897 for (uint j= 0; j < stab->lock_count; j++)
1901 table->db= key_buff;
1902 table->db_length= stab->db_length;
1903 table->table_name= table->db + table->db_length + 1;
1904 table->table_name_length= stab->table_name_length;
1905 table->alias= table->table_name + table->table_name_length + 1;
1906 table->lock_type= stab->lock_type;
1907 table->cacheable_table= 1;
1908 table->prelocking_placeholder= 1;
1909 table->belong_to_view= belong_to_view;
1916 table->mdl_request.
init(MDL_key::TABLE, table->db, table->table_name,
1917 table->lock_type >= TL_WRITE_ALLOW_WRITE ?
1918 MDL_SHARED_WRITE : MDL_SHARED_READ,
1923 **query_tables_last_ptr=
table;
1924 table->prev_global= *query_tables_last_ptr;
1925 *query_tables_last_ptr= &table->next_global;
1939 memset(&tables, 0,
sizeof(tables));
1940 tables.db= (
char*)
"mysql";
1941 tables.table_name= tables.alias= (
char*)
"proc";
1945 (tables.grant.
privilege & SELECT_ACL) != 0) ||
1946 (!strcmp(m_definer_user.str, thd->security_ctx->priv_user) &&
1947 !strcmp(m_definer_host.str, thd->security_ctx->priv_host)));
1949 return *full_access ?
1952 m_type == SP_TYPE_PROCEDURE);
1956 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1962 m_security_ctx.change_security_context(thd,
1963 &m_definer_user, &m_definer_host,
1976 check_routine_access(thd, EXECUTE_ACL, m_db.str, m_name.str,
1977 m_type == SP_TYPE_PROCEDURE,
false))
1979 m_security_ctx.restore_security_context(thd, *save_ctx);
1986 #endif // ! NO_EMBEDDED_ACCESS_CHECKS
1996 m_saved_memroot= thd->mem_root;
1997 m_saved_free_list= thd->free_list;
2000 thd->free_list= NULL;
2007 Backpatch_info *bp= (Backpatch_info *)sql_alloc(
sizeof(Backpatch_info));
2014 return m_backpatch.push_front(bp);
2025 if (bp->label == label)
2026 bp->instr->backpatch(dest);
2033 i->set_cont_dest(m_cont_level);
2034 return m_cont_backpatch.push_front(i);
2042 while ((i= m_cont_backpatch.head()) && i->
get_cont_dest() == m_cont_level)
2044 i->set_cont_dest(dest);
2045 m_cont_backpatch.pop();
2052 void sp_parser_data::process_new_sp_instr(THD* thd,
sp_instr *i)
2065 i->free_list= thd->free_list;
2067 thd->free_list= NULL;