16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
#include <drizzled/replication/replication.h>
19
#include <libdrizzle/libdrizzle.h>
20
#include <mysys/hash.h>
21
#include <drizzled/replication/binlog.h>
22
#include <drizzled/logging.h>
23
#include <drizzled/db.h>
24
#include <drizzled/error.h>
25
#include <drizzled/nested_join.h>
26
#include <drizzled/query_id.h>
27
#include <drizzled/sql_parse.h>
28
#include <drizzled/data_home.h>
29
#include <drizzled/sql_base.h>
30
#include <drizzled/show.h>
31
#include <drizzled/rename.h>
32
#include <drizzled/functions/time/unix_timestamp.h>
33
#include <drizzled/item/cmpfunc.h>
34
#include <drizzled/session.h>
35
#include <drizzled/sql_load.h>
19
#include "rpl_filter.h"
21
#include <drizzled/drizzled_error_messages.h>
41
24
@defgroup Runtime_Environment Runtime Environment
45
extern const CHARSET_INFO *character_set_filesystem;
46
29
const char *any_db="*any*"; // Special symbol for check_access
48
31
const LEX_STRING command_name[COM_END+1]={
73
56
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
76
static void unlock_locked_tables(Session *session)
59
static void unlock_locked_tables(THD *thd)
78
if (session->locked_tables)
61
if (thd->locked_tables)
80
session->lock=session->locked_tables;
81
session->locked_tables=0; // Will be automatically closed
82
close_thread_tables(session); // Free tables
63
thd->lock=thd->locked_tables;
64
thd->locked_tables=0; // Will be automatically closed
65
close_thread_tables(thd); // Free tables
87
bool end_active_trans(Session *session)
70
bool end_active_trans(THD *thd)
91
if (session->transaction.xid_state.xa_state != XA_NOTR)
74
if (thd->transaction.xid_state.xa_state != XA_NOTR)
93
76
my_error(ER_XAER_RMFAIL, MYF(0),
94
xa_state_names[session->transaction.xid_state.xa_state]);
77
xa_state_names[thd->transaction.xid_state.xa_state]);
97
if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
80
if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
98
81
OPTION_TABLE_LOCK))
100
83
/* Safety if one did "drop table" on locked tables */
101
if (!session->locked_tables)
102
session->options&= ~OPTION_TABLE_LOCK;
103
session->server_status&= ~SERVER_STATUS_IN_TRANS;
104
if (ha_commit(session))
84
if (!thd->locked_tables)
85
thd->options&= ~OPTION_TABLE_LOCK;
86
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
107
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
108
session->transaction.all.modified_non_trans_table= false;
90
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
91
thd->transaction.all.modified_non_trans_table= false;
113
bool begin_trans(Session *session)
96
bool begin_trans(THD *thd)
116
if (session->locked_tables)
99
if (thd->locked_tables)
118
session->lock=session->locked_tables;
119
session->locked_tables=0; // Will be automatically closed
120
close_thread_tables(session); // Free tables
101
thd->lock=thd->locked_tables;
102
thd->locked_tables=0; // Will be automatically closed
103
close_thread_tables(thd); // Free tables
122
if (end_active_trans(session))
105
if (end_active_trans(thd))
126
LEX *lex= session->lex;
127
session->options|= OPTION_BEGIN;
128
session->server_status|= SERVER_STATUS_IN_TRANS;
110
thd->options|= OPTION_BEGIN;
111
thd->server_status|= SERVER_STATUS_IN_TRANS;
129
112
if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
130
error= ha_start_consistent_snapshot(session);
113
error= ha_start_consistent_snapshot(thd);
136
119
Returns true if all tables should be ignored.
138
inline bool all_tables_not_ok(Session *, TableList *)
121
inline bool all_tables_not_ok(THD *thd, TableList *tables)
123
return rpl_filter->is_on() && tables &&
124
!rpl_filter->tables_ok(thd->db, tables);
144
static bool some_non_temp_table_to_be_updated(Session *session, TableList *tables)
128
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
146
130
for (TableList *table= tables; table; table= table->next_global)
148
132
assert(table->db && table->table_name);
149
133
if (table->updating &&
150
!find_temporary_table(session, table->db, table->table_name))
134
!find_temporary_table(thd, table->db, table->table_name))
167
151
a number of modified rows
170
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
154
uint32_t sql_command_flags[SQLCOM_END+1];
172
156
void init_update_queries(void)
176
for (x= 0; x <= SQLCOM_END; x++)
177
sql_command_flags[x].reset();
158
memset(&sql_command_flags, 0, sizeof(sql_command_flags));
179
160
sql_command_flags[SQLCOM_CREATE_TABLE]= CF_CHANGES_DATA;
180
161
sql_command_flags[SQLCOM_CREATE_INDEX]= CF_CHANGES_DATA;
229
210
bool is_update_query(enum enum_sql_command command)
231
212
assert(command >= 0 && command <= SQLCOM_END);
232
return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
213
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
235
void execute_init_command(Session *session, sys_var_str *init_command_var,
216
void execute_init_command(THD *thd, sys_var_str *init_command_var,
236
217
rw_lock_t *var_mutex)
239
220
ulong save_client_capabilities;
241
session->set_proc_info("Execution of init_command");
222
thd->set_proc_info("Execution of init_command");
243
224
We need to lock init_command_var because
244
225
during execution of init_command_var query
245
226
values of init_command_var can't be changed
247
228
rw_rdlock(var_mutex);
248
save_client_capabilities= session->client_capabilities;
249
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
229
save_client_capabilities= thd->client_capabilities;
230
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
251
232
We don't need return result of execution to client side.
252
To forbid this we should set session->net.vio to 0.
233
To forbid this we should set thd->net.vio to 0.
254
save_vio= session->net.vio;
256
dispatch_command(COM_QUERY, session,
235
save_vio= thd->net.vio;
237
dispatch_command(COM_QUERY, thd,
257
238
init_command_var->value,
258
239
init_command_var->value_length);
259
240
rw_unlock(var_mutex);
260
session->client_capabilities= save_client_capabilities;
261
session->net.vio= save_vio;
241
thd->client_capabilities= save_client_capabilities;
242
thd->net.vio= save_vio;
265
246
Ends the current transaction and (maybe) begin the next.
267
@param session Current thread
248
@param thd Current thread
268
249
@param completion Completion type
274
int end_trans(Session *session, enum enum_mysql_completiontype completion)
255
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
276
257
bool do_release= 0;
279
if (session->transaction.xid_state.xa_state != XA_NOTR)
260
if (thd->transaction.xid_state.xa_state != XA_NOTR)
281
262
my_error(ER_XAER_RMFAIL, MYF(0),
282
xa_state_names[session->transaction.xid_state.xa_state]);
263
xa_state_names[thd->transaction.xid_state.xa_state]);
285
266
switch (completion) {
289
270
even if there is a problem with the OPTION_AUTO_COMMIT flag
290
271
(Which of course should never happen...)
292
session->server_status&= ~SERVER_STATUS_IN_TRANS;
293
res= ha_commit(session);
294
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
295
session->transaction.all.modified_non_trans_table= false;
273
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
275
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
276
thd->transaction.all.modified_non_trans_table= false;
297
278
case COMMIT_RELEASE:
298
279
do_release= 1; /* fall through */
299
280
case COMMIT_AND_CHAIN:
300
res= end_active_trans(session);
281
res= end_active_trans(thd);
301
282
if (!res && completion == COMMIT_AND_CHAIN)
302
res= begin_trans(session);
283
res= begin_trans(thd);
304
285
case ROLLBACK_RELEASE:
305
286
do_release= 1; /* fall through */
307
288
case ROLLBACK_AND_CHAIN:
309
session->server_status&= ~SERVER_STATUS_IN_TRANS;
310
if (ha_rollback(session))
290
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
291
if (ha_rollback(thd))
312
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
session->transaction.all.modified_non_trans_table= false;
293
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
294
thd->transaction.all.modified_non_trans_table= false;
314
295
if (!res && (completion == ROLLBACK_AND_CHAIN))
315
res= begin_trans(session);
296
res= begin_trans(thd);
362
343
the client, the connection is closed or "net_wait_timeout"
363
344
number of seconds has passed
365
my_net_set_read_timeout(net, session->variables.net_wait_timeout);
346
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
368
349
XXX: this code is here only to clear possible errors of init_connect.
369
350
Consider moving to init_connect() instead.
371
session->clear_error(); // Clear error message
372
session->main_da.reset_diagnostics_area();
352
thd->clear_error(); // Clear error message
353
thd->main_da.reset_diagnostics_area();
374
355
net_new_transaction(net);
417
398
command= COM_END; // Wrong command
419
400
/* Restore read timeout value */
420
my_net_set_read_timeout(net, session->variables.net_read_timeout);
401
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
422
403
assert(packet_length);
423
return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
404
return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
426
407
return(return_value);
490
471
Perform one connection-level (COM_XXXX) command.
492
473
@param command type of command to perform
493
@param session connection handle
474
@param thd connection handle
494
475
@param packet data for the command, packet is always null-terminated
495
476
@param packet_length length of packet + 1 (to show that data is
496
477
null-terminated) except for COM_SLEEP, where it
500
set session->lex->sql_command to SQLCOM_END here.
481
set thd->lex->sql_command to SQLCOM_END here.
502
483
The following has to be changed to an 8 byte integer
507
488
1 request of thread shutdown, i. e. if command is
508
489
COM_QUIT/COM_SHUTDOWN
510
bool dispatch_command(enum enum_server_command command, Session *session,
511
char* packet, uint32_t packet_length)
491
bool dispatch_command(enum enum_server_command command, THD *thd,
492
char* packet, uint32_t packet_length)
513
NET *net= &session->net;
515
Query_id &query_id= Query_id::get_query_id();
517
session->command=command;
518
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
497
thd->command=command;
498
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
520
500
pthread_mutex_lock(&LOCK_thread_count);
521
session->query_id= query_id.value();
501
thd->query_id= global_query_id;
523
503
switch( command ) {
524
504
/* Ignore these statements. */
527
507
/* Increase id and count all other statements. */
529
statistic_increment(session->status_var.questions, &LOCK_status);
509
statistic_increment(thd->status_var.questions, &LOCK_status);
533
513
thread_running++;
534
/* TODO: set session->lex->sql_command to SQLCOM_END here */
514
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
535
515
pthread_mutex_unlock(&LOCK_thread_count);
537
logging_pre_do(session);
539
session->server_status&=
540
520
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
541
521
switch (command) {
542
522
case COM_INIT_DB:
545
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
546
session->convert_string(&tmp, system_charset_info,
547
packet, packet_length, session->charset());
548
if (!mysql_change_db(session, &tmp, false))
525
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
526
thd->convert_string(&tmp, system_charset_info,
527
packet, packet_length, thd->charset());
528
if (!mysql_change_db(thd, &tmp, false))
554
534
case COM_CHANGE_USER:
556
status_var_increment(session->status_var.com_other);
536
status_var_increment(thd->status_var.com_other);
557
537
char *user= (char*) packet, *packet_end= packet + packet_length;
558
538
/* Safe because there is always a trailing \0 at the end of the packet */
559
539
char *passwd= strchr(user, '\0')+1;
562
session->clear_error(); // if errors from rollback
542
thd->clear_error(); // if errors from rollback
565
545
Old clients send null-terminated string ('\0' for empty string) for
581
561
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
584
uint32_t passwd_len= (session->client_capabilities & CLIENT_SECURE_CONNECTION ?
564
uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
585
565
(unsigned char)(*passwd++) : strlen(passwd));
586
566
uint32_t dummy_errors, save_db_length, db_length;
588
Security_context save_security_ctx= *session->security_ctx;
568
Security_context save_security_ctx= *thd->security_ctx;
589
569
USER_CONN *save_user_connect;
591
571
db+= passwd_len + 1;
617
597
/* Convert database name to utf8 */
618
598
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
619
599
system_charset_info, db, db_length,
620
session->charset(), &dummy_errors)]= 0;
600
thd->charset(), &dummy_errors)]= 0;
623
603
/* Save user and privileges */
624
save_db_length= session->db_length;
625
save_db= session->db;
626
save_user_connect= session->user_connect;
604
save_db_length= thd->db_length;
606
save_user_connect= thd->user_connect;
628
if (!(session->security_ctx->user= my_strdup(user, MYF(0))))
608
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
630
session->security_ctx->user= save_security_ctx.user;
610
thd->security_ctx->user= save_security_ctx.user;
631
611
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
635
615
/* Clear variables that are allocated */
636
session->user_connect= 0;
637
res= check_user(session, passwd, passwd_len, db, false);
616
thd->user_connect= 0;
617
res= check_user(thd, passwd, passwd_len, db, false);
641
if (session->security_ctx->user)
642
free(session->security_ctx->user);
643
*session->security_ctx= save_security_ctx;
644
session->user_connect= save_user_connect;
645
session->db= save_db;
646
session->db_length= save_db_length;
621
if (thd->security_ctx->user)
622
free(thd->security_ctx->user);
623
*thd->security_ctx= save_security_ctx;
624
thd->user_connect= save_user_connect;
626
thd->db_length= save_db_length;
657
session_init_client_charset(session, cs_number);
658
session->update_charset();
637
thd_init_client_charset(thd, cs_number);
638
thd->update_charset();
665
if (alloc_query(session, packet, packet_length))
645
if (alloc_query(thd, packet, packet_length))
666
646
break; // fatal error is set
667
char *packet_end= session->query + session->query_length;
647
char *packet_end= thd->query + thd->query_length;
668
648
const char* end_of_stmt= NULL;
670
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
650
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
672
while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
652
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
674
654
char *beginning_of_next_stmt= (char*) end_of_stmt;
676
net_end_statement(session);
656
net_end_statement(thd);
678
658
Multiple queries exits, execute them individually
680
close_thread_tables(session);
660
close_thread_tables(thd);
681
661
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
683
log_slow_statement(session);
663
log_slow_statement(thd);
685
665
/* Remove garbage at start of query */
686
while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
666
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
688
668
beginning_of_next_stmt++;
692
672
pthread_mutex_lock(&LOCK_thread_count);
693
session->query_length= length;
694
session->query= beginning_of_next_stmt;
673
thd->query_length= length;
674
thd->query= beginning_of_next_stmt;
696
676
Count each statement from the client.
698
statistic_increment(session->status_var.questions, &LOCK_status);
699
session->query_id= query_id.next();
700
session->set_time(); /* Reset the query start time. */
701
/* TODO: set session->lex->sql_command to SQLCOM_END here */
678
statistic_increment(thd->status_var.questions, &LOCK_status);
679
thd->query_id= next_query_id();
680
thd->set_time(); /* Reset the query start time. */
681
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
702
682
pthread_mutex_unlock(&LOCK_thread_count);
704
mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
684
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
713
693
LEX_STRING conv_name;
715
695
/* used as fields initializator */
718
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
698
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
719
699
memset(&table_list, 0, sizeof(table_list));
720
if (session->copy_db_to(&table_list.db, &table_list.db_length))
700
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
723
703
We have name + wildcard in packet, separated by endzero
725
705
arg_end= strchr(packet, '\0');
726
session->convert_string(&conv_name, system_charset_info,
727
packet, (uint32_t) (arg_end - packet), session->charset());
706
thd->convert_string(&conv_name, system_charset_info,
707
packet, (uint32_t) (arg_end - packet), thd->charset());
728
708
table_list.alias= table_list.table_name= conv_name.str;
729
709
packet= arg_end + 1;
731
711
if (!my_strcasecmp(system_charset_info, table_list.db,
732
INFORMATION_SCHEMA_NAME.c_str()))
712
INFORMATION_SCHEMA_NAME.str))
734
ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
714
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
735
715
if (schema_table)
736
716
table_list.schema_table= schema_table;
739
session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
740
if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
719
thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
720
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
742
722
if (lower_case_table_names)
743
723
my_casedn_str(files_charset_info, table_list.table_name);
745
725
/* init structures for VIEW processing */
746
table_list.select_lex= &(session->lex->select_lex);
749
mysql_reset_session_for_next_command(session);
726
table_list.select_lex= &(thd->lex->select_lex);
729
mysql_reset_thd_for_next_command(thd);
752
732
select_lex.table_list.link_in_list((unsigned char*) &table_list,
753
733
(unsigned char**) &table_list.next_local);
754
session->lex->add_to_query_tables(&table_list);
734
thd->lex->add_to_query_tables(&table_list);
756
736
/* switch on VIEW optimisation: do not fill temporary tables */
757
session->lex->sql_command= SQLCOM_SHOW_FIELDS;
758
mysqld_list_fields(session,&table_list,fields);
759
session->lex->unit.cleanup();
760
session->cleanup_after_query();
737
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
738
mysqld_list_fields(thd,&table_list,fields);
739
thd->lex->unit.cleanup();
740
thd->cleanup_after_query();
764
744
/* We don't calculate statistics for this command */
765
745
net->error=0; // Don't give 'abort' message
766
session->main_da.disable_status(); // Don't send anything back
746
thd->main_da.disable_status(); // Don't send anything back
767
747
error=true; // End server
769
749
case COM_BINLOG_DUMP:
773
753
uint32_t slave_server_id;
775
status_var_increment(session->status_var.com_other);
755
status_var_increment(thd->status_var.com_other);
776
756
/* TODO: The following has to be changed to an 8 byte integer */
777
757
pos = uint4korr(packet);
778
758
flags = uint2korr(packet + 4);
779
session->server_id=0; /* avoid suicide */
759
thd->server_id=0; /* avoid suicide */
780
760
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
781
761
kill_zombie_dump_threads(slave_server_id);
782
session->server_id = slave_server_id;
762
thd->server_id = slave_server_id;
784
mysql_binlog_send(session, session->strdup(packet + 10), (my_off_t) pos, flags);
764
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
785
765
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
789
769
case COM_SHUTDOWN:
791
status_var_increment(session->status_var.com_other);
793
close_thread_tables(session); // Free before kill
771
status_var_increment(thd->status_var.com_other);
773
close_thread_tables(thd); // Free before kill
799
status_var_increment(session->status_var.com_other);
800
my_ok(session); // Tell client we are alive
779
status_var_increment(thd->status_var.com_other);
780
my_ok(thd); // Tell client we are alive
802
782
case COM_PROCESS_INFO:
803
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
804
mysqld_list_processes(session, NULL, 0);
783
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
784
mysqld_list_processes(thd, NULL, 0);
806
786
case COM_PROCESS_KILL:
808
status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
788
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
809
789
ulong id=(ulong) uint4korr(packet);
810
sql_kill(session,id,false);
790
sql_kill(thd,id,false);
813
793
case COM_SET_OPTION:
815
status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
795
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
816
796
uint32_t opt_command= uint2korr(packet);
818
798
switch (opt_command) {
819
799
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
820
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
800
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
823
803
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
824
session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
804
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
828
808
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
842
822
/* If commit fails, we should be able to reset the OK status. */
843
session->main_da.can_overwrite_status= true;
844
ha_autocommit_or_rollback(session, session->is_error());
845
session->main_da.can_overwrite_status= false;
823
thd->main_da.can_overwrite_status= true;
824
ha_autocommit_or_rollback(thd, thd->is_error());
825
thd->main_da.can_overwrite_status= false;
847
session->transaction.stmt.reset();
827
thd->transaction.stmt.reset();
850
830
/* report error issued during command execution */
851
if (session->killed_errno())
853
if (! session->main_da.is_set())
854
session->send_kill_message();
856
if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
858
session->killed= Session::NOT_KILLED;
859
session->mysys_var->abort= 0;
862
net_end_statement(session);
864
session->set_proc_info("closing tables");
831
if (thd->killed_errno())
833
if (! thd->main_da.is_set())
834
thd->send_kill_message();
836
if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
838
thd->killed= THD::NOT_KILLED;
839
thd->mysys_var->abort= 0;
842
net_end_statement(thd);
844
thd->set_proc_info("closing tables");
865
845
/* Free tables */
866
close_thread_tables(session);
868
log_slow_statement(session);
870
session->set_proc_info("cleaning up");
846
close_thread_tables(thd);
848
log_slow_statement(thd);
850
thd->set_proc_info("cleaning up");
871
851
pthread_mutex_lock(&LOCK_thread_count); // For process list
872
session->set_proc_info(0);
873
session->command=COM_SLEEP;
875
session->query_length=0;
852
thd->set_proc_info(0);
853
thd->command=COM_SLEEP;
876
856
thread_running--;
877
857
pthread_mutex_unlock(&LOCK_thread_count);
878
session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
879
free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
858
thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
859
free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
884
void log_slow_statement(Session *session)
864
void log_slow_statement(THD *thd)
886
logging_post_do(session);
866
logging_post_do(thd);
954
934
/* 'parent_lex' is used in init_query() so it must be before it. */
955
935
schema_select_lex->parent_lex= lex;
956
936
schema_select_lex->init_query();
957
if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
937
if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
959
939
lex->query_tables_last= query_tables_last;
989
Read query from packet and store in session->query.
969
Read query from packet and store in thd->query.
990
970
Used in COM_QUERY and COM_STMT_PREPARE.
992
Sets the following Session variables:
972
Sets the following THD variables:
999
true error; In this case session->fatal_error is set
979
true error; In this case thd->fatal_error is set
1002
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
982
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
1004
984
/* Remove garbage at start and end of query */
1005
while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
985
while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
1008
988
packet_length--;
1010
990
const char *pos= packet + packet_length; // Point at end null
1011
991
while (packet_length > 0 &&
1012
(pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
992
(pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
1015
995
packet_length--;
1017
997
/* We must allocate some extra memory for query cache */
1018
session->query_length= 0; // Extra safety: Avoid races
1019
if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
998
thd->query_length= 0; // Extra safety: Avoid races
999
if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
1021
session->db_length+ 1)))
1001
thd->db_length+ 1)))
1023
session->query[packet_length]=0;
1024
session->query_length= packet_length;
1003
thd->query[packet_length]=0;
1004
thd->query_length= packet_length;
1026
1006
/* Reclaim some memory */
1027
session->packet.shrink(session->variables.net_buffer_length);
1028
session->convert_buffer.shrink(session->variables.net_buffer_length);
1007
thd->packet.shrink(thd->variables.net_buffer_length);
1008
thd->convert_buffer.shrink(thd->variables.net_buffer_length);
1013
static void reset_one_shot_variables(THD *thd)
1015
thd->variables.character_set_client=
1016
global_system_variables.character_set_client;
1017
thd->variables.collation_connection=
1018
global_system_variables.collation_connection;
1019
thd->variables.collation_database=
1020
global_system_variables.collation_database;
1021
thd->variables.collation_server=
1022
global_system_variables.collation_server;
1023
thd->update_charset();
1024
thd->variables.time_zone=
1025
global_system_variables.time_zone;
1026
thd->variables.lc_time_names= &my_locale_en_US;
1027
thd->one_shot_set= 0;
1034
Execute command saved in session and lex->sql_command.
1032
Execute command saved in thd and lex->sql_command.
1036
1034
Before every operation that can request a write lock for a table
1037
1035
wait if a global read lock exists. However do not wait if this
1129
1127
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1130
1128
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1131
1129
lex->drop_temporary && lex->drop_if_exists) &&
1132
all_tables_not_ok(session, all_tables))
1130
all_tables_not_ok(thd, all_tables))
1134
1132
/* we warn the slave SQL thread */
1135
1133
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1134
if (thd->one_shot_set)
1137
It's ok to check thd->one_shot_set here:
1139
The charsets in a MySQL 5.0 slave can change by both a binlogged
1140
SET ONE_SHOT statement and the event-internal charset setting,
1141
and these two ways to change charsets do not seems to work
1144
At least there seems to be problems in the rli cache for
1145
charsets if we are using ONE_SHOT. Note that this is normally no
1146
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1147
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1149
reset_one_shot_variables(thd);
1142
1157
When option readonly is set deny operations which change non-temporary
1143
1158
tables. Except for the replication thread and the 'super' users.
1145
if (deny_updates_if_read_only_option(session, all_tables))
1160
if (deny_updates_if_read_only_option(thd, all_tables))
1147
1162
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1150
1165
} /* endif unlikely slave */
1151
status_var_increment(session->status_var.com_stat[lex->sql_command]);
1166
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1153
assert(session->transaction.stmt.modified_non_trans_table == false);
1168
assert(thd->transaction.stmt.modified_non_trans_table == false);
1155
1170
switch (lex->sql_command) {
1156
1171
case SQLCOM_SHOW_STATUS:
1158
system_status_var old_status_var= session->status_var;
1159
session->initial_status_var= &old_status_var;
1160
res= execute_sqlcom_select(session, all_tables);
1173
system_status_var old_status_var= thd->status_var;
1174
thd->initial_status_var= &old_status_var;
1175
res= execute_sqlcom_select(thd, all_tables);
1161
1176
/* Don't log SHOW STATUS commands to slow query log */
1162
session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1177
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1163
1178
SERVER_QUERY_NO_GOOD_INDEX_USED);
1165
1180
restore status variables, as we don't want 'show status' to cause
1168
1183
pthread_mutex_lock(&LOCK_status);
1169
add_diff_to_status(&global_status_var, &session->status_var,
1184
add_diff_to_status(&global_status_var, &thd->status_var,
1170
1185
&old_status_var);
1171
session->status_var= old_status_var;
1186
thd->status_var= old_status_var;
1172
1187
pthread_mutex_unlock(&LOCK_status);
1181
1196
case SQLCOM_SHOW_VARIABLES:
1182
1197
case SQLCOM_SELECT:
1184
session->status_var.last_query_cost= 0.0;
1185
res= execute_sqlcom_select(session, all_tables);
1199
thd->status_var.last_query_cost= 0.0;
1200
res= execute_sqlcom_select(thd, all_tables);
1188
1203
case SQLCOM_EMPTY_QUERY:
1192
1207
case SQLCOM_PURGE:
1194
res = purge_master_logs(session, lex->to_log);
1209
res = purge_master_logs(thd, lex->to_log);
1197
1212
case SQLCOM_PURGE_BEFORE:
1212
1227
value of constant
1214
1229
it->quick_fix_field();
1215
res = purge_master_logs_before_date(session, (ulong)it->val_int());
1230
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1218
1233
case SQLCOM_SHOW_WARNS:
1220
res= mysqld_show_warnings(session, (uint32_t)
1235
res= mysqld_show_warnings(thd, (uint32_t)
1221
1236
((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1222
1237
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1223
1238
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1227
1242
case SQLCOM_SHOW_ERRORS:
1229
res= mysqld_show_warnings(session, (uint32_t)
1244
res= mysqld_show_warnings(thd, (uint32_t)
1230
1245
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1233
1248
case SQLCOM_ASSIGN_TO_KEYCACHE:
1235
1250
assert(first_table == all_tables && first_table != 0);
1236
res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1251
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1239
1254
case SQLCOM_CHANGE_MASTER:
1241
1256
pthread_mutex_lock(&LOCK_active_mi);
1242
res = change_master(session,active_mi);
1257
res = change_master(thd,active_mi);
1243
1258
pthread_mutex_unlock(&LOCK_active_mi);
1248
1263
pthread_mutex_lock(&LOCK_active_mi);
1249
1264
if (active_mi != NULL)
1251
res = show_master_info(session, active_mi);
1266
res = show_master_info(thd, active_mi);
1255
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1270
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1256
1271
"the master info structure does not exist");
1259
1274
pthread_mutex_unlock(&LOCK_active_mi);
1262
1277
case SQLCOM_SHOW_MASTER_STAT:
1264
res = show_binlog_info(session);
1279
res = show_binlog_info(thd);
1268
1283
case SQLCOM_SHOW_ENGINE_STATUS:
1270
res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1285
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1273
1288
case SQLCOM_CREATE_TABLE:
1299
1314
safety, only in case of Alter_info we have to do (almost) a deep
1302
Alter_info alter_info(lex->alter_info, session->mem_root);
1317
Alter_info alter_info(lex->alter_info, thd->mem_root);
1304
if (session->is_fatal_error)
1319
if (thd->is_fatal_error)
1306
1321
/* If out of memory when creating a copy of alter_info. */
1308
1323
goto end_with_restore_list;
1311
if ((res= create_table_precheck(session, select_tables, create_table)))
1326
if ((res= create_table_precheck(thd, select_tables, create_table)))
1312
1327
goto end_with_restore_list;
1314
1329
/* Might have been updated in create_table_precheck */
1317
1332
#ifdef HAVE_READLINK
1318
1333
/* Fix names if symlinked tables */
1319
if (append_file_to_dir(session, &create_info.data_file_name,
1334
if (append_file_to_dir(thd, &create_info.data_file_name,
1320
1335
create_table->table_name) ||
1321
append_file_to_dir(session, &create_info.index_file_name,
1336
append_file_to_dir(thd, &create_info.index_file_name,
1322
1337
create_table->table_name))
1323
1338
goto end_with_restore_list;
1414
1429
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1415
1430
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1416
session->options|= OPTION_KEEP_LOG;
1431
thd->options|= OPTION_KEEP_LOG;
1417
1432
/* regular create */
1418
1433
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1419
res= mysql_create_like_table(session, create_table, select_tables,
1434
res= mysql_create_like_table(thd, create_table, select_tables,
1423
res= mysql_create_table(session, create_table->db,
1438
res= mysql_create_table(thd, create_table->db,
1424
1439
create_table->table_name, &create_info,
1425
1440
&alter_info, 0, 0);
1431
1446
/* put tables back for PS rexecuting */
1448
1463
/* Prepare stack copies to be re-execution safe */
1449
1464
HA_CREATE_INFO create_info;
1450
Alter_info alter_info(lex->alter_info, session->mem_root);
1465
Alter_info alter_info(lex->alter_info, thd->mem_root);
1452
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1467
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1455
1470
assert(first_table == all_tables && first_table != 0);
1456
if (end_active_trans(session))
1471
if (end_active_trans(thd))
1459
1474
memset(&create_info, 0, sizeof(create_info));
1460
1475
create_info.db_type= 0;
1461
1476
create_info.row_type= ROW_TYPE_NOT_USED;
1462
create_info.default_table_charset= session->variables.collation_database;
1477
create_info.default_table_charset= thd->variables.collation_database;
1464
res= mysql_alter_table(session, first_table->db, first_table->table_name,
1479
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1465
1480
&create_info, first_table, &alter_info,
1466
1481
0, (order_st*) 0, 0);
1530
1545
/* Don't yet allow changing of symlinks with ALTER TABLE */
1531
1546
if (create_info.data_file_name)
1532
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1547
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1533
1548
"DATA DIRECTORY option ignored");
1534
1549
if (create_info.index_file_name)
1535
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1550
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1536
1551
"INDEX DIRECTORY option ignored");
1537
1552
create_info.data_file_name= create_info.index_file_name= NULL;
1538
1553
/* ALTER TABLE ends previous transaction */
1539
if (end_active_trans(session))
1554
if (end_active_trans(thd))
1542
if (!session->locked_tables &&
1543
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1557
if (!thd->locked_tables &&
1558
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1549
res= mysql_alter_table(session, select_lex->db, lex->name.str,
1564
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1579
1594
case SQLCOM_SHOW_BINLOGS:
1581
res = show_binlogs(session);
1596
res = show_binlogs(thd);
1584
1599
case SQLCOM_SHOW_CREATE:
1585
1600
assert(first_table == all_tables && first_table != 0);
1587
res= mysqld_show_create(session, first_table);
1602
res= mysqld_show_create(thd, first_table);
1590
1605
case SQLCOM_CHECKSUM:
1592
1607
assert(first_table == all_tables && first_table != 0);
1593
res = mysql_checksum_table(session, first_table, &lex->check_opt);
1608
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1596
1611
case SQLCOM_REPAIR:
1598
1613
assert(first_table == all_tables && first_table != 0);
1599
res= mysql_repair_table(session, first_table, &lex->check_opt);
1614
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1600
1615
/* ! we write after unlocking the table */
1602
1617
Presumably, REPAIR and binlog writing doesn't require synchronization
1604
write_bin_log(session, true, session->query, session->query_length);
1619
write_bin_log(thd, true, thd->query, thd->query_length);
1605
1620
select_lex->table_list.first= (unsigned char*) first_table;
1606
1621
lex->query_tables=all_tables;
1617
1632
case SQLCOM_ANALYZE:
1619
1634
assert(first_table == all_tables && first_table != 0);
1620
res= mysql_analyze_table(session, first_table, &lex->check_opt);
1635
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1621
1636
/* ! we write after unlocking the table */
1622
write_bin_log(session, true, session->query, session->query_length);
1637
write_bin_log(thd, true, thd->query, thd->query_length);
1623
1638
select_lex->table_list.first= (unsigned char*) first_table;
1624
1639
lex->query_tables=all_tables;
1628
1643
case SQLCOM_OPTIMIZE:
1630
1645
assert(first_table == all_tables && first_table != 0);
1631
res= mysql_optimize_table(session, first_table, &lex->check_opt);
1646
res= mysql_optimize_table(thd, first_table, &lex->check_opt);
1632
1647
/* ! we write after unlocking the table */
1633
write_bin_log(session, true, session->query, session->query_length);
1648
write_bin_log(thd, true, thd->query, thd->query_length);
1634
1649
select_lex->table_list.first= (unsigned char*) first_table;
1635
1650
lex->query_tables=all_tables;
1638
1653
case SQLCOM_UPDATE:
1639
1654
assert(first_table == all_tables && first_table != 0);
1640
if (update_precheck(session, all_tables))
1655
if (update_precheck(thd, all_tables))
1642
1657
assert(select_lex->offset_limit == 0);
1643
1658
unit->set_limit(select_lex);
1644
res= (up_result= mysql_update(session, all_tables,
1659
res= (up_result= mysql_update(thd, all_tables,
1645
1660
select_lex->item_list,
1646
1661
lex->value_list,
1647
1662
select_lex->where,
1659
1674
/* if we switched from normal update, rights are checked */
1660
1675
if (up_result != 2)
1662
if ((res= multi_update_precheck(session, all_tables)))
1677
if ((res= multi_update_precheck(thd, all_tables)))
1668
res= mysql_multi_update_prepare(session);
1683
res= mysql_multi_update_prepare(thd);
1670
1685
/* Check slave filtering rules */
1671
if (unlikely(session->slave_thread))
1686
if (unlikely(thd->slave_thread))
1673
if (all_tables_not_ok(session, all_tables))
1688
if (all_tables_not_ok(thd, all_tables))
1677
1692
res= 0; /* don't care of prev failure */
1678
session->clear_error(); /* filters are of highest prior */
1693
thd->clear_error(); /* filters are of highest prior */
1680
1695
/* we warn the slave SQL thread */
1681
1696
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1708
1723
case SQLCOM_INSERT:
1710
1725
assert(first_table == all_tables && first_table != 0);
1711
if ((res= insert_precheck(session, all_tables)))
1726
if ((res= insert_precheck(thd, all_tables)))
1714
if (!session->locked_tables &&
1715
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1729
if (!thd->locked_tables &&
1730
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1721
res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1736
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1722
1737
lex->update_list, lex->value_list,
1723
1738
lex->duplicates, lex->ignore);
1742
1757
unit->set_limit(select_lex);
1744
if (! session->locked_tables &&
1745
! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1759
if (! thd->locked_tables &&
1760
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1751
if (!(res= open_and_lock_tables(session, all_tables)))
1766
if (!(res= open_and_lock_tables(thd, all_tables)))
1753
1768
/* Skip first table, which is the table we are inserting in */
1754
1769
TableList *second_table= first_table->next_local;
1755
1770
select_lex->table_list.first= (unsigned char*) second_table;
1756
1771
select_lex->context.table_list=
1757
1772
select_lex->context.first_name_resolution_table= second_table;
1758
res= mysql_insert_select_prepare(session);
1773
res= mysql_insert_select_prepare(thd);
1759
1774
if (!res && (sel_result= new select_insert(first_table,
1760
1775
first_table->table,
1761
1776
&lex->field_list,
1798
1813
Don't allow this within a transaction because we want to use
1799
1814
re-generate table
1801
if (session->locked_tables || session->active_transaction())
1816
if (thd->locked_tables || thd->active_transaction())
1803
1818
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1804
1819
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1808
res= mysql_truncate(session, first_table, 0);
1823
res= mysql_truncate(thd, first_table, 0);
1811
1826
case SQLCOM_DELETE:
1814
1829
assert(select_lex->offset_limit == 0);
1815
1830
unit->set_limit(select_lex);
1817
if (!session->locked_tables &&
1818
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1832
if (!thd->locked_tables &&
1833
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1824
res = mysql_delete(session, all_tables, select_lex->where,
1839
res = mysql_delete(thd, all_tables, select_lex->where,
1825
1840
&select_lex->order_list,
1826
1841
unit->select_limit_cnt, select_lex->options,
1832
1847
assert(first_table == all_tables && first_table != 0);
1833
1848
TableList *aux_tables=
1834
(TableList *)session->lex->auxiliary_table_list.first;
1849
(TableList *)thd->lex->auxiliary_table_list.first;
1835
1850
multi_delete *del_result;
1837
if (!session->locked_tables &&
1838
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1852
if (!thd->locked_tables &&
1853
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1844
if ((res= multi_delete_precheck(session, all_tables)))
1859
if ((res= multi_delete_precheck(thd, all_tables)))
1847
1862
/* condition will be true on SP re-excuting */
1848
1863
if (select_lex->item_list.elements != 0)
1849
1864
select_lex->item_list.empty();
1850
if (add_item_to_list(session, new Item_null()))
1865
if (add_item_to_list(thd, new Item_null()))
1853
session->set_proc_info("init");
1854
if ((res= open_and_lock_tables(session, all_tables)))
1868
thd->set_proc_info("init");
1869
if ((res= open_and_lock_tables(thd, all_tables)))
1857
if ((res= mysql_multi_delete_prepare(session)))
1872
if ((res= mysql_multi_delete_prepare(thd)))
1860
if (!session->is_fatal_error &&
1875
if (!thd->is_fatal_error &&
1861
1876
(del_result= new multi_delete(aux_tables, lex->table_count)))
1863
res= mysql_select(session, &select_lex->ref_pointer_array,
1878
res= mysql_select(thd, &select_lex->ref_pointer_array,
1864
1879
select_lex->get_table_list(),
1865
1880
select_lex->with_wild,
1866
1881
select_lex->item_list,
1867
1882
select_lex->where,
1868
1883
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1869
1884
(order_st *)NULL,
1870
select_lex->options | session->options |
1885
select_lex->options | thd->options |
1871
1886
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1872
1887
OPTION_SETUP_TABLES_DONE,
1873
1888
del_result, unit, select_lex);
1874
res|= session->is_error();
1889
res|= thd->is_error();
1876
1891
del_result->abort();
1877
1892
delete del_result;
1898
1913
To not generate such irrelevant "table does not exist errors",
1899
1914
we silently add IF EXISTS if TEMPORARY was used.
1901
if (session->slave_thread)
1916
if (thd->slave_thread)
1902
1917
lex->drop_if_exists= 1;
1904
1919
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1905
session->options|= OPTION_KEEP_LOG;
1920
thd->options|= OPTION_KEEP_LOG;
1907
1922
/* DDL and binlog write order protected by LOCK_open */
1908
res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1923
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
1911
1926
case SQLCOM_SHOW_PROCESSLIST:
1912
mysqld_list_processes(session, NULL, lex->verbose);
1927
mysqld_list_processes(thd, NULL, lex->verbose);
1914
1929
case SQLCOM_SHOW_ENGINE_LOGS:
1916
res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1931
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
1919
1934
case SQLCOM_CHANGE_DB:
1921
1936
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
1923
if (!mysql_change_db(session, &db_str, false))
1938
if (!mysql_change_db(thd, &db_str, false))
1942
res= mysql_load(session, lex->exchange, first_table, lex->field_list,
1957
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
1943
1958
lex->update_list, lex->value_list, lex->duplicates,
1944
1959
lex->ignore, (bool) lex->local_file);
1950
1965
List<set_var_base> *lex_var_list= &lex->var_list;
1952
if (lex->autocommit && end_active_trans(session))
1967
if (lex->autocommit && end_active_trans(thd))
1955
if (open_and_lock_tables(session, all_tables))
1957
if (!(res= sql_set_variables(session, lex_var_list)))
1970
if (open_and_lock_tables(thd, all_tables))
1972
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
1974
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
1977
if (!(res= sql_set_variables(thd, lex_var_list)))
1980
If the previous command was a SET ONE_SHOT, we don't want to forget
1981
about the ONE_SHOT property of that SET. So we use a |= instead of = .
1983
thd->one_shot_set|= lex->one_shot_set;
1980
2005
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
1981
2006
false, mysqldump will not work.
1983
unlock_locked_tables(session);
1984
if (session->options & OPTION_TABLE_LOCK)
2008
unlock_locked_tables(thd);
2009
if (thd->options & OPTION_TABLE_LOCK)
1986
end_active_trans(session);
1987
session->options&= ~(OPTION_TABLE_LOCK);
2011
end_active_trans(thd);
2012
thd->options&= ~(OPTION_TABLE_LOCK);
1989
if (session->global_read_lock)
1990
unlock_global_read_lock(session);
2014
if (thd->global_read_lock)
2015
unlock_global_read_lock(thd);
1993
2018
case SQLCOM_LOCK_TABLES:
1995
2020
We try to take transactional locks if
1996
2021
- only transactional locks are requested (lex->lock_transactional) and
1997
- no non-transactional locks exist (!session->locked_tables).
2022
- no non-transactional locks exist (!thd->locked_tables).
1999
if (lex->lock_transactional && !session->locked_tables)
2024
if (lex->lock_transactional && !thd->locked_tables)
2003
2028
All requested locks are transactional and no non-transactional
2006
if ((rc= try_transactional_lock(session, all_tables)) == -1)
2031
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2024
2049
requested. If yes, warn about the conversion to non-transactional
2025
2050
locks or abort in strict mode.
2027
if (check_transactional_lock(session, all_tables))
2052
if (check_transactional_lock(thd, all_tables))
2029
unlock_locked_tables(session);
2054
unlock_locked_tables(thd);
2030
2055
/* we must end the trasaction first, regardless of anything */
2031
if (end_active_trans(session))
2056
if (end_active_trans(thd))
2033
session->in_lock_tables=1;
2034
session->options|= OPTION_TABLE_LOCK;
2058
thd->in_lock_tables=1;
2059
thd->options|= OPTION_TABLE_LOCK;
2036
if (!(res= simple_open_n_lock_tables(session, all_tables)))
2061
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2038
session->locked_tables=session->lock;
2040
(void) set_handler_table_locks(session, all_tables, false);
2063
thd->locked_tables=thd->lock;
2065
(void) set_handler_table_locks(thd, all_tables, false);
2061
2086
prepared statement- safe.
2063
2088
HA_CREATE_INFO create_info(lex->create_info);
2064
if (end_active_trans(session))
2089
if (end_active_trans(thd))
2070
if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2095
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2071
2096
check_db_name(&lex->name))
2073
2098
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2076
res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2102
If in a slave thread :
2103
CREATE DATABASE DB was certainly not preceded by USE DB.
2104
For that reason, db_ok() in sql/slave.cc did not check the
2105
do_db/ignore_db. And as this query involves no tables, tables_ok()
2106
above was not called. So we have to check rules again here.
2108
if (thd->slave_thread &&
2109
(!rpl_filter->db_ok(lex->name.str) ||
2110
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2112
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2115
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2077
2116
lex->name.str), &create_info, 0);
2080
2119
case SQLCOM_DROP_DB:
2082
if (end_active_trans(session))
2121
if (end_active_trans(thd))
2089
2128
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2092
if (session->locked_tables || session->active_transaction())
2132
If in a slave thread :
2133
DROP DATABASE DB may not be preceded by USE DB.
2134
For that reason, maybe db_ok() in sql/slave.cc did not check the
2135
do_db/ignore_db. And as this query involves no tables, tables_ok()
2136
above was not called. So we have to check rules again here.
2138
if (thd->slave_thread &&
2139
(!rpl_filter->db_ok(lex->name.str) ||
2140
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2142
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2145
if (thd->locked_tables || thd->active_transaction())
2094
2147
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2095
2148
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2098
res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
2151
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2101
2154
case SQLCOM_ALTER_DB:
2107
2160
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2110
if (session->locked_tables || session->active_transaction())
2164
If in a slave thread :
2165
ALTER DATABASE DB may not be preceded by USE DB.
2166
For that reason, maybe db_ok() in sql/slave.cc did not check the
2167
do_db/ignore_db. And as this query involves no tables, tables_ok()
2168
above was not called. So we have to check rules again here.
2170
if (thd->slave_thread &&
2171
(!rpl_filter->db_ok(db->str) ||
2172
!rpl_filter->db_ok_with_wild_table(db->str)))
2174
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2177
if (thd->locked_tables || thd->active_transaction())
2112
2179
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2113
2180
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2116
res= mysql_alter_db(session, db->str, &create_info);
2183
res= mysql_alter_db(thd, db->str, &create_info);
2119
2186
case SQLCOM_SHOW_CREATE_DB:
2164
if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2231
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2166
2233
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2170
sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2237
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2173
2240
case SQLCOM_BEGIN:
2174
if (session->transaction.xid_state.xa_state != XA_NOTR)
2241
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2176
2243
my_error(ER_XAER_RMFAIL, MYF(0),
2177
xa_state_names[session->transaction.xid_state.xa_state]);
2244
xa_state_names[thd->transaction.xid_state.xa_state]);
2181
2248
Breakpoints for backup testing.
2183
if (begin_trans(session))
2250
if (begin_trans(thd))
2187
2254
case SQLCOM_COMMIT:
2188
if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2255
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2189
2256
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2193
2260
case SQLCOM_ROLLBACK:
2194
if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2261
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2195
2262
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2199
2266
case SQLCOM_RELEASE_SAVEPOINT:
2202
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2269
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2204
2271
if (my_strnncoll(system_charset_info,
2205
2272
(unsigned char *)lex->ident.str, lex->ident.length,
2233
if (ha_rollback_to_savepoint(session, sv))
2300
if (ha_rollback_to_savepoint(thd, sv))
2234
2301
res= true; // cannot happen
2237
if (((session->options & OPTION_KEEP_LOG) ||
2238
session->transaction.all.modified_non_trans_table) &&
2239
!session->slave_thread)
2240
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2304
if (((thd->options & OPTION_KEEP_LOG) ||
2305
thd->transaction.all.modified_non_trans_table) &&
2307
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2241
2308
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2242
2309
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2245
session->transaction.savepoints=sv;
2312
thd->transaction.savepoints=sv;
2248
2315
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2251
2318
case SQLCOM_SAVEPOINT:
2252
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2319
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2256
2323
SAVEPOINT **sv, *newsv;
2257
for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2324
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2259
2326
if (my_strnncoll(system_charset_info,
2260
2327
(unsigned char *)lex->ident.str, lex->ident.length,
2264
2331
if (*sv) /* old savepoint of the same name exists */
2267
ha_release_savepoint(session, *sv); // it cannot fail
2334
ha_release_savepoint(thd, *sv); // it cannot fail
2268
2335
*sv=(*sv)->prev;
2270
else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
2337
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2271
2338
savepoint_alloc_size)) == 0)
2273
2340
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2276
newsv->name=strmake_root(&session->transaction.mem_root,
2343
newsv->name=strmake_root(&thd->transaction.mem_root,
2277
2344
lex->ident.str, lex->ident.length);
2278
2345
newsv->length=lex->ident.length;
2281
2348
we'll lose a little bit of memory in transaction mem_root, but it'll
2282
2349
be free'd when transaction ends anyway
2284
if (ha_savepoint(session, newsv))
2351
if (ha_savepoint(thd, newsv))
2288
newsv->prev=session->transaction.savepoints;
2289
session->transaction.savepoints=newsv;
2355
newsv->prev=thd->transaction.savepoints;
2356
thd->transaction.savepoints=newsv;
2294
2361
case SQLCOM_BINLOG_BASE64_EVENT:
2296
mysql_client_binlog_statement(session);
2363
mysql_client_binlog_statement(thd);
2300
2367
assert(0); /* Impossible */
2304
session->set_proc_info("query end");
2371
thd->set_proc_info("query end");
2374
Binlog-related cleanup:
2375
Reset system variables temporarily modified by SET ONE SHOT.
2377
Exception: If this is a SET, do nothing. This is to allow
2378
mysqlbinlog to print many SET commands (in this case we want the
2379
charset temp setting to live until the real query). This is also
2380
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2383
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2384
reset_one_shot_variables(thd);
2307
2387
The return value for ROW_COUNT() is "implementation dependent" if the
2324
2404
Release the protection against the global read lock and wake
2325
2405
everyone, who might want to set a global read lock.
2327
start_waiting_global_read_lock(session);
2407
start_waiting_global_read_lock(thd);
2329
return(res || session->is_error());
2409
return(res || thd->is_error());
2332
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2412
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
2334
LEX *lex= session->lex;
2335
2415
select_result *result=lex->result;
2337
2417
/* assign global limit variable if limit is not given */
2354
2434
if (!(result= new select_send()))
2355
2435
return 1; /* purecov: inspected */
2356
session->send_explain_fields(result);
2357
res= mysql_explain_union(session, &session->lex->unit, result);
2436
thd->send_explain_fields(result);
2437
res= mysql_explain_union(thd, &thd->lex->unit, result);
2358
2438
if (lex->describe & DESCRIBE_EXTENDED)
2360
2440
char buff[1024];
2361
2441
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2363
session->lex->unit.print(&str, QT_ORDINARY);
2443
thd->lex->unit.print(&str, QT_ORDINARY);
2364
2444
str.append('\0');
2365
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2445
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2366
2446
ER_YES, str.ptr());
2399
2479
corresponding exec. (Thus we only have to check in fix_fields.)
2400
2480
- Passing to check_stack_overrun() prevents the compiler from removing it.
2402
bool check_stack_overrun(Session *session, long margin, unsigned char *)
2482
bool check_stack_overrun(THD *thd, long margin,
2483
unsigned char *buf __attribute__((unused)))
2404
2485
long stack_used;
2405
assert(session == current_session);
2406
if ((stack_used=used_stack(session->thread_stack,(char*) &stack_used)) >=
2486
assert(thd == current_thd);
2487
if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
2407
2488
(long) (my_thread_stack_size - margin))
2409
2490
sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2450
Reset Session part responsible for command processing state.
2531
Reset THD part responsible for command processing state.
2452
2533
This needs to be called before execution of every statement
2453
2534
(prepared or conventional).
2454
2535
It is not called by substatements of routines.
2457
Make it a method of Session and align its name with the rest of
2538
Make it a method of THD and align its name with the rest of
2458
2539
reset/end/start/init methods.
2460
Call it after we use Session for queries, not before.
2541
Call it after we use THD for queries, not before.
2463
void mysql_reset_session_for_next_command(Session *session)
2544
void mysql_reset_thd_for_next_command(THD *thd)
2465
session->free_list= 0;
2466
session->select_number= 1;
2547
thd->select_number= 1;
2468
2549
Those two lines below are theoretically unneeded as
2469
Session::cleanup_after_query() should take care of this already.
2550
THD::cleanup_after_query() should take care of this already.
2471
session->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2552
thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2553
thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2473
session->query_start_used= 0;
2474
session->is_fatal_error= 0;
2475
session->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS |
2555
thd->query_start_used= 0;
2556
thd->is_fatal_error= thd->time_zone_used= 0;
2557
thd->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS |
2476
2558
SERVER_QUERY_NO_INDEX_USED |
2477
2559
SERVER_QUERY_NO_GOOD_INDEX_USED);
2480
2562
OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2481
2563
in ha_rollback_trans() about some tables couldn't be rolled back.
2483
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2565
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2485
session->options&= ~OPTION_KEEP_LOG;
2486
session->transaction.all.modified_non_trans_table= false;
2567
thd->options&= ~OPTION_KEEP_LOG;
2568
thd->transaction.all.modified_non_trans_table= false;
2488
assert(session->security_ctx== &session->main_security_ctx);
2489
session->thread_specific_used= false;
2570
assert(thd->security_ctx== &thd->main_security_ctx);
2571
thd->thread_specific_used= false;
2491
2573
if (opt_bin_log)
2493
reset_dynamic(&session->user_var_events);
2494
session->user_var_events_alloc= session->mem_root;
2575
reset_dynamic(&thd->user_var_events);
2576
thd->user_var_events_alloc= thd->mem_root;
2496
session->clear_error();
2497
session->main_da.reset_diagnostics_area();
2498
session->total_warn_count=0; // Warnings for this query
2499
session->sent_row_count= session->examined_row_count= 0;
2579
thd->main_da.reset_diagnostics_area();
2580
thd->total_warn_count=0; // Warnings for this query
2582
thd->sent_row_count= thd->examined_row_count= 0;
2585
Because we come here only for start of top-statements, binlog format is
2586
constant inside a complex statement (using stored functions) etc.
2588
thd->reset_current_stmt_binlog_row_based();
2520
2609
mysql_new_select(LEX *lex, bool move_down)
2522
2611
SELECT_LEX *select_lex;
2523
Session *session= lex->session;
2525
if (!(select_lex= new (session->mem_root) SELECT_LEX()))
2614
if (!(select_lex= new (thd->mem_root) SELECT_LEX()))
2527
select_lex->select_number= ++session->select_number;
2616
select_lex->select_number= ++thd->select_number;
2528
2617
select_lex->parent_lex= lex; /* Used in init_query. */
2529
2618
select_lex->init_query();
2530
2619
select_lex->init_select();
2612
2701
We set the name of Item to @@session.var_name because that then is used
2613
2702
as the column name in the output.
2615
if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
2704
if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
2617
2706
end= strxmov(buff, "@@session.", var_name, NULL);
2618
2707
var->set_name(buff, end-buff, system_charset_info);
2619
add_item_to_list(session, var);
2708
add_item_to_list(thd, var);
2646
@param session Current thread
2735
@param thd Current thread
2647
2736
@param inBuf Begining of the query text
2648
2737
@param length Length of the query text
2649
2738
@param[out] found_semicolon For multi queries, position of the character of
2650
2739
the next query in the query text.
2653
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
2742
void mysql_parse(THD *thd, const char *inBuf, uint32_t length,
2654
2743
const char ** found_semicolon)
2661
2750
- first, call query_cache_send_result_to_client,
2662
2751
- second, if caching failed, initialise the lexical and syntactic parser.
2663
2752
The problem is that the query cache depends on a clean initialization
2664
of (among others) lex->safe_to_cache_query and session->server_status,
2753
of (among others) lex->safe_to_cache_query and thd->server_status,
2665
2754
which are reset respectively in
2667
- mysql_reset_session_for_next_command()
2756
- mysql_reset_thd_for_next_command()
2668
2757
So, initializing the lexical analyser *before* using the query cache
2669
2758
is required for the cache to work properly.
2670
2759
FIXME: cleanup the dependencies in the code to simplify this.
2673
mysql_reset_session_for_next_command(session);
2762
mysql_reset_thd_for_next_command(thd);
2676
LEX *lex= session->lex;
2678
Lex_input_stream lip(session, inBuf, length);
2680
bool err= parse_sql(session, &lip);
2767
Lex_input_stream lip(thd, inBuf, length);
2769
bool err= parse_sql(thd, &lip);
2681
2770
*found_semicolon= lip.found_semicolon;
2686
if (! session->is_error())
2775
if (! thd->is_error())
2689
Binlog logs a string starting from session->query and having length
2690
session->query_length; so we set session->query_length correctly (to not
2778
Binlog logs a string starting from thd->query and having length
2779
thd->query_length; so we set thd->query_length correctly (to not
2691
2780
log several statements in one event, when we executed only first).
2692
2781
We set it to not see the ';' (otherwise it would get into binlog
2693
2782
and Query_log_event::print() would give ';;' output).
2696
2785
Note that we don't need LOCK_thread_count to modify query_length.
2698
2787
if (*found_semicolon &&
2699
(session->query_length= (ulong)(*found_semicolon - session->query)))
2700
session->query_length--;
2788
(thd->query_length= (ulong)(*found_semicolon - thd->query)))
2789
thd->query_length--;
2701
2790
/* Actually execute the query */
2702
mysql_execute_command(session);
2791
mysql_execute_command(thd);
2708
assert(session->is_error());
2797
assert(thd->is_error());
2710
2799
lex->unit.cleanup();
2711
session->set_proc_info("freeing items");
2712
session->end_statement();
2713
session->cleanup_after_query();
2714
assert(session->change_list.is_empty());
2800
thd->set_proc_info("freeing items");
2801
thd->end_statement();
2802
thd->cleanup_after_query();
2803
assert(thd->change_list.is_empty());
2728
2817
1 can be ignored
2731
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
2820
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
2733
LEX *lex= session->lex;
2736
Lex_input_stream lip(session, inBuf, length);
2738
mysql_reset_session_for_next_command(session);
2825
Lex_input_stream lip(thd, inBuf, length);
2827
mysql_reset_thd_for_next_command(thd);
2740
if (!parse_sql(session, &lip) &&
2741
all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2829
if (!parse_sql(thd, &lip) &&
2830
all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
2742
2831
error= 1; /* Ignore question */
2743
session->end_statement();
2744
session->cleanup_after_query();
2832
thd->end_statement();
2833
thd->cleanup_after_query();
2833
2922
if (!(new_field= new Create_field()) ||
2834
new_field->init(session, field_name->str, type, length, decimals, type_modifier,
2923
new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
2835
2924
default_value, on_update_value, comment, change,
2836
2925
interval_list, cs, 0, column_format,
2848
2937
void store_position_for_column(const char *name)
2850
current_session->lex->last_field->after=const_cast<char*> (name);
2939
current_thd->lex->last_field->after=const_cast<char*> (name);
2854
add_proc_to_list(Session* session, Item *item)
2943
add_proc_to_list(THD* thd, Item *item)
2856
2945
order_st *order;
2857
2946
Item **item_ptr;
2859
if (!(order = (order_st *) session->alloc(sizeof(order_st)+sizeof(Item*))))
2948
if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
2861
2950
item_ptr = (Item**) (order+1);
2862
2951
*item_ptr= item;
2863
2952
order->item=item_ptr;
2864
2953
order->free_me=0;
2865
session->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
2954
thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
2974
3063
ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
2975
3064
ptr->derived= table->sel;
2976
3065
if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
2977
INFORMATION_SCHEMA_NAME.c_str()))
3066
INFORMATION_SCHEMA_NAME.str))
2979
ST_SCHEMA_TABLE *schema_table= find_schema_table(session, ptr->table_name);
3068
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
2980
3069
if (!schema_table ||
2981
3070
(schema_table->hidden &&
2982
((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
3071
((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 ||
2984
3073
this check is used for show columns|keys from I_S hidden table
3096
3185
If the current level contains only one member, the function
3097
3186
moves it one level up, eliminating the nest.
3099
@param session current thread
3188
@param thd current thread
3102
3191
- Pointer to TableList element added to the total table list, if success
3106
TableList *st_select_lex::end_nested_join(Session *)
3195
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
3108
3197
TableList *ptr;
3109
3198
nested_join_st *nested_join;
3144
3233
\# Pointer to TableList element created for the new nested join
3147
TableList *st_select_lex::nest_last_join(Session *session)
3236
TableList *st_select_lex::nest_last_join(THD *thd)
3149
3238
TableList *ptr;
3150
3239
nested_join_st *nested_join;
3151
3240
List<TableList> *embedded_list;
3153
if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
3242
if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3154
3243
sizeof(nested_join_st))))
3156
3245
nested_join= ptr->nested_join=
3306
bool st_select_lex_unit::add_fake_select_lex(Session *session_arg)
3395
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
3308
3397
SELECT_LEX *first_sl= first_select();
3309
3398
assert(!fake_select_lex);
3311
if (!(fake_select_lex= new (session_arg->mem_root) SELECT_LEX()))
3400
if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
3313
3402
fake_select_lex->include_standalone(this,
3314
3403
(SELECT_LEX_NODE**)&fake_select_lex);
3315
3404
fake_select_lex->select_number= INT_MAX;
3316
fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3405
fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
3317
3406
fake_select_lex->make_empty_select();
3318
3407
fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3319
3408
fake_select_lex->select_limit= 0;
3363
push_new_name_resolution_context(Session *session,
3452
push_new_name_resolution_context(THD *thd,
3364
3453
TableList *left_op, TableList *right_op)
3366
3455
Name_resolution_context *on_context;
3367
if (!(on_context= new (session->mem_root) Name_resolution_context))
3456
if (!(on_context= new (thd->mem_root) Name_resolution_context))
3369
3458
on_context->init();
3370
3459
on_context->first_name_resolution_table=
3371
3460
left_op->first_leaf_for_name_resolution();
3372
3461
on_context->last_name_resolution_table=
3373
3462
right_op->last_leaf_for_name_resolution();
3374
return session->lex->push_context(on_context);
3463
return thd->lex->push_context(on_context);
3514
3603
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3516
if ((options & REFRESH_READ_LOCK) && session)
3605
if ((options & REFRESH_READ_LOCK) && thd)
3519
3608
We must not try to aspire a global read lock if we have a write
3520
3609
locked table. This would lead to a deadlock when trying to
3521
3610
reopen (and re-lock) the table after the flush.
3523
if (session->locked_tables)
3612
if (thd->locked_tables)
3525
THR_LOCK_DATA **lock_p= session->locked_tables->locks;
3526
THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
3614
THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
3615
THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
3528
3617
for (; lock_p < end_p; lock_p++)
3541
3630
tmp_write_to_binlog= 0;
3542
if (lock_global_read_lock(session))
3631
if (lock_global_read_lock(thd))
3543
3632
return 1; // Killed
3544
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3633
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3545
3634
false : true, true);
3546
if (make_global_read_lock_block_commit(session)) // Killed
3635
if (make_global_read_lock_block_commit(thd)) // Killed
3548
3637
/* Don't leave things in a half-locked state */
3549
unlock_global_read_lock(session);
3638
unlock_global_read_lock(thd);
3554
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3643
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3555
3644
false : true, false);
3556
3645
my_dbopt_cleanup();
3558
if (session && (options & REFRESH_STATUS))
3559
refresh_status(session);
3647
if (thd && (options & REFRESH_STATUS))
3648
refresh_status(thd);
3649
if (options & REFRESH_THREADS)
3650
flush_thread_cache();
3560
3651
if (options & REFRESH_MASTER)
3563
3654
tmp_write_to_binlog= 0;
3564
if (reset_master(session))
3655
if (reset_master(thd))
3593
3684
static unsigned int
3594
kill_one_thread(Session *, ulong id, bool only_kill_query)
3685
kill_one_thread(THD *thd __attribute__((unused)),
3686
ulong id, bool only_kill_query)
3597
3689
uint32_t error=ER_NO_SUCH_THREAD;
3598
3690
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
3599
I_List_iterator<Session> it(threads);
3691
I_List_iterator<THD> it(threads);
3600
3692
while ((tmp=it++))
3602
3694
if (tmp->command == COM_DAEMON)
3626
session Thread class
3628
3720
only_kill_query Should it kill the query or the connection
3631
void sql_kill(Session *session, ulong id, bool only_kill_query)
3723
void sql_kill(THD *thd, ulong id, bool only_kill_query)
3633
3725
uint32_t error;
3634
if (!(error= kill_one_thread(session, id, only_kill_query)))
3726
if (!(error= kill_one_thread(thd, id, only_kill_query)))
3637
3729
my_error(error, MYF(0), id);
3657
3749
/* Fix is using unix filename format on dos */
3658
3750
my_stpcpy(buff,*filename_ptr);
3659
3751
end=convert_dirname(buff, *filename_ptr, NULL);
3660
if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3752
if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3661
3753
return 1; // End of memory
3662
3754
*filename_ptr=ptr;
3663
3755
strxmov(ptr,buff,table_name,NULL);
3677
3769
bool check_simple_select()
3679
Session *session= current_session;
3680
LEX *lex= session->lex;
3771
THD *thd= current_thd;
3681
3773
if (lex->current_select != &lex->select_lex)
3683
3775
char command[80];
3684
Lex_input_stream *lip= session->m_lip;
3776
Lex_input_stream *lip= thd->m_lip;
3685
3777
strmake(command, lip->yylval->symbol.str,
3686
3778
cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
3687
3779
my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3786
Comp_creator *comp_eq_creator(bool invert)
3788
return invert?(Comp_creator *)&ne_creator:(Comp_creator *)&eq_creator;
3792
Comp_creator *comp_ge_creator(bool invert)
3794
return invert?(Comp_creator *)<_creator:(Comp_creator *)&ge_creator;
3798
Comp_creator *comp_gt_creator(bool invert)
3800
return invert?(Comp_creator *)&le_creator:(Comp_creator *)>_creator;
3804
Comp_creator *comp_le_creator(bool invert)
3806
return invert?(Comp_creator *)>_creator:(Comp_creator *)&le_creator;
3810
Comp_creator *comp_lt_creator(bool invert)
3812
return invert?(Comp_creator *)&ge_creator:(Comp_creator *)<_creator;
3816
Comp_creator *comp_ne_creator(bool invert)
3818
return invert?(Comp_creator *)&eq_creator:(Comp_creator *)&ne_creator;
3695
3823
Construct ALL/ANY/SOME subquery Item.
3773
bool multi_delete_precheck(Session *session, TableList *)
3902
bool multi_delete_precheck(THD *thd,
3903
TableList *tables __attribute__((unused)))
3775
SELECT_LEX *select_lex= &session->lex->select_lex;
3776
TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
3778
session->lex->query_tables_own_last= 0;
3779
session->lex->query_tables_own_last= save_query_tables_own_last;
3781
if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3905
SELECT_LEX *select_lex= &thd->lex->select_lex;
3906
TableList **save_query_tables_own_last= thd->lex->query_tables_own_last;
3908
thd->lex->query_tables_own_last= 0;
3909
thd->lex->query_tables_own_last= save_query_tables_own_last;
3911
if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3783
3913
my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
3784
3914
ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
3952
bool create_table_precheck(Session *, TableList *,
4083
bool create_table_precheck(THD *thd __attribute__((unused)),
4084
TableList *tables __attribute__((unused)),
3953
4085
TableList *create_table)
3955
4087
bool error= true; // Error message is given
3957
4089
if (create_table && (strcmp(create_table->db, "information_schema") == 0))
3959
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
4091
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
3994
4126
return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
3997
if ((negated= expr->neg_transformer(session)) != 0)
4129
if ((negated= expr->neg_transformer(thd)) != 0)
3998
4130
return negated;
3999
4131
return new Item_func_not(expr);
4136
Check that byte length of a string does not exceed some limit.
4138
@param str string to be checked
4139
@param err_msg error message to be displayed if the string is too long
4140
@param max_length max length
4143
false the passed string is not longer than max_length
4145
true the passed string is longer than max_length
4148
The function is not used in existing code but can be useful later?
4151
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
4152
uint32_t max_byte_length)
4154
if (str->length <= max_byte_length)
4157
my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
4004
4164
Check that char length of a string does not exceed some limit.
4130
4289
@retval true on parsing error.
4133
bool parse_sql(Session *session, Lex_input_stream *lip)
4292
bool parse_sql(THD *thd, Lex_input_stream *lip)
4135
assert(session->m_lip == NULL);
4294
assert(thd->m_lip == NULL);
4137
4296
/* Set Lex_input_stream. */
4139
session->m_lip= lip;
4141
4300
/* Parse the query. */
4143
bool mysql_parse_status= DRIZZLEparse(session) != 0;
4145
/* Check that if DRIZZLEparse() failed, session->is_error() is set. */
4147
assert(!mysql_parse_status || session->is_error());
4302
bool mysql_parse_status= MYSQLparse(thd) != 0;
4304
/* Check that if MYSQLparse() failed, thd->is_error() is set. */
4306
assert(!mysql_parse_status || thd->is_error());
4149
4308
/* Reset Lex_input_stream. */
4151
session->m_lip= NULL;
4153
4312
/* That's it. */
4155
return mysql_parse_status || session->is_fatal_error;
4314
return mysql_parse_status || thd->is_fatal_error;