16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
#include <libdrizzleclient/libdrizzle.h>
19
#include <mysys/hash.h>
20
#include <drizzled/logging.h>
21
#include <drizzled/db.h>
22
#include <drizzled/error.h>
23
#include <drizzled/nested_join.h>
24
#include <drizzled/query_id.h>
25
#include <drizzled/sql_parse.h>
26
#include <drizzled/data_home.h>
27
#include <drizzled/sql_base.h>
28
#include <drizzled/show.h>
29
#include <drizzled/rename.h>
30
#include <drizzled/function/time/unix_timestamp.h>
31
#include <drizzled/function/get_system_var.h>
32
#include <drizzled/item/cmpfunc.h>
33
#include <drizzled/item/null.h>
34
#include <drizzled/session.h>
35
#include <drizzled/sql_load.h>
36
#include <drizzled/connect.h>
37
#include <drizzled/lock.h>
38
#include <drizzled/select_send.h>
19
#include "rpl_filter.h"
20
#include "repl_failsafe.h"
22
#include <drizzled/drizzled_error_messages.h>
44
25
@defgroup Runtime_Environment Runtime Environment
48
extern size_t my_thread_stack_size;
49
extern const CHARSET_INFO *character_set_filesystem;
50
30
const char *any_db="*any*"; // Special symbol for check_access
52
32
const LEX_STRING command_name[COM_END+1]={
75
57
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
78
static void unlock_locked_tables(Session *session)
60
static void unlock_locked_tables(THD *thd)
80
if (session->locked_tables)
62
if (thd->locked_tables)
82
session->lock=session->locked_tables;
83
session->locked_tables=0; // Will be automatically closed
84
close_thread_tables(session); // Free tables
64
thd->lock=thd->locked_tables;
65
thd->locked_tables=0; // Will be automatically closed
66
close_thread_tables(thd); // Free tables
89
bool end_active_trans(Session *session)
71
bool end_active_trans(THD *thd)
93
if (session->transaction.xid_state.xa_state != XA_NOTR)
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)
95
81
my_error(ER_XAER_RMFAIL, MYF(0),
96
xa_state_names[session->transaction.xid_state.xa_state]);
82
xa_state_names[thd->transaction.xid_state.xa_state]);
99
if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
85
if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
100
86
OPTION_TABLE_LOCK))
102
88
/* Safety if one did "drop table" on locked tables */
103
if (!session->locked_tables)
104
session->options&= ~OPTION_TABLE_LOCK;
105
session->server_status&= ~SERVER_STATUS_IN_TRANS;
106
if (ha_commit(session))
89
if (!thd->locked_tables)
90
thd->options&= ~OPTION_TABLE_LOCK;
91
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
109
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
110
session->transaction.all.modified_non_trans_table= false;
95
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
96
thd->transaction.all.modified_non_trans_table= false;
115
bool begin_trans(Session *session)
101
bool begin_trans(THD *thd)
118
if (session->locked_tables)
120
session->lock=session->locked_tables;
121
session->locked_tables=0; // Will be automatically closed
122
close_thread_tables(session); // Free tables
124
if (end_active_trans(session))
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))
128
LEX *lex= session->lex;
129
session->options|= OPTION_BEGIN;
130
session->server_status|= SERVER_STATUS_IN_TRANS;
120
thd->options|= OPTION_BEGIN;
121
thd->server_status|= SERVER_STATUS_IN_TRANS;
131
122
if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
132
error= ha_start_consistent_snapshot(session);
123
error= ha_start_consistent_snapshot(thd);
190
193
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
191
194
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
192
195
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
196
sql_command_flags[SQLCOM_SHOW_CHARSETS]= CF_STATUS_COMMAND;
197
sql_command_flags[SQLCOM_SHOW_COLLATIONS]= CF_STATUS_COMMAND;
198
sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
199
sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
200
sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
193
201
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
194
202
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
195
203
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
196
204
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
197
205
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
198
206
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
207
sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
208
sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
200
210
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
201
211
CF_SHOW_TABLE_COMMAND);
214
224
bool is_update_query(enum enum_sql_command command)
216
226
assert(command >= 0 && command <= SQLCOM_END);
217
return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
227
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
220
void execute_init_command(Session *session, sys_var_str *init_command_var,
221
pthread_rwlock_t *var_mutex)
230
void execute_init_command(THD *thd, sys_var_str *init_command_var,
231
rw_lock_t *var_mutex)
224
234
ulong save_client_capabilities;
226
session->set_proc_info("Execution of init_command");
236
thd_proc_info(thd, "Execution of init_command");
228
238
We need to lock init_command_var because
229
239
during execution of init_command_var query
230
240
values of init_command_var can't be changed
232
pthread_rwlock_rdlock(var_mutex);
233
save_client_capabilities= session->client_capabilities;
234
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
242
rw_rdlock(var_mutex);
243
save_client_capabilities= thd->client_capabilities;
244
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
236
246
We don't need return result of execution to client side.
237
To forbid this we should set session->net.vio to 0.
247
To forbid this we should set thd->net.vio to 0.
239
save_vio= session->net.vio;
241
dispatch_command(COM_QUERY, session,
249
save_vio= thd->net.vio;
251
dispatch_command(COM_QUERY, thd,
242
252
init_command_var->value,
243
253
init_command_var->value_length);
244
pthread_rwlock_unlock(var_mutex);
245
session->client_capabilities= save_client_capabilities;
246
session->net.vio= save_vio;
254
rw_unlock(var_mutex);
255
thd->client_capabilities= save_client_capabilities;
256
thd->net.vio= save_vio;
250
260
Ends the current transaction and (maybe) begin the next.
252
@param session Current thread
262
@param thd Current thread
253
263
@param completion Completion type
259
int end_trans(Session *session, enum enum_mysql_completiontype completion)
269
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
261
271
bool do_release= 0;
264
if (session->transaction.xid_state.xa_state != XA_NOTR)
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)
266
281
my_error(ER_XAER_RMFAIL, MYF(0),
267
xa_state_names[session->transaction.xid_state.xa_state]);
282
xa_state_names[thd->transaction.xid_state.xa_state]);
270
285
switch (completion) {
274
289
even if there is a problem with the OPTION_AUTO_COMMIT flag
275
290
(Which of course should never happen...)
277
session->server_status&= ~SERVER_STATUS_IN_TRANS;
278
res= ha_commit(session);
279
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
280
session->transaction.all.modified_non_trans_table= false;
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;
282
297
case COMMIT_RELEASE:
283
298
do_release= 1; /* fall through */
284
299
case COMMIT_AND_CHAIN:
285
res= end_active_trans(session);
300
res= end_active_trans(thd);
286
301
if (!res && completion == COMMIT_AND_CHAIN)
287
res= begin_trans(session);
302
res= begin_trans(thd);
289
304
case ROLLBACK_RELEASE:
290
305
do_release= 1; /* fall through */
292
307
case ROLLBACK_AND_CHAIN:
294
session->server_status&= ~SERVER_STATUS_IN_TRANS;
295
if (ha_rollback(session))
309
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
310
if (ha_rollback(thd))
297
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
298
session->transaction.all.modified_non_trans_table= false;
312
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
thd->transaction.all.modified_non_trans_table= false;
299
314
if (!res && (completion == ROLLBACK_AND_CHAIN))
300
res= begin_trans(session);
315
res= begin_trans(thd);
347
362
the client, the connection is closed or "net_wait_timeout"
348
363
number of seconds has passed
350
drizzleclient_net_set_read_timeout(net, session->variables.net_wait_timeout);
365
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
353
XXX: this code is here only to clear possible errors of init_connect.
368
XXX: this code is here only to clear possible errors of init_connect.
354
369
Consider moving to init_connect() instead.
356
session->clear_error(); // Clear error message
357
session->main_da.reset_diagnostics_area();
371
thd->clear_error(); // Clear error message
372
thd->main_da.reset_diagnostics_area();
359
374
net_new_transaction(net);
361
packet_length= drizzleclient_net_read(net);
376
packet_length= my_net_read(net);
362
377
if (packet_length == packet_error)
364
379
/* Check if we can continue without closing the connection */
366
381
/* The error must be set. */
367
/* This assert is killing me - and tracking down why the error isn't
368
* set here is a waste since the protocol lib is being replaced. */
369
//assert(session->is_error());
370
drizzleclient_net_end_statement(session);
382
assert(thd->is_error());
383
net_end_statement(thd);
372
385
if (net->error != 3)
404
417
command= COM_END; // Wrong command
406
419
/* Restore read timeout value */
407
drizzleclient_net_set_read_timeout(net, session->variables.net_read_timeout);
420
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
409
422
assert(packet_length);
410
return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
423
return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
413
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. */
417
490
Perform one connection-level (COM_XXXX) command.
419
492
@param command type of command to perform
420
@param session connection handle
493
@param thd connection handle
421
494
@param packet data for the command, packet is always null-terminated
422
495
@param packet_length length of packet + 1 (to show that data is
423
496
null-terminated) except for COM_SLEEP, where it
427
set session->lex->sql_command to SQLCOM_END here.
500
set thd->lex->sql_command to SQLCOM_END here.
429
502
The following has to be changed to an 8 byte integer
434
507
1 request of thread shutdown, i. e. if command is
435
508
COM_QUIT/COM_SHUTDOWN
437
bool dispatch_command(enum enum_server_command command, Session *session,
438
char* packet, uint32_t packet_length)
510
bool dispatch_command(enum enum_server_command command, THD *thd,
511
char* packet, uint32_t packet_length)
440
NET *net= &session->net;
442
Query_id &query_id= Query_id::get_query_id();
444
session->command=command;
445
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
516
thd->command=command;
518
Commands which always take a long time are logged into
519
the slow log only if opt_log_slow_admin_statements is set.
521
thd->enable_slow_log= true;
522
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
447
524
pthread_mutex_lock(&LOCK_thread_count);
448
session->query_id= query_id.value();
525
thd->query_id= global_query_id;
450
527
switch( command ) {
451
528
/* Ignore these statements. */
454
531
/* Increase id and count all other statements. */
456
statistic_increment(session->status_var.questions, &LOCK_status);
533
statistic_increment(thd->status_var.questions, &LOCK_status);
460
537
thread_running++;
461
/* TODO: set session->lex->sql_command to SQLCOM_END here */
538
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
462
539
pthread_mutex_unlock(&LOCK_thread_count);
464
logging_pre_do(session);
466
session->server_status&=
467
544
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
468
545
switch (command) {
469
546
case COM_INIT_DB:
472
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
473
session->convert_string(&tmp, system_charset_info,
474
packet, packet_length, session->charset());
475
if (!mysql_change_db(session, &tmp, false))
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))
481
564
case COM_CHANGE_USER:
483
status_var_increment(session->status_var.com_other);
566
status_var_increment(thd->status_var.com_other);
484
567
char *user= (char*) packet, *packet_end= packet + packet_length;
485
568
/* Safe because there is always a trailing \0 at the end of the packet */
486
569
char *passwd= strchr(user, '\0')+1;
489
session->clear_error(); // if errors from rollback
572
thd->clear_error(); // if errors from rollback
492
575
Old clients send null-terminated string ('\0' for empty string) for
544
627
/* Convert database name to utf8 */
545
628
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
546
629
system_charset_info, db, db_length,
547
session->charset(), &dummy_errors)]= 0;
630
thd->charset(), &dummy_errors)]= 0;
550
633
/* Save user and privileges */
551
save_db_length= session->db_length;
552
save_db= session->db;
553
save_user_connect= session->user_connect;
634
save_db_length= thd->db_length;
636
save_user_connect= thd->user_connect;
555
old_username= session->security_ctx.user;
556
session->security_ctx.user.assign(user);
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));
558
645
/* Clear variables that are allocated */
559
session->user_connect= 0;
560
res= check_user(session, passwd, passwd_len, db);
646
thd->user_connect= 0;
647
res= check_user(thd, passwd, passwd_len, db, false);
564
session->security_ctx.user= old_username;
565
session->user_connect= save_user_connect;
566
session->db= save_db;
567
session->db_length= save_db_length;
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);
576
session->update_charset();
667
thd_init_client_charset(thd, cs_number);
668
thd->update_charset();
583
if (alloc_query(session, packet, packet_length))
675
if (alloc_query(thd, packet, packet_length))
584
676
break; // fatal error is set
585
char *packet_end= session->query + session->query_length;
677
char *packet_end= thd->query + thd->query_length;
586
678
const char* end_of_stmt= NULL;
588
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
680
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
590
while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
682
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
592
684
char *beginning_of_next_stmt= (char*) end_of_stmt;
594
drizzleclient_net_end_statement(session);
686
net_end_statement(thd);
596
688
Multiple queries exits, execute them individually
598
close_thread_tables(session);
690
close_thread_tables(thd);
599
691
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
601
log_slow_statement(session);
693
log_slow_statement(thd);
603
695
/* Remove garbage at start of query */
604
while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
696
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
606
698
beginning_of_next_stmt++;
702
pthread_mutex_lock(&LOCK_thread_count);
703
thd->query_length= length;
704
thd->query= beginning_of_next_stmt;
611
706
Count each statement from the client.
613
statistic_increment(session->status_var.questions, &LOCK_status);
614
session->query_id= query_id.next();
615
session->set_time(); /* Reset the query start time. */
616
pthread_mutex_lock(&LOCK_thread_count);
617
session->query_length= length;
618
session->query= beginning_of_next_stmt;
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 */
619
712
pthread_mutex_unlock(&LOCK_thread_count);
620
/* TODO: set session->lex->sql_command to SQLCOM_END here */
622
mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
714
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
631
723
LEX_STRING conv_name;
633
725
/* used as fields initializator */
636
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
728
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
637
729
memset(&table_list, 0, sizeof(table_list));
638
if (session->copy_db_to(&table_list.db, &table_list.db_length))
730
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
641
733
We have name + wildcard in packet, separated by endzero
643
735
arg_end= strchr(packet, '\0');
644
session->convert_string(&conv_name, system_charset_info,
645
packet, (uint32_t) (arg_end - packet), session->charset());
736
thd->convert_string(&conv_name, system_charset_info,
737
packet, (uint32_t) (arg_end - packet), thd->charset());
646
738
table_list.alias= table_list.table_name= conv_name.str;
647
739
packet= arg_end + 1;
649
741
if (!my_strcasecmp(system_charset_info, table_list.db,
650
INFORMATION_SCHEMA_NAME.c_str()))
742
INFORMATION_SCHEMA_NAME.str))
652
ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
744
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
653
745
if (schema_table)
654
746
table_list.schema_table= schema_table;
657
session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
658
if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
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)))
660
752
if (lower_case_table_names)
661
753
my_casedn_str(files_charset_info, table_list.table_name);
663
755
/* init structures for VIEW processing */
664
table_list.select_lex= &(session->lex->select_lex);
667
session->reset_for_next_command();
756
table_list.select_lex= &(thd->lex->select_lex);
759
mysql_reset_thd_for_next_command(thd);
670
762
select_lex.table_list.link_in_list((unsigned char*) &table_list,
671
763
(unsigned char**) &table_list.next_local);
672
session->lex->add_to_query_tables(&table_list);
764
thd->lex->add_to_query_tables(&table_list);
674
766
/* switch on VIEW optimisation: do not fill temporary tables */
675
session->lex->sql_command= SQLCOM_SHOW_FIELDS;
676
mysqld_list_fields(session,&table_list,fields);
677
session->lex->unit.cleanup();
678
session->cleanup_after_query();
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();
682
774
/* We don't calculate statistics for this command */
683
775
net->error=0; // Don't give 'abort' message
684
session->main_da.disable_status(); // Don't send anything back
776
thd->main_da.disable_status(); // Don't send anything back
685
777
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 */
687
801
case COM_SHUTDOWN:
689
status_var_increment(session->status_var.com_other);
691
close_thread_tables(session); // Free before kill
803
status_var_increment(thd->status_var.com_other);
805
close_thread_tables(thd); // Free before kill
697
status_var_increment(session->status_var.com_other);
698
session->my_ok(); // Tell client we are alive
811
status_var_increment(thd->status_var.com_other);
812
my_ok(thd); // Tell client we are alive
700
814
case COM_PROCESS_INFO:
701
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
702
mysqld_list_processes(session, NULL, 0);
815
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
mysqld_list_processes(thd, NULL, 0);
704
818
case COM_PROCESS_KILL:
706
status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
820
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
707
821
ulong id=(ulong) uint4korr(packet);
708
sql_kill(session,id,false);
822
sql_kill(thd,id,false);
711
825
case COM_SET_OPTION:
713
status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
827
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
714
828
uint32_t opt_command= uint2korr(packet);
716
830
switch (opt_command) {
717
831
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
718
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
832
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
721
835
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
722
session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
836
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
726
840
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
740
854
/* If commit fails, we should be able to reset the OK status. */
741
session->main_da.can_overwrite_status= true;
742
ha_autocommit_or_rollback(session, session->is_error());
743
session->main_da.can_overwrite_status= false;
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;
745
session->transaction.stmt.reset();
859
thd->transaction.stmt.reset();
748
862
/* report error issued during command execution */
749
if (session->killed_errno())
751
if (! session->main_da.is_set())
752
session->send_kill_message();
754
if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
756
session->killed= Session::NOT_KILLED;
757
session->mysys_var->abort= 0;
760
drizzleclient_net_end_statement(session);
762
session->set_proc_info("closing tables");
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");
763
877
/* Free tables */
764
close_thread_tables(session);
766
log_slow_statement(session);
768
session->set_proc_info("cleaning up");
878
close_thread_tables(thd);
880
log_slow_statement(thd);
882
thd_proc_info(thd, "cleaning up");
769
883
pthread_mutex_lock(&LOCK_thread_count); // For process list
770
session->set_proc_info(0);
771
session->command=COM_SLEEP;
773
session->query_length=0;
884
thd_proc_info(thd, 0);
885
thd->command=COM_SLEEP;
774
888
thread_running--;
775
889
pthread_mutex_unlock(&LOCK_thread_count);
776
session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
777
free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
890
thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
891
free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
782
void log_slow_statement(Session *session)
896
void log_slow_statement(THD *thd)
784
logging_post_do(session);
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);
887
Read query from packet and store in session->query.
1009
Read query from packet and store in thd->query.
888
1010
Used in COM_QUERY and COM_STMT_PREPARE.
890
Sets the following Session variables:
1012
Sets the following THD variables:
897
true error; In this case session->fatal_error is set
1019
true error; In this case thd->fatal_error is set
900
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
1022
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
902
1024
/* Remove garbage at start and end of query */
903
while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
1025
while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
906
1028
packet_length--;
908
1030
const char *pos= packet + packet_length; // Point at end null
909
1031
while (packet_length > 0 &&
910
(pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
1032
(pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
913
1035
packet_length--;
915
1037
/* We must allocate some extra memory for query cache */
916
session->query_length= 0; // Extra safety: Avoid races
917
if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
1038
thd->query_length= 0; // Extra safety: Avoid races
1039
if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
919
session->db_length+ 1)))
1041
thd->db_length+ 1)))
921
session->query[packet_length]=0;
922
session->query_length= packet_length;
1043
thd->query[packet_length]=0;
1044
thd->query_length= packet_length;
924
1046
/* Reclaim some memory */
925
session->packet.shrink(session->variables.net_buffer_length);
926
session->convert_buffer.shrink(session->variables.net_buffer_length);
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;
932
Execute command saved in session and lex->sql_command.
1072
Execute command saved in thd and lex->sql_command.
934
1074
Before every operation that can request a write lock for a table
935
1075
wait if a global read lock exists. However do not wait if this
962
mysql_execute_command(Session *session)
1102
mysql_execute_command(THD *thd)
965
1105
bool need_start_waiting= false; // have protection against global read lock
966
LEX *lex= session->lex;
967
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
968
Select_Lex *select_lex= &lex->select_lex;
969
/* first table of first Select_Lex */
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 */
970
1111
TableList *first_table= (TableList*) select_lex->table_list.first;
971
1112
/* list of all tables in query */
972
1113
TableList *all_tables;
973
/* most outer Select_Lex_Unit of query */
974
Select_Lex_Unit *unit= &lex->unit;
1114
/* most outer SELECT_LEX_UNIT of query */
1115
SELECT_LEX_UNIT *unit= &lex->unit;
975
1116
/* Saved variable value */
978
In many cases first table of main Select_Lex have special meaning =>
979
check that it is first table in global list and relink it first in
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
980
1121
queries_tables list if it is necessary (we need such relinking only
981
1122
for queries with subqueries in select list, in this case tables of
982
1123
subqueries will go to global list first)
984
all_tables will differ from first_table only if most upper Select_Lex
1125
all_tables will differ from first_table only if most upper SELECT_LEX
985
1126
do not contain tables.
987
1128
Because of above in place where should be at least one table in most
988
outer Select_Lex we have following check:
1129
outer SELECT_LEX we have following check:
989
1130
assert(first_table == all_tables);
990
1131
assert(first_table == all_tables && first_table != 0);
1005
1146
Don't reset warnings when executing a stored routine.
1007
1148
if (all_tables || !lex->is_single_level_stmt())
1008
drizzle_reset_errors(session, 0);
1010
status_var_increment(session->status_var.com_stat[lex->sql_command]);
1012
assert(session->transaction.stmt.modified_non_trans_table == false);
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);
1014
1210
switch (lex->sql_command) {
1015
1211
case SQLCOM_SHOW_STATUS:
1017
system_status_var old_status_var= session->status_var;
1018
session->initial_status_var= &old_status_var;
1019
res= execute_sqlcom_select(session, all_tables);
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);
1020
1216
/* Don't log SHOW STATUS commands to slow query log */
1021
session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1217
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1022
1218
SERVER_QUERY_NO_GOOD_INDEX_USED);
1024
1220
restore status variables, as we don't want 'show status' to cause
1027
1223
pthread_mutex_lock(&LOCK_status);
1028
add_diff_to_status(&global_status_var, &session->status_var,
1224
add_diff_to_status(&global_status_var, &thd->status_var,
1029
1225
&old_status_var);
1030
session->status_var= old_status_var;
1226
thd->status_var= old_status_var;
1031
1227
pthread_mutex_unlock(&LOCK_status);
1038
1234
case SQLCOM_SHOW_FIELDS:
1039
1235
case SQLCOM_SHOW_KEYS:
1040
1236
case SQLCOM_SHOW_VARIABLES:
1237
case SQLCOM_SHOW_CHARSETS:
1238
case SQLCOM_SHOW_COLLATIONS:
1041
1239
case SQLCOM_SELECT:
1043
session->status_var.last_query_cost= 0.0;
1044
res= execute_sqlcom_select(session, all_tables);
1241
thd->status_var.last_query_cost= 0.0;
1242
res= execute_sqlcom_select(thd, all_tables);
1047
1245
case SQLCOM_EMPTY_QUERY:
1251
res = purge_master_logs(thd, lex->to_log);
1254
case SQLCOM_PURGE_BEFORE:
1258
/* PURGE MASTER LOGS BEFORE 'data' */
1259
it= (Item *)lex->value_list.head();
1260
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1263
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1266
it= new Item_func_unix_timestamp(it);
1268
it is OK only emulate fix_fieds, because we need only
1271
it->quick_fix_field();
1272
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1051
1275
case SQLCOM_SHOW_WARNS:
1053
res= mysqld_show_warnings(session, (uint32_t)
1277
res= mysqld_show_warnings(thd, (uint32_t)
1054
1278
((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1055
1279
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1056
1280
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1060
1284
case SQLCOM_SHOW_ERRORS:
1062
res= mysqld_show_warnings(session, (uint32_t)
1286
res= mysqld_show_warnings(thd, (uint32_t)
1063
1287
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1290
case SQLCOM_SHOW_SLAVE_HOSTS:
1292
res = show_slave_hosts(thd);
1295
case SQLCOM_SHOW_BINLOG_EVENTS:
1297
res = mysql_show_binlog_events(thd);
1066
1301
case SQLCOM_ASSIGN_TO_KEYCACHE:
1068
1303
assert(first_table == all_tables && first_table != 0);
1069
res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1304
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1307
case SQLCOM_CHANGE_MASTER:
1309
pthread_mutex_lock(&LOCK_active_mi);
1310
res = change_master(thd,active_mi);
1311
pthread_mutex_unlock(&LOCK_active_mi);
1314
case SQLCOM_SHOW_SLAVE_STAT:
1316
pthread_mutex_lock(&LOCK_active_mi);
1317
if (active_mi != NULL)
1319
res = show_master_info(thd, active_mi);
1323
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1324
"the master info structure does not exist");
1327
pthread_mutex_unlock(&LOCK_active_mi);
1330
case SQLCOM_SHOW_MASTER_STAT:
1332
res = show_binlog_info(thd);
1072
1336
case SQLCOM_SHOW_ENGINE_STATUS:
1074
res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1338
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1077
1341
case SQLCOM_CREATE_TABLE:
1252
1516
/* Prepare stack copies to be re-execution safe */
1253
1517
HA_CREATE_INFO create_info;
1254
Alter_info alter_info(lex->alter_info, session->mem_root);
1518
Alter_info alter_info(lex->alter_info, thd->mem_root);
1256
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1520
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1259
1523
assert(first_table == all_tables && first_table != 0);
1260
if (end_active_trans(session))
1524
if (end_active_trans(thd))
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;
1263
1533
memset(&create_info, 0, sizeof(create_info));
1264
1534
create_info.db_type= 0;
1265
1535
create_info.row_type= ROW_TYPE_NOT_USED;
1266
create_info.default_table_charset= session->variables.collation_database;
1536
create_info.default_table_charset= thd->variables.collation_database;
1268
res= mysql_alter_table(session, first_table->db, first_table->table_name,
1538
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1269
1539
&create_info, first_table, &alter_info,
1270
1540
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);
1273
1577
case SQLCOM_ALTER_TABLE:
1274
1578
assert(first_table == all_tables && first_table != 0);
1300
1604
/* Don't yet allow changing of symlinks with ALTER TABLE */
1301
1605
if (create_info.data_file_name)
1302
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1606
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1303
1607
"DATA DIRECTORY option ignored");
1304
1608
if (create_info.index_file_name)
1305
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1609
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1306
1610
"INDEX DIRECTORY option ignored");
1307
1611
create_info.data_file_name= create_info.index_file_name= NULL;
1308
1612
/* ALTER TABLE ends previous transaction */
1309
if (end_active_trans(session))
1613
if (end_active_trans(thd))
1312
if (!session->locked_tables &&
1313
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1616
if (!thd->locked_tables &&
1617
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1319
res= mysql_alter_table(session, select_lex->db, lex->name.str,
1623
thd->enable_slow_log= opt_log_slow_admin_statements;
1624
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1340
1645
new_list= table->next_local[0];
1343
if (end_active_trans(session) || drizzle_rename_tables(session, first_table, 0))
1648
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1654
case SQLCOM_SHOW_BINLOGS:
1656
res = show_binlogs(thd);
1349
1659
case SQLCOM_SHOW_CREATE:
1350
1660
assert(first_table == all_tables && first_table != 0);
1352
res= mysqld_show_create(session, first_table);
1662
res= mysqld_show_create(thd, first_table);
1355
1665
case SQLCOM_CHECKSUM:
1357
1667
assert(first_table == all_tables && first_table != 0);
1358
res = mysql_checksum_table(session, first_table, &lex->check_opt);
1668
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1361
1671
case SQLCOM_REPAIR:
1363
1673
assert(first_table == all_tables && first_table != 0);
1364
res= mysql_repair_table(session, first_table, &lex->check_opt);
1674
thd->enable_slow_log= opt_log_slow_admin_statements;
1675
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1365
1676
/* ! we write after unlocking the table */
1367
1678
Presumably, REPAIR and binlog writing doesn't require synchronization
1369
write_bin_log(session, true, session->query, session->query_length);
1680
write_bin_log(thd, true, thd->query, thd->query_length);
1370
1681
select_lex->table_list.first= (unsigned char*) first_table;
1371
1682
lex->query_tables=all_tables;
1393
1706
case SQLCOM_OPTIMIZE:
1395
1708
assert(first_table == all_tables && first_table != 0);
1396
res= mysql_optimize_table(session, first_table, &lex->check_opt);
1709
thd->enable_slow_log= opt_log_slow_admin_statements;
1710
res= mysql_optimize_table(thd, first_table, &lex->check_opt);
1397
1711
/* ! we write after unlocking the table */
1398
write_bin_log(session, true, session->query, session->query_length);
1712
write_bin_log(thd, true, thd->query, thd->query_length);
1399
1713
select_lex->table_list.first= (unsigned char*) first_table;
1400
1714
lex->query_tables=all_tables;
1403
1717
case SQLCOM_UPDATE:
1404
1718
assert(first_table == all_tables && first_table != 0);
1405
if ((res= update_precheck(session, all_tables)))
1719
if (update_precheck(thd, all_tables))
1407
1721
assert(select_lex->offset_limit == 0);
1408
1722
unit->set_limit(select_lex);
1409
res= mysql_update(session, all_tables,
1410
select_lex->item_list,
1413
select_lex->order_list.elements,
1414
(order_st *) select_lex->order_list.first,
1415
unit->select_limit_cnt,
1416
lex->duplicates, lex->ignore);
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 */
1418
1735
case SQLCOM_UPDATE_MULTI:
1420
1737
assert(first_table == all_tables && first_table != 0);
1421
if ((res= update_precheck(session, all_tables)))
1424
if ((res= mysql_multi_update_prepare(session)))
1427
res= mysql_multi_update(session, all_tables,
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,
1428
1779
&select_lex->item_list,
1429
1780
&lex->value_list,
1430
1781
select_lex->where,
1436
1787
case SQLCOM_INSERT:
1438
1789
assert(first_table == all_tables && first_table != 0);
1439
if ((res= insert_precheck(session, all_tables)))
1790
if ((res= insert_precheck(thd, all_tables)))
1442
if (!session->locked_tables &&
1443
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1793
if (!thd->locked_tables &&
1794
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1449
res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1800
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1450
1801
lex->update_list, lex->value_list,
1451
1802
lex->duplicates, lex->ignore);
1470
1821
unit->set_limit(select_lex);
1472
if (! session->locked_tables &&
1473
! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1823
if (! thd->locked_tables &&
1824
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1479
if (!(res= open_and_lock_tables(session, all_tables)))
1830
if (!(res= open_and_lock_tables(thd, all_tables)))
1481
1832
/* Skip first table, which is the table we are inserting in */
1482
1833
TableList *second_table= first_table->next_local;
1483
1834
select_lex->table_list.first= (unsigned char*) second_table;
1484
select_lex->context.table_list=
1835
select_lex->context.table_list=
1485
1836
select_lex->context.first_name_resolution_table= second_table;
1486
res= mysql_insert_select_prepare(session);
1837
res= mysql_insert_select_prepare(thd);
1487
1838
if (!res && (sel_result= new select_insert(first_table,
1488
1839
first_table->table,
1489
1840
&lex->field_list,
1560
1911
assert(first_table == all_tables && first_table != 0);
1561
1912
TableList *aux_tables=
1562
(TableList *)session->lex->auxiliary_table_list.first;
1913
(TableList *)thd->lex->auxiliary_table_list.first;
1563
1914
multi_delete *del_result;
1565
if (!session->locked_tables &&
1566
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1916
if (!thd->locked_tables &&
1917
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1572
if ((res= multi_delete_precheck(session, all_tables)))
1923
if ((res= multi_delete_precheck(thd, all_tables)))
1575
1926
/* condition will be true on SP re-excuting */
1576
1927
if (select_lex->item_list.elements != 0)
1577
1928
select_lex->item_list.empty();
1578
if (session->add_item_to_list(new Item_null()))
1929
if (add_item_to_list(thd, new Item_null()))
1581
session->set_proc_info("init");
1582
if ((res= open_and_lock_tables(session, all_tables)))
1932
thd_proc_info(thd, "init");
1933
if ((res= open_and_lock_tables(thd, all_tables)))
1585
if ((res= mysql_multi_delete_prepare(session)))
1936
if ((res= mysql_multi_delete_prepare(thd)))
1588
if (!session->is_fatal_error &&
1939
if (!thd->is_fatal_error &&
1589
1940
(del_result= new multi_delete(aux_tables, lex->table_count)))
1591
res= mysql_select(session, &select_lex->ref_pointer_array,
1942
res= mysql_select(thd, &select_lex->ref_pointer_array,
1592
1943
select_lex->get_table_list(),
1593
1944
select_lex->with_wild,
1594
1945
select_lex->item_list,
1595
1946
select_lex->where,
1596
1947
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1597
1948
(order_st *)NULL,
1598
select_lex->options | session->options |
1949
select_lex->options | thd->options |
1599
1950
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1600
1951
OPTION_SETUP_TABLES_DONE,
1601
1952
del_result, unit, select_lex);
1602
res|= session->is_error();
1953
res|= thd->is_error();
1604
1955
del_result->abort();
1605
1956
delete del_result;
1613
1964
assert(first_table == all_tables && first_table != 0);
1614
1965
if (!lex->drop_temporary)
1616
if (end_active_trans(session))
1967
if (end_active_trans(thd))
1973
If this is a slave thread, we may sometimes execute some
1974
DROP / * 40005 TEMPORARY * / TABLE
1975
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
1976
MASTER TO), while the temporary table has already been dropped.
1977
To not generate such irrelevant "table does not exist errors",
1978
we silently add IF EXISTS if TEMPORARY was used.
1980
if (thd->slave_thread)
1981
lex->drop_if_exists= 1;
1621
1983
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1622
session->options|= OPTION_KEEP_LOG;
1984
thd->options|= OPTION_KEEP_LOG;
1624
1986
/* DDL and binlog write order protected by LOCK_open */
1625
res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1987
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
1628
1990
case SQLCOM_SHOW_PROCESSLIST:
1629
mysqld_list_processes(session, NULL, lex->verbose);
1991
mysqld_list_processes(thd, NULL, lex->verbose);
1631
1993
case SQLCOM_SHOW_ENGINE_LOGS:
1633
res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1995
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
1636
1998
case SQLCOM_CHANGE_DB:
1638
2000
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
1640
if (!mysql_change_db(session, &db_str, false))
2002
if (!mysql_change_db(thd, &db_str, false))
1697
2069
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
1698
2070
false, mysqldump will not work.
1700
unlock_locked_tables(session);
1701
if (session->options & OPTION_TABLE_LOCK)
2072
unlock_locked_tables(thd);
2073
if (thd->options & OPTION_TABLE_LOCK)
1703
end_active_trans(session);
1704
session->options&= ~(OPTION_TABLE_LOCK);
2075
end_active_trans(thd);
2076
thd->options&= ~(OPTION_TABLE_LOCK);
1706
if (session->global_read_lock)
1707
unlock_global_read_lock(session);
2078
if (thd->global_read_lock)
2079
unlock_global_read_lock(thd);
1710
2082
case SQLCOM_LOCK_TABLES:
1712
2084
We try to take transactional locks if
1713
2085
- only transactional locks are requested (lex->lock_transactional) and
1714
- no non-transactional locks exist (!session->locked_tables).
2086
- no non-transactional locks exist (!thd->locked_tables).
1716
if (lex->lock_transactional && !session->locked_tables)
2088
if (lex->lock_transactional && !thd->locked_tables)
1720
2092
All requested locks are transactional and no non-transactional
1723
if ((rc= try_transactional_lock(session, all_tables)) == -1)
2095
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
1741
2113
requested. If yes, warn about the conversion to non-transactional
1742
2114
locks or abort in strict mode.
1744
if (check_transactional_lock(session, all_tables))
2116
if (check_transactional_lock(thd, all_tables))
1746
unlock_locked_tables(session);
2118
unlock_locked_tables(thd);
1747
2119
/* we must end the trasaction first, regardless of anything */
1748
if (end_active_trans(session))
2120
if (end_active_trans(thd))
1750
session->in_lock_tables=1;
1751
session->options|= OPTION_TABLE_LOCK;
2122
thd->in_lock_tables=1;
2123
thd->options|= OPTION_TABLE_LOCK;
1753
if (!(res= simple_open_n_lock_tables(session, all_tables)))
2125
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
1755
session->locked_tables=session->lock;
1757
(void) set_handler_table_locks(session, all_tables, false);
2127
thd->locked_tables=thd->lock;
2129
(void) set_handler_table_locks(thd, all_tables, false);
1763
2135
Need to end the current transaction, so the storage engine (InnoDB)
1764
2136
can free its locks if LOCK TABLES locked some tables before finding
1765
2137
that it can't lock a table in its list
1767
ha_autocommit_or_rollback(session, 1);
1768
end_active_trans(session);
1769
session->options&= ~(OPTION_TABLE_LOCK);
2139
ha_autocommit_or_rollback(thd, 1);
2140
end_active_trans(thd);
2141
thd->options&= ~(OPTION_TABLE_LOCK);
1771
session->in_lock_tables=0;
2143
thd->in_lock_tables=0;
1773
2145
case SQLCOM_CREATE_DB:
1778
2150
prepared statement- safe.
1780
2152
HA_CREATE_INFO create_info(lex->create_info);
1781
if (end_active_trans(session))
2153
if (end_active_trans(thd))
1787
if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2159
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
1788
2160
check_db_name(&lex->name))
1790
2162
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
1793
res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2166
If in a slave thread :
2167
CREATE DATABASE DB was certainly not preceded by USE DB.
2168
For that reason, db_ok() in sql/slave.cc did not check the
2169
do_db/ignore_db. And as this query involves no tables, tables_ok()
2170
above was not called. So we have to check rules again here.
2172
if (thd->slave_thread &&
2173
(!rpl_filter->db_ok(lex->name.str) ||
2174
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2176
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2179
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
1794
2180
lex->name.str), &create_info, 0);
1797
2183
case SQLCOM_DROP_DB:
1799
if (end_active_trans(session))
2185
if (end_active_trans(thd))
1806
2192
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
1809
if (session->locked_tables || session->active_transaction())
2196
If in a slave thread :
2197
DROP DATABASE DB may not be preceded by USE DB.
2198
For that reason, maybe db_ok() in sql/slave.cc did not check the
2199
do_db/ignore_db. And as this query involves no tables, tables_ok()
2200
above was not called. So we have to check rules again here.
2202
if (thd->slave_thread &&
2203
(!rpl_filter->db_ok(lex->name.str) ||
2204
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2206
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2209
if (thd->locked_tables || thd->active_transaction())
1811
2211
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1812
2212
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1815
res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
2215
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
1818
2218
case SQLCOM_ALTER_DB:
1824
2224
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
1827
if (session->locked_tables || session->active_transaction())
2228
If in a slave thread :
2229
ALTER DATABASE DB may not be preceded by USE DB.
2230
For that reason, maybe db_ok() in sql/slave.cc did not check the
2231
do_db/ignore_db. And as this query involves no tables, tables_ok()
2232
above was not called. So we have to check rules again here.
2234
if (thd->slave_thread &&
2235
(!rpl_filter->db_ok(db->str) ||
2236
!rpl_filter->db_ok_with_wild_table(db->str)))
2238
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2241
if (thd->locked_tables || thd->active_transaction())
1829
2243
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1830
2244
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1833
res= mysql_alter_db(session, db->str, &create_info);
2247
res= mysql_alter_db(thd, db->str, &create_info);
1836
2250
case SQLCOM_SHOW_CREATE_DB:
1861
2276
Presumably, RESET and binlog writing doesn't require synchronization
1863
write_bin_log(session, false, session->query, session->query_length);
2278
write_bin_log(thd, false, thd->query, thd->query_length);
1869
2284
case SQLCOM_KILL:
1871
2286
Item *it= (Item *)lex->value_list.head();
1873
if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
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))
1875
2297
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
1879
sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2301
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
1882
2304
case SQLCOM_BEGIN:
1883
if (session->transaction.xid_state.xa_state != XA_NOTR)
2305
if (thd->transaction.xid_state.xa_state != XA_NOTR)
1885
2307
my_error(ER_XAER_RMFAIL, MYF(0),
1886
xa_state_names[session->transaction.xid_state.xa_state]);
2308
xa_state_names[thd->transaction.xid_state.xa_state]);
1890
2312
Breakpoints for backup testing.
1892
if (begin_trans(session))
2314
if (begin_trans(thd))
1896
2318
case SQLCOM_COMMIT:
1897
if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2319
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
1898
2320
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
1902
2324
case SQLCOM_ROLLBACK:
1903
if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2325
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
1904
2326
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
1908
2330
case SQLCOM_RELEASE_SAVEPOINT:
1911
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2333
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
1913
2335
if (my_strnncoll(system_charset_info,
1914
2336
(unsigned char *)lex->ident.str, lex->ident.length,
1942
if (ha_rollback_to_savepoint(session, sv))
2364
if (ha_rollback_to_savepoint(thd, sv))
1943
2365
res= true; // cannot happen
1946
if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
1947
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2368
if (((thd->options & OPTION_KEEP_LOG) ||
2369
thd->transaction.all.modified_non_trans_table) &&
2371
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1948
2372
ER_WARNING_NOT_COMPLETE_ROLLBACK,
1949
2373
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
1952
session->transaction.savepoints=sv;
2376
thd->transaction.savepoints=sv;
1955
2379
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
1958
2382
case SQLCOM_SAVEPOINT:
1959
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2383
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2384
thd->in_sub_stmt) || !opt_using_transactions)
1963
2388
SAVEPOINT **sv, *newsv;
1964
for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2389
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
1966
2391
if (my_strnncoll(system_charset_info,
1967
2392
(unsigned char *)lex->ident.str, lex->ident.length,
1988
2413
we'll lose a little bit of memory in transaction mem_root, but it'll
1989
2414
be free'd when transaction ends anyway
1991
if (ha_savepoint(session, newsv))
2416
if (ha_savepoint(thd, newsv))
1995
newsv->prev=session->transaction.savepoints;
1996
session->transaction.savepoints=newsv;
2420
newsv->prev=thd->transaction.savepoints;
2421
thd->transaction.savepoints=newsv;
2426
case SQLCOM_BINLOG_BASE64_EVENT:
2428
mysql_client_binlog_statement(thd);
2002
2432
assert(0); /* Impossible */
2006
session->set_proc_info("query end");
2436
thd_proc_info(thd, "query end");
2439
Binlog-related cleanup:
2440
Reset system variables temporarily modified by SET ONE SHOT.
2442
Exception: If this is a SET, do nothing. This is to allow
2443
mysqlbinlog to print many SET commands (in this case we want the
2444
charset temp setting to live until the real query). This is also
2445
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2448
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2449
reset_one_shot_variables(thd);
2009
2452
The return value for ROW_COUNT() is "implementation dependent" if the
2026
2469
Release the protection against the global read lock and wake
2027
2470
everyone, who might want to set a global read lock.
2029
start_waiting_global_read_lock(session);
2472
start_waiting_global_read_lock(thd);
2031
return(res || session->is_error());
2474
return(res || thd->is_error());
2034
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2477
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
2036
LEX *lex= session->lex;
2037
2480
select_result *result=lex->result;
2039
2482
/* assign global limit variable if limit is not given */
2041
Select_Lex *param= lex->unit.global_parameters;
2484
SELECT_LEX *param= lex->unit.global_parameters;
2042
2485
if (!param->explicit_limit)
2043
2486
param->select_limit=
2044
new Item_int((uint64_t) session->variables.select_limit);
2487
new Item_int((uint64_t) thd->variables.select_limit);
2046
if (!(res= open_and_lock_tables(session, all_tables)))
2489
if (!(res= open_and_lock_tables(thd, all_tables)))
2048
2491
if (lex->describe)
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));
2089
2563
#define MY_YACC_INIT 1000 // Start with big alloc
2090
2564
#define MY_YACC_MAX 32000 // Because of 'short'
2092
2566
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2094
LEX *lex= current_session->lex;
2568
LEX *lex= current_thd->lex;
2095
2569
ulong old_info=0;
2096
2570
if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2098
2572
if (!lex->yacc_yyvs)
2099
2573
old_info= *yystacksize;
2100
2574
*yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2101
unsigned char *tmpptr= NULL;
2102
if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
2103
*yystacksize* sizeof(**yyvs))))
2105
lex->yacc_yyvs= tmpptr;
2107
if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
2108
*yystacksize* sizeof(**yyss))))
2110
lex->yacc_yyss= tmpptr;
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))))
2112
2585
{ // Copy old info from stack
2113
2586
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();
2123
2661
mysql_init_select(LEX *lex)
2125
Select_Lex *select_lex= lex->current_select;
2663
SELECT_LEX *select_lex= lex->current_select;
2126
2664
select_lex->init_select();
2128
2666
if (select_lex == &lex->select_lex)
2279
2816
- first, call query_cache_send_result_to_client,
2280
2817
- second, if caching failed, initialise the lexical and syntactic parser.
2281
2818
The problem is that the query cache depends on a clean initialization
2282
of (among others) lex->safe_to_cache_query and session->server_status,
2819
of (among others) lex->safe_to_cache_query and thd->server_status,
2283
2820
which are reset respectively in
2285
- mysql_reset_session_for_next_command()
2822
- mysql_reset_thd_for_next_command()
2286
2823
So, initializing the lexical analyser *before* using the query cache
2287
2824
is required for the cache to work properly.
2288
2825
FIXME: cleanup the dependencies in the code to simplify this.
2291
session->reset_for_next_command();
2828
mysql_reset_thd_for_next_command(thd);
2294
LEX *lex= session->lex;
2296
Lex_input_stream lip(session, inBuf, length);
2298
bool err= parse_sql(session, &lip);
2833
Lex_input_stream lip(thd, inBuf, length);
2835
bool err= parse_sql(thd, &lip);
2299
2836
*found_semicolon= lip.found_semicolon;
2304
if (! session->is_error())
2841
if (! thd->is_error())
2307
Binlog logs a string starting from session->query and having length
2308
session->query_length; so we set session->query_length correctly (to not
2844
Binlog logs a string starting from thd->query and having length
2845
thd->query_length; so we set thd->query_length correctly (to not
2309
2846
log several statements in one event, when we executed only first).
2310
2847
We set it to not see the ';' (otherwise it would get into binlog
2311
2848
and Query_log_event::print() would give ';;' output).
2346
2883
1 can be ignored
2349
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
2886
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
2351
LEX *lex= session->lex;
2354
Lex_input_stream lip(session, inBuf, length);
2356
session->reset_for_next_command();
2891
Lex_input_stream lip(thd, inBuf, length);
2893
mysql_reset_thd_for_next_command(thd);
2358
if (!parse_sql(session, &lip) &&
2359
all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2895
if (!parse_sql(thd, &lip) &&
2896
all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
2360
2897
error= 1; /* Ignore question */
2361
session->end_statement();
2362
session->cleanup_after_query();
2898
thd->end_statement();
2899
thd->cleanup_after_query();
2924
bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
3459
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
2926
Select_Lex *first_sl= first_select();
3461
SELECT_LEX *first_sl= first_select();
2927
3462
assert(!fake_select_lex);
2929
if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
3464
if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
2931
fake_select_lex->include_standalone(this,
2932
(Select_Lex_Node**)&fake_select_lex);
3466
fake_select_lex->include_standalone(this,
3467
(SELECT_LEX_NODE**)&fake_select_lex);
2933
3468
fake_select_lex->select_number= INT_MAX;
2934
fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3469
fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
2935
3470
fake_select_lex->make_empty_select();
2936
3471
fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
2937
3472
fake_select_lex->select_limit= 0;
3108
3646
than it would help them)
3110
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);
3112
3660
if (ha_flush_logs(NULL))
3662
if (flush_error_log())
3116
3666
Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
3117
3667
(see sql_yacc.yy)
3119
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3669
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3121
if ((options & REFRESH_READ_LOCK) && session)
3671
if ((options & REFRESH_READ_LOCK) && thd)
3124
3674
We must not try to aspire a global read lock if we have a write
3125
3675
locked table. This would lead to a deadlock when trying to
3126
3676
reopen (and re-lock) the table after the flush.
3128
if (session->locked_tables)
3678
if (thd->locked_tables)
3130
THR_LOCK_DATA **lock_p= session->locked_tables->locks;
3131
THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
3680
THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
3681
THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
3133
3683
for (; lock_p < end_p; lock_p++)
3146
3696
tmp_write_to_binlog= 0;
3147
if (lock_global_read_lock(session))
3697
if (lock_global_read_lock(thd))
3148
3698
return 1; // Killed
3149
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3699
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3150
3700
false : true, true);
3151
if (make_global_read_lock_block_commit(session)) // Killed
3701
if (make_global_read_lock_block_commit(thd)) // Killed
3153
3703
/* Don't leave things in a half-locked state */
3154
unlock_global_read_lock(session);
3704
unlock_global_read_lock(thd);
3159
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3709
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3160
3710
false : true, false);
3162
if (session && (options & REFRESH_STATUS))
3163
session->refresh_status();
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);
3164
3734
*write_to_binlog= tmp_write_to_binlog;
3326
bool update_precheck(Session *session, TableList *)
3931
bool multi_update_precheck(THD *thd,
3932
TableList *tables __attribute__((unused)))
3328
3934
const char *msg= 0;
3329
LEX *lex= session->lex;
3330
Select_Lex *select_lex= &lex->select_lex;
3936
SELECT_LEX *select_lex= &lex->select_lex;
3332
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
3938
if (select_lex->item_list.elements != lex->value_list.elements)
3334
3940
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
3338
if (session->lex->select_lex.table_list.elements > 1)
3944
if (select_lex->order_list.elements)
3946
else if (select_lex->select_limit)
3340
if (select_lex->order_list.elements)
3342
else if (select_lex->select_limit)
3346
my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
3950
my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
3563
4192
return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
3566
if ((negated= expr->neg_transformer(session)) != 0)
4195
if ((negated= expr->neg_transformer(thd)) != 0)
3567
4196
return negated;
3568
4197
return new Item_func_not(expr);
4202
Check that byte length of a string does not exceed some limit.
4204
@param str string to be checked
4205
@param err_msg error message to be displayed if the string is too long
4206
@param max_length max length
4209
false the passed string is not longer than max_length
4211
true the passed string is longer than max_length
4214
The function is not used in existing code but can be useful later?
4217
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
4218
uint32_t max_byte_length)
4220
if (str->length <= max_byte_length)
4223
my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
3573
4230
Check that char length of a string does not exceed some limit.
3605
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
3606
uint32_t max_char_length,
3607
const char *param_for_err_msg)
4262
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
4263
uint32_t err_code, const char *param_for_err_msg)
4265
#ifdef HAVE_CHARSET_utf8mb3
3610
4267
We don't support non-BMP characters in identifiers at the moment,
3611
4268
so they should be prohibited until such support is done.
3612
4269
This is why we use the 3-byte utf8 to check well-formedness here.
3614
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
4271
const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
4273
const CHARSET_INFO * const cs= system_charset_info;
3616
4275
int well_formed_error;
3617
4276
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
3618
4277
max_char_length, &well_formed_error);