16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
19
#include "rpl_filter.h"
20
#include "repl_failsafe.h"
22
#include <drizzled/drizzled_error_messages.h>
18
#include <drizzled/sql_repl.h>
19
#include <drizzled/rpl_filter.h>
20
#include <drizzled/logging.h>
21
#include <drizzled/error.h>
22
#include <drizzled/nested_join.h>
23
#include <drizzled/query_id.h>
24
#include <drizzled/sql_parse.h>
25
#include <drizzled/data_home.h>
25
28
@defgroup Runtime_Environment Runtime Environment
32
extern const CHARSET_INFO *character_set_filesystem;
30
33
const char *any_db="*any*"; // Special symbol for check_access
32
35
const LEX_STRING command_name[COM_END+1]={
57
60
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
60
static void unlock_locked_tables(THD *thd)
63
static void unlock_locked_tables(Session *session)
62
if (thd->locked_tables)
65
if (session->locked_tables)
64
thd->lock=thd->locked_tables;
65
thd->locked_tables=0; // Will be automatically closed
66
close_thread_tables(thd); // Free tables
67
session->lock=session->locked_tables;
68
session->locked_tables=0; // Will be automatically closed
69
close_thread_tables(session); // Free tables
71
bool end_active_trans(THD *thd)
74
bool end_active_trans(Session *session)
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)
78
if (session->transaction.xid_state.xa_state != XA_NOTR)
81
80
my_error(ER_XAER_RMFAIL, MYF(0),
82
xa_state_names[thd->transaction.xid_state.xa_state]);
81
xa_state_names[session->transaction.xid_state.xa_state]);
85
if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
84
if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
86
85
OPTION_TABLE_LOCK))
88
87
/* 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;
88
if (!session->locked_tables)
89
session->options&= ~OPTION_TABLE_LOCK;
90
session->server_status&= ~SERVER_STATUS_IN_TRANS;
91
if (ha_commit(session))
95
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
96
thd->transaction.all.modified_non_trans_table= false;
94
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
95
session->transaction.all.modified_non_trans_table= false;
101
bool begin_trans(THD *thd)
100
bool begin_trans(Session *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))
103
if (session->locked_tables)
105
session->lock=session->locked_tables;
106
session->locked_tables=0; // Will be automatically closed
107
close_thread_tables(session); // Free tables
109
if (end_active_trans(session))
120
thd->options|= OPTION_BEGIN;
121
thd->server_status|= SERVER_STATUS_IN_TRANS;
113
LEX *lex= session->lex;
114
session->options|= OPTION_BEGIN;
115
session->server_status|= SERVER_STATUS_IN_TRANS;
122
116
if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
123
error= ha_start_consistent_snapshot(thd);
117
error= ha_start_consistent_snapshot(session);
129
123
Returns true if all tables should be ignored.
131
inline bool all_tables_not_ok(THD *thd, TableList *tables)
125
inline bool all_tables_not_ok(Session *session, TableList *tables)
133
127
return rpl_filter->is_on() && tables &&
134
!rpl_filter->tables_ok(thd->db, tables);
128
!rpl_filter->tables_ok(session->db, tables);
138
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
132
static bool some_non_temp_table_to_be_updated(Session *session, TableList *tables)
140
134
for (TableList *table= tables; table; table= table->next_global)
142
136
assert(table->db && table->table_name);
143
137
if (table->updating &&
144
!find_temporary_table(thd, table->db, table->table_name))
138
!find_temporary_table(session, table->db, table->table_name))
193
187
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
194
188
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
195
189
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
190
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;
201
191
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
202
192
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
203
193
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
227
217
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
230
void execute_init_command(THD *thd, sys_var_str *init_command_var,
220
void execute_init_command(Session *session, sys_var_str *init_command_var,
231
221
rw_lock_t *var_mutex)
234
224
ulong save_client_capabilities;
236
thd_proc_info(thd, "Execution of init_command");
226
session->set_proc_info("Execution of init_command");
238
228
We need to lock init_command_var because
239
229
during execution of init_command_var query
240
230
values of init_command_var can't be changed
242
232
rw_rdlock(var_mutex);
243
save_client_capabilities= thd->client_capabilities;
244
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
233
save_client_capabilities= session->client_capabilities;
234
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
246
236
We don't need return result of execution to client side.
247
To forbid this we should set thd->net.vio to 0.
237
To forbid this we should set session->net.vio to 0.
249
save_vio= thd->net.vio;
251
dispatch_command(COM_QUERY, thd,
239
save_vio= session->net.vio;
241
dispatch_command(COM_QUERY, session,
252
242
init_command_var->value,
253
243
init_command_var->value_length);
254
244
rw_unlock(var_mutex);
255
thd->client_capabilities= save_client_capabilities;
256
thd->net.vio= save_vio;
245
session->client_capabilities= save_client_capabilities;
246
session->net.vio= save_vio;
260
250
Ends the current transaction and (maybe) begin the next.
262
@param thd Current thread
252
@param session Current thread
263
253
@param completion Completion type
269
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
259
int end_trans(Session *session, enum enum_mysql_completiontype completion)
271
261
bool do_release= 0;
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)
264
if (session->transaction.xid_state.xa_state != XA_NOTR)
281
266
my_error(ER_XAER_RMFAIL, MYF(0),
282
xa_state_names[thd->transaction.xid_state.xa_state]);
267
xa_state_names[session->transaction.xid_state.xa_state]);
285
270
switch (completion) {
289
274
even if there is a problem with the OPTION_AUTO_COMMIT flag
290
275
(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;
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;
297
282
case COMMIT_RELEASE:
298
283
do_release= 1; /* fall through */
299
284
case COMMIT_AND_CHAIN:
300
res= end_active_trans(thd);
285
res= end_active_trans(session);
301
286
if (!res && completion == COMMIT_AND_CHAIN)
302
res= begin_trans(thd);
287
res= begin_trans(session);
304
289
case ROLLBACK_RELEASE:
305
290
do_release= 1; /* fall through */
307
292
case ROLLBACK_AND_CHAIN:
309
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
310
if (ha_rollback(thd))
294
session->server_status&= ~SERVER_STATUS_IN_TRANS;
295
if (ha_rollback(session))
312
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
thd->transaction.all.modified_non_trans_table= false;
297
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
298
session->transaction.all.modified_non_trans_table= false;
314
299
if (!res && (completion == ROLLBACK_AND_CHAIN))
315
res= begin_trans(thd);
300
res= begin_trans(session);
362
347
the client, the connection is closed or "net_wait_timeout"
363
348
number of seconds has passed
365
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
350
my_net_set_read_timeout(net, session->variables.net_wait_timeout);
368
353
XXX: this code is here only to clear possible errors of init_connect.
369
354
Consider moving to init_connect() instead.
371
thd->clear_error(); // Clear error message
372
thd->main_da.reset_diagnostics_area();
356
session->clear_error(); // Clear error message
357
session->main_da.reset_diagnostics_area();
374
359
net_new_transaction(net);
417
402
command= COM_END; // Wrong command
419
404
/* Restore read timeout value */
420
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
405
my_net_set_read_timeout(net, session->variables.net_read_timeout);
422
407
assert(packet_length);
423
return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
408
return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
426
411
return(return_value);
490
475
Perform one connection-level (COM_XXXX) command.
492
477
@param command type of command to perform
493
@param thd connection handle
478
@param session connection handle
494
479
@param packet data for the command, packet is always null-terminated
495
480
@param packet_length length of packet + 1 (to show that data is
496
481
null-terminated) except for COM_SLEEP, where it
500
set thd->lex->sql_command to SQLCOM_END here.
485
set session->lex->sql_command to SQLCOM_END here.
502
487
The following has to be changed to an 8 byte integer
507
492
1 request of thread shutdown, i. e. if command is
508
493
COM_QUIT/COM_SHUTDOWN
510
bool dispatch_command(enum enum_server_command command, THD *thd,
511
char* packet, uint32_t packet_length)
495
bool dispatch_command(enum enum_server_command command, Session *session,
496
char* packet, uint32_t packet_length)
498
NET *net= &session->net;
500
Query_id &query_id= Query_id::get_query_id();
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 */
502
session->command=command;
503
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
524
505
pthread_mutex_lock(&LOCK_thread_count);
525
thd->query_id= global_query_id;
506
session->query_id= query_id.value();
527
508
switch( command ) {
528
509
/* Ignore these statements. */
531
512
/* Increase id and count all other statements. */
533
statistic_increment(thd->status_var.questions, &LOCK_status);
514
statistic_increment(session->status_var.questions, &LOCK_status);
537
518
thread_running++;
538
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
519
/* TODO: set session->lex->sql_command to SQLCOM_END here */
539
520
pthread_mutex_unlock(&LOCK_thread_count);
522
logging_pre_do(session);
524
session->server_status&=
544
525
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
545
526
switch (command) {
546
527
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))
530
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
531
session->convert_string(&tmp, system_charset_info,
532
packet, packet_length, session->charset());
533
if (!mysql_change_db(session, &tmp, false))
558
case COM_REGISTER_SLAVE:
560
if (!register_slave(thd, (unsigned char*)packet, packet_length))
564
539
case COM_CHANGE_USER:
566
status_var_increment(thd->status_var.com_other);
541
status_var_increment(session->status_var.com_other);
567
542
char *user= (char*) packet, *packet_end= packet + packet_length;
568
543
/* Safe because there is always a trailing \0 at the end of the packet */
569
544
char *passwd= strchr(user, '\0')+1;
572
thd->clear_error(); // if errors from rollback
547
session->clear_error(); // if errors from rollback
575
550
Old clients send null-terminated string ('\0' for empty string) for
591
566
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
594
uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
569
uint32_t passwd_len= (session->client_capabilities & CLIENT_SECURE_CONNECTION ?
595
570
(unsigned char)(*passwd++) : strlen(passwd));
596
571
uint32_t dummy_errors, save_db_length, db_length;
598
Security_context save_security_ctx= *thd->security_ctx;
573
Security_context save_security_ctx= *session->security_ctx;
599
574
USER_CONN *save_user_connect;
601
576
db+= passwd_len + 1;
627
602
/* Convert database name to utf8 */
628
603
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
629
604
system_charset_info, db, db_length,
630
thd->charset(), &dummy_errors)]= 0;
605
session->charset(), &dummy_errors)]= 0;
633
608
/* Save user and privileges */
634
save_db_length= thd->db_length;
636
save_user_connect= thd->user_connect;
609
save_db_length= session->db_length;
610
save_db= session->db;
611
save_user_connect= session->user_connect;
638
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
613
if (!(session->security_ctx->user= my_strdup(user, MYF(0))))
640
thd->security_ctx->user= save_security_ctx.user;
615
session->security_ctx->user= save_security_ctx.user;
641
616
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
645
620
/* Clear variables that are allocated */
646
thd->user_connect= 0;
647
res= check_user(thd, passwd, passwd_len, db, false);
621
session->user_connect= 0;
622
res= check_user(session, 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;
626
if (session->security_ctx->user)
627
free(session->security_ctx->user);
628
*session->security_ctx= save_security_ctx;
629
session->user_connect= save_user_connect;
630
session->db= save_db;
631
session->db_length= save_db_length;
667
thd_init_client_charset(thd, cs_number);
668
thd->update_charset();
642
session_init_client_charset(session, cs_number);
643
session->update_charset();
675
if (alloc_query(thd, packet, packet_length))
650
if (alloc_query(session, packet, packet_length))
676
651
break; // fatal error is set
677
char *packet_end= thd->query + thd->query_length;
652
char *packet_end= session->query + session->query_length;
678
653
const char* end_of_stmt= NULL;
680
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
655
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
682
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
657
while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
684
659
char *beginning_of_next_stmt= (char*) end_of_stmt;
686
net_end_statement(thd);
661
net_end_statement(session);
688
663
Multiple queries exits, execute them individually
690
close_thread_tables(thd);
665
close_thread_tables(session);
691
666
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
693
log_slow_statement(thd);
668
log_slow_statement(session);
695
670
/* Remove garbage at start of query */
696
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
671
while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
698
673
beginning_of_next_stmt++;
702
677
pthread_mutex_lock(&LOCK_thread_count);
703
thd->query_length= length;
704
thd->query= beginning_of_next_stmt;
678
session->query_length= length;
679
session->query= beginning_of_next_stmt;
706
681
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 */
683
statistic_increment(session->status_var.questions, &LOCK_status);
684
session->query_id= query_id.next();
685
session->set_time(); /* Reset the query start time. */
686
/* TODO: set session->lex->sql_command to SQLCOM_END here */
712
687
pthread_mutex_unlock(&LOCK_thread_count);
714
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
689
mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
723
698
LEX_STRING conv_name;
725
700
/* used as fields initializator */
728
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
703
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
704
memset(&table_list, 0, sizeof(table_list));
730
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
705
if (session->copy_db_to(&table_list.db, &table_list.db_length))
733
708
We have name + wildcard in packet, separated by endzero
735
710
arg_end= strchr(packet, '\0');
736
thd->convert_string(&conv_name, system_charset_info,
737
packet, (uint32_t) (arg_end - packet), thd->charset());
711
session->convert_string(&conv_name, system_charset_info,
712
packet, (uint32_t) (arg_end - packet), session->charset());
738
713
table_list.alias= table_list.table_name= conv_name.str;
739
714
packet= arg_end + 1;
741
716
if (!my_strcasecmp(system_charset_info, table_list.db,
742
717
INFORMATION_SCHEMA_NAME.str))
744
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
719
ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
745
720
if (schema_table)
746
721
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)))
724
session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
725
if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
752
727
if (lower_case_table_names)
753
728
my_casedn_str(files_charset_info, table_list.table_name);
755
730
/* init structures for VIEW processing */
756
table_list.select_lex= &(thd->lex->select_lex);
759
mysql_reset_thd_for_next_command(thd);
731
table_list.select_lex= &(session->lex->select_lex);
734
mysql_reset_session_for_next_command(session);
762
737
select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
738
(unsigned char**) &table_list.next_local);
764
thd->lex->add_to_query_tables(&table_list);
739
session->lex->add_to_query_tables(&table_list);
766
741
/* 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();
742
session->lex->sql_command= SQLCOM_SHOW_FIELDS;
743
mysqld_list_fields(session,&table_list,fields);
744
session->lex->unit.cleanup();
745
session->cleanup_after_query();
774
749
/* We don't calculate statistics for this command */
775
750
net->error=0; // Don't give 'abort' message
776
thd->main_da.disable_status(); // Don't send anything back
751
session->main_da.disable_status(); // Don't send anything back
777
752
error=true; // End server
779
754
case COM_BINLOG_DUMP:
783
758
uint32_t slave_server_id;
785
status_var_increment(thd->status_var.com_other);
786
thd->enable_slow_log= opt_log_slow_admin_statements;
760
status_var_increment(session->status_var.com_other);
787
761
/* TODO: The following has to be changed to an 8 byte integer */
788
762
pos = uint4korr(packet);
789
763
flags = uint2korr(packet + 4);
790
thd->server_id=0; /* avoid suicide */
764
session->server_id=0; /* avoid suicide */
791
765
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
792
766
kill_zombie_dump_threads(slave_server_id);
793
thd->server_id = slave_server_id;
767
session->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);
769
mysql_binlog_send(session, session->strdup(packet + 10), (my_off_t) pos, flags);
797
770
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
801
774
case COM_SHUTDOWN:
803
status_var_increment(thd->status_var.com_other);
805
close_thread_tables(thd); // Free before kill
776
status_var_increment(session->status_var.com_other);
778
close_thread_tables(session); // Free before kill
811
status_var_increment(thd->status_var.com_other);
812
my_ok(thd); // Tell client we are alive
784
status_var_increment(session->status_var.com_other);
785
my_ok(session); // Tell client we are alive
814
787
case COM_PROCESS_INFO:
815
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
mysqld_list_processes(thd, NULL, 0);
788
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
789
mysqld_list_processes(session, NULL, 0);
818
791
case COM_PROCESS_KILL:
820
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
793
status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
821
794
ulong id=(ulong) uint4korr(packet);
822
sql_kill(thd,id,false);
795
sql_kill(session,id,false);
825
798
case COM_SET_OPTION:
827
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
800
status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
828
801
uint32_t opt_command= uint2korr(packet);
830
803
switch (opt_command) {
831
804
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
832
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
805
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
835
808
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
836
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
809
session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
840
813
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
854
827
/* 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;
828
session->main_da.can_overwrite_status= true;
829
ha_autocommit_or_rollback(session, session->is_error());
830
session->main_da.can_overwrite_status= false;
859
thd->transaction.stmt.reset();
832
session->transaction.stmt.reset();
862
835
/* 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");
836
if (session->killed_errno())
838
if (! session->main_da.is_set())
839
session->send_kill_message();
841
if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
843
session->killed= Session::NOT_KILLED;
844
session->mysys_var->abort= 0;
847
net_end_statement(session);
849
session->set_proc_info("closing tables");
877
850
/* Free tables */
878
close_thread_tables(thd);
880
log_slow_statement(thd);
882
thd_proc_info(thd, "cleaning up");
851
close_thread_tables(session);
853
log_slow_statement(session);
855
session->set_proc_info("cleaning up");
883
856
pthread_mutex_lock(&LOCK_thread_count); // For process list
884
thd_proc_info(thd, 0);
885
thd->command=COM_SLEEP;
857
session->set_proc_info(0);
858
session->command=COM_SLEEP;
860
session->query_length=0;
888
861
thread_running--;
889
862
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));
863
session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
864
free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
896
void log_slow_statement(THD *thd)
869
void log_slow_statement(Session *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);
871
logging_post_do(session);
974
939
/* 'parent_lex' is used in init_query() so it must be before it. */
975
940
schema_select_lex->parent_lex= lex;
976
941
schema_select_lex->init_query();
977
if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
942
if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
979
944
lex->query_tables_last= query_tables_last;
1009
Read query from packet and store in thd->query.
974
Read query from packet and store in session->query.
1010
975
Used in COM_QUERY and COM_STMT_PREPARE.
1012
Sets the following THD variables:
977
Sets the following Session variables:
1019
true error; In this case thd->fatal_error is set
984
true error; In this case session->fatal_error is set
1022
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
987
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
1024
989
/* Remove garbage at start and end of query */
1025
while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
990
while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
1028
993
packet_length--;
1030
995
const char *pos= packet + packet_length; // Point at end null
1031
996
while (packet_length > 0 &&
1032
(pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
997
(pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
1035
1000
packet_length--;
1037
1002
/* 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),
1003
session->query_length= 0; // Extra safety: Avoid races
1004
if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
1041
thd->db_length+ 1)))
1006
session->db_length+ 1)))
1043
thd->query[packet_length]=0;
1044
thd->query_length= packet_length;
1008
session->query[packet_length]=0;
1009
session->query_length= packet_length;
1046
1011
/* Reclaim some memory */
1047
thd->packet.shrink(thd->variables.net_buffer_length);
1048
thd->convert_buffer.shrink(thd->variables.net_buffer_length);
1012
session->packet.shrink(session->variables.net_buffer_length);
1013
session->convert_buffer.shrink(session->variables.net_buffer_length);
1053
static void reset_one_shot_variables(THD *thd)
1018
static void reset_one_shot_variables(Session *session)
1055
thd->variables.character_set_client=
1020
session->variables.character_set_client=
1056
1021
global_system_variables.character_set_client;
1057
thd->variables.collation_connection=
1022
session->variables.collation_connection=
1058
1023
global_system_variables.collation_connection;
1059
thd->variables.collation_database=
1024
session->variables.collation_database=
1060
1025
global_system_variables.collation_database;
1061
thd->variables.collation_server=
1026
session->variables.collation_server=
1062
1027
global_system_variables.collation_server;
1063
thd->update_charset();
1064
thd->variables.time_zone=
1028
session->update_charset();
1029
session->variables.time_zone=
1065
1030
global_system_variables.time_zone;
1066
thd->variables.lc_time_names= &my_locale_en_US;
1067
thd->one_shot_set= 0;
1031
session->variables.lc_time_names= &my_locale_en_US;
1032
session->one_shot_set= 0;
1072
Execute command saved in thd and lex->sql_command.
1037
Execute command saved in session and lex->sql_command.
1074
1039
Before every operation that can request a write lock for a table
1075
1040
wait if a global read lock exists. However do not wait if this
1167
1132
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1168
1133
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1169
1134
lex->drop_temporary && lex->drop_if_exists) &&
1170
all_tables_not_ok(thd, all_tables))
1135
all_tables_not_ok(session, all_tables))
1172
1137
/* we warn the slave SQL thread */
1173
1138
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1174
if (thd->one_shot_set)
1139
if (session->one_shot_set)
1177
It's ok to check thd->one_shot_set here:
1142
It's ok to check session->one_shot_set here:
1179
1144
The charsets in a MySQL 5.0 slave can change by both a binlogged
1180
1145
SET ONE_SHOT statement and the event-internal charset setting,
1197
1162
When option readonly is set deny operations which change non-temporary
1198
1163
tables. Except for the replication thread and the 'super' users.
1200
if (deny_updates_if_read_only_option(thd, all_tables))
1165
if (deny_updates_if_read_only_option(session, all_tables))
1202
1167
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1205
1170
} /* endif unlikely slave */
1206
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1171
status_var_increment(session->status_var.com_stat[lex->sql_command]);
1208
assert(thd->transaction.stmt.modified_non_trans_table == false);
1173
assert(session->transaction.stmt.modified_non_trans_table == false);
1210
1175
switch (lex->sql_command) {
1211
1176
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);
1178
system_status_var old_status_var= session->status_var;
1179
session->initial_status_var= &old_status_var;
1180
res= execute_sqlcom_select(session, all_tables);
1216
1181
/* Don't log SHOW STATUS commands to slow query log */
1217
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1182
session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1218
1183
SERVER_QUERY_NO_GOOD_INDEX_USED);
1220
1185
restore status variables, as we don't want 'show status' to cause
1223
1188
pthread_mutex_lock(&LOCK_status);
1224
add_diff_to_status(&global_status_var, &thd->status_var,
1189
add_diff_to_status(&global_status_var, &session->status_var,
1225
1190
&old_status_var);
1226
thd->status_var= old_status_var;
1191
session->status_var= old_status_var;
1227
1192
pthread_mutex_unlock(&LOCK_status);
1234
1199
case SQLCOM_SHOW_FIELDS:
1235
1200
case SQLCOM_SHOW_KEYS:
1236
1201
case SQLCOM_SHOW_VARIABLES:
1237
case SQLCOM_SHOW_CHARSETS:
1238
case SQLCOM_SHOW_COLLATIONS:
1239
1202
case SQLCOM_SELECT:
1241
thd->status_var.last_query_cost= 0.0;
1242
res= execute_sqlcom_select(thd, all_tables);
1204
session->status_var.last_query_cost= 0.0;
1205
res= execute_sqlcom_select(session, all_tables);
1245
1208
case SQLCOM_EMPTY_QUERY:
1249
1212
case SQLCOM_PURGE:
1251
res = purge_master_logs(thd, lex->to_log);
1214
res = purge_master_logs(session, lex->to_log);
1254
1217
case SQLCOM_PURGE_BEFORE:
1269
1232
value of constant
1271
1234
it->quick_fix_field();
1272
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1235
res = purge_master_logs_before_date(session, (ulong)it->val_int());
1275
1238
case SQLCOM_SHOW_WARNS:
1277
res= mysqld_show_warnings(thd, (uint32_t)
1240
res= mysqld_show_warnings(session, (uint32_t)
1278
1241
((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1279
1242
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1280
1243
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1284
1247
case SQLCOM_SHOW_ERRORS:
1286
res= mysqld_show_warnings(thd, (uint32_t)
1249
res= mysqld_show_warnings(session, (uint32_t)
1287
1250
(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);
1301
1253
case SQLCOM_ASSIGN_TO_KEYCACHE:
1303
1255
assert(first_table == all_tables && first_table != 0);
1304
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1256
res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1307
1259
case SQLCOM_CHANGE_MASTER:
1309
1261
pthread_mutex_lock(&LOCK_active_mi);
1310
res = change_master(thd,active_mi);
1262
res = change_master(session,active_mi);
1311
1263
pthread_mutex_unlock(&LOCK_active_mi);
1316
1268
pthread_mutex_lock(&LOCK_active_mi);
1317
1269
if (active_mi != NULL)
1319
res = show_master_info(thd, active_mi);
1271
res = show_master_info(session, active_mi);
1323
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1275
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1324
1276
"the master info structure does not exist");
1327
1279
pthread_mutex_unlock(&LOCK_active_mi);
1330
1282
case SQLCOM_SHOW_MASTER_STAT:
1332
res = show_binlog_info(thd);
1284
res = show_binlog_info(session);
1336
1288
case SQLCOM_SHOW_ENGINE_STATUS:
1338
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1290
res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1341
1293
case SQLCOM_CREATE_TABLE:
1367
1319
safety, only in case of Alter_info we have to do (almost) a deep
1370
Alter_info alter_info(lex->alter_info, thd->mem_root);
1322
Alter_info alter_info(lex->alter_info, session->mem_root);
1372
if (thd->is_fatal_error)
1324
if (session->is_fatal_error)
1374
1326
/* If out of memory when creating a copy of alter_info. */
1376
1328
goto end_with_restore_list;
1379
if ((res= create_table_precheck(thd, select_tables, create_table)))
1331
if ((res= create_table_precheck(session, select_tables, create_table)))
1380
1332
goto end_with_restore_list;
1382
1334
/* Might have been updated in create_table_precheck */
1385
1337
#ifdef HAVE_READLINK
1386
1338
/* Fix names if symlinked tables */
1387
if (append_file_to_dir(thd, &create_info.data_file_name,
1339
if (append_file_to_dir(session, &create_info.data_file_name,
1388
1340
create_table->table_name) ||
1389
append_file_to_dir(thd, &create_info.index_file_name,
1341
append_file_to_dir(session, &create_info.index_file_name,
1390
1342
create_table->table_name))
1391
1343
goto end_with_restore_list;
1482
1434
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1483
1435
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1484
thd->options|= OPTION_KEEP_LOG;
1436
session->options|= OPTION_KEEP_LOG;
1485
1437
/* regular create */
1486
1438
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1487
res= mysql_create_like_table(thd, create_table, select_tables,
1439
res= mysql_create_like_table(session, create_table, select_tables,
1491
res= mysql_create_table(thd, create_table->db,
1443
res= mysql_create_table(session, create_table->db,
1492
1444
create_table->table_name, &create_info,
1493
1445
&alter_info, 0, 0);
1499
1451
/* put tables back for PS rexecuting */
1516
1468
/* Prepare stack copies to be re-execution safe */
1517
1469
HA_CREATE_INFO create_info;
1518
Alter_info alter_info(lex->alter_info, thd->mem_root);
1470
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 */
1472
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1523
1475
assert(first_table == all_tables && first_table != 0);
1524
if (end_active_trans(thd))
1476
if (end_active_trans(session))
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
1479
memset(&create_info, 0, sizeof(create_info));
1534
1480
create_info.db_type= 0;
1535
1481
create_info.row_type= ROW_TYPE_NOT_USED;
1536
create_info.default_table_charset= thd->variables.collation_database;
1482
create_info.default_table_charset= session->variables.collation_database;
1538
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1484
res= mysql_alter_table(session, first_table->db, first_table->table_name,
1539
1485
&create_info, first_table, &alter_info,
1540
1486
0, (order_st*) 0, 0);
1604
1550
/* Don't yet allow changing of symlinks with ALTER TABLE */
1605
1551
if (create_info.data_file_name)
1606
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1552
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1607
1553
"DATA DIRECTORY option ignored");
1608
1554
if (create_info.index_file_name)
1609
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1555
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1610
1556
"INDEX DIRECTORY option ignored");
1611
1557
create_info.data_file_name= create_info.index_file_name= NULL;
1612
1558
/* ALTER TABLE ends previous transaction */
1613
if (end_active_trans(thd))
1559
if (end_active_trans(session))
1616
if (!thd->locked_tables &&
1617
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1562
if (!session->locked_tables &&
1563
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1623
thd->enable_slow_log= opt_log_slow_admin_statements;
1624
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1569
res= mysql_alter_table(session, select_lex->db, lex->name.str,
1654
1599
case SQLCOM_SHOW_BINLOGS:
1656
res = show_binlogs(thd);
1601
res = show_binlogs(session);
1659
1604
case SQLCOM_SHOW_CREATE:
1660
1605
assert(first_table == all_tables && first_table != 0);
1662
res= mysqld_show_create(thd, first_table);
1607
res= mysqld_show_create(session, first_table);
1665
1610
case SQLCOM_CHECKSUM:
1667
1612
assert(first_table == all_tables && first_table != 0);
1668
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1613
res = mysql_checksum_table(session, first_table, &lex->check_opt);
1671
1616
case SQLCOM_REPAIR:
1673
1618
assert(first_table == all_tables && first_table != 0);
1674
thd->enable_slow_log= opt_log_slow_admin_statements;
1675
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1619
res= mysql_repair_table(session, first_table, &lex->check_opt);
1676
1620
/* ! we write after unlocking the table */
1678
1622
Presumably, REPAIR and binlog writing doesn't require synchronization
1680
write_bin_log(thd, true, thd->query, thd->query_length);
1624
write_bin_log(session, true, session->query, session->query_length);
1681
1625
select_lex->table_list.first= (unsigned char*) first_table;
1682
1626
lex->query_tables=all_tables;
1694
1637
case SQLCOM_ANALYZE:
1696
1639
assert(first_table == all_tables && first_table != 0);
1697
thd->enable_slow_log= opt_log_slow_admin_statements;
1698
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1640
res= mysql_analyze_table(session, first_table, &lex->check_opt);
1699
1641
/* ! we write after unlocking the table */
1700
write_bin_log(thd, true, thd->query, thd->query_length);
1642
write_bin_log(session, true, session->query, session->query_length);
1701
1643
select_lex->table_list.first= (unsigned char*) first_table;
1702
1644
lex->query_tables=all_tables;
1706
1648
case SQLCOM_OPTIMIZE:
1708
1650
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);
1651
res= mysql_optimize_table(session, first_table, &lex->check_opt);
1711
1652
/* ! we write after unlocking the table */
1712
write_bin_log(thd, true, thd->query, thd->query_length);
1653
write_bin_log(session, true, session->query, session->query_length);
1713
1654
select_lex->table_list.first= (unsigned char*) first_table;
1714
1655
lex->query_tables=all_tables;
1717
1658
case SQLCOM_UPDATE:
1718
1659
assert(first_table == all_tables && first_table != 0);
1719
if (update_precheck(thd, all_tables))
1660
if (update_precheck(session, all_tables))
1721
1662
assert(select_lex->offset_limit == 0);
1722
1663
unit->set_limit(select_lex);
1723
res= (up_result= mysql_update(thd, all_tables,
1664
res= (up_result= mysql_update(session, all_tables,
1724
1665
select_lex->item_list,
1725
1666
lex->value_list,
1726
1667
select_lex->where,
1738
1679
/* if we switched from normal update, rights are checked */
1739
1680
if (up_result != 2)
1741
if ((res= multi_update_precheck(thd, all_tables)))
1682
if ((res= multi_update_precheck(session, all_tables)))
1747
res= mysql_multi_update_prepare(thd);
1688
res= mysql_multi_update_prepare(session);
1749
1690
/* Check slave filtering rules */
1750
if (unlikely(thd->slave_thread))
1691
if (unlikely(session->slave_thread))
1752
if (all_tables_not_ok(thd, all_tables))
1693
if (all_tables_not_ok(session, all_tables))
1756
1697
res= 0; /* don't care of prev failure */
1757
thd->clear_error(); /* filters are of highest prior */
1698
session->clear_error(); /* filters are of highest prior */
1759
1700
/* we warn the slave SQL thread */
1760
1701
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1787
1728
case SQLCOM_INSERT:
1789
1730
assert(first_table == all_tables && first_table != 0);
1790
if ((res= insert_precheck(thd, all_tables)))
1731
if ((res= insert_precheck(session, all_tables)))
1793
if (!thd->locked_tables &&
1794
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1734
if (!session->locked_tables &&
1735
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1800
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1741
res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1801
1742
lex->update_list, lex->value_list,
1802
1743
lex->duplicates, lex->ignore);
1821
1762
unit->set_limit(select_lex);
1823
if (! thd->locked_tables &&
1824
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1764
if (! session->locked_tables &&
1765
! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1830
if (!(res= open_and_lock_tables(thd, all_tables)))
1771
if (!(res= open_and_lock_tables(session, all_tables)))
1832
1773
/* Skip first table, which is the table we are inserting in */
1833
1774
TableList *second_table= first_table->next_local;
1834
1775
select_lex->table_list.first= (unsigned char*) second_table;
1835
1776
select_lex->context.table_list=
1836
1777
select_lex->context.first_name_resolution_table= second_table;
1837
res= mysql_insert_select_prepare(thd);
1778
res= mysql_insert_select_prepare(session);
1838
1779
if (!res && (sel_result= new select_insert(first_table,
1839
1780
first_table->table,
1840
1781
&lex->field_list,
1877
1818
Don't allow this within a transaction because we want to use
1878
1819
re-generate table
1880
if (thd->locked_tables || thd->active_transaction())
1821
if (session->locked_tables || session->active_transaction())
1882
1823
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1883
1824
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1887
res= mysql_truncate(thd, first_table, 0);
1828
res= mysql_truncate(session, first_table, 0);
1890
1831
case SQLCOM_DELETE:
1893
1834
assert(select_lex->offset_limit == 0);
1894
1835
unit->set_limit(select_lex);
1896
if (!thd->locked_tables &&
1897
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1837
if (!session->locked_tables &&
1838
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1903
res = mysql_delete(thd, all_tables, select_lex->where,
1844
res = mysql_delete(session, all_tables, select_lex->where,
1904
1845
&select_lex->order_list,
1905
1846
unit->select_limit_cnt, select_lex->options,
1911
1852
assert(first_table == all_tables && first_table != 0);
1912
1853
TableList *aux_tables=
1913
(TableList *)thd->lex->auxiliary_table_list.first;
1854
(TableList *)session->lex->auxiliary_table_list.first;
1914
1855
multi_delete *del_result;
1916
if (!thd->locked_tables &&
1917
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1857
if (!session->locked_tables &&
1858
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1923
if ((res= multi_delete_precheck(thd, all_tables)))
1864
if ((res= multi_delete_precheck(session, all_tables)))
1926
1867
/* condition will be true on SP re-excuting */
1927
1868
if (select_lex->item_list.elements != 0)
1928
1869
select_lex->item_list.empty();
1929
if (add_item_to_list(thd, new Item_null()))
1870
if (add_item_to_list(session, new Item_null()))
1932
thd_proc_info(thd, "init");
1933
if ((res= open_and_lock_tables(thd, all_tables)))
1873
session->set_proc_info("init");
1874
if ((res= open_and_lock_tables(session, all_tables)))
1936
if ((res= mysql_multi_delete_prepare(thd)))
1877
if ((res= mysql_multi_delete_prepare(session)))
1939
if (!thd->is_fatal_error &&
1880
if (!session->is_fatal_error &&
1940
1881
(del_result= new multi_delete(aux_tables, lex->table_count)))
1942
res= mysql_select(thd, &select_lex->ref_pointer_array,
1883
res= mysql_select(session, &select_lex->ref_pointer_array,
1943
1884
select_lex->get_table_list(),
1944
1885
select_lex->with_wild,
1945
1886
select_lex->item_list,
1946
1887
select_lex->where,
1947
1888
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1948
1889
(order_st *)NULL,
1949
select_lex->options | thd->options |
1890
select_lex->options | session->options |
1950
1891
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
1892
OPTION_SETUP_TABLES_DONE,
1952
1893
del_result, unit, select_lex);
1953
res|= thd->is_error();
1894
res|= session->is_error();
1955
1896
del_result->abort();
1956
1897
delete del_result;
1977
1918
To not generate such irrelevant "table does not exist errors",
1978
1919
we silently add IF EXISTS if TEMPORARY was used.
1980
if (thd->slave_thread)
1921
if (session->slave_thread)
1981
1922
lex->drop_if_exists= 1;
1983
1924
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1984
thd->options|= OPTION_KEEP_LOG;
1925
session->options|= OPTION_KEEP_LOG;
1986
1927
/* DDL and binlog write order protected by LOCK_open */
1987
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
1928
res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1990
1931
case SQLCOM_SHOW_PROCESSLIST:
1991
mysqld_list_processes(thd, NULL, lex->verbose);
1932
mysqld_list_processes(session, NULL, lex->verbose);
1993
1934
case SQLCOM_SHOW_ENGINE_LOGS:
1995
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
1936
res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1998
1939
case SQLCOM_CHANGE_DB:
2000
1941
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2002
if (!mysql_change_db(thd, &db_str, false))
1943
if (!mysql_change_db(session, &db_str, false))
2021
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
1962
res= mysql_load(session, lex->exchange, first_table, lex->field_list,
2022
1963
lex->update_list, lex->value_list, lex->duplicates,
2023
1964
lex->ignore, (bool) lex->local_file);
2029
1970
List<set_var_base> *lex_var_list= &lex->var_list;
2031
if (lex->autocommit && end_active_trans(thd))
1972
if (lex->autocommit && end_active_trans(session))
2034
if (open_and_lock_tables(thd, all_tables))
1975
if (open_and_lock_tables(session, all_tables))
2036
1977
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2038
1979
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2041
if (!(res= sql_set_variables(thd, lex_var_list)))
1982
if (!(res= sql_set_variables(session, lex_var_list)))
2044
1985
If the previous command was a SET ONE_SHOT, we don't want to forget
2045
1986
about the ONE_SHOT property of that SET. So we use a |= instead of = .
2047
thd->one_shot_set|= lex->one_shot_set;
1988
session->one_shot_set|= lex->one_shot_set;
2069
2010
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2070
2011
false, mysqldump will not work.
2072
unlock_locked_tables(thd);
2073
if (thd->options & OPTION_TABLE_LOCK)
2013
unlock_locked_tables(session);
2014
if (session->options & OPTION_TABLE_LOCK)
2075
end_active_trans(thd);
2076
thd->options&= ~(OPTION_TABLE_LOCK);
2016
end_active_trans(session);
2017
session->options&= ~(OPTION_TABLE_LOCK);
2078
if (thd->global_read_lock)
2079
unlock_global_read_lock(thd);
2019
if (session->global_read_lock)
2020
unlock_global_read_lock(session);
2082
2023
case SQLCOM_LOCK_TABLES:
2084
2025
We try to take transactional locks if
2085
2026
- only transactional locks are requested (lex->lock_transactional) and
2086
- no non-transactional locks exist (!thd->locked_tables).
2027
- no non-transactional locks exist (!session->locked_tables).
2088
if (lex->lock_transactional && !thd->locked_tables)
2029
if (lex->lock_transactional && !session->locked_tables)
2092
2033
All requested locks are transactional and no non-transactional
2095
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2036
if ((rc= try_transactional_lock(session, all_tables)) == -1)
2113
2054
requested. If yes, warn about the conversion to non-transactional
2114
2055
locks or abort in strict mode.
2116
if (check_transactional_lock(thd, all_tables))
2057
if (check_transactional_lock(session, all_tables))
2118
unlock_locked_tables(thd);
2059
unlock_locked_tables(session);
2119
2060
/* we must end the trasaction first, regardless of anything */
2120
if (end_active_trans(thd))
2061
if (end_active_trans(session))
2122
thd->in_lock_tables=1;
2123
thd->options|= OPTION_TABLE_LOCK;
2063
session->in_lock_tables=1;
2064
session->options|= OPTION_TABLE_LOCK;
2125
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2066
if (!(res= simple_open_n_lock_tables(session, all_tables)))
2127
thd->locked_tables=thd->lock;
2129
(void) set_handler_table_locks(thd, all_tables, false);
2068
session->locked_tables=session->lock;
2070
(void) set_handler_table_locks(session, all_tables, false);
2150
2091
prepared statement- safe.
2152
2093
HA_CREATE_INFO create_info(lex->create_info);
2153
if (end_active_trans(thd))
2094
if (end_active_trans(session))
2159
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2100
if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2160
2101
check_db_name(&lex->name))
2162
2103
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2169
2110
do_db/ignore_db. And as this query involves no tables, tables_ok()
2170
2111
above was not called. So we have to check rules again here.
2172
if (thd->slave_thread &&
2113
if (session->slave_thread &&
2173
2114
(!rpl_filter->db_ok(lex->name.str) ||
2174
2115
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2176
2117
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 :
2120
res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2180
2121
lex->name.str), &create_info, 0);
2183
2124
case SQLCOM_DROP_DB:
2185
if (end_active_trans(thd))
2126
if (end_active_trans(session))
2199
2140
do_db/ignore_db. And as this query involves no tables, tables_ok()
2200
2141
above was not called. So we have to check rules again here.
2202
if (thd->slave_thread &&
2143
if (session->slave_thread &&
2203
2144
(!rpl_filter->db_ok(lex->name.str) ||
2204
2145
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2206
2147
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2209
if (thd->locked_tables || thd->active_transaction())
2150
if (session->locked_tables || session->active_transaction())
2211
2152
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2212
2153
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2215
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2156
res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
2218
2159
case SQLCOM_ALTER_DB:
2231
2172
do_db/ignore_db. And as this query involves no tables, tables_ok()
2232
2173
above was not called. So we have to check rules again here.
2234
if (thd->slave_thread &&
2175
if (session->slave_thread &&
2235
2176
(!rpl_filter->db_ok(db->str) ||
2236
2177
!rpl_filter->db_ok_with_wild_table(db->str)))
2238
2179
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2241
if (thd->locked_tables || thd->active_transaction())
2182
if (session->locked_tables || session->active_transaction())
2243
2184
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2244
2185
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2247
res= mysql_alter_db(thd, db->str, &create_info);
2188
res= mysql_alter_db(session, db->str, &create_info);
2250
2191
case SQLCOM_SHOW_CREATE_DB:
2295
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2236
if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2297
2238
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2301
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2242
sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2304
2245
case SQLCOM_BEGIN:
2305
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2246
if (session->transaction.xid_state.xa_state != XA_NOTR)
2307
2248
my_error(ER_XAER_RMFAIL, MYF(0),
2308
xa_state_names[thd->transaction.xid_state.xa_state]);
2249
xa_state_names[session->transaction.xid_state.xa_state]);
2312
2253
Breakpoints for backup testing.
2314
if (begin_trans(thd))
2255
if (begin_trans(session))
2318
2259
case SQLCOM_COMMIT:
2319
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2260
if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2320
2261
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2324
2265
case SQLCOM_ROLLBACK:
2325
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2266
if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2326
2267
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2330
2271
case SQLCOM_RELEASE_SAVEPOINT:
2333
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2274
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2335
2276
if (my_strnncoll(system_charset_info,
2336
2277
(unsigned char *)lex->ident.str, lex->ident.length,
2364
if (ha_rollback_to_savepoint(thd, sv))
2305
if (ha_rollback_to_savepoint(session, sv))
2365
2306
res= true; // cannot happen
2368
if (((thd->options & OPTION_KEEP_LOG) ||
2369
thd->transaction.all.modified_non_trans_table) &&
2371
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2309
if (((session->options & OPTION_KEEP_LOG) ||
2310
session->transaction.all.modified_non_trans_table) &&
2311
!session->slave_thread)
2312
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2372
2313
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2373
2314
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2376
thd->transaction.savepoints=sv;
2317
session->transaction.savepoints=sv;
2379
2320
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2382
2323
case SQLCOM_SAVEPOINT:
2383
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2384
thd->in_sub_stmt) || !opt_using_transactions)
2324
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2388
2328
SAVEPOINT **sv, *newsv;
2389
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2329
for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2391
2331
if (my_strnncoll(system_charset_info,
2392
2332
(unsigned char *)lex->ident.str, lex->ident.length,
2396
2336
if (*sv) /* old savepoint of the same name exists */
2399
ha_release_savepoint(thd, *sv); // it cannot fail
2339
ha_release_savepoint(session, *sv); // it cannot fail
2400
2340
*sv=(*sv)->prev;
2402
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2342
else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
2403
2343
savepoint_alloc_size)) == 0)
2405
2345
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2408
newsv->name=strmake_root(&thd->transaction.mem_root,
2348
newsv->name=strmake_root(&session->transaction.mem_root,
2409
2349
lex->ident.str, lex->ident.length);
2410
2350
newsv->length=lex->ident.length;
2413
2353
we'll lose a little bit of memory in transaction mem_root, but it'll
2414
2354
be free'd when transaction ends anyway
2416
if (ha_savepoint(thd, newsv))
2356
if (ha_savepoint(session, newsv))
2420
newsv->prev=thd->transaction.savepoints;
2421
thd->transaction.savepoints=newsv;
2360
newsv->prev=session->transaction.savepoints;
2361
session->transaction.savepoints=newsv;
2426
2366
case SQLCOM_BINLOG_BASE64_EVENT:
2428
mysql_client_binlog_statement(thd);
2368
mysql_client_binlog_statement(session);
2432
2372
assert(0); /* Impossible */
2436
thd_proc_info(thd, "query end");
2376
session->set_proc_info("query end");
2439
2379
Binlog-related cleanup:
2469
2409
Release the protection against the global read lock and wake
2470
2410
everyone, who might want to set a global read lock.
2472
start_waiting_global_read_lock(thd);
2412
start_waiting_global_read_lock(session);
2474
return(res || thd->is_error());
2414
return(res || session->is_error());
2477
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
2417
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2419
LEX *lex= session->lex;
2480
2420
select_result *result=lex->result;
2482
2422
/* assign global limit variable if limit is not given */
2499
2439
if (!(result= new select_send()))
2500
2440
return 1; /* purecov: inspected */
2501
thd->send_explain_fields(result);
2502
res= mysql_explain_union(thd, &thd->lex->unit, result);
2441
session->send_explain_fields(result);
2442
res= mysql_explain_union(session, &session->lex->unit, result);
2503
2443
if (lex->describe & DESCRIBE_EXTENDED)
2505
2445
char buff[1024];
2506
2446
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2508
thd->lex->unit.print(&str, QT_ORDINARY);
2448
session->lex->unit.print(&str, QT_ORDINARY);
2509
2449
str.append('\0');
2510
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2450
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2511
2451
ER_YES, str.ptr());
2544
2484
corresponding exec. (Thus we only have to check in fix_fields.)
2545
2485
- Passing to check_stack_overrun() prevents the compiler from removing it.
2547
bool check_stack_overrun(THD *thd, long margin,
2487
bool check_stack_overrun(Session *session, long margin,
2548
2488
unsigned char *buf __attribute__((unused)))
2550
2490
long stack_used;
2551
assert(thd == current_thd);
2552
if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
2491
assert(session == current_session);
2492
if ((stack_used=used_stack(session->thread_stack,(char*) &stack_used)) >=
2553
2493
(long) (my_thread_stack_size - margin))
2555
2495
sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2596
Reset THD part responsible for command processing state.
2536
Reset Session part responsible for command processing state.
2598
2538
This needs to be called before execution of every statement
2599
2539
(prepared or conventional).
2600
2540
It is not called by substatements of routines.
2603
Make it a method of THD and align its name with the rest of
2543
Make it a method of Session and align its name with the rest of
2604
2544
reset/end/start/init methods.
2606
Call it after we use THD for queries, not before.
2546
Call it after we use Session for queries, not before.
2609
void mysql_reset_thd_for_next_command(THD *thd)
2549
void mysql_reset_session_for_next_command(Session *session)
2611
assert(! thd->in_sub_stmt);
2613
thd->select_number= 1;
2551
session->free_list= 0;
2552
session->select_number= 1;
2615
2554
Those two lines below are theoretically unneeded as
2616
THD::cleanup_after_query() should take care of this already.
2555
Session::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;
2557
session->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2558
session->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 |
2560
session->query_start_used= 0;
2561
session->is_fatal_error= session->time_zone_used= 0;
2562
session->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS |
2624
2563
SERVER_QUERY_NO_INDEX_USED |
2625
2564
SERVER_QUERY_NO_GOOD_INDEX_USED);
2628
2567
OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2629
2568
in ha_rollback_trans() about some tables couldn't be rolled back.
2631
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2570
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2633
thd->options&= ~OPTION_KEEP_LOG;
2634
thd->transaction.all.modified_non_trans_table= false;
2572
session->options&= ~OPTION_KEEP_LOG;
2573
session->transaction.all.modified_non_trans_table= false;
2636
assert(thd->security_ctx== &thd->main_security_ctx);
2637
thd->thread_specific_used= false;
2575
assert(session->security_ctx== &session->main_security_ctx);
2576
session->thread_specific_used= false;
2639
2578
if (opt_bin_log)
2641
reset_dynamic(&thd->user_var_events);
2642
thd->user_var_events_alloc= thd->mem_root;
2580
reset_dynamic(&session->user_var_events);
2581
session->user_var_events_alloc= session->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;
2583
session->clear_error();
2584
session->main_da.reset_diagnostics_area();
2585
session->total_warn_count=0; // Warnings for this query
2586
session->rand_used= 0;
2587
session->sent_row_count= session->examined_row_count= 0;
2651
2590
Because we come here only for start of top-statements, binlog format is
2652
2591
constant inside a complex statement (using stored functions) etc.
2654
thd->reset_current_stmt_binlog_row_based();
2593
session->reset_current_stmt_binlog_row_based();
2675
2614
mysql_new_select(LEX *lex, bool move_down)
2677
2616
SELECT_LEX *select_lex;
2617
Session *session= lex->session;
2680
if (!(select_lex= new (thd->mem_root) SELECT_LEX()))
2619
if (!(select_lex= new (session->mem_root) SELECT_LEX()))
2682
select_lex->select_number= ++thd->select_number;
2621
select_lex->select_number= ++session->select_number;
2683
2622
select_lex->parent_lex= lex; /* Used in init_query. */
2684
2623
select_lex->init_query();
2685
2624
select_lex->init_select();
2767
2706
We set the name of Item to @@session.var_name because that then is used
2768
2707
as the column name in the output.
2770
if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
2709
if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
2772
2711
end= strxmov(buff, "@@session.", var_name, NULL);
2773
2712
var->set_name(buff, end-buff, system_charset_info);
2774
add_item_to_list(thd, var);
2713
add_item_to_list(session, var);
2801
@param thd Current thread
2740
@param session Current thread
2802
2741
@param inBuf Begining of the query text
2803
2742
@param length Length of the query text
2804
2743
@param[out] found_semicolon For multi queries, position of the character of
2805
2744
the next query in the query text.
2808
void mysql_parse(THD *thd, const char *inBuf, uint32_t length,
2747
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
2809
2748
const char ** found_semicolon)
2816
2755
- first, call query_cache_send_result_to_client,
2817
2756
- second, if caching failed, initialise the lexical and syntactic parser.
2818
2757
The problem is that the query cache depends on a clean initialization
2819
of (among others) lex->safe_to_cache_query and thd->server_status,
2758
of (among others) lex->safe_to_cache_query and session->server_status,
2820
2759
which are reset respectively in
2822
- mysql_reset_thd_for_next_command()
2761
- mysql_reset_session_for_next_command()
2823
2762
So, initializing the lexical analyser *before* using the query cache
2824
2763
is required for the cache to work properly.
2825
2764
FIXME: cleanup the dependencies in the code to simplify this.
2828
mysql_reset_thd_for_next_command(thd);
2767
mysql_reset_session_for_next_command(session);
2833
Lex_input_stream lip(thd, inBuf, length);
2835
bool err= parse_sql(thd, &lip);
2770
LEX *lex= session->lex;
2772
Lex_input_stream lip(session, inBuf, length);
2774
bool err= parse_sql(session, &lip);
2836
2775
*found_semicolon= lip.found_semicolon;
2841
if (! thd->is_error())
2780
if (! session->is_error())
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
2783
Binlog logs a string starting from session->query and having length
2784
session->query_length; so we set session->query_length correctly (to not
2846
2785
log several statements in one event, when we executed only first).
2847
2786
We set it to not see the ';' (otherwise it would get into binlog
2848
2787
and Query_log_event::print() would give ';;' output).
2851
2790
Note that we don't need LOCK_thread_count to modify query_length.
2853
2792
if (*found_semicolon &&
2854
(thd->query_length= (ulong)(*found_semicolon - thd->query)))
2855
thd->query_length--;
2793
(session->query_length= (ulong)(*found_semicolon - session->query)))
2794
session->query_length--;
2856
2795
/* Actually execute the query */
2857
mysql_execute_command(thd);
2796
mysql_execute_command(session);
2863
assert(thd->is_error());
2802
assert(session->is_error());
2865
2804
lex->unit.cleanup();
2866
thd_proc_info(thd, "freeing items");
2867
thd->end_statement();
2868
thd->cleanup_after_query();
2869
assert(thd->change_list.is_empty());
2805
session->set_proc_info("freeing items");
2806
session->end_statement();
2807
session->cleanup_after_query();
2808
assert(session->change_list.is_empty());
2883
2822
1 can be ignored
2886
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
2825
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
2827
LEX *lex= session->lex;
2891
Lex_input_stream lip(thd, inBuf, length);
2893
mysql_reset_thd_for_next_command(thd);
2830
Lex_input_stream lip(session, inBuf, length);
2832
mysql_reset_session_for_next_command(session);
2895
if (!parse_sql(thd, &lip) &&
2896
all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
2834
if (!parse_sql(session, &lip) &&
2835
all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2897
2836
error= 1; /* Ignore question */
2898
thd->end_statement();
2899
thd->cleanup_after_query();
2837
session->end_statement();
2838
session->cleanup_after_query();
2912
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
2851
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
2913
2852
char *length, char *decimals,
2914
2853
uint32_t type_modifier,
2915
2854
enum column_format_type column_format,
2916
2855
Item *default_value, Item *on_update_value,
2917
2856
LEX_STRING *comment,
2919
List<String> *interval_list, const CHARSET_INFO * const cs)
2858
List<String> *interval_list, const CHARSET_INFO * const cs,
2859
virtual_column_info *vcol_info)
2921
2861
register Create_field *new_field;
2862
LEX *lex= session->lex;
2924
2864
if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
2925
2865
return(1); /* purecov: inspected */
2987
2927
if (!(new_field= new Create_field()) ||
2988
new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
2928
new_field->init(session, field_name->str, type, length, decimals, type_modifier,
2989
2929
default_value, on_update_value, comment, change,
2990
interval_list, cs, 0, column_format))
2930
interval_list, cs, 0, column_format,
2993
2934
lex->alter_info.create_list.push_back(new_field);
3001
2942
void store_position_for_column(const char *name)
3003
current_thd->lex->last_field->after=const_cast<char*> (name);
2944
current_session->lex->last_field->after=const_cast<char*> (name);
3007
add_proc_to_list(THD* thd, Item *item)
2948
add_proc_to_list(Session* session, Item *item)
3009
2950
order_st *order;
3010
2951
Item **item_ptr;
3012
if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
2953
if (!(order = (order_st *) session->alloc(sizeof(order_st)+sizeof(Item*))))
3014
2955
item_ptr = (Item**) (order+1);
3015
2956
*item_ptr= item;
3016
2957
order->item=item_ptr;
3017
2958
order->free_me=0;
3018
thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
2959
session->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
3024
2965
save order by and tables in own lists.
3027
bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc)
2968
bool add_to_list(Session *session, SQL_LIST &list,Item *item,bool asc)
3029
2970
order_st *order;
3030
if (!(order = (order_st *) thd->alloc(sizeof(order_st))))
2971
if (!(order = (order_st *) session->alloc(sizeof(order_st))))
3032
2973
order->item_ptr= item;
3033
2974
order->item= &order->item_ptr;
3249
3190
If the current level contains only one member, the function
3250
3191
moves it one level up, eliminating the nest.
3252
@param thd current thread
3193
@param session current thread
3255
3196
- Pointer to TableList element added to the total table list, if success
3259
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
3200
TableList *st_select_lex::end_nested_join(Session *session __attribute__((unused)))
3261
3202
TableList *ptr;
3262
3203
nested_join_st *nested_join;
3297
3238
\# Pointer to TableList element created for the new nested join
3300
TableList *st_select_lex::nest_last_join(THD *thd)
3241
TableList *st_select_lex::nest_last_join(Session *session)
3302
3243
TableList *ptr;
3303
3244
nested_join_st *nested_join;
3304
3245
List<TableList> *embedded_list;
3306
if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3247
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
3307
3248
sizeof(nested_join_st))))
3309
3250
nested_join= ptr->nested_join=
3459
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
3400
bool st_select_lex_unit::add_fake_select_lex(Session *session_arg)
3461
3402
SELECT_LEX *first_sl= first_select();
3462
3403
assert(!fake_select_lex);
3464
if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
3405
if (!(fake_select_lex= new (session_arg->mem_root) SELECT_LEX()))
3466
3407
fake_select_lex->include_standalone(this,
3467
3408
(SELECT_LEX_NODE**)&fake_select_lex);
3468
3409
fake_select_lex->select_number= INT_MAX;
3469
fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
3410
fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3470
3411
fake_select_lex->make_empty_select();
3471
3412
fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3472
3413
fake_select_lex->select_limit= 0;
3516
push_new_name_resolution_context(THD *thd,
3457
push_new_name_resolution_context(Session *session,
3517
3458
TableList *left_op, TableList *right_op)
3519
3460
Name_resolution_context *on_context;
3520
if (!(on_context= new (thd->mem_root) Name_resolution_context))
3461
if (!(on_context= new (session->mem_root) Name_resolution_context))
3522
3463
on_context->init();
3523
3464
on_context->first_name_resolution_table=
3524
3465
left_op->first_leaf_for_name_resolution();
3525
3466
on_context->last_name_resolution_table=
3526
3467
right_op->last_leaf_for_name_resolution();
3527
return thd->lex->push_context(on_context);
3468
return session->lex->push_context(on_context);
3620
3561
@return Error status code
3622
@retval !=0 Error; thd->killed is set or thd->is_error() is true
3563
@retval !=0 Error; session->killed is set or session->is_error() is true
3625
bool reload_cache(THD *thd, ulong options, TableList *tables,
3566
bool reload_cache(Session *session, ulong options, TableList *tables,
3626
3567
bool *write_to_binlog)
3629
3570
select_errors=0; /* Write if more errors */
3630
3571
bool tmp_write_to_binlog= 1;
3632
assert(!thd || !thd->in_sub_stmt);
3634
3573
if (options & REFRESH_LOG)
3669
3608
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3671
if ((options & REFRESH_READ_LOCK) && thd)
3610
if ((options & REFRESH_READ_LOCK) && session)
3674
3613
We must not try to aspire a global read lock if we have a write
3675
3614
locked table. This would lead to a deadlock when trying to
3676
3615
reopen (and re-lock) the table after the flush.
3678
if (thd->locked_tables)
3617
if (session->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;
3619
THR_LOCK_DATA **lock_p= session->locked_tables->locks;
3620
THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
3683
3622
for (; lock_p < end_p; lock_p++)
3696
3635
tmp_write_to_binlog= 0;
3697
if (lock_global_read_lock(thd))
3636
if (lock_global_read_lock(session))
3698
3637
return 1; // Killed
3699
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3638
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3700
3639
false : true, true);
3701
if (make_global_read_lock_block_commit(thd)) // Killed
3640
if (make_global_read_lock_block_commit(session)) // Killed
3703
3642
/* Don't leave things in a half-locked state */
3704
unlock_global_read_lock(thd);
3643
unlock_global_read_lock(session);
3709
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3648
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3710
3649
false : true, false);
3711
3650
my_dbopt_cleanup();
3713
if (thd && (options & REFRESH_STATUS))
3714
refresh_status(thd);
3715
if (options & REFRESH_THREADS)
3716
flush_thread_cache();
3652
if (session && (options & REFRESH_STATUS))
3653
refresh_status(session);
3717
3654
if (options & REFRESH_MASTER)
3720
3657
tmp_write_to_binlog= 0;
3721
if (reset_master(thd))
3658
if (reset_master(session))
3750
3687
static unsigned int
3751
kill_one_thread(THD *thd __attribute__((unused)),
3688
kill_one_thread(Session *session __attribute__((unused)),
3752
3689
ulong id, bool only_kill_query)
3755
3692
uint32_t error=ER_NO_SUCH_THREAD;
3756
3693
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
3757
I_List_iterator<THD> it(threads);
3694
I_List_iterator<Session> it(threads);
3758
3695
while ((tmp=it++))
3760
3697
if (tmp->command == COM_DAEMON)
3721
session Thread class
3786
3723
only_kill_query Should it kill the query or the connection
3789
void sql_kill(THD *thd, ulong id, bool only_kill_query)
3726
void sql_kill(Session *session, ulong id, bool only_kill_query)
3791
3728
uint32_t error;
3792
if (!(error= kill_one_thread(thd, id, only_kill_query)))
3729
if (!(error= kill_one_thread(session, id, only_kill_query)))
3795
3732
my_error(error, MYF(0), id);
3815
3752
/* Fix is using unix filename format on dos */
3816
3753
my_stpcpy(buff,*filename_ptr);
3817
3754
end=convert_dirname(buff, *filename_ptr, NULL);
3818
if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3755
if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3819
3756
return 1; // End of memory
3820
3757
*filename_ptr=ptr;
3821
3758
strxmov(ptr,buff,table_name,NULL);
3835
3772
bool check_simple_select()
3837
THD *thd= current_thd;
3774
Session *session= current_session;
3775
LEX *lex= session->lex;
3839
3776
if (lex->current_select != &lex->select_lex)
3841
3778
char command[80];
3842
Lex_input_stream *lip= thd->m_lip;
3779
Lex_input_stream *lip= session->m_lip;
3843
3780
strmake(command, lip->yylval->symbol.str,
3844
3781
cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
3845
3782
my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3968
bool multi_delete_precheck(THD *thd,
3905
bool multi_delete_precheck(Session *session,
3969
3906
TableList *tables __attribute__((unused)))
3971
SELECT_LEX *select_lex= &thd->lex->select_lex;
3972
TableList **save_query_tables_own_last= thd->lex->query_tables_own_last;
3974
thd->lex->query_tables_own_last= 0;
3975
thd->lex->query_tables_own_last= save_query_tables_own_last;
3977
if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3908
SELECT_LEX *select_lex= &session->lex->select_lex;
3909
TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
3911
session->lex->query_tables_own_last= 0;
3912
session->lex->query_tables_own_last= save_query_tables_own_last;
3914
if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3979
3916
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
3980
3917
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
4355
4292
@retval true on parsing error.
4358
bool parse_sql(THD *thd, Lex_input_stream *lip)
4295
bool parse_sql(Session *session, Lex_input_stream *lip)
4360
assert(thd->m_lip == NULL);
4297
assert(session->m_lip == NULL);
4362
4299
/* Set Lex_input_stream. */
4301
session->m_lip= lip;
4366
4303
/* Parse the query. */
4368
bool mysql_parse_status= MYSQLparse(thd) != 0;
4370
/* Check that if MYSQLparse() failed, thd->is_error() is set. */
4372
assert(!mysql_parse_status || thd->is_error());
4305
bool mysql_parse_status= MYSQLparse(session) != 0;
4307
/* Check that if MYSQLparse() failed, session->is_error() is set. */
4309
assert(!mysql_parse_status || session->is_error());
4374
4311
/* Reset Lex_input_stream. */
4313
session->m_lip= NULL;
4378
4315
/* That's it. */
4380
return mysql_parse_status || thd->is_fatal_error;
4317
return mysql_parse_status || session->is_fatal_error;