57
67
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
60
static void unlock_locked_tables(THD *thd)
62
if (thd->locked_tables)
64
thd->lock=thd->locked_tables;
65
thd->locked_tables=0; // Will be automatically closed
66
close_thread_tables(thd); // Free tables
71
bool end_active_trans(THD *thd)
74
if (unlikely(thd->in_sub_stmt))
76
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
79
if (thd->transaction.xid_state.xa_state != XA_NOTR)
81
my_error(ER_XAER_RMFAIL, MYF(0),
82
xa_state_names[thd->transaction.xid_state.xa_state]);
85
if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
88
/* Safety if one did "drop table" on locked tables */
89
if (!thd->locked_tables)
90
thd->options&= ~OPTION_TABLE_LOCK;
91
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
95
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
96
thd->transaction.all.modified_non_trans_table= false;
101
bool begin_trans(THD *thd)
104
if (unlikely(thd->in_sub_stmt))
106
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
109
if (thd->locked_tables)
111
thd->lock=thd->locked_tables;
112
thd->locked_tables=0; // Will be automatically closed
113
close_thread_tables(thd); // Free tables
115
if (end_active_trans(thd))
120
thd->options|= OPTION_BEGIN;
121
thd->server_status|= SERVER_STATUS_IN_TRANS;
122
if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
123
error= ha_start_consistent_snapshot(thd);
129
Returns true if all tables should be ignored.
131
inline bool all_tables_not_ok(THD *thd, TableList *tables)
133
return rpl_filter->is_on() && tables &&
134
!rpl_filter->tables_ok(thd->db, tables);
138
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
140
for (TableList *table= tables; table; table= table->next_global)
142
assert(table->db && table->table_name);
143
if (table->updating &&
144
!find_temporary_table(thd, table->db, table->table_name))
152
71
Mark all commands that somehow changes a table.
224
138
bool is_update_query(enum enum_sql_command command)
226
140
assert(command >= 0 && command <= SQLCOM_END);
227
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
230
void execute_init_command(THD *thd, sys_var_str *init_command_var,
231
rw_lock_t *var_mutex)
234
ulong save_client_capabilities;
236
thd_proc_info(thd, "Execution of init_command");
238
We need to lock init_command_var because
239
during execution of init_command_var query
240
values of init_command_var can't be changed
242
rw_rdlock(var_mutex);
243
save_client_capabilities= thd->client_capabilities;
244
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
246
We don't need return result of execution to client side.
247
To forbid this we should set thd->net.vio to 0.
249
save_vio= thd->net.vio;
251
dispatch_command(COM_QUERY, thd,
252
init_command_var->value,
253
init_command_var->value_length);
254
rw_unlock(var_mutex);
255
thd->client_capabilities= save_client_capabilities;
256
thd->net.vio= save_vio;
260
Ends the current transaction and (maybe) begin the next.
262
@param thd Current thread
263
@param completion Completion type
269
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
274
if (unlikely(thd->in_sub_stmt))
276
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
279
if (thd->transaction.xid_state.xa_state != XA_NOTR)
281
my_error(ER_XAER_RMFAIL, MYF(0),
282
xa_state_names[thd->transaction.xid_state.xa_state]);
285
switch (completion) {
288
We don't use end_active_trans() here to ensure that this works
289
even if there is a problem with the OPTION_AUTO_COMMIT flag
290
(Which of course should never happen...)
292
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
294
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
295
thd->transaction.all.modified_non_trans_table= false;
298
do_release= 1; /* fall through */
299
case COMMIT_AND_CHAIN:
300
res= end_active_trans(thd);
301
if (!res && completion == COMMIT_AND_CHAIN)
302
res= begin_trans(thd);
304
case ROLLBACK_RELEASE:
305
do_release= 1; /* fall through */
307
case ROLLBACK_AND_CHAIN:
309
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
310
if (ha_rollback(thd))
312
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
thd->transaction.all.modified_non_trans_table= false;
314
if (!res && (completion == ROLLBACK_AND_CHAIN))
315
res= begin_trans(thd);
320
my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
325
my_error(thd->killed_errno(), MYF(0));
326
else if ((res == 0) && do_release)
327
thd->killed= THD::KILL_CONNECTION;
334
Read one command from connection and execute it (query or simple command).
335
This function is called in loop from thread function.
337
For profiling to work, it must never be called recursively.
342
1 request of thread shutdown (see dispatch_command() description)
345
bool do_command(THD *thd)
351
enum enum_server_command command;
354
indicator of uninitialized lex => normal flow of errors handling
357
thd->lex->current_select= 0;
360
This thread will do a blocking read from the client which
361
will be interrupted when the next command is received from
362
the client, the connection is closed or "net_wait_timeout"
363
number of seconds has passed
365
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
368
XXX: this code is here only to clear possible errors of init_connect.
369
Consider moving to init_connect() instead.
371
thd->clear_error(); // Clear error message
372
thd->main_da.reset_diagnostics_area();
374
net_new_transaction(net);
376
packet_length= my_net_read(net);
377
if (packet_length == packet_error)
379
/* Check if we can continue without closing the connection */
381
/* The error must be set. */
382
assert(thd->is_error());
383
net_end_statement(thd);
387
return_value= true; // We have to close it.
396
packet= (char*) net->read_pos;
398
'packet_length' contains length of data, as it was stored in packet
399
header. In case of malformed header, my_net_read returns zero.
400
If packet_length is not zero, my_net_read ensures that the returned
401
number of bytes was actually read from network.
402
There is also an extra safety measure in my_net_read:
403
it sets packet[packet_length]= 0, but only for non-zero packets.
405
if (packet_length == 0) /* safety */
407
/* Initialize with COM_SLEEP packet */
408
packet[0]= (unsigned char) COM_SLEEP;
411
/* Do not rely on my_net_read, extra safety against programming errors. */
412
packet[packet_length]= '\0'; /* safety */
414
command= (enum enum_server_command) (unsigned char) packet[0];
416
if (command >= COM_END)
417
command= COM_END; // Wrong command
419
/* Restore read timeout value */
420
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
422
assert(packet_length);
423
return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
426
return(return_value);
430
Determine if an attempt to update a non-temporary table while the
431
read-only option was enabled has been made.
433
This is a helper function to mysql_execute_command.
435
@note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
437
@see mysql_execute_command
440
@retval true The statement should be denied.
441
@retval false The statement isn't updating any relevant tables.
444
static bool deny_updates_if_read_only_option(THD *thd,
445
TableList *all_tables)
452
if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
455
/* Multi update is an exception and is dealt with later. */
456
if (lex->sql_command == SQLCOM_UPDATE_MULTI)
459
const bool create_temp_tables=
460
(lex->sql_command == SQLCOM_CREATE_TABLE) &&
461
(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
463
const bool drop_temp_tables=
464
(lex->sql_command == SQLCOM_DROP_TABLE) &&
467
const bool update_real_tables=
468
some_non_temp_table_to_be_updated(thd, all_tables) &&
469
!(create_temp_tables || drop_temp_tables);
472
const bool create_or_drop_databases=
473
(lex->sql_command == SQLCOM_CREATE_DB) ||
474
(lex->sql_command == SQLCOM_DROP_DB);
476
if (update_real_tables || create_or_drop_databases)
479
An attempt was made to modify one or more non-temporary tables.
485
/* Assuming that only temporary tables are modified. */
141
return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
490
145
Perform one connection-level (COM_XXXX) command.
492
147
@param command type of command to perform
493
@param thd connection handle
148
@param session connection handle
494
149
@param packet data for the command, packet is always null-terminated
495
150
@param packet_length length of packet + 1 (to show that data is
496
151
null-terminated) except for COM_SLEEP, where it
500
set thd->lex->sql_command to SQLCOM_END here.
155
set session->lex->sql_command to SQLCOM_END here.
502
157
The following has to be changed to an 8 byte integer
531
180
/* Increase id and count all other statements. */
533
statistic_increment(thd->status_var.questions, &LOCK_status);
182
statistic_increment(session->status_var.questions, &LOCK_status);
538
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
539
pthread_mutex_unlock(&LOCK_thread_count);
186
/* TODO: set session->lex->sql_command to SQLCOM_END here */
188
logging_pre_do(session);
190
session->server_status&=
544
191
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
545
192
switch (command) {
546
193
case COM_INIT_DB:
549
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
550
thd->convert_string(&tmp, system_charset_info,
551
packet, packet_length, thd->charset());
552
if (!mysql_change_db(thd, &tmp, false))
558
case COM_REGISTER_SLAVE:
560
if (!register_slave(thd, (unsigned char*)packet, packet_length))
564
case COM_CHANGE_USER:
566
status_var_increment(thd->status_var.com_other);
567
char *user= (char*) packet, *packet_end= packet + packet_length;
568
/* Safe because there is always a trailing \0 at the end of the packet */
569
char *passwd= strchr(user, '\0')+1;
572
thd->clear_error(); // if errors from rollback
575
Old clients send null-terminated string ('\0' for empty string) for
576
password. New clients send the size (1 byte) + string (not null
577
terminated, so also '\0' for empty string).
579
Cast *passwd to an unsigned char, so that it doesn't extend the sign
580
for *passwd > 127 and become 2**32-127 after casting to uint32_t.
582
char db_buff[NAME_LEN+1]; // buffer to store db in utf8
586
If there is no password supplied, the packet must contain '\0',
587
in any type of handshake (4.1 or pre-4.1).
589
if (passwd >= packet_end)
591
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
594
uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
595
(unsigned char)(*passwd++) : strlen(passwd));
596
uint32_t dummy_errors, save_db_length, db_length;
598
Security_context save_security_ctx= *thd->security_ctx;
599
USER_CONN *save_user_connect;
603
Database name is always NUL-terminated, so in case of empty database
604
the packet must contain at least the trailing '\0'.
606
if (db >= packet_end)
608
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
611
db_length= strlen(db);
613
char *ptr= db + db_length + 1;
614
uint32_t cs_number= 0;
616
if (ptr < packet_end)
618
if (ptr + 2 > packet_end)
620
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
624
cs_number= uint2korr(ptr);
627
/* Convert database name to utf8 */
628
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
629
system_charset_info, db, db_length,
630
thd->charset(), &dummy_errors)]= 0;
633
/* Save user and privileges */
634
save_db_length= thd->db_length;
636
save_user_connect= thd->user_connect;
638
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
640
thd->security_ctx->user= save_security_ctx.user;
641
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
645
/* Clear variables that are allocated */
646
thd->user_connect= 0;
647
res= check_user(thd, passwd, passwd_len, db, false);
651
if (thd->security_ctx->user)
652
free(thd->security_ctx->user);
653
*thd->security_ctx= save_security_ctx;
654
thd->user_connect= save_user_connect;
656
thd->db_length= save_db_length;
662
if (save_security_ctx.user)
663
free(save_security_ctx.user);
667
thd_init_client_charset(thd, cs_number);
668
thd->update_charset();
196
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
198
tmp.length= packet_length;
199
if (!mysql_change_db(session, &tmp, false))
675
if (alloc_query(thd, packet, packet_length))
207
if (! session->readAndStoreQuery(packet, packet_length))
676
208
break; // fatal error is set
677
char *packet_end= thd->query + thd->query_length;
678
209
const char* end_of_stmt= NULL;
680
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
682
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
684
char *beginning_of_next_stmt= (char*) end_of_stmt;
686
net_end_statement(thd);
688
Multiple queries exits, execute them individually
690
close_thread_tables(thd);
691
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
693
log_slow_statement(thd);
695
/* Remove garbage at start of query */
696
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
698
beginning_of_next_stmt++;
702
pthread_mutex_lock(&LOCK_thread_count);
703
thd->query_length= length;
704
thd->query= beginning_of_next_stmt;
706
Count each statement from the client.
708
statistic_increment(thd->status_var.questions, &LOCK_status);
709
thd->query_id= next_query_id();
710
thd->set_time(); /* Reset the query start time. */
711
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
712
pthread_mutex_unlock(&LOCK_thread_count);
714
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
718
case COM_FIELD_LIST: // This isn't actually needed
720
char *fields, *packet_end= packet + packet_length, *arg_end;
721
/* Locked closure of all tables */
722
TableList table_list;
723
LEX_STRING conv_name;
725
/* used as fields initializator */
728
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
memset(&table_list, 0, sizeof(table_list));
730
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
733
We have name + wildcard in packet, separated by endzero
735
arg_end= strchr(packet, '\0');
736
thd->convert_string(&conv_name, system_charset_info,
737
packet, (uint32_t) (arg_end - packet), thd->charset());
738
table_list.alias= table_list.table_name= conv_name.str;
741
if (!my_strcasecmp(system_charset_info, table_list.db,
742
INFORMATION_SCHEMA_NAME.str))
744
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
746
table_list.schema_table= schema_table;
749
thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
750
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
752
if (lower_case_table_names)
753
my_casedn_str(files_charset_info, table_list.table_name);
755
/* init structures for VIEW processing */
756
table_list.select_lex= &(thd->lex->select_lex);
759
mysql_reset_thd_for_next_command(thd);
762
select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
(unsigned char**) &table_list.next_local);
764
thd->lex->add_to_query_tables(&table_list);
766
/* switch on VIEW optimisation: do not fill temporary tables */
767
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
768
mysqld_list_fields(thd,&table_list,fields);
769
thd->lex->unit.cleanup();
770
thd->cleanup_after_query();
211
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
774
216
/* We don't calculate statistics for this command */
775
net->error=0; // Don't give 'abort' message
776
thd->main_da.disable_status(); // Don't send anything back
217
session->protocol->setError(0);
218
session->main_da.disable_status(); // Don't send anything back
777
219
error=true; // End server
779
case COM_BINLOG_DUMP:
783
uint32_t slave_server_id;
785
status_var_increment(thd->status_var.com_other);
786
thd->enable_slow_log= opt_log_slow_admin_statements;
787
/* TODO: The following has to be changed to an 8 byte integer */
788
pos = uint4korr(packet);
789
flags = uint2korr(packet + 4);
790
thd->server_id=0; /* avoid suicide */
791
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
792
kill_zombie_dump_threads(slave_server_id);
793
thd->server_id = slave_server_id;
795
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
796
unregister_slave(thd,1,1);
797
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
801
221
case COM_SHUTDOWN:
803
status_var_increment(thd->status_var.com_other);
805
close_thread_tables(thd); // Free before kill
223
status_var_increment(session->status_var.com_other);
225
session->close_thread_tables(); // Free before kill
811
status_var_increment(thd->status_var.com_other);
812
my_ok(thd); // Tell client we are alive
814
case COM_PROCESS_INFO:
815
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
mysqld_list_processes(thd, NULL, 0);
818
case COM_PROCESS_KILL:
820
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
821
ulong id=(ulong) uint4korr(packet);
822
sql_kill(thd,id,false);
827
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
828
uint32_t opt_command= uint2korr(packet);
830
switch (opt_command) {
831
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
832
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
835
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
836
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
840
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
231
status_var_increment(session->status_var.com_other);
232
session->my_ok(); // Tell client we are alive
846
235
case COM_CONNECT: // Impossible here
847
case COM_TIME: // Impossible from client
850
238
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
854
242
/* If commit fails, we should be able to reset the OK status. */
855
thd->main_da.can_overwrite_status= true;
856
ha_autocommit_or_rollback(thd, thd->is_error());
857
thd->main_da.can_overwrite_status= false;
243
session->main_da.can_overwrite_status= true;
244
ha_autocommit_or_rollback(session, session->is_error());
245
session->main_da.can_overwrite_status= false;
859
thd->transaction.stmt.reset();
247
session->transaction.stmt.reset();
862
250
/* report error issued during command execution */
863
if (thd->killed_errno())
865
if (! thd->main_da.is_set())
866
thd->send_kill_message();
868
if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
870
thd->killed= THD::NOT_KILLED;
871
thd->mysys_var->abort= 0;
874
net_end_statement(thd);
876
thd->set_proc_info("closing tables");
251
if (session->killed_errno())
253
if (! session->main_da.is_set())
254
session->send_kill_message();
256
if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
258
session->killed= Session::NOT_KILLED;
259
session->mysys_var->abort= 0;
262
/* Can not be true, but do not take chances in production. */
263
assert(! session->main_da.is_sent);
265
switch (session->main_da.status())
267
case Diagnostics_area::DA_ERROR:
268
/* The query failed, send error to log and abort bootstrap. */
269
session->protocol->sendError(session->main_da.sql_errno(),
270
session->main_da.message());
273
case Diagnostics_area::DA_EOF:
274
session->protocol->sendEOF();
277
case Diagnostics_area::DA_OK:
278
session->protocol->sendOK();
281
case Diagnostics_area::DA_DISABLED:
284
case Diagnostics_area::DA_EMPTY:
286
session->protocol->sendOK();
290
session->main_da.is_sent= true;
292
session->set_proc_info("closing tables");
877
293
/* Free tables */
878
close_thread_tables(thd);
880
log_slow_statement(thd);
882
thd_proc_info(thd, "cleaning up");
883
pthread_mutex_lock(&LOCK_thread_count); // For process list
884
thd_proc_info(thd, 0);
885
thd->command=COM_SLEEP;
889
pthread_mutex_unlock(&LOCK_thread_count);
890
thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
891
free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
294
session->close_thread_tables();
296
logging_post_do(session);
298
/* Store temp state for processlist */
299
session->set_proc_info("cleaning up");
300
session->command=COM_SLEEP;
301
memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
303
session->query_length=0;
305
session->set_proc_info(NULL);
306
session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
307
free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
896
void log_slow_statement(THD *thd)
899
The following should never be true with our current code base,
900
but better to keep this here so we don't accidently try to log a
901
statement in a trigger or stored function
903
if (unlikely(thd->in_sub_stmt))
904
return; // Don't set time for sub stmt
906
logging_post_do(thd);
913
313
Create a TableList object for an INFORMATION_SCHEMA table.
915
315
This function is used in the parser to convert a SHOW or DESCRIBE
916
316
table_name command to a SELECT from INFORMATION_SCHEMA.
917
It prepares a SELECT_LEX and a TableList object to represent the
317
It prepares a Select_Lex and a TableList object to represent the
918
318
given command as a SELECT parse tree.
920
@param thd thread handle
320
@param session thread handle
921
321
@param lex current lex
922
322
@param table_ident table alias if it's used
923
323
@param schema_table_idx the type of the INFORMATION_SCHEMA table to be
994
SELECT_LEX *select_lex= lex->current_select;
394
Select_Lex *select_lex= lex->current_select;
995
395
assert(select_lex);
996
if (make_schema_select(thd, select_lex, schema_table_idx))
396
if (make_schema_select(session, select_lex, schema_table_idx))
1000
400
TableList *table_list= (TableList*) select_lex->table_list.first;
1001
401
assert(table_list);
1002
402
table_list->schema_select_lex= schema_select_lex;
1003
table_list->schema_table_reformed= 1;
1009
Read query from packet and store in thd->query.
1010
Used in COM_QUERY and COM_STMT_PREPARE.
1012
Sets the following THD variables:
1019
true error; In this case thd->fatal_error is set
1022
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
1024
/* Remove garbage at start and end of query */
1025
while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
1030
const char *pos= packet + packet_length; // Point at end null
1031
while (packet_length > 0 &&
1032
(pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
1037
/* We must allocate some extra memory for query cache */
1038
thd->query_length= 0; // Extra safety: Avoid races
1039
if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
1041
thd->db_length+ 1)))
1043
thd->query[packet_length]=0;
1044
thd->query_length= packet_length;
1046
/* Reclaim some memory */
1047
thd->packet.shrink(thd->variables.net_buffer_length);
1048
thd->convert_buffer.shrink(thd->variables.net_buffer_length);
1053
static void reset_one_shot_variables(THD *thd)
1055
thd->variables.character_set_client=
1056
global_system_variables.character_set_client;
1057
thd->variables.collation_connection=
1058
global_system_variables.collation_connection;
1059
thd->variables.collation_database=
1060
global_system_variables.collation_database;
1061
thd->variables.collation_server=
1062
global_system_variables.collation_server;
1063
thd->update_charset();
1064
thd->variables.time_zone=
1065
global_system_variables.time_zone;
1066
thd->variables.lc_time_names= &my_locale_en_US;
1067
thd->one_shot_set= 0;
1072
Execute command saved in thd and lex->sql_command.
408
Execute command saved in session and lex->sql_command.
1074
410
Before every operation that can request a write lock for a table
1075
411
wait if a global read lock exists. However do not wait if this
1102
mysql_execute_command(THD *thd)
438
mysql_execute_command(Session *session)
1105
441
bool need_start_waiting= false; // have protection against global read lock
1108
/* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1109
SELECT_LEX *select_lex= &lex->select_lex;
1110
/* first table of first SELECT_LEX */
442
LEX *lex= session->lex;
443
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
444
Select_Lex *select_lex= &lex->select_lex;
445
/* first table of first Select_Lex */
1111
446
TableList *first_table= (TableList*) select_lex->table_list.first;
1112
447
/* list of all tables in query */
1113
448
TableList *all_tables;
1114
/* most outer SELECT_LEX_UNIT of query */
1115
SELECT_LEX_UNIT *unit= &lex->unit;
1116
/* Saved variable value */
449
/* most outer Select_Lex_Unit of query */
450
Select_Lex_Unit *unit= &lex->unit;
451
/* A peek into the query string */
452
size_t proc_info_len= session->query_length > PROCESS_LIST_WIDTH ?
453
PROCESS_LIST_WIDTH : session->query_length;
455
memcpy(session->process_list_info, session->query, proc_info_len);
456
session->process_list_info[proc_info_len]= '\0';
1119
In many cases first table of main SELECT_LEX have special meaning =>
1120
check that it is first table in global list and relink it first in
459
In many cases first table of main Select_Lex have special meaning =>
460
check that it is first table in global list and relink it first in
1121
461
queries_tables list if it is necessary (we need such relinking only
1122
462
for queries with subqueries in select list, in this case tables of
1123
463
subqueries will go to global list first)
1125
all_tables will differ from first_table only if most upper SELECT_LEX
465
all_tables will differ from first_table only if most upper Select_Lex
1126
466
do not contain tables.
1128
468
Because of above in place where should be at least one table in most
1129
outer SELECT_LEX we have following check:
469
outer Select_Lex we have following check:
1130
470
assert(first_table == all_tables);
1131
471
assert(first_table == all_tables && first_table != 0);
1146
486
Don't reset warnings when executing a stored routine.
1148
488
if (all_tables || !lex->is_single_level_stmt())
1149
drizzle_reset_errors(thd, 0);
1151
if (unlikely(thd->slave_thread))
1154
Check if statment should be skipped because of slave filtering
1158
- UPDATE MULTI: For this statement, we want to check the filtering
1159
rules later in the code
1160
- SET: we always execute it (Not that many SET commands exists in
1161
the binary log anyway -- only 4.1 masters write SET statements,
1162
in 5.0 there are no SET statements in the binary log)
1163
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1164
have stale files on slave caused by exclusion of one tmp table).
1166
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1167
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1168
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1169
lex->drop_temporary && lex->drop_if_exists) &&
1170
all_tables_not_ok(thd, all_tables))
1172
/* we warn the slave SQL thread */
1173
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1174
if (thd->one_shot_set)
1177
It's ok to check thd->one_shot_set here:
1179
The charsets in a MySQL 5.0 slave can change by both a binlogged
1180
SET ONE_SHOT statement and the event-internal charset setting,
1181
and these two ways to change charsets do not seems to work
1184
At least there seems to be problems in the rli cache for
1185
charsets if we are using ONE_SHOT. Note that this is normally no
1186
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1187
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1189
reset_one_shot_variables(thd);
1197
When option readonly is set deny operations which change non-temporary
1198
tables. Except for the replication thread and the 'super' users.
1200
if (deny_updates_if_read_only_option(thd, all_tables))
1202
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1205
} /* endif unlikely slave */
1206
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1208
assert(thd->transaction.stmt.modified_non_trans_table == false);
489
drizzle_reset_errors(session, 0);
491
status_var_increment(session->status_var.com_stat[lex->sql_command]);
493
assert(session->transaction.stmt.modified_non_trans_table == false);
1210
495
switch (lex->sql_command) {
1211
496
case SQLCOM_SHOW_STATUS:
1213
system_status_var old_status_var= thd->status_var;
1214
thd->initial_status_var= &old_status_var;
1215
res= execute_sqlcom_select(thd, all_tables);
498
system_status_var old_status_var= session->status_var;
499
session->initial_status_var= &old_status_var;
500
res= execute_sqlcom_select(session, all_tables);
1216
501
/* Don't log SHOW STATUS commands to slow query log */
1217
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
502
session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1218
503
SERVER_QUERY_NO_GOOD_INDEX_USED);
1220
505
restore status variables, as we don't want 'show status' to cause
1223
508
pthread_mutex_lock(&LOCK_status);
1224
add_diff_to_status(&global_status_var, &thd->status_var,
509
add_diff_to_status(&global_status_var, &session->status_var,
1225
510
&old_status_var);
1226
thd->status_var= old_status_var;
511
session->status_var= old_status_var;
1227
512
pthread_mutex_unlock(&LOCK_status);
1516
720
/* Prepare stack copies to be re-execution safe */
1517
721
HA_CREATE_INFO create_info;
1518
Alter_info alter_info(lex->alter_info, thd->mem_root);
722
Alter_info alter_info(lex->alter_info, session->mem_root);
1520
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
724
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1523
727
assert(first_table == all_tables && first_table != 0);
1524
if (end_active_trans(thd))
728
if (! session->endActiveTransaction())
1527
Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1528
and thus classify as slow administrative statements just like
1531
thd->enable_slow_log= opt_log_slow_admin_statements;
1533
731
memset(&create_info, 0, sizeof(create_info));
1534
732
create_info.db_type= 0;
1535
733
create_info.row_type= ROW_TYPE_NOT_USED;
1536
create_info.default_table_charset= thd->variables.collation_database;
734
create_info.default_table_charset= get_default_db_collation(session->db);
1538
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
736
res= mysql_alter_table(session, first_table->db, first_table->table_name,
1539
737
&create_info, first_table, &alter_info,
1540
738
0, (order_st*) 0, 0);
1543
case SQLCOM_SLAVE_START:
1545
pthread_mutex_lock(&LOCK_active_mi);
1546
start_slave(thd,active_mi,1 /* net report*/);
1547
pthread_mutex_unlock(&LOCK_active_mi);
1550
case SQLCOM_SLAVE_STOP:
1552
If the client thread has locked tables, a deadlock is possible.
1554
- the client thread does LOCK TABLE t READ.
1555
- then the master updates t.
1556
- then the SQL slave thread wants to update t,
1557
so it waits for the client thread because t is locked by it.
1558
- then the client thread does SLAVE STOP.
1559
SLAVE STOP waits for the SQL slave thread to terminate its
1560
update t, which waits for the client thread because t is locked by it.
1561
To prevent that, refuse SLAVE STOP if the
1562
client thread has locked tables
1564
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1566
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1567
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1571
pthread_mutex_lock(&LOCK_active_mi);
1572
stop_slave(thd,active_mi,1/* net report*/);
1573
pthread_mutex_unlock(&LOCK_active_mi);
1577
741
case SQLCOM_ALTER_TABLE:
1578
742
assert(first_table == all_tables && first_table != 0);
1706
860
case SQLCOM_OPTIMIZE:
1708
862
assert(first_table == all_tables && first_table != 0);
1709
thd->enable_slow_log= opt_log_slow_admin_statements;
1710
res= mysql_optimize_table(thd, first_table, &lex->check_opt);
863
res= mysql_optimize_table(session, first_table, &lex->check_opt);
1711
864
/* ! we write after unlocking the table */
1712
write_bin_log(thd, true, thd->query, thd->query_length);
865
write_bin_log(session, true, session->query, session->query_length);
1713
866
select_lex->table_list.first= (unsigned char*) first_table;
1714
867
lex->query_tables=all_tables;
1717
870
case SQLCOM_UPDATE:
1718
871
assert(first_table == all_tables && first_table != 0);
1719
if (update_precheck(thd, all_tables))
872
if ((res= update_precheck(session, all_tables)))
1721
874
assert(select_lex->offset_limit == 0);
1722
875
unit->set_limit(select_lex);
1723
res= (up_result= mysql_update(thd, all_tables,
1724
select_lex->item_list,
1727
select_lex->order_list.elements,
1728
(order_st *) select_lex->order_list.first,
1729
unit->select_limit_cnt,
1730
lex->duplicates, lex->ignore));
1731
/* mysql_update return 2 if we need to switch to multi-update */
876
res= mysql_update(session, all_tables,
877
select_lex->item_list,
880
select_lex->order_list.elements,
881
(order_st *) select_lex->order_list.first,
882
unit->select_limit_cnt,
883
lex->duplicates, lex->ignore);
1735
885
case SQLCOM_UPDATE_MULTI:
1737
887
assert(first_table == all_tables && first_table != 0);
1738
/* if we switched from normal update, rights are checked */
1741
if ((res= multi_update_precheck(thd, all_tables)))
1747
res= mysql_multi_update_prepare(thd);
1749
/* Check slave filtering rules */
1750
if (unlikely(thd->slave_thread))
1752
if (all_tables_not_ok(thd, all_tables))
1756
res= 0; /* don't care of prev failure */
1757
thd->clear_error(); /* filters are of highest prior */
1759
/* we warn the slave SQL thread */
1760
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1771
some_non_temp_table_to_be_updated(thd, all_tables))
1773
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1778
res= mysql_multi_update(thd, all_tables,
888
if ((res= update_precheck(session, all_tables)))
891
if ((res= mysql_multi_update_prepare(session)))
894
res= mysql_multi_update(session, all_tables,
1779
895
&select_lex->item_list,
1780
896
&lex->value_list,
1781
897
select_lex->where,
1911
1019
assert(first_table == all_tables && first_table != 0);
1912
1020
TableList *aux_tables=
1913
(TableList *)thd->lex->auxiliary_table_list.first;
1021
(TableList *)session->lex->auxiliary_table_list.first;
1914
1022
multi_delete *del_result;
1916
if (!thd->locked_tables &&
1917
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1024
if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1923
if ((res= multi_delete_precheck(thd, all_tables)))
1030
if ((res= multi_delete_precheck(session, all_tables)))
1926
1033
/* condition will be true on SP re-excuting */
1927
1034
if (select_lex->item_list.elements != 0)
1928
1035
select_lex->item_list.empty();
1929
if (add_item_to_list(thd, new Item_null()))
1036
if (session->add_item_to_list(new Item_null()))
1932
thd_proc_info(thd, "init");
1933
if ((res= open_and_lock_tables(thd, all_tables)))
1039
session->set_proc_info("init");
1040
if ((res= session->open_and_lock_tables(all_tables)))
1936
if ((res= mysql_multi_delete_prepare(thd)))
1043
if ((res= mysql_multi_delete_prepare(session)))
1939
if (!thd->is_fatal_error &&
1046
if (!session->is_fatal_error &&
1940
1047
(del_result= new multi_delete(aux_tables, lex->table_count)))
1942
res= mysql_select(thd, &select_lex->ref_pointer_array,
1049
res= mysql_select(session, &select_lex->ref_pointer_array,
1943
1050
select_lex->get_table_list(),
1944
1051
select_lex->with_wild,
1945
1052
select_lex->item_list,
1946
1053
select_lex->where,
1947
1054
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1949
select_lex->options | thd->options |
1950
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
OPTION_SETUP_TABLES_DONE,
1055
select_lex->options | session->options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK | OPTION_SETUP_TABLES_DONE,
1952
1056
del_result, unit, select_lex);
1953
res|= thd->is_error();
1057
res|= session->is_error();
1955
1059
del_result->abort();
1956
1060
delete del_result;
2069
1138
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2070
1139
false, mysqldump will not work.
2072
unlock_locked_tables(thd);
2073
if (thd->options & OPTION_TABLE_LOCK)
2075
end_active_trans(thd);
2076
thd->options&= ~(OPTION_TABLE_LOCK);
2078
if (thd->global_read_lock)
2079
unlock_global_read_lock(thd);
2082
case SQLCOM_LOCK_TABLES:
2084
We try to take transactional locks if
2085
- only transactional locks are requested (lex->lock_transactional) and
2086
- no non-transactional locks exist (!thd->locked_tables).
2088
if (lex->lock_transactional && !thd->locked_tables)
2092
All requested locks are transactional and no non-transactional
2095
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2103
Non-transactional locking has been requested or
2104
non-transactional locks exist already or transactional locks are
2105
not supported by all storage engines. Take non-transactional
2110
One or more requested locks are non-transactional and/or
2111
non-transactional locks exist or a storage engine does not support
2112
transactional locks. Check if at least one transactional lock is
2113
requested. If yes, warn about the conversion to non-transactional
2114
locks or abort in strict mode.
2116
if (check_transactional_lock(thd, all_tables))
2118
unlock_locked_tables(thd);
2119
/* we must end the trasaction first, regardless of anything */
2120
if (end_active_trans(thd))
2122
thd->in_lock_tables=1;
2123
thd->options|= OPTION_TABLE_LOCK;
2125
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2127
thd->locked_tables=thd->lock;
2129
(void) set_handler_table_locks(thd, all_tables, false);
2135
Need to end the current transaction, so the storage engine (InnoDB)
2136
can free its locks if LOCK TABLES locked some tables before finding
2137
that it can't lock a table in its list
2139
ha_autocommit_or_rollback(thd, 1);
2140
end_active_trans(thd);
2141
thd->options&= ~(OPTION_TABLE_LOCK);
2143
thd->in_lock_tables=0;
1141
if (session->global_read_lock)
1142
unlock_global_read_lock(session);
2145
1145
case SQLCOM_CREATE_DB:
2276
1228
Presumably, RESET and binlog writing doesn't require synchronization
2278
write_bin_log(thd, false, thd->query, thd->query_length);
1230
write_bin_log(session, false, session->query, session->query_length);
2284
1236
case SQLCOM_KILL:
2286
1238
Item *it= (Item *)lex->value_list.head();
2288
if (lex->table_or_sp_used())
2290
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2291
"function calls as part of this statement");
2295
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
1240
if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2297
1242
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2301
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
1246
sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2304
1249
case SQLCOM_BEGIN:
2305
if (thd->transaction.xid_state.xa_state != XA_NOTR)
1250
if (session->transaction.xid_state.xa_state != XA_NOTR)
2307
1252
my_error(ER_XAER_RMFAIL, MYF(0),
2308
xa_state_names[thd->transaction.xid_state.xa_state]);
1253
xa_state_names[session->transaction.xid_state.xa_state]);
2312
1257
Breakpoints for backup testing.
2314
if (begin_trans(thd))
1259
if (! session->startTransaction())
2318
1263
case SQLCOM_COMMIT:
2319
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2320
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
1264
if (! session->endTransaction(lex->tx_release ? COMMIT_RELEASE : lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2324
1268
case SQLCOM_ROLLBACK:
2325
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2326
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
1269
if (! session->endTransaction(lex->tx_release ? ROLLBACK_RELEASE : lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2330
1273
case SQLCOM_RELEASE_SAVEPOINT:
2333
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
1276
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2335
1278
if (my_strnncoll(system_charset_info,
2336
1279
(unsigned char *)lex->ident.str, lex->ident.length,
2531
/****************************************************************************
2532
Check stack size; Send error if there isn't enough stack to continue
2533
****************************************************************************/
2534
#if STACK_DIRECTION < 0
2535
#define used_stack(A,B) (long) (A - B)
2537
#define used_stack(A,B) (long) (B - A)
2542
Note: The 'buf' parameter is necessary, even if it is unused here.
2543
- fix_fields functions has a "dummy" buffer large enough for the
2544
corresponding exec. (Thus we only have to check in fix_fields.)
2545
- Passing to check_stack_overrun() prevents the compiler from removing it.
2547
bool check_stack_overrun(THD *thd, long margin,
2548
unsigned char *buf __attribute__((unused)))
2551
assert(thd == current_thd);
2552
if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
2553
(long) (my_thread_stack_size - margin))
2555
sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2556
stack_used,my_thread_stack_size,margin);
2557
my_message(ER_STACK_OVERRUN_NEED_MORE,errbuff[0],MYF(ME_FATALERROR));
2563
1454
#define MY_YACC_INIT 1000 // Start with big alloc
2564
1455
#define MY_YACC_MAX 32000 // Because of 'short'
2566
1457
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2568
LEX *lex= current_thd->lex;
1459
LEX *lex= current_session->lex;
2569
1460
ulong old_info=0;
2570
1461
if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2572
1463
if (!lex->yacc_yyvs)
2573
1464
old_info= *yystacksize;
2574
1465
*yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2575
if (!(lex->yacc_yyvs= (unsigned char*)
2576
my_realloc(lex->yacc_yyvs,
2577
*yystacksize*sizeof(**yyvs),
2578
MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
2579
!(lex->yacc_yyss= (unsigned char*)
2580
my_realloc(lex->yacc_yyss,
2581
*yystacksize*sizeof(**yyss),
2582
MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
1466
unsigned char *tmpptr= NULL;
1467
if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
1468
*yystacksize* sizeof(**yyvs))))
1470
lex->yacc_yyvs= tmpptr;
1472
if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
1473
*yystacksize* sizeof(**yyss))))
1475
lex->yacc_yyss= tmpptr;
2585
1477
{ // Copy old info from stack
2586
1478
memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2596
Reset THD part responsible for command processing state.
2598
This needs to be called before execution of every statement
2599
(prepared or conventional).
2600
It is not called by substatements of routines.
2603
Make it a method of THD and align its name with the rest of
2604
reset/end/start/init methods.
2606
Call it after we use THD for queries, not before.
2609
void mysql_reset_thd_for_next_command(THD *thd)
2611
assert(! thd->in_sub_stmt);
2613
thd->select_number= 1;
2615
Those two lines below are theoretically unneeded as
2616
THD::cleanup_after_query() should take care of this already.
2618
thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2619
thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2621
thd->query_start_used= 0;
2622
thd->is_fatal_error= thd->time_zone_used= 0;
2623
thd->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS |
2624
SERVER_QUERY_NO_INDEX_USED |
2625
SERVER_QUERY_NO_GOOD_INDEX_USED);
2627
If in autocommit mode and not in a transaction, reset
2628
OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2629
in ha_rollback_trans() about some tables couldn't be rolled back.
2631
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2633
thd->options&= ~OPTION_KEEP_LOG;
2634
thd->transaction.all.modified_non_trans_table= false;
2636
assert(thd->security_ctx== &thd->main_security_ctx);
2637
thd->thread_specific_used= false;
2641
reset_dynamic(&thd->user_var_events);
2642
thd->user_var_events_alloc= thd->mem_root;
2645
thd->main_da.reset_diagnostics_area();
2646
thd->total_warn_count=0; // Warnings for this query
2648
thd->sent_row_count= thd->examined_row_count= 0;
2651
Because we come here only for start of top-statements, binlog format is
2652
constant inside a complex statement (using stored functions) etc.
2654
thd->reset_current_stmt_binlog_row_based();
2661
1488
mysql_init_select(LEX *lex)
2663
SELECT_LEX *select_lex= lex->current_select;
1490
Select_Lex *select_lex= lex->current_select;
2664
1491
select_lex->init_select();
2666
1493
if (select_lex == &lex->select_lex)
3620
2392
@return Error status code
3622
@retval !=0 Error; thd->killed is set or thd->is_error() is true
2394
@retval !=0 Error; session->killed is set or session->is_error() is true
3625
bool reload_cache(THD *thd, ulong options, TableList *tables,
3626
bool *write_to_binlog)
2397
bool reload_cache(Session *session, ulong options, TableList *tables)
3629
2400
select_errors=0; /* Write if more errors */
3630
bool tmp_write_to_binlog= 1;
3632
assert(!thd || !thd->in_sub_stmt);
3634
2402
if (options & REFRESH_LOG)
3637
Flush the normal query log, the update log, the binary log,
3638
the slow query log, the relay log (if it exists) and the log
3643
Writing this command to the binlog may result in infinite loops
3644
when doing mysqlbinlog|mysql, and anyway it does not really make
3645
sense to log it automatically (would cause more trouble to users
3646
than it would help them)
3648
tmp_write_to_binlog= 0;
3649
if( mysql_bin_log.is_open() )
3651
mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
3653
pthread_mutex_lock(&LOCK_active_mi);
3654
rotate_relay_log(active_mi);
3655
pthread_mutex_unlock(&LOCK_active_mi);
3657
/* flush slow and general logs */
3658
logger.flush_logs(thd);
3660
2404
if (ha_flush_logs(NULL))
3662
if (flush_error_log())
3666
2408
Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
3667
2409
(see sql_yacc.yy)
3669
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
2411
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3671
if ((options & REFRESH_READ_LOCK) && thd)
2413
if ((options & REFRESH_READ_LOCK) && session)
3674
We must not try to aspire a global read lock if we have a write
3675
locked table. This would lead to a deadlock when trying to
3676
reopen (and re-lock) the table after the flush.
3678
if (thd->locked_tables)
3680
THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
3681
THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
3683
for (; lock_p < end_p; lock_p++)
3685
if ((*lock_p)->type >= TL_WRITE_ALLOW_WRITE)
3687
my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
3693
Writing to the binlog could cause deadlocks, as we don't log
3696
tmp_write_to_binlog= 0;
3697
if (lock_global_read_lock(thd))
3699
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
2415
if (lock_global_read_lock(session))
2416
return true; // Killed
2417
result= close_cached_tables(session, tables, (options & REFRESH_FAST) ?
3700
2418
false : true, true);
3701
if (make_global_read_lock_block_commit(thd)) // Killed
2419
if (make_global_read_lock_block_commit(session)) // Killed
3703
2421
/* Don't leave things in a half-locked state */
3704
unlock_global_read_lock(thd);
2422
unlock_global_read_lock(session);
3709
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
2428
result= close_cached_tables(session, tables, (options & REFRESH_FAST) ?
3710
2429
false : true, false);
3713
if (thd && (options & REFRESH_STATUS))
3714
refresh_status(thd);
3715
if (options & REFRESH_THREADS)
3716
flush_thread_cache();
3717
if (options & REFRESH_MASTER)
3720
tmp_write_to_binlog= 0;
3721
if (reset_master(thd))
3726
if (options & REFRESH_SLAVE)
3728
tmp_write_to_binlog= 0;
3729
pthread_mutex_lock(&LOCK_active_mi);
3730
if (reset_slave(thd, active_mi))
3732
pthread_mutex_unlock(&LOCK_active_mi);
3734
*write_to_binlog= tmp_write_to_binlog;
2431
if (session && (options & REFRESH_STATUS))
2432
session->refresh_status();