26 #include "my_global.h"
30 #include "sql_class.h"
31 #include "sql_cache.h"
37 #include "sql_handler.h"
39 #include "rpl_filter.h"
40 #include "rpl_record.h"
42 #include <my_bitmap.h>
44 #include "sql_audit.h"
47 #include <thr_alarm.h>
51 #include <mysys_err.h>
54 #include "sp_rcontext.h"
56 #include "transaction.h"
58 #include "sql_parse.h"
59 #include "sql_callback.h"
61 #include "global_threads.h"
73 char internal_table_name[2]=
"*";
74 char empty_c_string[1]= {0};
79 const char *
const THD::DEFAULT_WHERE=
"field list";
85 extern "C" uchar *get_var_key(user_var_entry *
entry,
size_t *length,
86 my_bool not_used __attribute__((unused)))
88 *length= entry->entry_name.length();
89 return (uchar*) entry->entry_name.ptr();
92 extern "C" void free_user_var(user_var_entry *entry)
97 bool Key_part_spec::operator==(
const Key_part_spec& other)
const
99 return length == other.length &&
100 !my_strcasecmp(system_charset_info, field_name.str,
101 other.field_name.str);
113 key_create_info(rhs.key_create_info),
114 columns(rhs.columns, mem_root),
116 generated(rhs.generated)
118 list_copy_and_replace_each_value(columns, mem_root);
131 ref_table(rhs.ref_table),
132 ref_columns(rhs.ref_columns, mem_root),
133 delete_opt(rhs.delete_opt),
134 update_opt(rhs.update_opt),
135 match_opt(rhs.match_opt)
137 list_copy_and_replace_each_value(ref_columns, mem_root);
155 bool foreign_key_prefix(
Key *a,
Key *b)
160 if (b->generated && a->columns.elements > b->columns.elements)
161 swap_variables(
Key*, a, b);
167 swap_variables(
Key*, a, b);
171 if (a->columns.elements > b->columns.elements)
178 #ifdef ENABLE_WHEN_INNODB_CAN_HANDLE_SWAPED_FOREIGN_KEY_COLUMNS
179 while ((col1= col_it1++))
183 while ((col2= col_it2++))
196 while ((col1= col_it1++))
199 if (!(*col1 == *col2))
218 void *thd_get_scheduler_data(THD *thd)
220 return thd->scheduler.data;
229 void thd_set_scheduler_data(THD *thd,
void *data)
231 thd->scheduler.data= data;
241 PSI_thread *thd_get_psi(THD *thd)
243 return thd->scheduler.m_psi;
253 ulong thd_get_net_wait_timeout(THD* thd)
255 return thd->variables.net_wait_timeout;
264 void thd_set_psi(THD *thd, PSI_thread *psi)
266 thd->scheduler.m_psi= psi;
274 void thd_set_killed(THD *thd)
276 thd->killed= THD::KILL_CONNECTION;
284 void thd_clear_errors(THD *thd)
287 thd->mysys_var->abort= 0;
296 void thd_set_thread_stack(THD *thd,
char *stack_start)
298 thd->thread_stack= stack_start;
307 void thd_lock_thread_count(THD *)
318 void thd_unlock_thread_count(THD *)
329 void thd_close_connection(THD *thd)
332 vio_shutdown(thd->net.vio);
340 THD *thd_get_current_thd()
350 Thread_iterator thd_get_global_thread_list_begin()
352 return global_thread_list_begin();
359 Thread_iterator thd_get_global_thread_list_end()
361 return global_thread_list_end();
365 void thd_binlog_pos(
const THD *thd,
366 const char **file_var,
367 unsigned long long *pos_var)
369 thd->get_trans_pos(file_var, pos_var);
382 void thd_new_connection_setup(THD *thd,
char *stack_start)
384 DBUG_ENTER(
"thd_new_connection_setup");
386 #ifdef HAVE_PSI_INTERFACE
388 PSI_THREAD_CALL(new_thread)
389 (key_thread_one_connection, thd, thd->thread_id));
392 thd->prior_thr_create_utime= thd->thr_create_utime= thd->start_utime=
395 add_global_thread(thd);
398 DBUG_PRINT(
"info", (
"init new connection. thd: 0x%lx fd: %d",
399 (ulong)thd, mysql_socket_getfd(thd->net.vio->mysql_socket)));
400 thd_set_thread_stack(thd, stack_start);
409 void thd_lock_data(THD *thd)
419 void thd_unlock_data(THD *thd)
431 bool thd_is_transaction_active(THD *thd)
433 return thd->transaction.is_active();
441 int thd_connection_has_data(THD *thd)
443 Vio *vio= thd->net.vio;
444 return vio->has_data(vio);
453 void thd_set_net_read_write(THD *thd, uint val)
455 thd->net.reading_or_writing= val;
464 uint thd_get_net_read_write(THD *thd)
466 return thd->net.reading_or_writing;
477 thd->set_mysys_var(mysys_var);
487 my_socket thd_get_fd(THD *thd)
489 return mysql_socket_getfd(thd->net.vio->mysql_socket);
499 int thd_store_globals(THD* thd)
501 return thd->store_globals();
509 pthread_attr_t *get_connection_attrib(
void)
511 return &connection_attrib;
519 ulong get_max_connections(
void)
521 return max_connections;
528 extern "C" int mysql_tmpfile(
const char *prefix)
530 char filename[FN_REFLEN];
531 File fd = create_temp_file(filename, mysql_tmpdir, prefix,
533 O_BINARY | O_TRUNC | O_SEQUENTIAL |
536 O_CREAT | O_EXCL | O_RDWR | O_TEMPORARY,
554 int thd_in_lock_tables(
const THD *thd)
556 return test(thd->in_lock_tables);
561 int thd_tablespace_op(
const THD *thd)
563 return test(thd->tablespace_op);
568 const char *set_thd_proc_info(
void *thd_arg,
const char *info,
569 const char *calling_function,
570 const char *calling_file,
571 const unsigned int calling_line)
579 set_thd_stage_info(thd_arg, & old_stage, & new_stage,
580 calling_function, calling_file, calling_line);
586 void set_thd_stage_info(
void *opaque_thd,
589 const char *calling_func,
590 const char *calling_file,
591 const unsigned int calling_line)
593 THD *thd= (THD*) opaque_thd;
597 thd->enter_stage(new_stage, old_stage, calling_func, calling_file, calling_line);
602 const char *calling_func,
603 const char *calling_file,
604 const unsigned int calling_line)
606 DBUG_PRINT(
"THD::enter_stage", (
"%s:%d", calling_file, calling_line));
608 if (old_stage != NULL)
610 old_stage->
m_key= m_current_stage_key;
611 old_stage->
m_name= proc_info;
614 if (new_stage != NULL)
618 #if defined(ENABLED_PROFILING)
619 profiling.status_change(msg, calling_func, calling_file, calling_line);
622 m_current_stage_key= new_stage->
m_key;
625 MYSQL_SET_STAGE(m_current_stage_key, calling_file, calling_line);
637 return thd->ENTER_COND(cond, mutex, stage, old_stage);
646 thd->EXIT_COND(stage);
651 void **thd_ha_data(
const THD *thd,
const struct handlerton *hton)
653 return (
void **) &thd->ha_data[hton->slot].ha_ptr;
657 void thd_storage_lock_wait(THD *thd,
long long value)
659 thd->utime_after_lock+= value;
666 void *thd_get_ha_data(
const THD *thd,
const struct handlerton *hton)
668 return *thd_ha_data(thd, hton);
677 void thd_set_ha_data(THD *thd,
const struct handlerton *hton,
680 plugin_ref *lock= &thd->ha_data[hton->slot].lock;
681 if (ha_data && !*lock)
682 *lock= ha_lock_engine(NULL, (
handlerton*) hton);
683 else if (!ha_data && *lock)
685 plugin_unlock(NULL, *lock);
688 *thd_ha_data(thd, hton)= (
void*) ha_data;
693 long long thd_test_options(
const THD *thd,
long long test_options)
695 return thd->variables.option_bits & test_options;
699 int thd_sql_command(
const THD *thd)
701 return (
int) thd->lex->sql_command;
705 int thd_tx_isolation(
const THD *thd)
707 return (
int) thd->tx_isolation;
711 int thd_tx_is_read_only(
const THD *thd)
713 return (
int) thd->tx_read_only;
717 void thd_inc_row_count(THD *thd)
719 thd->get_stmt_da()->inc_current_row_for_warning();
742 char *thd_security_context(THD *thd,
char *buffer,
unsigned int length,
743 unsigned int max_query_len)
745 String str(buffer, length, &my_charset_latin1);
746 Security_context *sctx= &thd->main_security_ctx;
758 const char *proc_info= thd->proc_info;
760 len= my_snprintf(header,
sizeof(header),
761 "MySQL thread id %lu, OS thread handle 0x%lx, query id %lu",
762 thd->thread_id, (ulong) thd->real_id, (ulong) thd->query_id);
764 str.append(header, len);
766 if (sctx->get_host()->length())
769 str.append(sctx->get_host()->ptr());
772 if (sctx->get_ip()->length())
775 str.append(sctx->get_ip()->ptr());
781 str.append(sctx->user);
787 str.append(proc_info);
794 if (max_query_len < 1)
795 len= thd->query_length();
797 len= min(thd->query_length(), max_query_len);
799 str.append(thd->query(), len);
804 if (str.c_ptr_safe() == buffer)
811 DBUG_ASSERT(buffer != NULL);
812 length= min(str.length(), length-1);
813 memcpy(buffer, str.c_ptr_quick(), length);
815 buffer[length]=
'\0';
834 bool Drop_table_error_handler::handle_condition(THD *thd,
836 const char* sqlstate,
837 Sql_condition::enum_warning_level
level,
842 return ((sql_errno == EE_DELETE && my_errno == ENOENT) ||
843 sql_errno == ER_TRG_NO_DEFINER);
847 void Open_tables_state::set_open_tables_state(Open_tables_state *state)
851 this->temporary_tables= state->temporary_tables;
852 this->derived_tables= state->derived_tables;
854 this->lock= state->lock;
855 this->extra_lock= state->extra_lock;
857 this->locked_tables_mode= state->locked_tables_mode;
858 this->current_tablenr= state->current_tablenr;
860 this->state_flags= state->state_flags;
862 this->reset_reprepare_observers();
863 for (
int i= 0;
i < state->m_reprepare_observers.elements(); ++
i)
864 this->push_reprepare_observer(state->m_reprepare_observers.at(
i));
868 void Open_tables_state::reset_open_tables_state()
871 temporary_tables= NULL;
872 derived_tables= NULL;
875 locked_tables_mode= LTM_NONE;
878 reset_reprepare_observers();
882 THD::THD(
bool enable_plugins)
883 :Statement(&main_lex, &main_mem_root, STMT_CONVENTIONAL_EXECUTION,
885 rli_fake(0), rli_slave(NULL),
887 fill_status_recursion_level(0),
888 binlog_row_event_extra_data(NULL),
889 binlog_unsafe_warning_flags(0),
890 binlog_table_maps(0),
891 binlog_accessed_db_names(NULL),
892 m_trans_log_file(NULL),
893 m_trans_fixed_log_file(NULL),
895 table_map_for_update(0),
896 arg_of_last_insert_id_function(FALSE),
897 first_successful_insert_id_in_prev_stmt(0),
898 first_successful_insert_id_in_prev_stmt_for_binlog(0),
899 first_successful_insert_id_in_cur_stmt(0),
900 stmt_depends_on_first_successful_insert_id_in_prev_stmt(FALSE),
901 m_examined_row_count(0),
902 m_statement_psi(NULL),
904 m_server_idle(false),
905 next_to_commit(NULL),
907 transaction_rollback_request(0),
908 is_fatal_sub_stmt_error(0),
913 derived_tables_processing(FALSE),
914 sp_runtime_ctx(NULL),
915 m_parser_state(NULL),
916 #if defined(ENABLED_DEBUG_SYNC)
917 debug_sync_control(0),
919 m_enable_plugins(enable_plugins),
920 owned_gtid_set(global_sid_map),
926 mdl_context.init(
this);
932 init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
935 catalog= (
char*)
"std";
936 main_security_ctx.init();
937 security_ctx= &main_security_ctx;
940 query_start_used= query_start_usec_used= 0;
941 count_cuted_fields= CHECK_FIELD_IGNORE;
944 is_slave_error= thread_specific_used= FALSE;
945 my_hash_clear(&handler_tables_hash);
948 m_sent_row_count= 0L;
950 m_row_count_func= -1;
951 statement_id_counter= 0UL;
953 lex->current_select= 0;
955 user_time.tv_usec= 0;
956 start_time.tv_sec= 0;
957 start_time.tv_usec= 0;
958 start_utime= prior_thr_create_utime= 0L;
959 utime_after_lock= 0L;
962 memset(&variables, 0,
sizeof(variables));
967 query_name_consts= 0;
968 db_charset= global_system_variables.collation_database;
969 memset(ha_data, 0,
sizeof(ha_data));
971 binlog_evt_union.do_union= FALSE;
973 commit_error= CE_NONE;
974 durability_property= HA_REGULAR_DURABILITY;
976 dbug_sentry=THD_SENTRY_MAGIC;
978 #ifndef EMBEDDED_LIBRARY
979 mysql_audit_init_thd(
this);
982 client_capabilities= 0;
984 system_thread= NON_SYSTEM_THREAD;
985 cleanup_done= abort_on_warning= 0;
986 m_release_resources_done=
false;
988 transaction.m_pending_rows_event= 0;
989 transaction.flags.enabled=
true;
990 #ifdef SIGNAL_WITH_VIO_SHUTDOWN
997 where= THD::DEFAULT_WHERE;
998 server_id = ::server_id;
999 unmasked_server_id = server_id;
1001 set_command(COM_CONNECT);
1005 reset_open_tables_state();
1008 #if defined(ENABLED_PROFILING)
1009 profiling.set_thd(
this);
1011 m_user_connect= NULL;
1012 my_hash_init(&user_vars, system_charset_info, USER_VARS_HASH_SIZE, 0, 0,
1013 (my_hash_get_key) get_var_key,
1014 (my_hash_free_key) free_user_var, 0);
1016 sp_proc_cache= NULL;
1017 sp_func_cache= NULL;
1021 my_init_dynamic_array(&user_var_events,
1024 memset(&user_var_events, 0,
sizeof(user_var_events));
1027 protocol= &protocol_text;
1028 protocol_text.init(
this);
1029 protocol_binary.init(
this);
1031 tablespace_op=FALSE;
1032 tmp= sql_rnd_with_mutex();
1033 randominit(&rand, tmp + (ulong) &rand, tmp + (ulong) ::global_query_id);
1034 substitute_null_with_insert_id = FALSE;
1035 thr_lock_info_init(&lock_info);
1037 m_internal_handler= NULL;
1038 m_binlog_invoker= FALSE;
1039 memset(&invoker_user, 0,
sizeof(invoker_user));
1040 memset(&invoker_host, 0,
sizeof(invoker_host));
1042 binlog_next_event_pos.file_name= NULL;
1043 binlog_next_event_pos.pos= 0;
1050 void THD::push_internal_handler(Internal_error_handler *
handler)
1052 if (m_internal_handler)
1054 handler->m_prev_internal_handler= m_internal_handler;
1055 m_internal_handler= handler;
1059 m_internal_handler= handler;
1063 bool THD::handle_condition(uint sql_errno,
1064 const char* sqlstate,
1065 Sql_condition::enum_warning_level level,
1069 if (!m_internal_handler)
1075 for (Internal_error_handler *error_handler= m_internal_handler;
1077 error_handler= error_handler->m_prev_internal_handler)
1079 if (error_handler->handle_condition(
this, sql_errno, sqlstate, level, msg,
1090 Internal_error_handler *THD::pop_internal_handler()
1092 DBUG_ASSERT(m_internal_handler != NULL);
1093 Internal_error_handler *popped_handler= m_internal_handler;
1094 m_internal_handler= m_internal_handler->m_prev_internal_handler;
1095 return popped_handler;
1099 void THD::raise_error(uint sql_errno)
1101 const char* msg= ER(sql_errno);
1102 (void) raise_condition(sql_errno,
1104 Sql_condition::WARN_LEVEL_ERROR,
1108 void THD::raise_error_printf(uint sql_errno, ...)
1111 char ebuff[MYSQL_ERRMSG_SIZE];
1112 DBUG_ENTER(
"THD::raise_error_printf");
1113 DBUG_PRINT(
"my", (
"nr: %d errno: %d", sql_errno, errno));
1114 const char* format= ER(sql_errno);
1115 va_start(args, sql_errno);
1116 my_vsnprintf(ebuff,
sizeof(ebuff), format, args);
1118 (void) raise_condition(sql_errno,
1120 Sql_condition::WARN_LEVEL_ERROR,
1125 void THD::raise_warning(uint sql_errno)
1127 const char* msg= ER(sql_errno);
1128 (void) raise_condition(sql_errno,
1130 Sql_condition::WARN_LEVEL_WARN,
1134 void THD::raise_warning_printf(uint sql_errno, ...)
1137 char ebuff[MYSQL_ERRMSG_SIZE];
1138 DBUG_ENTER(
"THD::raise_warning_printf");
1139 DBUG_PRINT(
"enter", (
"warning: %u", sql_errno));
1140 const char* format= ER(sql_errno);
1141 va_start(args, sql_errno);
1142 my_vsnprintf(ebuff,
sizeof(ebuff), format, args);
1144 (void) raise_condition(sql_errno,
1146 Sql_condition::WARN_LEVEL_WARN,
1151 void THD::raise_note(uint sql_errno)
1153 DBUG_ENTER(
"THD::raise_note");
1154 DBUG_PRINT(
"enter", (
"code: %d", sql_errno));
1157 const char* msg= ER(sql_errno);
1158 (void) raise_condition(sql_errno,
1160 Sql_condition::WARN_LEVEL_NOTE,
1165 void THD::raise_note_printf(uint sql_errno, ...)
1168 char ebuff[MYSQL_ERRMSG_SIZE];
1169 DBUG_ENTER(
"THD::raise_note_printf");
1170 DBUG_PRINT(
"enter",(
"code: %u", sql_errno));
1173 const char* format= ER(sql_errno);
1174 va_start(args, sql_errno);
1175 my_vsnprintf(ebuff,
sizeof(ebuff), format, args);
1177 (void) raise_condition(sql_errno,
1179 Sql_condition::WARN_LEVEL_NOTE,
1185 struct timeval THD::query_start_timeval_trunc(uint decimals)
1188 tv.tv_sec= start_time.tv_sec;
1189 query_start_used= 1;
1192 tv.tv_usec= start_time.tv_usec;
1193 my_timeval_trunc(&tv, decimals);
1194 query_start_usec_used= 1;
1205 const char* sqlstate,
1206 Sql_condition::enum_warning_level level,
1211 DBUG_ENTER(
"THD::raise_condition");
1214 (level == Sql_condition::WARN_LEVEL_NOTE))
1217 da->opt_clear_warning_info(query_id);
1225 sql_errno= ER_UNKNOWN_ERROR;
1228 if (sqlstate == NULL)
1229 sqlstate= mysql_errno_to_sqlstate(sql_errno);
1231 if ((level == Sql_condition::WARN_LEVEL_WARN) &&
1232 really_abort_on_warning())
1238 level= Sql_condition::WARN_LEVEL_ERROR;
1239 killed= THD::KILL_BAD_DATA;
1244 case Sql_condition::WARN_LEVEL_NOTE:
1245 case Sql_condition::WARN_LEVEL_WARN:
1248 case Sql_condition::WARN_LEVEL_ERROR:
1254 if (handle_condition(sql_errno, sqlstate, level, msg, &cond))
1257 if (level == Sql_condition::WARN_LEVEL_ERROR)
1265 if (lex->current_select &&
1266 lex->current_select->no_error && !is_fatal_error)
1269 (
"Error converted to warning: current_select: no_error %d "
1271 (lex->current_select ?
1272 lex->current_select->no_error : 0),
1273 (
int) is_fatal_error));
1277 if (!da->is_error())
1279 set_row_count_func(-1);
1285 query_cache_abort(&query_cache_tls);
1292 if (!(is_fatal_error && (sql_errno == EE_OUTOFMEMORY ||
1293 sql_errno == ER_OUTOFMEMORY)))
1295 cond= da->push_warning(
this, sql_errno, sqlstate, level, msg);
1303 return thd->alloc(size);
1309 return thd->calloc(size);
1315 return thd->strdup(str);
1321 return thd->strmake(str, size);
1326 const char *str,
unsigned int size,
1327 int allocate_lex_string)
1329 return thd->make_lex_string(lex_str, str, size,
1330 (
bool) allocate_lex_string);
1334 void *
thd_memdup(MYSQL_THD thd,
const void* str,
unsigned int size)
1336 return thd->memdup(str, size);
1340 void thd_get_xid(
const MYSQL_THD thd,
MYSQL_XID *xid)
1342 *xid = *(
MYSQL_XID *) &thd->transaction.xid_state.xid;
1346 extern "C" THD *_current_thd_noinline(
void)
1348 return my_pthread_getspecific_ptr(THD*,THR_THD);
1355 void THD::init(
void)
1358 plugin_thdvar_init(
this, m_enable_plugins);
1364 variables.pseudo_thread_id= thread_id;
1366 server_status= SERVER_STATUS_AUTOCOMMIT;
1367 if (variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)
1368 server_status|= SERVER_STATUS_NO_BACKSLASH_ESCAPES;
1370 transaction.all.reset_unsafe_rollback_flags();
1371 transaction.stmt.reset_unsafe_rollback_flags();
1372 open_options=ha_open_options;
1373 update_lock_default= (variables.low_priority_updates ?
1374 TL_WRITE_LOW_PRIORITY :
1376 tx_isolation= (enum_tx_isolation) variables.tx_isolation;
1377 tx_read_only= variables.tx_read_only;
1379 reset_current_stmt_binlog_format_row();
1380 reset_binlog_local_stmt_filter();
1381 memset(&status_var, 0,
sizeof(status_var));
1382 binlog_row_event_extra_data= 0;
1384 if (variables.sql_log_bin)
1385 variables.option_bits|= OPTION_BIN_LOG;
1387 variables.option_bits&= ~OPTION_BIN_LOG;
1389 #if defined(ENABLED_DEBUG_SYNC)
1391 debug_sync_init_thread(
this);
1394 owned_gtid.sidno= 0;
1410 reset_root_defaults(mem_root, variables.query_alloc_block_size,
1411 variables.query_prealloc_size);
1412 reset_root_defaults(&transaction.mem_root,
1413 variables.trans_alloc_block_size,
1414 variables.trans_prealloc_size);
1415 transaction.xid_state.xid.null();
1416 transaction.xid_state.in_thd=1;
1417 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
1420 if ((rli->deferred_events_collecting= rpl_filter->is_on()))
1426 DBUG_ASSERT(rli_slave->info_thd ==
this && slave_thread);
1443 void THD::change_user(
void)
1446 add_to_status(&global_status_var, &status_var);
1454 my_hash_init(&user_vars, system_charset_info, USER_VARS_HASH_SIZE, 0, 0,
1455 (my_hash_get_key) get_var_key,
1456 (my_hash_free_key) free_user_var, 0);
1457 sp_cache_clear(&sp_proc_cache);
1458 sp_cache_clear(&sp_func_cache);
1466 void THD::cleanup(
void)
1468 DBUG_ENTER(
"THD::cleanup");
1469 DBUG_ASSERT(cleanup_done == 0);
1471 killed= KILL_CONNECTION;
1472 #ifdef ENABLE_WHEN_BINLOG_WILL_BE_ABLE_TO_PREPARE
1473 if (transaction.xid_state.xa_state == XA_PREPARED)
1475 #error xid_state in the cache should be replaced by the allocated value
1479 transaction.xid_state.xa_state= XA_NOTR;
1480 trans_rollback(
this);
1481 xid_cache_delete(&transaction.xid_state);
1484 locked_tables_list.unlock_locked_tables(
this);
1485 mysql_ha_cleanup(
this);
1494 mdl_context.release_transactional_locks();
1497 if (global_read_lock.is_acquired())
1498 global_read_lock.unlock_global_read_lock(
this);
1501 DBUG_ASSERT(!mdl_context.has_locks());
1503 #if defined(ENABLED_DEBUG_SYNC)
1505 debug_sync_end_thread(
this);
1508 delete_dynamic(&user_var_events);
1509 my_hash_free(&user_vars);
1511 variables.gtid_next.set_automatic();
1512 close_temporary_tables(
this);
1513 sp_cache_clear(&sp_proc_cache);
1514 sp_cache_clear(&sp_func_cache);
1519 item_user_lock_release(ull);
1530 tc_log->
commit(
this,
true);
1540 void THD::release_resources()
1543 DBUG_ASSERT(m_release_resources_done ==
false);
1546 add_to_status(&global_status_var, &status_var);
1553 #ifndef EMBEDDED_LIBRARY
1556 vio_delete(net.vio);
1567 mdl_context.destroy();
1569 mysql_audit_release(
this);
1570 if (m_enable_plugins)
1571 plugin_thdvar_cleanup(
this);
1573 m_release_resources_done=
true;
1580 THD_CHECK_SENTRY(
this);
1581 DBUG_ENTER(
"~THD()");
1582 DBUG_PRINT(
"info", (
"THD dtor, this %p",
this));
1584 if (!m_release_resources_done)
1585 release_resources();
1587 clear_next_event_pos();
1593 DBUG_PRINT(
"info", (
"freeing security context"));
1594 main_security_ctx.destroy();
1597 free_root(&transaction.mem_root,MYF(0));
1600 dbug_sentry= THD_SENTRY_GONE;
1602 #ifndef EMBEDDED_LIBRARY
1605 rli_fake->end_info();
1610 if (variables.gtid_next_list.gtid_set != NULL)
1612 #ifdef HAVE_GTID_NEXT_LIST
1613 delete variables.gtid_next_list.gtid_set;
1614 variables.gtid_next_list.gtid_set= NULL;
1615 variables.gtid_next_list.is_non_null=
false;
1621 mysql_audit_free_thd(
this);
1623 rli_slave->cleanup_after_session();
1626 free_root(&main_mem_root, MYF(0));
1648 ulonglong *end= (ulonglong*) ((uchar*) to_var +
1649 offsetof(
STATUS_VAR, last_system_status_var) +
1651 ulonglong *
to= (ulonglong*) to_var, *from= (ulonglong*) from_var;
1654 *(to++)+= *(from++);
1656 to_var->com_other+= from_var->com_other;
1658 for (c= 0; c< SQLCOM_END; c++)
1659 to_var->com_stat[(uint) c] += from_var->com_stat[(uint) c];
1679 ulonglong *end= (ulonglong*) ((uchar*) to_var + offsetof(
STATUS_VAR,
1680 last_system_status_var) +
1682 ulonglong *to= (ulonglong*) to_var,
1683 *from= (ulonglong*) from_var,
1684 *dec= (ulonglong*) dec_var;
1687 *(to++)+= *(from++) - *(dec++);
1689 to_var->com_other+= from_var->com_other - dec_var->com_other;
1691 for (c= 0; c< SQLCOM_END; c++)
1692 to_var->com_stat[(uint) c] += from_var->com_stat[(uint) c] -dec_var->com_stat[(uint) c];
1706 void THD::awake(THD::killed_state state_to_set)
1708 DBUG_ENTER(
"THD::awake");
1709 DBUG_PRINT(
"enter", (
"this: %p current_thd: %p",
this, current_thd));
1710 THD_CHECK_SENTRY(
this);
1714 killed= state_to_set;
1716 if (state_to_set != THD::KILL_QUERY)
1718 #ifdef SIGNAL_WITH_VIO_SHUTDOWN
1719 if (
this != current_thd)
1747 shutdown_active_vio();
1752 thr_alarm_kill(thread_id);
1756 MYSQL_CALLBACK(thread_scheduler, post_kill_notification, (
this));
1789 if (mysys_var->current_cond && mysys_var->current_mutex)
1808 void THD::disconnect()
1814 killed= THD::KILL_CONNECTION;
1816 #ifdef SIGNAL_WITH_VIO_SHUTDOWN
1823 shutdown_active_vio();
1827 if (net.vio != vio && net.vio != NULL)
1829 vio_shutdown(net.vio);
1837 bool needs_thr_lock_abort)
1839 THD *in_use= ctx_in_use->
get_thd();
1840 bool signalled= FALSE;
1841 if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) &&
1844 in_use->killed= THD::KILL_CONNECTION;
1846 if (in_use->mysys_var->current_cond)
1852 if (needs_thr_lock_abort)
1855 for (
TABLE *thd_table= in_use->open_tables;
1857 thd_table= thd_table->next)
1866 if (!thd_table->needs_reopen())
1880 bool THD::store_globals()
1886 DBUG_ASSERT(thread_stack);
1888 if (my_pthread_setspecific_ptr(THR_THD,
this) ||
1889 my_pthread_setspecific_ptr(THR_MALLOC, &mem_root))
1900 mysys_var=my_thread_var;
1901 DBUG_PRINT(
"debug", (
"mysys_var: 0x%llx", (ulonglong) mysys_var));
1906 mysys_var->id= thread_id;
1907 real_id= pthread_self();
1913 thr_lock_info_init(&lock_info);
1921 bool THD::restore_globals()
1927 DBUG_ASSERT(thread_stack);
1930 my_pthread_setspecific_ptr(THR_THD, NULL);
1931 my_pthread_setspecific_ptr(THR_MALLOC, NULL);
1953 void THD::cleanup_after_query()
1968 stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
1969 auto_inc_intervals_in_cur_stmt_for_binlog.empty();
1971 binlog_accessed_db_names= NULL;
1972 m_trans_fixed_log_file= NULL;
1975 gtid_post_statement_checks(
this);
1976 #ifndef EMBEDDED_LIBRARY
1986 if ((rli_slave || rli_fake) && is_update_query(lex->sql_command))
1987 auto_inc_intervals_forced.empty();
1997 reset_binlog_local_stmt_filter();
1998 if (first_successful_insert_id_in_cur_stmt > 0)
2001 first_successful_insert_id_in_prev_stmt=
2002 first_successful_insert_id_in_cur_stmt;
2003 first_successful_insert_id_in_cur_stmt= 0;
2004 substitute_null_with_insert_id= TRUE;
2006 arg_of_last_insert_id_function= 0;
2010 where= THD::DEFAULT_WHERE;
2012 table_map_for_update= 0;
2013 m_binlog_invoker= FALSE;
2015 if (lex && lex->mi.repl_ignore_server_ids.buffer)
2017 delete_dynamic(&lex->mi.repl_ignore_server_ids);
2019 #ifndef EMBEDDED_LIBRARY
2021 rli_slave->cleanup_after_query();
2027 make_lex_string_root(
MEM_ROOT *mem_root,
2028 LEX_STRING *lex_str,
const char* str, uint length,
2029 bool allocate_lex_string)
2031 if (allocate_lex_string)
2034 if (!(lex_str->str= strmake_root(mem_root, str, length)))
2036 lex_str->length= length;
2051 const char* str, uint length,
2052 bool allocate_lex_string)
2054 return make_lex_string_root (mem_root, lex_str, str,
2055 length, allocate_lex_string);
2080 const char *from, uint from_length,
2083 DBUG_ENTER(
"convert_string");
2084 size_t new_length= to_cs->mbmaxlen * from_length;
2086 if (!(to->str= (
char*) alloc(new_length+1)))
2091 to->length= copy_and_convert((
char*) to->str, new_length, to_cs,
2092 from, from_length, from_cs, &dummy_errors);
2093 to->str[to->length]=0;
2117 if (convert_buffer.copy(s->ptr(), s->length(), from_cs, to_cs, &dummy_errors))
2120 if (convert_buffer.alloced_length() >= convert_buffer.length() * 2 ||
2123 return s->copy(convert_buffer);
2125 s->swap(convert_buffer);
2134 void THD::update_charset()
2137 charset_is_system_charset=
2138 !String::needs_conversion(0,
2139 variables.character_set_client,
2140 system_charset_info,
2142 charset_is_collation_connection=
2143 !String::needs_conversion(0,
2144 variables.character_set_client,
2145 variables.collation_connection,
2147 charset_is_character_set_filesystem=
2148 !String::needs_conversion(0,
2149 variables.character_set_client,
2150 variables.character_set_filesystem,
2164 (*prev)->next = curr;
2172 DBUG_ENTER(
"THD::add_changed_table(table)");
2174 DBUG_ASSERT(in_multi_stmt_transaction_mode() && table->file->has_transactions());
2175 add_changed_table(table->s->table_cache_key.str,
2176 (
long) table->s->table_cache_key.length);
2181 void THD::add_changed_table(
const char *key,
long key_length)
2183 DBUG_ENTER(
"THD::add_changed_table(key)");
2187 for (; curr; prev_changed = &(curr->next), curr = curr->next)
2189 int cmp = (long)curr->key_length - (
long)key_length;
2192 list_include(prev_changed, curr, changed_table_dup(key, key_length));
2194 (
"key_length: %ld %u", key_length,
2195 (*prev_changed)->key_length));
2200 cmp = memcmp(curr->key, key, curr->key_length);
2203 list_include(prev_changed, curr, changed_table_dup(key, key_length));
2205 (
"key_length: %ld %u", key_length,
2206 (*prev_changed)->key_length));
2211 DBUG_PRINT(
"info", (
"already in list"));
2216 *prev_changed = changed_table_dup(key, key_length);
2217 DBUG_PRINT(
"info", (
"key_length: %ld %u", key_length,
2218 (*prev_changed)->key_length));
2230 my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_FATALERROR),
2231 ALIGN_SIZE(
sizeof(
TABLE_LIST)) + key_length + 1);
2232 killed= KILL_CONNECTION;
2237 new_table->next = 0;
2238 new_table->key_length = key_length;
2239 ::memcpy(new_table->key, key, key_length);
2244 int THD::send_explain_fields(select_result *result)
2249 field_list.push_back(
new Item_return_int(
"id",3, MYSQL_TYPE_LONGLONG));
2252 item->maybe_null= 1;
2253 if (lex->describe & DESCRIBE_PARTITIONS)
2258 field_list.push_back(item);
2259 item->maybe_null= 1;
2262 item->maybe_null= 1;
2264 NAME_CHAR_LEN*MAX_KEY, cs));
2269 NAME_CHAR_LEN*MAX_KEY));
2272 NAME_CHAR_LEN*MAX_REF_PARTS,
2276 MYSQL_TYPE_LONGLONG));
2277 item->maybe_null= 1;
2278 if (lex->describe & DESCRIBE_EXTENDED)
2280 field_list.push_back(item=
new Item_float(NAME_STRING(
"filtered"),
2285 item->maybe_null= 1;
2286 return (result->send_result_set_metadata(field_list,
2287 Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF));
2290 #ifdef SIGNAL_WITH_VIO_SHUTDOWN
2291 void THD::shutdown_active_vio()
2293 DBUG_ENTER(
"shutdown_active_vio");
2295 #ifndef EMBEDDED_LIBRARY
2298 vio_shutdown(active_vio);
2313 void THD::nocheck_register_item_tree_change(
Item **place,
Item *old_value,
2316 Item_change_record *change;
2322 void *change_mem= alloc_root(runtime_memroot,
sizeof(*change));
2323 if (change_mem == 0)
2331 change=
new (change_mem) Item_change_record;
2332 change->place= place;
2333 change->old_value= old_value;
2334 change_list.push_front(change);
2338 void THD::change_item_tree_place(
Item **old_ref,
Item **new_ref)
2341 Item_change_record *change;
2342 while ((change= it++))
2344 if (change->place == old_ref)
2346 DBUG_PRINT(
"info", (
"change_item_tree_place old_ref %p new_ref %p",
2348 change->place= new_ref;
2355 void THD::rollback_item_tree_changes()
2358 Item_change_record *change;
2359 DBUG_ENTER(
"rollback_item_tree_changes");
2361 while ((change= it++))
2364 (
"rollback_item_tree_changes "
2365 "place %p curr_value %p old_value %p",
2366 change->place, *change->place, change->old_value));
2367 *change->place= change->old_value;
2370 change_list.empty();
2379 select_result::select_result():
2380 estimated_rowcount(0)
2385 void select_result::send_error(uint errcode,
const char *err)
2387 my_message(errcode, err, MYF(0));
2391 void select_result::cleanup()
2398 my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
2403 static const String default_line_term(
"\n",default_charset_info);
2404 static const String default_escaped(
"\\",default_charset_info);
2405 static const String default_field_term(
"\t",default_charset_info);
2406 static const String default_xml_row_term(
"<row>", default_charset_info);
2407 static const String my_empty_string(
"",default_charset_info);
2410 sql_exchange::sql_exchange(
char *
name,
bool flag,
2411 enum enum_filetype filetype_arg)
2412 :file_name(name), opt_enclosed(0), dumpfile(flag), skip_lines(0)
2414 filetype= filetype_arg;
2415 field_term= &default_field_term;
2416 enclosed= line_start= &my_empty_string;
2417 line_term= filetype == FILETYPE_CSV ?
2418 &default_line_term : &default_xml_row_term;
2419 escaped= &default_escaped;
2423 bool sql_exchange::escaped_given(
void)
2425 return escaped != &default_escaped;
2429 bool select_send::send_result_set_metadata(
List<Item> &list, uint
flags)
2432 if (!(res= thd->protocol->send_result_set_metadata(&list, flags)))
2433 is_result_set_started= 1;
2437 void select_send::abort_result_set()
2439 DBUG_ENTER(
"select_send::abort_result_set");
2441 if (is_result_set_started && thd->sp_runtime_ctx)
2452 thd->sp_runtime_ctx->end_partial_result_set= TRUE;
2464 void select_send::cleanup()
2466 is_result_set_started= FALSE;
2474 DBUG_ENTER(
"select_send::send_data");
2476 if (unit->offset_limit_cnt)
2478 unit->offset_limit_cnt--;
2489 protocol->prepare_for_resend();
2492 protocol->remove_last_row();
2496 thd->inc_sent_row_count(1);
2499 DBUG_RETURN(protocol->write());
2504 bool select_send::send_eof()
2517 if (thd->is_error())
2520 is_result_set_started= 0;
2529 void select_to_file::send_error(uint errcode,
const char *err)
2531 my_message(errcode, err, MYF(0));
2534 (void) end_io_cache(&cache);
2543 bool select_to_file::send_eof()
2545 int error=
test(end_io_cache(&cache));
2551 ::my_ok(thd,row_count);
2558 void select_to_file::cleanup()
2563 (void) end_io_cache(&cache);
2572 select_to_file::~select_to_file()
2576 (void) end_io_cache(&cache);
2586 select_export::~select_export()
2588 thd->set_sent_row_count(row_count);
2608 static File create_file(THD *thd,
char *path, sql_exchange *exchange,
2612 uint
option= MY_UNPACK_FILENAME | MY_RELATIVE_PATH;
2614 #ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
2615 option|= MY_REPLACE_DIR;
2618 if (!dirname_length(exchange->file_name))
2620 strxnmov(path, FN_REFLEN-1, mysql_real_data_home, thd->db ? thd->db :
"",
2622 (void) fn_format(path, exchange->file_name, path,
"", option);
2625 (
void) fn_format(path, exchange->file_name, mysql_real_data_home,
"", option);
2627 if (!is_secure_file_path(path))
2630 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"--secure-file-priv");
2634 if (!access(path, F_OK))
2636 my_error(ER_FILE_EXISTS_ERROR, MYF(0), exchange->file_name);
2641 path, 0666, O_WRONLY|O_EXCL, MYF(MY_WME))) < 0)
2644 (void) fchmod(file, 0666);
2646 (void) chmod(path, 0666);
2648 if (init_io_cache(cache, file, 0L, WRITE_CACHE, 0L, 1, MYF(MY_WME)))
2660 select_export::prepare(
List<Item> &list, SELECT_LEX_UNIT *u)
2663 bool string_results= FALSE, non_string_results= FALSE;
2665 if ((uint) strlen(exchange->file_name) + NAME_LEN >= FN_REFLEN)
2666 strmake(path,exchange->file_name,FN_REFLEN-1);
2668 write_cs= exchange->cs ? exchange->cs : &my_charset_bin;
2670 if ((file= create_file(thd, path, exchange, &cache)) < 0)
2678 if (item->max_length >= MAX_BLOB_WIDTH)
2683 if (item->result_type() == STRING_RESULT)
2684 string_results= TRUE;
2686 non_string_results= TRUE;
2689 if (exchange->escaped->numchars() > 1 || exchange->enclosed->numchars() > 1)
2691 my_error(ER_WRONG_FIELD_TERMINATORS, MYF(0));
2694 if (exchange->escaped->length() > 1 || exchange->enclosed->length() > 1 ||
2695 !my_isascii(exchange->escaped->ptr()[0]) ||
2696 !my_isascii(exchange->enclosed->ptr()[0]) ||
2697 !exchange->field_term->is_ascii() || !exchange->line_term->is_ascii() ||
2698 !exchange->line_start->is_ascii())
2710 push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
2711 WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED,
2712 ER(WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED));
2714 field_term_length=exchange->field_term->length();
2715 field_term_char= field_term_length ?
2716 (int) (uchar) (*exchange->field_term)[0] : INT_MAX;
2717 if (!exchange->line_term->length())
2718 exchange->line_term=exchange->field_term;
2719 field_sep_char= (exchange->enclosed->length() ?
2720 (int) (uchar) (*exchange->enclosed)[0] : field_term_char);
2721 if (exchange->escaped->length() && (exchange->escaped_given() ||
2722 !(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)))
2723 escape_char= (
int) (uchar) (*exchange->escaped)[0];
2726 is_ambiguous_field_sep=
test(strchr(ESCAPE_CHARS, field_sep_char));
2727 is_unsafe_field_sep=
test(strchr(NUMERIC_CHARS, field_sep_char));
2728 line_sep_char= (exchange->line_term->length() ?
2729 (int) (uchar) (*exchange->line_term)[0] : INT_MAX);
2730 if (!field_term_length)
2731 exchange->opt_enclosed=0;
2732 if (!exchange->enclosed->length())
2733 exchange->opt_enclosed=1;
2734 fixed_row_size= (!field_term_length && !exchange->enclosed->length() &&
2736 if ((is_ambiguous_field_sep && exchange->enclosed->is_empty() &&
2737 (string_results || is_unsafe_field_sep)) ||
2738 (exchange->opt_enclosed && non_string_results &&
2739 field_term_length && strchr(NUMERIC_CHARS, field_term_char)))
2741 push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
2742 ER_AMBIGUOUS_FIELD_TERM, ER(ER_AMBIGUOUS_FIELD_TERM));
2743 is_ambiguous_field_term= TRUE;
2746 is_ambiguous_field_term= FALSE;
2752 #define NEED_ESCAPING(x) ((int) (uchar) (x) == escape_char || \
2753 (enclosed ? (int) (uchar) (x) == field_sep_char \
2754 : (int) (uchar) (x) == field_term_char) || \
2755 (int) (uchar) (x) == line_sep_char || \
2758 bool select_export::send_data(
List<Item> &items)
2761 DBUG_ENTER(
"select_export::send_data");
2762 char buff[MAX_FIELD_WIDTH],null_buff[2],
space[MAX_FIELD_WIDTH];
2763 char cvt_buff[MAX_FIELD_WIDTH];
2764 String cvt_str(cvt_buff,
sizeof(cvt_buff), write_cs);
2765 bool space_inited=0;
2766 String tmp(buff,
sizeof(buff),&my_charset_bin),*res;
2769 if (unit->offset_limit_cnt)
2771 unit->offset_limit_cnt--;
2776 uint used_length=0,items_left=items.elements;
2779 if (my_b_write(&cache,(uchar*) exchange->line_start->ptr(),
2780 exchange->line_start->length()))
2784 Item_result result_type=item->result_type();
2785 bool enclosed = (exchange->enclosed->length() &&
2786 (!exchange->opt_enclosed || result_type == STRING_RESULT));
2787 res=item->str_result(&tmp);
2788 if (res && !my_charset_same(write_cs, res->charset()) &&
2789 !my_charset_same(write_cs, &my_charset_bin))
2791 const char *well_formed_error_pos;
2792 const char *cannot_convert_error_pos;
2793 const char *from_end_pos;
2794 const char *error_pos;
2796 uint64 estimated_bytes=
2797 ((uint64) res->length() / res->charset()->mbminlen + 1) *
2798 write_cs->mbmaxlen + 1;
2799 set_if_smaller(estimated_bytes, UINT_MAX32);
2800 if (cvt_str.realloc((uint32) estimated_bytes))
2802 my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), (uint32) estimated_bytes);
2806 bytes= well_formed_copy_nchars(write_cs, (
char *) cvt_str.ptr(),
2807 cvt_str.alloced_length(),
2808 res->charset(), res->ptr(), res->length(),
2811 &well_formed_error_pos,
2812 &cannot_convert_error_pos,
2814 error_pos= well_formed_error_pos ? well_formed_error_pos
2815 : cannot_convert_error_pos;
2818 char printable_buff[32];
2819 convert_to_printable(printable_buff,
sizeof(printable_buff),
2820 error_pos, res->ptr() + res->length() - error_pos,
2822 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
2823 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
2824 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2825 "string", printable_buff,
2826 item->item_name.
ptr(),
static_cast<long>(row_count));
2828 else if (from_end_pos < res->ptr() + res->length())
2833 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
2834 WARN_DATA_TRUNCATED, ER(WARN_DATA_TRUNCATED),
2835 item->full_name(),
static_cast<long>(row_count));
2837 cvt_str.length(bytes);
2840 if (res && enclosed)
2842 if (my_b_write(&cache,(uchar*) exchange->enclosed->ptr(),
2843 exchange->enclosed->length()))
2848 if (!fixed_row_size)
2850 if (escape_char != -1)
2852 null_buff[0]=escape_char;
2854 if (my_b_write(&cache,(uchar*) null_buff,2))
2857 else if (my_b_write(&cache,(uchar*)
"NULL",4))
2868 used_length=min(res->length(),item->max_length);
2870 used_length=res->length();
2871 if ((result_type == STRING_RESULT || is_unsafe_field_sep) &&
2874 char *pos, *start, *end;
2877 thd->variables.character_set_client;
2878 bool check_second_byte= (res_charset == &my_charset_bin) &&
2879 character_set_client->
2880 escape_with_backslash_is_dangerous;
2881 DBUG_ASSERT(character_set_client->mbmaxlen == 2 ||
2882 !character_set_client->escape_with_backslash_is_dangerous);
2883 for (start=pos=(
char*) res->ptr(),end=pos+used_length ;
2888 if (use_mb(res_charset))
2891 if ((l=my_ismbchar(res_charset, pos, end)))
2931 if ((NEED_ESCAPING(*pos) ||
2932 (check_second_byte &&
2933 my_mbcharlen(character_set_client, (uchar) *pos) == 2 &&
2935 NEED_ESCAPING(pos[1]))) &&
2940 (enclosed || !is_ambiguous_field_term ||
2941 (int) (uchar) *pos != field_term_char))
2944 tmp_buff[0]= ((int) (uchar) *pos == field_sep_char &&
2945 is_ambiguous_field_sep) ?
2946 field_sep_char : escape_char;
2947 tmp_buff[1]= *pos ? *pos :
'0';
2948 if (my_b_write(&cache,(uchar*) start,(uint) (pos-start)) ||
2949 my_b_write(&cache,(uchar*) tmp_buff,2))
2954 if (my_b_write(&cache,(uchar*) start,(uint) (pos-start)))
2957 else if (my_b_write(&cache,(uchar*) res->ptr(),used_length))
2962 if (item->max_length > used_length)
2968 memset(space,
' ',
sizeof(space));
2970 uint length=item->max_length-used_length;
2971 for (; length >
sizeof(
space) ; length-=
sizeof(
space))
2973 if (my_b_write(&cache,(uchar*)
space,
sizeof(
space)))
2976 if (my_b_write(&cache,(uchar*)
space,length))
2980 if (res && enclosed)
2982 if (my_b_write(&cache, (uchar*) exchange->enclosed->ptr(),
2983 exchange->enclosed->length()))
2988 if (my_b_write(&cache, (uchar*) exchange->field_term->ptr(),
2993 if (my_b_write(&cache,(uchar*) exchange->line_term->ptr(),
2994 exchange->line_term->length()))
3008 select_dump::prepare(
List<Item> &list __attribute__((unused)),
3012 return (
int) ((file= create_file(thd, path, exchange, &cache)) < 0);
3016 bool select_dump::send_data(
List<Item> &items)
3019 char buff[MAX_FIELD_WIDTH];
3020 String tmp(buff,
sizeof(buff),&my_charset_bin),*res;
3023 DBUG_ENTER(
"select_dump::send_data");
3025 if (unit->offset_limit_cnt)
3027 unit->offset_limit_cnt--;
3030 if (row_count++ > 1)
3032 my_message(ER_TOO_MANY_ROWS, ER(ER_TOO_MANY_ROWS), MYF(0));
3037 res=item->str_result(&tmp);
3040 if (my_b_write(&cache,(uchar*)
"",1))
3043 else if (my_b_write(&cache,(uchar*) res->ptr(),res->length()))
3045 char errbuf[MYSYS_STRERROR_SIZE];
3046 my_error(ER_ERROR_ON_WRITE, MYF(0), path, my_errno,
3047 my_strerror(errbuf,
sizeof(errbuf), my_errno));
3063 bool select_singlerow_subselect::send_data(
List<Item> &items)
3065 DBUG_ENTER(
"select_singlerow_subselect::send_data");
3069 my_message(ER_SUBQUERY_NO_1_ROW, ER(ER_SUBQUERY_NO_1_ROW), MYF(0));
3072 if (unit->offset_limit_cnt)
3074 unit->offset_limit_cnt--;
3079 for (uint
i= 0; (val_item= li++);
i++)
3080 it->store(
i, val_item);
3086 void select_max_min_finder_subselect::cleanup()
3088 DBUG_ENTER(
"select_max_min_finder_subselect::cleanup");
3094 bool select_max_min_finder_subselect::send_data(
List<Item> &items)
3096 DBUG_ENTER(
"select_max_min_finder_subselect::send_data");
3099 Item *val_item= li++;
3100 it->register_value();
3103 cache->store(val_item);
3105 it->store(0, cache);
3111 cache= Item_cache::get_cache(val_item);
3112 switch (val_item->result_type())
3115 op= &select_max_min_finder_subselect::cmp_real;
3118 op= &select_max_min_finder_subselect::cmp_int;
3121 op= &select_max_min_finder_subselect::cmp_str;
3123 case DECIMAL_RESULT:
3124 op= &select_max_min_finder_subselect::cmp_decimal;
3132 cache->store(val_item);
3133 it->store(0, cache);
3159 bool select_max_min_finder_subselect::cmp_real()
3162 double val1= cache->val_real(), val2= maxmin->val_real();
3174 if (cache->null_value || maxmin->null_value)
3175 return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
3176 return (fmax) ? (val1 > val2) : (val1 < val2);
3184 bool select_max_min_finder_subselect::cmp_int()
3187 longlong val1= cache->val_int(), val2= maxmin->val_int();
3188 if (cache->null_value || maxmin->null_value)
3189 return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
3190 return (fmax) ? (val1 > val2) : (val1 < val2);
3198 bool select_max_min_finder_subselect::cmp_decimal()
3201 my_decimal cval, *cvalue= cache->val_decimal(&cval);
3202 my_decimal mval, *mvalue= maxmin->val_decimal(&mval);
3203 if (cache->null_value || maxmin->null_value)
3204 return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
3215 bool select_max_min_finder_subselect::cmp_str()
3217 String *val1, *val2, buf1, buf2;
3223 val1= cache->val_str(&buf1);
3224 val2= maxmin->val_str(&buf1);
3225 if (cache->null_value || maxmin->null_value)
3226 return (ignore_nulls) ? !(cache->null_value) : !(maxmin->null_value);
3228 ? (sortcmp(val1, val2, cache->collation.collation) > 0)
3229 : (sortcmp(val1, val2, cache->collation.collation) < 0);
3232 bool select_exists_subselect::send_data(
List<Item> &items)
3234 DBUG_ENTER(
"select_exists_subselect::send_data");
3236 if (unit->offset_limit_cnt)
3238 unit->offset_limit_cnt--;
3257 int select_dumpvar::prepare(
List<Item> &list, SELECT_LEX_UNIT *u)
3261 if (var_list.elements != list.elements)
3263 my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
3264 ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT), MYF(0));
3274 my_error(ER_SP_BAD_CURSOR_SELECT, MYF(0));
3279 void select_dumpvar::cleanup()
3292 void Query_arena::free_items()
3295 DBUG_ENTER(
"Query_arena::free_items");
3297 for (; free_list; free_list= next)
3299 next= free_list->next;
3300 free_list->delete_self();
3307 void Query_arena::set_query_arena(Query_arena *
set)
3309 mem_root=
set->mem_root;
3310 free_list=
set->free_list;
3315 void Query_arena::cleanup_stmt()
3317 DBUG_ASSERT(!
"Query_arena::cleanup_stmt() not implemented");
3324 Statement::Statement(LEX *lex_arg,
MEM_ROOT *mem_root_arg,
3325 enum enum_state state_arg, ulong id_arg)
3326 :Query_arena(mem_root_arg, state_arg),
3328 mark_used_columns(MARK_COLUMNS_READ),
3343 void Statement::set_statement(Statement *stmt)
3346 mark_used_columns= stmt->mark_used_columns;
3348 query_string= stmt->query_string;
3353 Statement::set_n_backup_statement(Statement *stmt, Statement *backup)
3355 DBUG_ENTER(
"Statement::set_n_backup_statement");
3356 backup->set_statement(
this);
3357 set_statement(stmt);
3362 void Statement::restore_backup_statement(Statement *stmt, Statement *backup)
3364 DBUG_ENTER(
"Statement::restore_backup_statement");
3365 stmt->set_statement(
this);
3366 set_statement(backup);
3371 void THD::end_statement()
3386 void THD::set_n_backup_active_arena(Query_arena *
set, Query_arena *backup)
3388 DBUG_ENTER(
"THD::set_n_backup_active_arena");
3389 DBUG_ASSERT(backup->is_backup_arena == FALSE);
3391 backup->set_query_arena(
this);
3392 set_query_arena(
set);
3394 backup->is_backup_arena= TRUE;
3400 void THD::restore_active_arena(Query_arena *
set, Query_arena *backup)
3402 DBUG_ENTER(
"THD::restore_active_arena");
3403 DBUG_ASSERT(backup->is_backup_arena);
3404 set->set_query_arena(
this);
3405 set_query_arena(backup);
3407 backup->is_backup_arena= FALSE;
3412 Statement::~Statement()
3419 get_statement_id_as_hash_key(
const uchar *
record,
size_t *key_length,
3420 my_bool not_used __attribute__((unused)))
3422 const Statement *
statement= (
const Statement *) record;
3423 *key_length=
sizeof(statement->id);
3424 return (uchar *) &((
const Statement *) statement)->id;
3427 static void delete_statement_as_hash_key(
void *key)
3429 delete (Statement *) key;
3432 static uchar *get_stmt_name_hash_key(Statement *entry,
size_t *length,
3433 my_bool not_used __attribute__((unused)))
3435 *length= entry->name.length;
3436 return (uchar*) entry->name.str;
3441 Statement_map::Statement_map() :
3442 last_found_statement(0)
3446 START_STMT_HASH_SIZE = 16,
3447 START_NAME_HASH_SIZE = 16
3449 my_hash_init(&
st_hash, &my_charset_bin, START_STMT_HASH_SIZE, 0, 0,
3450 get_statement_id_as_hash_key,
3451 delete_statement_as_hash_key, MYF(0));
3452 my_hash_init(&names_hash, system_charset_info, START_NAME_HASH_SIZE, 0, 0,
3453 (my_hash_get_key) get_stmt_name_hash_key,
3479 int Statement_map::insert(THD *thd, Statement *statement)
3481 if (my_hash_insert(&
st_hash, (uchar*) statement))
3488 my_error(ER_OUT_OF_RESOURCES, MYF(0));
3491 if (statement->name.str && my_hash_insert(&names_hash, (uchar*) statement))
3493 my_error(ER_OUT_OF_RESOURCES, MYF(0));
3494 goto err_names_hash;
3504 if (prepared_stmt_count >= max_prepared_stmt_count)
3507 my_error(ER_MAX_PREPARED_STMT_COUNT_REACHED, MYF(0),
3508 max_prepared_stmt_count);
3511 prepared_stmt_count++;
3514 last_found_statement= statement;
3518 if (statement->name.str)
3519 my_hash_delete(&names_hash, (uchar*) statement);
3521 my_hash_delete(&
st_hash, (uchar*) statement);
3527 void Statement_map::close_transient_cursors()
3529 #ifdef TO_BE_IMPLEMENTED
3531 while ((stmt= transient_cursor_list.head()))
3532 stmt->close_cursor();
3537 void Statement_map::erase(Statement *statement)
3539 if (statement == last_found_statement)
3540 last_found_statement= 0;
3541 if (statement->name.str)
3542 my_hash_delete(&names_hash, (uchar *) statement);
3544 my_hash_delete(&
st_hash, (uchar *) statement);
3546 DBUG_ASSERT(prepared_stmt_count > 0);
3547 prepared_stmt_count--;
3552 void Statement_map::reset()
3556 DBUG_ASSERT(prepared_stmt_count >=
st_hash.records);
3557 prepared_stmt_count-=
st_hash.records;
3560 my_hash_reset(&names_hash);
3562 last_found_statement= 0;
3566 Statement_map::~Statement_map()
3572 DBUG_ASSERT(
st_hash.records == 0);
3574 my_hash_free(&names_hash);
3578 bool select_dumpvar::send_data(
List<Item> &items)
3584 DBUG_ENTER(
"select_dumpvar::send_data");
3586 if (unit->offset_limit_cnt)
3588 unit->offset_limit_cnt--;
3593 my_message(ER_TOO_MANY_ROWS, ER(ER_TOO_MANY_ROWS), MYF(0));
3596 while ((mv= var_li++) && (item= it++))
3600 if (thd->sp_runtime_ctx->set_variable(thd, mv->offset, &item))
3614 if (suv->fix_fields(thd, 0))
3621 DBUG_RETURN(thd->is_error());
3624 bool select_dumpvar::send_eof()
3627 push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
3628 ER_SP_FETCH_NO_DATA, ER(ER_SP_FETCH_NO_DATA));
3633 if (thd->is_error())
3636 ::my_ok(thd,row_count);
3644 void TMP_TABLE_PARAM::init()
3646 DBUG_ENTER(
"TMP_TABLE_PARAM::init");
3647 DBUG_PRINT(
"enter", (
"this: 0x%lx", (ulong)
this));
3648 field_count= sum_func_count= func_count= hidden_field_count= 0;
3649 group_parts= group_length= group_null_parts= 0;
3652 precomputed_group_by= 0;
3653 skip_create_table= 0;
3654 bit_fields_as_long= 0;
3662 void thd_increment_bytes_sent(ulong length)
3664 THD *thd=current_thd;
3665 if (likely(thd != 0))
3667 thd->status_var.bytes_sent+= length;
3672 void thd_increment_bytes_received(ulong length)
3674 current_thd->status_var.bytes_received+= length;
3678 void THD::set_status_var_init()
3680 memset(&status_var, 0,
sizeof(status_var));
3684 void Security_context::init()
3687 ip.set(
"", 0, system_charset_info);
3688 host.set(
"", 0, system_charset_info);
3689 external_user.set(
"", 0, system_charset_info);
3690 host_or_ip=
"connecting host";
3691 priv_user[0]= priv_host[0]= proxy_user[0]=
'\0';
3693 #ifndef NO_EMBEDDED_ACCESS_CHECKS
3694 db_access= NO_ACCESS;
3696 password_expired=
false;
3699 void Security_context::destroy()
3701 if (host.ptr() != my_localhost && host.length())
3703 char *c= (
char *) host.ptr();
3704 host.set(
"", 0, system_charset_info);
3708 if (user && user != delayed_user)
3714 if (external_user.length())
3716 char *c= (
char *) external_user.ptr();
3717 external_user.set(
"", 0, system_charset_info);
3723 char *c= (
char *) ip.ptr();
3724 ip.set(
"", 0, system_charset_info);
3731 void Security_context::skip_grants()
3734 host_or_ip= (
char *)
"";
3735 master_access= ~NO_ACCESS;
3736 *priv_user= *priv_host=
'\0';
3740 bool Security_context::set_user(
char *user_arg)
3743 user= my_strdup(user_arg, MYF(0));
3747 String *Security_context::get_host()
3752 String *Security_context::get_ip()
3757 String *Security_context::get_external_user()
3759 return (&external_user);
3762 void Security_context::set_host(
const char *str)
3764 uint len= str ? strlen(str) : 0;
3765 host.set(str, len, system_charset_info);
3768 void Security_context::set_ip(
const char *str)
3770 uint len= str ? strlen(str) : 0;
3771 ip.set(str, len, system_charset_info);
3774 void Security_context::set_external_user(
const char *str)
3776 uint len= str ? strlen(str) : 0;
3777 external_user.set(str, len, system_charset_info);
3780 void Security_context::set_host(
const char * str,
size_t len)
3782 host.set(str, len, system_charset_info);
3786 #ifndef NO_EMBEDDED_ACCESS_CHECKS
3838 change_security_context(THD *thd,
3842 Security_context **backup)
3846 DBUG_ENTER(
"Security_context::change_security_context");
3848 DBUG_ASSERT(definer_user->str && definer_host->str);
3851 needs_change= (strcmp(definer_user->str, thd->security_ctx->priv_user) ||
3852 my_strcasecmp(system_charset_info, definer_host->str,
3853 thd->security_ctx->priv_host));
3856 if (acl_getroot(
this, definer_user->str, definer_host->str,
3857 definer_host->str, db->str))
3859 my_error(ER_NO_SUCH_USER, MYF(0), definer_user->str,
3863 *backup= thd->security_ctx;
3864 thd->security_ctx=
this;
3872 Security_context::restore_security_context(THD *thd,
3873 Security_context *backup)
3876 thd->security_ctx= backup;
3881 bool Security_context::user_matches(Security_context *them)
3883 return ((user != NULL) && (them->user != NULL) &&
3884 !strcmp(user, them->user));
3888 void Log_throttle::new_window(ulonglong now)
3891 window_end= now + window_size;
3895 void Slow_log_throttle::new_window(ulonglong now)
3897 Log_throttle::new_window(now);
3903 Slow_log_throttle::Slow_log_throttle(ulong *threshold,
mysql_mutex_t *lock,
3905 bool (*logger)(THD *,
const char *, uint),
3907 : Log_throttle(window_usecs, msg), total_exec_time(0), total_lock_time(0),
3908 rate(threshold), log_summary(logger), LOCK_log_throttle(lock)
3910 aggregate_sctx.init();
3914 ulong Log_throttle::prepare_summary(ulong rate)
3931 void Slow_log_throttle::print_summary(THD *thd, ulong suppressed,
3932 ulonglong print_lock_time,
3933 ulonglong print_exec_time)
3941 ulonglong save_start_utime= thd->start_utime;
3942 ulonglong save_utime_after_lock= thd->utime_after_lock;
3943 Security_context *save_sctx= thd->security_ctx;
3947 snprintf(buf,
sizeof(buf), summary_template, suppressed);
3950 thd->start_utime= thd->current_utime() - print_exec_time;
3951 thd->utime_after_lock= thd->start_utime + print_lock_time;
3952 thd->security_ctx= (Security_context *) &aggregate_sctx;
3955 (*log_summary)(thd,
buf, strlen(buf));
3958 thd->security_ctx = save_sctx;
3959 thd->start_utime = save_start_utime;
3960 thd->utime_after_lock= save_utime_after_lock;
3965 bool Slow_log_throttle::flush(THD *thd)
3969 ulonglong print_lock_time= total_lock_time;
3970 ulonglong print_exec_time= total_exec_time;
3971 ulong suppressed_count= prepare_summary(*rate);
3973 if (suppressed_count > 0)
3975 print_summary(thd, suppressed_count, print_lock_time, print_exec_time);
3982 bool Slow_log_throttle::log(THD *thd,
bool eligible)
3984 bool suppress_current=
false;
3994 ulong suppressed_count= 0;
3995 ulonglong print_lock_time= total_lock_time;
3996 ulonglong print_exec_time= total_exec_time;
3997 ulonglong end_utime_of_query= thd->current_utime();
4003 if (!in_window(end_utime_of_query))
4005 suppressed_count= prepare_summary(*rate);
4008 new_window(end_utime_of_query);
4010 if (eligible && inc_log_count(*rate))
4016 total_exec_time += (end_utime_of_query - thd->start_utime);
4017 total_lock_time += (thd->utime_after_lock - thd->start_utime);
4018 suppress_current=
true;
4039 if (suppressed_count > 0)
4040 print_summary(thd, suppressed_count, print_lock_time, print_exec_time);
4043 return suppress_current;
4047 bool Error_log_throttle::log(THD *thd)
4049 ulonglong end_utime_of_query= thd->current_utime();
4055 if (!in_window(end_utime_of_query))
4057 ulong suppressed_count= prepare_summary(1);
4059 new_window(end_utime_of_query);
4061 if (suppressed_count > 0)
4062 print_summary(suppressed_count);
4068 return inc_log_count(1);
4072 bool Error_log_throttle::flush(THD *thd)
4075 ulong suppressed_count= prepare_summary(1);
4076 if (suppressed_count > 0)
4078 print_summary(suppressed_count);
4093 void THD::reset_n_backup_open_tables_state(Open_tables_backup *backup)
4095 DBUG_ENTER(
"reset_n_backup_open_tables_state");
4096 backup->set_open_tables_state(
this);
4097 backup->mdl_system_tables_svp= mdl_context.mdl_savepoint();
4098 reset_open_tables_state();
4099 state_flags|= Open_tables_state::BACKUPS_AVAIL;
4104 void THD::restore_backup_open_tables_state(Open_tables_backup *backup)
4106 DBUG_ENTER(
"restore_backup_open_tables_state");
4107 mdl_context.rollback_to_savepoint(backup->mdl_system_tables_svp);
4112 DBUG_ASSERT(
open_tables == 0 && temporary_tables == 0 &&
4113 derived_tables == 0 &&
4115 locked_tables_mode == LTM_NONE &&
4116 get_reprepare_observer() == NULL);
4118 set_open_tables_state(backup);
4128 extern "C" int thd_killed(
const MYSQL_THD thd)
4130 return(thd->killed);
4138 extern "C" unsigned long thd_get_thread_id(
const MYSQL_THD thd)
4140 return((
unsigned long)thd->thread_id);
4149 extern "C" int thd_allow_batch(MYSQL_THD thd)
4151 if ((thd->variables.option_bits & OPTION_ALLOW_BATCH) ||
4152 (thd->slave_thread && opt_slave_allow_batching))
4157 enum_tx_isolation thd_get_trx_isolation(
const MYSQL_THD thd)
4159 return thd->tx_isolation;
4162 #ifdef INNODB_COMPATIBILITY_HOOKS
4165 return(thd->charset());
4172 extern "C" char **thd_query(MYSQL_THD thd)
4174 return (&thd->query_string.string.str);
4183 extern "C" LEX_STRING * thd_query_string (MYSQL_THD thd)
4185 return(&thd->query_string.string);
4188 extern "C" int thd_slave_thread(
const MYSQL_THD thd)
4190 return(thd->slave_thread);
4193 extern "C" int thd_non_transactional_update(
const MYSQL_THD thd)
4195 return thd->transaction.all.has_modified_non_trans_table();
4198 extern "C" int thd_binlog_format(
const MYSQL_THD thd)
4200 if (mysql_bin_log.is_open() && (thd->variables.option_bits & OPTION_BIN_LOG))
4201 return (
int) thd->variables.binlog_format;
4203 return BINLOG_FORMAT_UNSPEC;
4206 extern "C" void thd_mark_transaction_to_rollback(MYSQL_THD thd,
bool all)
4208 mark_transaction_to_rollback(thd, all);
4211 extern "C" bool thd_binlog_filter_ok(
const MYSQL_THD thd)
4213 return binlog_filter->db_ok(thd->db);
4216 extern "C" bool thd_sqlcom_can_generate_row_events(
const MYSQL_THD thd)
4218 return sqlcom_can_generate_row_events(thd);
4221 extern "C" enum durability_properties thd_get_durability_property(
const MYSQL_THD thd)
4223 enum durability_properties ret= HA_REGULAR_DURABILITY;
4226 ret= thd->durability_property;
4236 extern "C" void thd_get_autoinc(
const MYSQL_THD thd, ulong* off, ulong* inc)
4238 *off = thd->variables.auto_increment_offset;
4239 *inc = thd->variables.auto_increment_increment;
4251 extern "C" bool thd_is_strict_mode(
const MYSQL_THD thd)
4253 return thd->is_strict_mode();
4257 #ifndef EMBEDDED_LIBRARY
4258 extern "C" void thd_pool_wait_begin(MYSQL_THD thd,
int wait_type);
4259 extern "C" void thd_pool_wait_end(MYSQL_THD thd);
4284 MYSQL_CALLBACK(thread_scheduler, thd_wait_begin, (thd, wait_type));
4293 extern "C" void thd_wait_end(MYSQL_THD thd)
4295 MYSQL_CALLBACK(thread_scheduler, thd_wait_end, (thd));
4304 extern "C" void thd_wait_end(MYSQL_THD thd)
4310 #endif // INNODB_COMPATIBILITY_HOOKS */
4341 void THD::reset_sub_statement_state(Sub_statement_state *backup,
4344 #ifndef EMBEDDED_LIBRARY
4349 if (rpl_master_erroneous_autoinc(
this))
4351 DBUG_ASSERT(backup->auto_inc_intervals_forced.nb_elements() == 0);
4352 auto_inc_intervals_forced.swap(&backup->auto_inc_intervals_forced);
4356 backup->option_bits= variables.option_bits;
4357 backup->count_cuted_fields= count_cuted_fields;
4358 backup->in_sub_stmt= in_sub_stmt;
4359 backup->enable_slow_log= enable_slow_log;
4360 backup->limit_found_rows= limit_found_rows;
4361 backup->examined_row_count= m_examined_row_count;
4362 backup->sent_row_count= m_sent_row_count;
4363 backup->cuted_fields= cuted_fields;
4364 backup->client_capabilities= client_capabilities;
4365 backup->savepoints= transaction.savepoints;
4366 backup->first_successful_insert_id_in_prev_stmt=
4367 first_successful_insert_id_in_prev_stmt;
4368 backup->first_successful_insert_id_in_cur_stmt=
4369 first_successful_insert_id_in_cur_stmt;
4371 if ((!lex->requires_prelocking() || is_update_query(lex->sql_command)) &&
4372 !is_current_stmt_binlog_format_row())
4374 variables.option_bits&= ~OPTION_BIN_LOG;
4377 if ((backup->option_bits & OPTION_BIN_LOG) &&
4378 is_update_query(lex->sql_command) &&
4379 !is_current_stmt_binlog_format_row())
4380 mysql_bin_log.start_union_events(
this, this->query_id);
4383 client_capabilities &= ~CLIENT_MULTI_RESULTS;
4384 in_sub_stmt|= new_state;
4385 m_examined_row_count= 0;
4386 m_sent_row_count= 0;
4388 transaction.savepoints= 0;
4389 first_successful_insert_id_in_cur_stmt= 0;
4393 void THD::restore_sub_statement_state(Sub_statement_state *backup)
4395 DBUG_ENTER(
"THD::restore_sub_statement_state");
4396 #ifndef EMBEDDED_LIBRARY
4401 if (rpl_master_erroneous_autoinc(
this))
4403 backup->auto_inc_intervals_forced.swap(&auto_inc_intervals_forced);
4404 DBUG_ASSERT(backup->auto_inc_intervals_forced.nb_elements() == 0);
4414 if (transaction.savepoints)
4417 for (sv= transaction.savepoints; sv->prev; sv= sv->prev)
4420 (void)ha_release_savepoint(
this, sv);
4422 count_cuted_fields= backup->count_cuted_fields;
4423 transaction.savepoints= backup->savepoints;
4424 variables.option_bits= backup->option_bits;
4425 in_sub_stmt= backup->in_sub_stmt;
4426 enable_slow_log= backup->enable_slow_log;
4427 first_successful_insert_id_in_prev_stmt=
4428 backup->first_successful_insert_id_in_prev_stmt;
4429 first_successful_insert_id_in_cur_stmt=
4430 backup->first_successful_insert_id_in_cur_stmt;
4431 limit_found_rows= backup->limit_found_rows;
4432 set_sent_row_count(backup->sent_row_count);
4433 client_capabilities= backup->client_capabilities;
4440 is_fatal_sub_stmt_error= FALSE;
4442 if ((variables.option_bits & OPTION_BIN_LOG) && is_update_query(lex->sql_command) &&
4443 !is_current_stmt_binlog_format_row())
4444 mysql_bin_log.stop_union_events(
this);
4450 inc_examined_row_count(backup->examined_row_count);
4451 cuted_fields+= backup->cuted_fields;
4456 void THD::set_statement(Statement *stmt)
4459 Statement::set_statement(stmt);
4463 void THD::set_sent_row_count(ha_rows count)
4465 m_sent_row_count= count;
4466 MYSQL_SET_STATEMENT_ROWS_SENT(m_statement_psi, m_sent_row_count);
4469 void THD::set_examined_row_count(ha_rows count)
4471 m_examined_row_count= count;
4472 MYSQL_SET_STATEMENT_ROWS_EXAMINED(m_statement_psi, m_examined_row_count);
4475 void THD::inc_sent_row_count(ha_rows count)
4477 m_sent_row_count+= count;
4478 MYSQL_SET_STATEMENT_ROWS_SENT(m_statement_psi, m_sent_row_count);
4481 void THD::inc_examined_row_count(ha_rows count)
4483 m_examined_row_count+= count;
4484 MYSQL_SET_STATEMENT_ROWS_EXAMINED(m_statement_psi, m_examined_row_count);
4487 void THD::inc_status_created_tmp_disk_tables()
4489 status_var_increment(status_var.created_tmp_disk_tables);
4490 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4491 PSI_STATEMENT_CALL(inc_statement_created_tmp_disk_tables)(m_statement_psi, 1);
4495 void THD::inc_status_created_tmp_tables()
4497 status_var_increment(status_var.created_tmp_tables);
4498 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4499 PSI_STATEMENT_CALL(inc_statement_created_tmp_tables)(m_statement_psi, 1);
4503 void THD::inc_status_select_full_join()
4505 status_var_increment(status_var.select_full_join_count);
4506 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4507 PSI_STATEMENT_CALL(inc_statement_select_full_join)(m_statement_psi, 1);
4511 void THD::inc_status_select_full_range_join()
4513 status_var_increment(status_var.select_full_range_join_count);
4514 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4515 PSI_STATEMENT_CALL(inc_statement_select_full_range_join)(m_statement_psi, 1);
4519 void THD::inc_status_select_range()
4521 status_var_increment(status_var.select_range_count);
4522 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4523 PSI_STATEMENT_CALL(inc_statement_select_range)(m_statement_psi, 1);
4527 void THD::inc_status_select_range_check()
4529 status_var_increment(status_var.select_range_check_count);
4530 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4531 PSI_STATEMENT_CALL(inc_statement_select_range_check)(m_statement_psi, 1);
4535 void THD::inc_status_select_scan()
4537 status_var_increment(status_var.select_scan_count);
4538 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4539 PSI_STATEMENT_CALL(inc_statement_select_scan)(m_statement_psi, 1);
4543 void THD::inc_status_sort_merge_passes()
4545 status_var_increment(status_var.filesort_merge_passes);
4546 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4547 PSI_STATEMENT_CALL(inc_statement_sort_merge_passes)(m_statement_psi, 1);
4551 void THD::inc_status_sort_range()
4553 status_var_increment(status_var.filesort_range_count);
4554 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4555 PSI_STATEMENT_CALL(inc_statement_sort_range)(m_statement_psi, 1);
4559 void THD::inc_status_sort_rows(ha_rows count)
4561 statistic_add(status_var.filesort_rows, count, &LOCK_status);
4562 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4563 PSI_STATEMENT_CALL(inc_statement_sort_rows)(m_statement_psi, count);
4567 void THD::inc_status_sort_scan()
4569 status_var_increment(status_var.filesort_scan_count);
4570 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4571 PSI_STATEMENT_CALL(inc_statement_sort_scan)(m_statement_psi, 1);
4575 void THD::set_status_no_index_used()
4577 server_status|= SERVER_QUERY_NO_INDEX_USED;
4578 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4579 PSI_STATEMENT_CALL(set_statement_no_index_used)(m_statement_psi);
4583 void THD::set_status_no_good_index_used()
4585 server_status|= SERVER_QUERY_NO_GOOD_INDEX_USED;
4586 #ifdef HAVE_PSI_STATEMENT_INTERFACE
4587 PSI_STATEMENT_CALL(set_statement_no_good_index_used)(m_statement_psi);
4591 void THD::set_command(
enum enum_server_command command)
4594 #ifdef HAVE_PSI_THREAD_INTERFACE
4595 PSI_STATEMENT_CALL(set_thread_command)(m_command);
4602 void THD::set_query(
const CSET_STRING &string_arg)
4605 set_query_inner(string_arg);
4608 #ifdef HAVE_PSI_THREAD_INTERFACE
4609 PSI_THREAD_CALL(set_thread_info)(
query(), query_length());
4615 void THD::set_query_and_id(
char *query_arg, uint32 query_length_arg,
4617 query_id_t new_query_id)
4620 set_query_inner(query_arg, query_length_arg, cs);
4621 query_id= new_query_id;
4627 void THD::set_query_id(query_id_t new_query_id)
4630 query_id= new_query_id;
4638 mysys_var= new_mysys_var;
4647 void THD::leave_locked_tables_mode()
4649 if (locked_tables_mode == LTM_LOCK_TABLES)
4656 mdl_context.set_transaction_duration_for_all_locks();
4661 global_read_lock.set_explicit_lock_duration(
this);
4663 if (handler_tables_hash.records)
4664 mysql_ha_set_explicit_lock_duration(
this);
4666 locked_tables_mode= LTM_NONE;
4669 void THD::get_definer(
LEX_USER *definer)
4672 #if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
4673 if (slave_thread && has_invoker())
4675 definer->user = invoker_user;
4676 definer->host= invoker_host;
4677 definer->password.str= NULL;
4678 definer->password.length= 0;
4679 definer->plugin.str= (
char *)
"";
4680 definer->plugin.length= 0;
4681 definer->auth.str= (
char *)
"";
4682 definer->auth.length= 0;
4697 void mark_transaction_to_rollback(THD *thd,
bool all)
4701 thd->is_fatal_sub_stmt_error= TRUE;
4702 thd->transaction_rollback_request= all;
4711 if (thd->lex->current_select)
4712 thd->lex->current_select->no_error= FALSE;
4722 extern "C" uchar *xid_get_hash_key(
const uchar *,
size_t *, my_bool);
4723 extern "C" void xid_free_hash(
void *);
4725 uchar *xid_get_hash_key(
const uchar *ptr,
size_t *length,
4726 my_bool not_used __attribute__((unused)))
4728 *length=((XID_STATE*)ptr)->xid.key_length();
4729 return ((XID_STATE*)ptr)->xid.key();
4732 void xid_free_hash(
void *ptr)
4734 if (!((XID_STATE*)ptr)->in_thd)
4738 #ifdef HAVE_PSI_INTERFACE
4739 static PSI_mutex_key key_LOCK_xid_cache;
4741 static PSI_mutex_info all_xid_mutexes[]=
4743 { &key_LOCK_xid_cache,
"LOCK_xid_cache", PSI_FLAG_GLOBAL}
4746 static void init_xid_psi_keys(
void)
4748 const char* category=
"sql";
4751 count= array_elements(all_xid_mutexes);
4756 bool xid_cache_init()
4758 #ifdef HAVE_PSI_INTERFACE
4759 init_xid_psi_keys();
4763 return my_hash_init(&xid_cache, &my_charset_bin, 100, 0, 0,
4764 xid_get_hash_key, xid_free_hash, 0) != 0;
4767 void xid_cache_free()
4769 if (my_hash_inited(&xid_cache))
4771 my_hash_free(&xid_cache);
4776 XID_STATE *xid_cache_search(
XID *xid)
4779 XID_STATE *res=(XID_STATE *)my_hash_search(&xid_cache, xid->key(),
4786 bool xid_cache_insert(
XID *xid,
enum xa_states xa_state)
4791 if (my_hash_search(&xid_cache, xid->key(), xid->key_length()))
4793 else if (!(xs=(XID_STATE *)my_malloc(
sizeof(*xs), MYF(MY_WME))))
4797 xs->xa_state=xa_state;
4801 res=my_hash_insert(&xid_cache, (uchar*)xs);
4808 bool xid_cache_insert(XID_STATE *xid_state)
4811 if (my_hash_search(&xid_cache, xid_state->xid.key(),
4812 xid_state->xid.key_length()))
4815 my_error(ER_XAER_DUPID, MYF(0));
4818 bool res= my_hash_insert(&xid_cache, (uchar*)xid_state);
4824 void xid_cache_delete(XID_STATE *xid_state)
4827 my_hash_delete(&xid_cache, (uchar *)xid_state);
4832 void THD::set_next_event_pos(
const char* _filename, ulonglong _pos)
4834 char*& filename= binlog_next_event_pos.file_name;
4835 if (filename == NULL)
4838 filename= (
char*) my_malloc(FN_REFLEN+1, MYF(MY_WME));
4839 if (filename == NULL)
return;
4842 assert(strlen(_filename) <= FN_REFLEN);
4843 strcpy(filename, _filename);
4844 filename[ FN_REFLEN ]= 0;
4846 binlog_next_event_pos.pos= _pos;
4849 void THD::clear_next_event_pos()
4851 if (binlog_next_event_pos.file_name != NULL)
4853 my_free(binlog_next_event_pos.file_name);
4855 binlog_next_event_pos.file_name= NULL;
4856 binlog_next_event_pos.pos= 0;
4859 void THD::set_user_connect(
USER_CONN *uc)
4861 DBUG_ENTER(
"THD::set_user_connect");
4868 void THD::increment_user_connections_counter()
4870 DBUG_ENTER(
"THD::increment_user_connections_counter");
4872 m_user_connect->connections++;
4877 void THD::decrement_user_connections_counter()
4879 DBUG_ENTER(
"THD::decrement_user_connections_counter");
4881 DBUG_ASSERT(m_user_connect->connections > 0);
4882 m_user_connect->connections--;
4887 void THD::increment_con_per_hour_counter()
4889 DBUG_ENTER(
"THD::decrement_conn_per_hour_counter");
4891 m_user_connect->conn_per_hour++;
4896 void THD::increment_updates_counter()
4898 DBUG_ENTER(
"THD::increment_updates_counter");
4900 m_user_connect->updates++;
4905 void THD::increment_questions_counter()
4907 DBUG_ENTER(
"THD::increment_updates_counter");
4909 m_user_connect->questions++;
4925 void THD::time_out_user_resource_limits()
4928 ulonglong check_time= start_utime;
4929 DBUG_ENTER(
"time_out_user_resource_limits");
4932 if (check_time - m_user_connect->reset_utime >= LL(3600000000))
4934 m_user_connect->questions=1;
4935 m_user_connect->updates=0;
4936 m_user_connect->conn_per_hour=0;
4937 m_user_connect->reset_utime= check_time;