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 <libdrizzleclient/libdrizzle.h>
19
#include <mysys/hash.h>
20
#include <drizzled/logging.h>
21
#include <drizzled/db.h>
22
#include <drizzled/error.h>
23
#include <drizzled/nested_join.h>
24
#include <drizzled/query_id.h>
25
#include <drizzled/sql_parse.h>
26
#include <drizzled/data_home.h>
27
#include <drizzled/sql_base.h>
28
#include <drizzled/show.h>
29
#include <drizzled/rename.h>
30
#include <drizzled/function/time/unix_timestamp.h>
31
#include <drizzled/function/get_system_var.h>
32
#include <drizzled/item/cmpfunc.h>
33
#include <drizzled/item/null.h>
34
#include <drizzled/session.h>
35
#include <drizzled/sql_load.h>
36
#include <drizzled/connect.h>
37
#include <drizzled/lock.h>
38
#include <drizzled/select_send.h>
25
44
@defgroup Runtime_Environment Runtime Environment
48
extern size_t my_thread_stack_size;
49
extern const CHARSET_INFO *character_set_filesystem;
30
50
const char *any_db="*any*"; // Special symbol for check_access
32
52
const LEX_STRING command_name[COM_END+1]={
57
75
"NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
60
static void unlock_locked_tables(THD *thd)
78
static void unlock_locked_tables(Session *session)
62
if (thd->locked_tables)
80
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
82
session->lock=session->locked_tables;
83
session->locked_tables=0; // Will be automatically closed
84
close_thread_tables(session); // Free tables
71
bool end_active_trans(THD *thd)
89
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)
93
if (session->transaction.xid_state.xa_state != XA_NOTR)
81
95
my_error(ER_XAER_RMFAIL, MYF(0),
82
xa_state_names[thd->transaction.xid_state.xa_state]);
96
xa_state_names[session->transaction.xid_state.xa_state]);
85
if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
99
if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
86
100
OPTION_TABLE_LOCK))
88
102
/* 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;
103
if (!session->locked_tables)
104
session->options&= ~OPTION_TABLE_LOCK;
105
session->server_status&= ~SERVER_STATUS_IN_TRANS;
106
if (ha_commit(session))
95
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
96
thd->transaction.all.modified_non_trans_table= false;
109
session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
110
session->transaction.all.modified_non_trans_table= false;
101
bool begin_trans(THD *thd)
115
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))
118
if (session->locked_tables)
120
session->lock=session->locked_tables;
121
session->locked_tables=0; // Will be automatically closed
122
close_thread_tables(session); // Free tables
124
if (end_active_trans(session))
120
thd->options|= OPTION_BEGIN;
121
thd->server_status|= SERVER_STATUS_IN_TRANS;
128
LEX *lex= session->lex;
129
session->options|= OPTION_BEGIN;
130
session->server_status|= SERVER_STATUS_IN_TRANS;
122
131
if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
123
error= ha_start_consistent_snapshot(thd);
132
error= ha_start_consistent_snapshot(session);
193
190
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
194
191
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
195
192
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
196
sql_command_flags[SQLCOM_SHOW_CHARSETS]= CF_STATUS_COMMAND;
197
sql_command_flags[SQLCOM_SHOW_COLLATIONS]= CF_STATUS_COMMAND;
198
sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
199
sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
200
sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
201
193
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
202
194
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
203
195
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
204
196
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
205
197
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
206
198
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
207
sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
208
sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
210
200
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
211
201
CF_SHOW_TABLE_COMMAND);
224
214
bool is_update_query(enum enum_sql_command command)
226
216
assert(command >= 0 && command <= SQLCOM_END);
227
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
217
return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
230
void execute_init_command(THD *thd, sys_var_str *init_command_var,
231
rw_lock_t *var_mutex)
220
void execute_init_command(Session *session, sys_var_str *init_command_var,
221
pthread_rwlock_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
rw_rdlock(var_mutex);
243
save_client_capabilities= thd->client_capabilities;
244
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
232
pthread_rwlock_rdlock(var_mutex);
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
rw_unlock(var_mutex);
255
thd->client_capabilities= save_client_capabilities;
256
thd->net.vio= save_vio;
244
pthread_rwlock_unlock(var_mutex);
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
drizzleclient_net_set_read_timeout(net, session->variables.net_wait_timeout);
368
XXX: this code is here only to clear possible errors of init_connect.
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);
376
packet_length= my_net_read(net);
361
packet_length= drizzleclient_net_read(net);
377
362
if (packet_length == packet_error)
379
364
/* Check if we can continue without closing the connection */
381
366
/* The error must be set. */
382
assert(thd->is_error());
383
net_end_statement(thd);
367
/* This assert is killing me - and tracking down why the error isn't
368
* set here is a waste since the protocol lib is being replaced. */
369
//assert(session->is_error());
370
drizzleclient_net_end_statement(session);
385
372
if (net->error != 3)
417
404
command= COM_END; // Wrong command
419
406
/* Restore read timeout value */
420
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
407
drizzleclient_net_set_read_timeout(net, session->variables.net_read_timeout);
422
409
assert(packet_length);
423
return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
410
return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
426
413
return(return_value);
430
Determine if an attempt to update a non-temporary table while the
431
read-only option was enabled has been made.
433
This is a helper function to mysql_execute_command.
435
@note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
437
@see mysql_execute_command
440
@retval true The statement should be denied.
441
@retval false The statement isn't updating any relevant tables.
444
static bool deny_updates_if_read_only_option(THD *thd,
445
TableList *all_tables)
452
if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
455
/* Multi update is an exception and is dealt with later. */
456
if (lex->sql_command == SQLCOM_UPDATE_MULTI)
459
const bool create_temp_tables=
460
(lex->sql_command == SQLCOM_CREATE_TABLE) &&
461
(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
463
const bool drop_temp_tables=
464
(lex->sql_command == SQLCOM_DROP_TABLE) &&
467
const bool update_real_tables=
468
some_non_temp_table_to_be_updated(thd, all_tables) &&
469
!(create_temp_tables || drop_temp_tables);
472
const bool create_or_drop_databases=
473
(lex->sql_command == SQLCOM_CREATE_DB) ||
474
(lex->sql_command == SQLCOM_DROP_DB);
476
if (update_real_tables || create_or_drop_databases)
479
An attempt was made to modify one or more non-temporary tables.
485
/* Assuming that only temporary tables are modified. */
490
417
Perform one connection-level (COM_XXXX) command.
492
419
@param command type of command to perform
493
@param thd connection handle
420
@param session connection handle
494
421
@param packet data for the command, packet is always null-terminated
495
422
@param packet_length length of packet + 1 (to show that data is
496
423
null-terminated) except for COM_SLEEP, where it
500
set thd->lex->sql_command to SQLCOM_END here.
427
set session->lex->sql_command to SQLCOM_END here.
502
429
The following has to be changed to an 8 byte integer
507
434
1 request of thread shutdown, i. e. if command is
508
435
COM_QUIT/COM_SHUTDOWN
510
bool dispatch_command(enum enum_server_command command, THD *thd,
511
char* packet, uint32_t packet_length)
437
bool dispatch_command(enum enum_server_command command, Session *session,
438
char* packet, uint32_t packet_length)
440
NET *net= &session->net;
442
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 */
444
session->command=command;
445
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
524
447
pthread_mutex_lock(&LOCK_thread_count);
525
thd->query_id= global_query_id;
448
session->query_id= query_id.value();
527
450
switch( command ) {
528
451
/* Ignore these statements. */
531
454
/* Increase id and count all other statements. */
533
statistic_increment(thd->status_var.questions, &LOCK_status);
456
statistic_increment(session->status_var.questions, &LOCK_status);
537
460
thread_running++;
538
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
461
/* TODO: set session->lex->sql_command to SQLCOM_END here */
539
462
pthread_mutex_unlock(&LOCK_thread_count);
464
logging_pre_do(session);
466
session->server_status&=
544
467
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
545
468
switch (command) {
546
469
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))
472
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
473
session->convert_string(&tmp, system_charset_info,
474
packet, packet_length, session->charset());
475
if (!mysql_change_db(session, &tmp, false))
558
case COM_REGISTER_SLAVE:
560
if (!register_slave(thd, (unsigned char*)packet, packet_length))
564
481
case COM_CHANGE_USER:
566
status_var_increment(thd->status_var.com_other);
483
status_var_increment(session->status_var.com_other);
567
484
char *user= (char*) packet, *packet_end= packet + packet_length;
568
485
/* Safe because there is always a trailing \0 at the end of the packet */
569
486
char *passwd= strchr(user, '\0')+1;
572
thd->clear_error(); // if errors from rollback
489
session->clear_error(); // if errors from rollback
575
492
Old clients send null-terminated string ('\0' for empty string) for
627
544
/* Convert database name to utf8 */
628
545
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
629
546
system_charset_info, db, db_length,
630
thd->charset(), &dummy_errors)]= 0;
547
session->charset(), &dummy_errors)]= 0;
633
550
/* Save user and privileges */
634
save_db_length= thd->db_length;
636
save_user_connect= thd->user_connect;
551
save_db_length= session->db_length;
552
save_db= session->db;
553
save_user_connect= session->user_connect;
638
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
640
thd->security_ctx->user= save_security_ctx.user;
641
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
555
old_username= session->security_ctx.user;
556
session->security_ctx.user.assign(user);
645
558
/* Clear variables that are allocated */
646
thd->user_connect= 0;
647
res= check_user(thd, passwd, passwd_len, db, false);
559
session->user_connect= 0;
560
res= check_user(session, passwd, passwd_len, db);
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;
564
session->security_ctx.user= old_username;
565
session->user_connect= save_user_connect;
566
session->db= save_db;
567
session->db_length= save_db_length;
662
if (save_security_ctx.user)
663
free(save_security_ctx.user);
667
thd_init_client_charset(thd, cs_number);
668
thd->update_charset();
576
session->update_charset();
675
if (alloc_query(thd, packet, packet_length))
583
if (alloc_query(session, packet, packet_length))
676
584
break; // fatal error is set
677
char *packet_end= thd->query + thd->query_length;
585
char *packet_end= session->query + session->query_length;
678
586
const char* end_of_stmt= NULL;
680
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
588
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
682
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
590
while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
684
592
char *beginning_of_next_stmt= (char*) end_of_stmt;
686
net_end_statement(thd);
594
drizzleclient_net_end_statement(session);
688
596
Multiple queries exits, execute them individually
690
close_thread_tables(thd);
598
close_thread_tables(session);
691
599
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
693
log_slow_statement(thd);
601
log_slow_statement(session);
695
603
/* Remove garbage at start of query */
696
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
604
while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
698
606
beginning_of_next_stmt++;
611
Count each statement from the client.
613
statistic_increment(session->status_var.questions, &LOCK_status);
614
session->query_id= query_id.next();
615
session->set_time(); /* Reset the query start time. */
702
616
pthread_mutex_lock(&LOCK_thread_count);
703
thd->query_length= length;
704
thd->query= beginning_of_next_stmt;
706
Count each statement from the client.
708
statistic_increment(thd->status_var.questions, &LOCK_status);
709
thd->query_id= next_query_id();
710
thd->set_time(); /* Reset the query start time. */
711
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
617
session->query_length= length;
618
session->query= beginning_of_next_stmt;
712
619
pthread_mutex_unlock(&LOCK_thread_count);
620
/* TODO: set session->lex->sql_command to SQLCOM_END here */
714
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
622
mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
723
631
LEX_STRING conv_name;
725
633
/* used as fields initializator */
728
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
636
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
637
memset(&table_list, 0, sizeof(table_list));
730
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
638
if (session->copy_db_to(&table_list.db, &table_list.db_length))
733
641
We have name + wildcard in packet, separated by endzero
735
643
arg_end= strchr(packet, '\0');
736
thd->convert_string(&conv_name, system_charset_info,
737
packet, (uint32_t) (arg_end - packet), thd->charset());
644
session->convert_string(&conv_name, system_charset_info,
645
packet, (uint32_t) (arg_end - packet), session->charset());
738
646
table_list.alias= table_list.table_name= conv_name.str;
739
647
packet= arg_end + 1;
741
649
if (!my_strcasecmp(system_charset_info, table_list.db,
742
INFORMATION_SCHEMA_NAME.str))
650
INFORMATION_SCHEMA_NAME.c_str()))
744
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
652
ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
745
653
if (schema_table)
746
654
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)))
657
session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
658
if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
752
660
if (lower_case_table_names)
753
661
my_casedn_str(files_charset_info, table_list.table_name);
755
663
/* init structures for VIEW processing */
756
table_list.select_lex= &(thd->lex->select_lex);
759
mysql_reset_thd_for_next_command(thd);
664
table_list.select_lex= &(session->lex->select_lex);
667
session->reset_for_next_command();
762
670
select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
671
(unsigned char**) &table_list.next_local);
764
thd->lex->add_to_query_tables(&table_list);
672
session->lex->add_to_query_tables(&table_list);
766
674
/* 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();
675
session->lex->sql_command= SQLCOM_SHOW_FIELDS;
676
mysqld_list_fields(session,&table_list,fields);
677
session->lex->unit.cleanup();
678
session->cleanup_after_query();
774
682
/* We don't calculate statistics for this command */
775
683
net->error=0; // Don't give 'abort' message
776
thd->main_da.disable_status(); // Don't send anything back
684
session->main_da.disable_status(); // Don't send anything back
777
685
error=true; // End server
779
case COM_BINLOG_DUMP:
783
uint32_t slave_server_id;
785
status_var_increment(thd->status_var.com_other);
786
thd->enable_slow_log= opt_log_slow_admin_statements;
787
/* TODO: The following has to be changed to an 8 byte integer */
788
pos = uint4korr(packet);
789
flags = uint2korr(packet + 4);
790
thd->server_id=0; /* avoid suicide */
791
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
792
kill_zombie_dump_threads(slave_server_id);
793
thd->server_id = slave_server_id;
795
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
796
unregister_slave(thd,1,1);
797
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
801
687
case COM_SHUTDOWN:
803
status_var_increment(thd->status_var.com_other);
805
close_thread_tables(thd); // Free before kill
689
status_var_increment(session->status_var.com_other);
691
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
697
status_var_increment(session->status_var.com_other);
698
session->my_ok(); // Tell client we are alive
814
700
case COM_PROCESS_INFO:
815
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
mysqld_list_processes(thd, NULL, 0);
701
status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
702
mysqld_list_processes(session, NULL, 0);
818
704
case COM_PROCESS_KILL:
820
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
706
status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
821
707
ulong id=(ulong) uint4korr(packet);
822
sql_kill(thd,id,false);
708
sql_kill(session,id,false);
825
711
case COM_SET_OPTION:
827
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
713
status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
828
714
uint32_t opt_command= uint2korr(packet);
830
716
switch (opt_command) {
831
717
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
832
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
718
session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
835
721
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
836
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
722
session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
840
726
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
854
740
/* 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;
741
session->main_da.can_overwrite_status= true;
742
ha_autocommit_or_rollback(session, session->is_error());
743
session->main_da.can_overwrite_status= false;
859
thd->transaction.stmt.reset();
745
session->transaction.stmt.reset();
862
748
/* 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");
749
if (session->killed_errno())
751
if (! session->main_da.is_set())
752
session->send_kill_message();
754
if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
756
session->killed= Session::NOT_KILLED;
757
session->mysys_var->abort= 0;
760
drizzleclient_net_end_statement(session);
762
session->set_proc_info("closing tables");
877
763
/* Free tables */
878
close_thread_tables(thd);
880
log_slow_statement(thd);
882
thd_proc_info(thd, "cleaning up");
764
close_thread_tables(session);
766
log_slow_statement(session);
768
session->set_proc_info("cleaning up");
883
769
pthread_mutex_lock(&LOCK_thread_count); // For process list
884
thd_proc_info(thd, 0);
885
thd->command=COM_SLEEP;
770
session->set_proc_info(0);
771
session->command=COM_SLEEP;
773
session->query_length=0;
888
774
thread_running--;
889
775
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));
776
session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
777
free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
896
void log_slow_statement(THD *thd)
782
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);
784
logging_post_do(session);
1009
Read query from packet and store in thd->query.
887
Read query from packet and store in session->query.
1010
888
Used in COM_QUERY and COM_STMT_PREPARE.
1012
Sets the following THD variables:
890
Sets the following Session variables:
1019
true error; In this case thd->fatal_error is set
897
true error; In this case session->fatal_error is set
1022
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
900
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
1024
902
/* Remove garbage at start and end of query */
1025
while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
903
while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
1028
906
packet_length--;
1030
908
const char *pos= packet + packet_length; // Point at end null
1031
909
while (packet_length > 0 &&
1032
(pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
910
(pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
1035
913
packet_length--;
1037
915
/* 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),
916
session->query_length= 0; // Extra safety: Avoid races
917
if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
1041
thd->db_length+ 1)))
919
session->db_length+ 1)))
1043
thd->query[packet_length]=0;
1044
thd->query_length= packet_length;
921
session->query[packet_length]=0;
922
session->query_length= packet_length;
1046
924
/* Reclaim some memory */
1047
thd->packet.shrink(thd->variables.net_buffer_length);
1048
thd->convert_buffer.shrink(thd->variables.net_buffer_length);
925
session->packet.shrink(session->variables.net_buffer_length);
926
session->convert_buffer.shrink(session->variables.net_buffer_length);
1053
static void reset_one_shot_variables(THD *thd)
1055
thd->variables.character_set_client=
1056
global_system_variables.character_set_client;
1057
thd->variables.collation_connection=
1058
global_system_variables.collation_connection;
1059
thd->variables.collation_database=
1060
global_system_variables.collation_database;
1061
thd->variables.collation_server=
1062
global_system_variables.collation_server;
1063
thd->update_charset();
1064
thd->variables.time_zone=
1065
global_system_variables.time_zone;
1066
thd->variables.lc_time_names= &my_locale_en_US;
1067
thd->one_shot_set= 0;
1072
Execute command saved in thd and lex->sql_command.
932
Execute command saved in session and lex->sql_command.
1074
934
Before every operation that can request a write lock for a table
1075
935
wait if a global read lock exists. However do not wait if this
1102
mysql_execute_command(THD *thd)
962
mysql_execute_command(Session *session)
1105
965
bool need_start_waiting= false; // have protection against global read lock
1108
/* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1109
SELECT_LEX *select_lex= &lex->select_lex;
1110
/* first table of first SELECT_LEX */
966
LEX *lex= session->lex;
967
/* first Select_Lex (have special meaning for many of non-SELECTcommands) */
968
Select_Lex *select_lex= &lex->select_lex;
969
/* first table of first Select_Lex */
1111
970
TableList *first_table= (TableList*) select_lex->table_list.first;
1112
971
/* list of all tables in query */
1113
972
TableList *all_tables;
1114
/* most outer SELECT_LEX_UNIT of query */
1115
SELECT_LEX_UNIT *unit= &lex->unit;
973
/* most outer Select_Lex_Unit of query */
974
Select_Lex_Unit *unit= &lex->unit;
1116
975
/* Saved variable value */
1119
In many cases first table of main SELECT_LEX have special meaning =>
1120
check that it is first table in global list and relink it first in
978
In many cases first table of main Select_Lex have special meaning =>
979
check that it is first table in global list and relink it first in
1121
980
queries_tables list if it is necessary (we need such relinking only
1122
981
for queries with subqueries in select list, in this case tables of
1123
982
subqueries will go to global list first)
1125
all_tables will differ from first_table only if most upper SELECT_LEX
984
all_tables will differ from first_table only if most upper Select_Lex
1126
985
do not contain tables.
1128
987
Because of above in place where should be at least one table in most
1129
outer SELECT_LEX we have following check:
988
outer Select_Lex we have following check:
1130
989
assert(first_table == all_tables);
1131
990
assert(first_table == all_tables && first_table != 0);
1146
1005
Don't reset warnings when executing a stored routine.
1148
1007
if (all_tables || !lex->is_single_level_stmt())
1149
drizzle_reset_errors(thd, 0);
1151
if (unlikely(thd->slave_thread))
1154
Check if statment should be skipped because of slave filtering
1158
- UPDATE MULTI: For this statement, we want to check the filtering
1159
rules later in the code
1160
- SET: we always execute it (Not that many SET commands exists in
1161
the binary log anyway -- only 4.1 masters write SET statements,
1162
in 5.0 there are no SET statements in the binary log)
1163
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1164
have stale files on slave caused by exclusion of one tmp table).
1166
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1167
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1168
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1169
lex->drop_temporary && lex->drop_if_exists) &&
1170
all_tables_not_ok(thd, all_tables))
1172
/* we warn the slave SQL thread */
1173
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1174
if (thd->one_shot_set)
1177
It's ok to check thd->one_shot_set here:
1179
The charsets in a MySQL 5.0 slave can change by both a binlogged
1180
SET ONE_SHOT statement and the event-internal charset setting,
1181
and these two ways to change charsets do not seems to work
1184
At least there seems to be problems in the rli cache for
1185
charsets if we are using ONE_SHOT. Note that this is normally no
1186
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1187
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1189
reset_one_shot_variables(thd);
1197
When option readonly is set deny operations which change non-temporary
1198
tables. Except for the replication thread and the 'super' users.
1200
if (deny_updates_if_read_only_option(thd, all_tables))
1202
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1205
} /* endif unlikely slave */
1206
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1208
assert(thd->transaction.stmt.modified_non_trans_table == false);
1008
drizzle_reset_errors(session, 0);
1010
status_var_increment(session->status_var.com_stat[lex->sql_command]);
1012
assert(session->transaction.stmt.modified_non_trans_table == false);
1210
1014
switch (lex->sql_command) {
1211
1015
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);
1017
system_status_var old_status_var= session->status_var;
1018
session->initial_status_var= &old_status_var;
1019
res= execute_sqlcom_select(session, all_tables);
1216
1020
/* Don't log SHOW STATUS commands to slow query log */
1217
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1021
session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1218
1022
SERVER_QUERY_NO_GOOD_INDEX_USED);
1220
1024
restore status variables, as we don't want 'show status' to cause
1223
1027
pthread_mutex_lock(&LOCK_status);
1224
add_diff_to_status(&global_status_var, &thd->status_var,
1028
add_diff_to_status(&global_status_var, &session->status_var,
1225
1029
&old_status_var);
1226
thd->status_var= old_status_var;
1030
session->status_var= old_status_var;
1227
1031
pthread_mutex_unlock(&LOCK_status);
1234
1038
case SQLCOM_SHOW_FIELDS:
1235
1039
case SQLCOM_SHOW_KEYS:
1236
1040
case SQLCOM_SHOW_VARIABLES:
1237
case SQLCOM_SHOW_CHARSETS:
1238
case SQLCOM_SHOW_COLLATIONS:
1239
1041
case SQLCOM_SELECT:
1241
thd->status_var.last_query_cost= 0.0;
1242
res= execute_sqlcom_select(thd, all_tables);
1043
session->status_var.last_query_cost= 0.0;
1044
res= execute_sqlcom_select(session, all_tables);
1245
1047
case SQLCOM_EMPTY_QUERY:
1251
res = purge_master_logs(thd, lex->to_log);
1254
case SQLCOM_PURGE_BEFORE:
1258
/* PURGE MASTER LOGS BEFORE 'data' */
1259
it= (Item *)lex->value_list.head();
1260
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1263
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1266
it= new Item_func_unix_timestamp(it);
1268
it is OK only emulate fix_fieds, because we need only
1271
it->quick_fix_field();
1272
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1275
1051
case SQLCOM_SHOW_WARNS:
1277
res= mysqld_show_warnings(thd, (uint32_t)
1053
res= mysqld_show_warnings(session, (uint32_t)
1278
1054
((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1279
1055
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1280
1056
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1284
1060
case SQLCOM_SHOW_ERRORS:
1286
res= mysqld_show_warnings(thd, (uint32_t)
1062
res= mysqld_show_warnings(session, (uint32_t)
1287
1063
(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
1066
case SQLCOM_ASSIGN_TO_KEYCACHE:
1303
1068
assert(first_table == all_tables && first_table != 0);
1304
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1307
case SQLCOM_CHANGE_MASTER:
1309
pthread_mutex_lock(&LOCK_active_mi);
1310
res = change_master(thd,active_mi);
1311
pthread_mutex_unlock(&LOCK_active_mi);
1314
case SQLCOM_SHOW_SLAVE_STAT:
1316
pthread_mutex_lock(&LOCK_active_mi);
1317
if (active_mi != NULL)
1319
res = show_master_info(thd, active_mi);
1323
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1324
"the master info structure does not exist");
1327
pthread_mutex_unlock(&LOCK_active_mi);
1330
case SQLCOM_SHOW_MASTER_STAT:
1332
res = show_binlog_info(thd);
1069
res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1336
1072
case SQLCOM_SHOW_ENGINE_STATUS:
1338
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1074
res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1341
1077
case SQLCOM_CREATE_TABLE:
1516
1252
/* Prepare stack copies to be re-execution safe */
1517
1253
HA_CREATE_INFO create_info;
1518
Alter_info alter_info(lex->alter_info, thd->mem_root);
1254
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 */
1256
if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1523
1259
assert(first_table == all_tables && first_table != 0);
1524
if (end_active_trans(thd))
1260
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
1263
memset(&create_info, 0, sizeof(create_info));
1534
1264
create_info.db_type= 0;
1535
1265
create_info.row_type= ROW_TYPE_NOT_USED;
1536
create_info.default_table_charset= thd->variables.collation_database;
1266
create_info.default_table_charset= session->variables.collation_database;
1538
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1268
res= mysql_alter_table(session, first_table->db, first_table->table_name,
1539
1269
&create_info, first_table, &alter_info,
1540
1270
0, (order_st*) 0, 0);
1543
case SQLCOM_SLAVE_START:
1545
pthread_mutex_lock(&LOCK_active_mi);
1546
start_slave(thd,active_mi,1 /* net report*/);
1547
pthread_mutex_unlock(&LOCK_active_mi);
1550
case SQLCOM_SLAVE_STOP:
1552
If the client thread has locked tables, a deadlock is possible.
1554
- the client thread does LOCK TABLE t READ.
1555
- then the master updates t.
1556
- then the SQL slave thread wants to update t,
1557
so it waits for the client thread because t is locked by it.
1558
- then the client thread does SLAVE STOP.
1559
SLAVE STOP waits for the SQL slave thread to terminate its
1560
update t, which waits for the client thread because t is locked by it.
1561
To prevent that, refuse SLAVE STOP if the
1562
client thread has locked tables
1564
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1566
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1567
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1571
pthread_mutex_lock(&LOCK_active_mi);
1572
stop_slave(thd,active_mi,1/* net report*/);
1573
pthread_mutex_unlock(&LOCK_active_mi);
1577
1273
case SQLCOM_ALTER_TABLE:
1578
1274
assert(first_table == all_tables && first_table != 0);
1604
1300
/* Don't yet allow changing of symlinks with ALTER TABLE */
1605
1301
if (create_info.data_file_name)
1606
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1302
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1607
1303
"DATA DIRECTORY option ignored");
1608
1304
if (create_info.index_file_name)
1609
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1305
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1610
1306
"INDEX DIRECTORY option ignored");
1611
1307
create_info.data_file_name= create_info.index_file_name= NULL;
1612
1308
/* ALTER TABLE ends previous transaction */
1613
if (end_active_trans(thd))
1309
if (end_active_trans(session))
1616
if (!thd->locked_tables &&
1617
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1312
if (!session->locked_tables &&
1313
!(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,
1319
res= mysql_alter_table(session, select_lex->db, lex->name.str,
1645
1340
new_list= table->next_local[0];
1648
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1343
if (end_active_trans(session) || drizzle_rename_tables(session, first_table, 0))
1654
case SQLCOM_SHOW_BINLOGS:
1656
res = show_binlogs(thd);
1659
1349
case SQLCOM_SHOW_CREATE:
1660
1350
assert(first_table == all_tables && first_table != 0);
1662
res= mysqld_show_create(thd, first_table);
1352
res= drizzled_show_create(session, first_table);
1665
1355
case SQLCOM_CHECKSUM:
1667
1357
assert(first_table == all_tables && first_table != 0);
1668
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1358
res = mysql_checksum_table(session, first_table, &lex->check_opt);
1671
1361
case SQLCOM_REPAIR:
1673
1363
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);
1364
res= mysql_repair_table(session, first_table, &lex->check_opt);
1676
1365
/* ! we write after unlocking the table */
1678
1367
Presumably, REPAIR and binlog writing doesn't require synchronization
1680
write_bin_log(thd, true, thd->query, thd->query_length);
1369
write_bin_log(session, true, session->query, session->query_length);
1681
1370
select_lex->table_list.first= (unsigned char*) first_table;
1682
1371
lex->query_tables=all_tables;
1706
1393
case SQLCOM_OPTIMIZE:
1708
1395
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);
1396
res= mysql_optimize_table(session, first_table, &lex->check_opt);
1711
1397
/* ! we write after unlocking the table */
1712
write_bin_log(thd, true, thd->query, thd->query_length);
1398
write_bin_log(session, true, session->query, session->query_length);
1713
1399
select_lex->table_list.first= (unsigned char*) first_table;
1714
1400
lex->query_tables=all_tables;
1717
1403
case SQLCOM_UPDATE:
1718
1404
assert(first_table == all_tables && first_table != 0);
1719
if (update_precheck(thd, all_tables))
1405
if ((res= update_precheck(session, all_tables)))
1721
1407
assert(select_lex->offset_limit == 0);
1722
1408
unit->set_limit(select_lex);
1723
res= (up_result= mysql_update(thd, all_tables,
1724
select_lex->item_list,
1727
select_lex->order_list.elements,
1728
(order_st *) select_lex->order_list.first,
1729
unit->select_limit_cnt,
1730
lex->duplicates, lex->ignore));
1731
/* mysql_update return 2 if we need to switch to multi-update */
1409
res= mysql_update(session, all_tables,
1410
select_lex->item_list,
1413
select_lex->order_list.elements,
1414
(order_st *) select_lex->order_list.first,
1415
unit->select_limit_cnt,
1416
lex->duplicates, lex->ignore);
1735
1418
case SQLCOM_UPDATE_MULTI:
1737
1420
assert(first_table == all_tables && first_table != 0);
1738
/* if we switched from normal update, rights are checked */
1741
if ((res= multi_update_precheck(thd, all_tables)))
1747
res= mysql_multi_update_prepare(thd);
1749
/* Check slave filtering rules */
1750
if (unlikely(thd->slave_thread))
1752
if (all_tables_not_ok(thd, all_tables))
1756
res= 0; /* don't care of prev failure */
1757
thd->clear_error(); /* filters are of highest prior */
1759
/* we warn the slave SQL thread */
1760
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1771
some_non_temp_table_to_be_updated(thd, all_tables))
1773
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1778
res= mysql_multi_update(thd, all_tables,
1421
if ((res= update_precheck(session, all_tables)))
1424
if ((res= mysql_multi_update_prepare(session)))
1427
res= mysql_multi_update(session, all_tables,
1779
1428
&select_lex->item_list,
1780
1429
&lex->value_list,
1781
1430
select_lex->where,
1787
1436
case SQLCOM_INSERT:
1789
1438
assert(first_table == all_tables && first_table != 0);
1790
if ((res= insert_precheck(thd, all_tables)))
1439
if ((res= insert_precheck(session, all_tables)))
1793
if (!thd->locked_tables &&
1794
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1442
if (!session->locked_tables &&
1443
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1800
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1449
res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1801
1450
lex->update_list, lex->value_list,
1802
1451
lex->duplicates, lex->ignore);
1821
1470
unit->set_limit(select_lex);
1823
if (! thd->locked_tables &&
1824
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1472
if (! session->locked_tables &&
1473
! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1830
if (!(res= open_and_lock_tables(thd, all_tables)))
1479
if (!(res= open_and_lock_tables(session, all_tables)))
1832
1481
/* Skip first table, which is the table we are inserting in */
1833
1482
TableList *second_table= first_table->next_local;
1834
1483
select_lex->table_list.first= (unsigned char*) second_table;
1835
select_lex->context.table_list=
1484
select_lex->context.table_list=
1836
1485
select_lex->context.first_name_resolution_table= second_table;
1837
res= mysql_insert_select_prepare(thd);
1486
res= mysql_insert_select_prepare(session);
1838
1487
if (!res && (sel_result= new select_insert(first_table,
1839
1488
first_table->table,
1840
1489
&lex->field_list,
1911
1560
assert(first_table == all_tables && first_table != 0);
1912
1561
TableList *aux_tables=
1913
(TableList *)thd->lex->auxiliary_table_list.first;
1562
(TableList *)session->lex->auxiliary_table_list.first;
1914
1563
multi_delete *del_result;
1916
if (!thd->locked_tables &&
1917
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1565
if (!session->locked_tables &&
1566
!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1923
if ((res= multi_delete_precheck(thd, all_tables)))
1572
if ((res= multi_delete_precheck(session, all_tables)))
1926
1575
/* condition will be true on SP re-excuting */
1927
1576
if (select_lex->item_list.elements != 0)
1928
1577
select_lex->item_list.empty();
1929
if (add_item_to_list(thd, new Item_null()))
1578
if (session->add_item_to_list(new Item_null()))
1932
thd_proc_info(thd, "init");
1933
if ((res= open_and_lock_tables(thd, all_tables)))
1581
session->set_proc_info("init");
1582
if ((res= open_and_lock_tables(session, all_tables)))
1936
if ((res= mysql_multi_delete_prepare(thd)))
1585
if ((res= mysql_multi_delete_prepare(session)))
1939
if (!thd->is_fatal_error &&
1588
if (!session->is_fatal_error &&
1940
1589
(del_result= new multi_delete(aux_tables, lex->table_count)))
1942
res= mysql_select(thd, &select_lex->ref_pointer_array,
1591
res= mysql_select(session, &select_lex->ref_pointer_array,
1943
1592
select_lex->get_table_list(),
1944
1593
select_lex->with_wild,
1945
1594
select_lex->item_list,
1946
1595
select_lex->where,
1947
1596
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1948
1597
(order_st *)NULL,
1949
select_lex->options | thd->options |
1598
select_lex->options | session->options |
1950
1599
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
1600
OPTION_SETUP_TABLES_DONE,
1952
1601
del_result, unit, select_lex);
1953
res|= thd->is_error();
1602
res|= session->is_error();
1955
1604
del_result->abort();
1956
1605
delete del_result;
1964
1613
assert(first_table == all_tables && first_table != 0);
1965
1614
if (!lex->drop_temporary)
1967
if (end_active_trans(thd))
1616
if (end_active_trans(session))
1973
If this is a slave thread, we may sometimes execute some
1974
DROP / * 40005 TEMPORARY * / TABLE
1975
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
1976
MASTER TO), while the temporary table has already been dropped.
1977
To not generate such irrelevant "table does not exist errors",
1978
we silently add IF EXISTS if TEMPORARY was used.
1980
if (thd->slave_thread)
1981
lex->drop_if_exists= 1;
1983
1621
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1984
thd->options|= OPTION_KEEP_LOG;
1622
session->options|= OPTION_KEEP_LOG;
1986
1624
/* DDL and binlog write order protected by LOCK_open */
1987
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
1625
res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1990
1628
case SQLCOM_SHOW_PROCESSLIST:
1991
mysqld_list_processes(thd, NULL, lex->verbose);
1629
mysqld_list_processes(session, NULL, lex->verbose);
1993
1631
case SQLCOM_SHOW_ENGINE_LOGS:
1995
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
1633
res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1998
1636
case SQLCOM_CHANGE_DB:
2000
1638
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2002
if (!mysql_change_db(thd, &db_str, false))
1640
if (!mysql_change_db(session, &db_str, false))
2069
1697
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2070
1698
false, mysqldump will not work.
2072
unlock_locked_tables(thd);
2073
if (thd->options & OPTION_TABLE_LOCK)
1700
unlock_locked_tables(session);
1701
if (session->options & OPTION_TABLE_LOCK)
2075
end_active_trans(thd);
2076
thd->options&= ~(OPTION_TABLE_LOCK);
1703
end_active_trans(session);
1704
session->options&= ~(OPTION_TABLE_LOCK);
2078
if (thd->global_read_lock)
2079
unlock_global_read_lock(thd);
1706
if (session->global_read_lock)
1707
unlock_global_read_lock(session);
2082
1710
case SQLCOM_LOCK_TABLES:
2084
1712
We try to take transactional locks if
2085
1713
- only transactional locks are requested (lex->lock_transactional) and
2086
- no non-transactional locks exist (!thd->locked_tables).
1714
- no non-transactional locks exist (!session->locked_tables).
2088
if (lex->lock_transactional && !thd->locked_tables)
1716
if (lex->lock_transactional && !session->locked_tables)
2092
1720
All requested locks are transactional and no non-transactional
2095
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
1723
if ((rc= try_transactional_lock(session, all_tables)) == -1)
2113
1741
requested. If yes, warn about the conversion to non-transactional
2114
1742
locks or abort in strict mode.
2116
if (check_transactional_lock(thd, all_tables))
1744
if (check_transactional_lock(session, all_tables))
2118
unlock_locked_tables(thd);
1746
unlock_locked_tables(session);
2119
1747
/* we must end the trasaction first, regardless of anything */
2120
if (end_active_trans(thd))
1748
if (end_active_trans(session))
2122
thd->in_lock_tables=1;
2123
thd->options|= OPTION_TABLE_LOCK;
1750
session->in_lock_tables=1;
1751
session->options|= OPTION_TABLE_LOCK;
2125
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
1753
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);
1755
session->locked_tables=session->lock;
1757
(void) set_handler_table_locks(session, all_tables, false);
2135
1763
Need to end the current transaction, so the storage engine (InnoDB)
2136
1764
can free its locks if LOCK TABLES locked some tables before finding
2137
1765
that it can't lock a table in its list
2139
ha_autocommit_or_rollback(thd, 1);
2140
end_active_trans(thd);
2141
thd->options&= ~(OPTION_TABLE_LOCK);
1767
ha_autocommit_or_rollback(session, 1);
1768
end_active_trans(session);
1769
session->options&= ~(OPTION_TABLE_LOCK);
2143
thd->in_lock_tables=0;
1771
session->in_lock_tables=0;
2145
1773
case SQLCOM_CREATE_DB:
2150
1778
prepared statement- safe.
2152
1780
HA_CREATE_INFO create_info(lex->create_info);
2153
if (end_active_trans(thd))
1781
if (end_active_trans(session))
2159
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
1787
if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2160
1788
check_db_name(&lex->name))
2162
1790
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2166
If in a slave thread :
2167
CREATE DATABASE DB was certainly not preceded by USE DB.
2168
For that reason, db_ok() in sql/slave.cc did not check the
2169
do_db/ignore_db. And as this query involves no tables, tables_ok()
2170
above was not called. So we have to check rules again here.
2172
if (thd->slave_thread &&
2173
(!rpl_filter->db_ok(lex->name.str) ||
2174
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2176
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2179
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
1793
res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2180
1794
lex->name.str), &create_info, 0);
2183
1797
case SQLCOM_DROP_DB:
2185
if (end_active_trans(thd))
1799
if (end_active_trans(session))
2192
1806
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2196
If in a slave thread :
2197
DROP DATABASE DB may not be preceded by USE DB.
2198
For that reason, maybe db_ok() in sql/slave.cc did not check the
2199
do_db/ignore_db. And as this query involves no tables, tables_ok()
2200
above was not called. So we have to check rules again here.
2202
if (thd->slave_thread &&
2203
(!rpl_filter->db_ok(lex->name.str) ||
2204
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2206
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2209
if (thd->locked_tables || thd->active_transaction())
1809
if (session->locked_tables || session->active_transaction())
2211
1811
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2212
1812
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2215
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
1815
res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
2218
1818
case SQLCOM_ALTER_DB:
2224
1824
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2228
If in a slave thread :
2229
ALTER DATABASE DB may not be preceded by USE DB.
2230
For that reason, maybe db_ok() in sql/slave.cc did not check the
2231
do_db/ignore_db. And as this query involves no tables, tables_ok()
2232
above was not called. So we have to check rules again here.
2234
if (thd->slave_thread &&
2235
(!rpl_filter->db_ok(db->str) ||
2236
!rpl_filter->db_ok_with_wild_table(db->str)))
2238
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2241
if (thd->locked_tables || thd->active_transaction())
1827
if (session->locked_tables || session->active_transaction())
2243
1829
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2244
1830
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2247
res= mysql_alter_db(thd, db->str, &create_info);
1833
res= mysql_alter_db(session, db->str, &create_info);
2250
1836
case SQLCOM_SHOW_CREATE_DB:
2276
1861
Presumably, RESET and binlog writing doesn't require synchronization
2278
write_bin_log(thd, false, thd->query, thd->query_length);
1863
write_bin_log(session, false, session->query, session->query_length);
2284
1869
case SQLCOM_KILL:
2286
1871
Item *it= (Item *)lex->value_list.head();
2288
if (lex->table_or_sp_used())
2290
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2291
"function calls as part of this statement");
2295
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
1873
if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2297
1875
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2301
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
1879
sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2304
1882
case SQLCOM_BEGIN:
2305
if (thd->transaction.xid_state.xa_state != XA_NOTR)
1883
if (session->transaction.xid_state.xa_state != XA_NOTR)
2307
1885
my_error(ER_XAER_RMFAIL, MYF(0),
2308
xa_state_names[thd->transaction.xid_state.xa_state]);
1886
xa_state_names[session->transaction.xid_state.xa_state]);
2312
1890
Breakpoints for backup testing.
2314
if (begin_trans(thd))
1892
if (begin_trans(session))
2318
1896
case SQLCOM_COMMIT:
2319
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
1897
if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2320
1898
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2324
1902
case SQLCOM_ROLLBACK:
2325
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
1903
if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2326
1904
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2330
1908
case SQLCOM_RELEASE_SAVEPOINT:
2333
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
1911
for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2335
1913
if (my_strnncoll(system_charset_info,
2336
1914
(unsigned char *)lex->ident.str, lex->ident.length,
2364
if (ha_rollback_to_savepoint(thd, sv))
1942
if (ha_rollback_to_savepoint(session, sv))
2365
1943
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,
1946
if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
1947
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2372
1948
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2373
1949
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2376
thd->transaction.savepoints=sv;
1952
session->transaction.savepoints=sv;
2379
1955
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2382
1958
case SQLCOM_SAVEPOINT:
2383
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2384
thd->in_sub_stmt) || !opt_using_transactions)
1959
if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2388
1963
SAVEPOINT **sv, *newsv;
2389
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
1964
for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2391
1966
if (my_strnncoll(system_charset_info,
2392
1967
(unsigned char *)lex->ident.str, lex->ident.length,
2413
1988
we'll lose a little bit of memory in transaction mem_root, but it'll
2414
1989
be free'd when transaction ends anyway
2416
if (ha_savepoint(thd, newsv))
1991
if (ha_savepoint(session, newsv))
2420
newsv->prev=thd->transaction.savepoints;
2421
thd->transaction.savepoints=newsv;
1995
newsv->prev=session->transaction.savepoints;
1996
session->transaction.savepoints=newsv;
2426
case SQLCOM_BINLOG_BASE64_EVENT:
2428
mysql_client_binlog_statement(thd);
2432
2002
assert(0); /* Impossible */
2436
thd_proc_info(thd, "query end");
2439
Binlog-related cleanup:
2440
Reset system variables temporarily modified by SET ONE SHOT.
2442
Exception: If this is a SET, do nothing. This is to allow
2443
mysqlbinlog to print many SET commands (in this case we want the
2444
charset temp setting to live until the real query). This is also
2445
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2448
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2449
reset_one_shot_variables(thd);
2006
session->set_proc_info("query end");
2452
2009
The return value for ROW_COUNT() is "implementation dependent" if the
2469
2026
Release the protection against the global read lock and wake
2470
2027
everyone, who might want to set a global read lock.
2472
start_waiting_global_read_lock(thd);
2029
start_waiting_global_read_lock(session);
2474
return(res || thd->is_error());
2031
return(res || session->is_error());
2477
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
2034
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2036
LEX *lex= session->lex;
2480
2037
select_result *result=lex->result;
2482
2039
/* assign global limit variable if limit is not given */
2484
SELECT_LEX *param= lex->unit.global_parameters;
2041
Select_Lex *param= lex->unit.global_parameters;
2485
2042
if (!param->explicit_limit)
2486
2043
param->select_limit=
2487
new Item_int((uint64_t) thd->variables.select_limit);
2044
new Item_int((uint64_t) session->variables.select_limit);
2489
if (!(res= open_and_lock_tables(thd, all_tables)))
2046
if (!(res= open_and_lock_tables(session, all_tables)))
2491
2048
if (lex->describe)
2497
2054
even if the query itself redirects the output.
2499
2056
if (!(result= new select_send()))
2500
return 1; /* purecov: inspected */
2501
thd->send_explain_fields(result);
2502
res= mysql_explain_union(thd, &thd->lex->unit, result);
2057
return true; /* purecov: inspected */
2058
session->send_explain_fields(result);
2059
res= mysql_explain_union(session, &session->lex->unit, result);
2503
2060
if (lex->describe & DESCRIBE_EXTENDED)
2505
2062
char buff[1024];
2506
2063
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2508
thd->lex->unit.print(&str, QT_ORDINARY);
2065
session->lex->unit.print(&str, QT_ORDINARY);
2509
2066
str.append('\0');
2510
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2067
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2511
2068
ER_YES, str.ptr());
2531
/****************************************************************************
2532
Check stack size; Send error if there isn't enough stack to continue
2533
****************************************************************************/
2534
#if STACK_DIRECTION < 0
2535
#define used_stack(A,B) (long) (A - B)
2537
#define used_stack(A,B) (long) (B - A)
2542
Note: The 'buf' parameter is necessary, even if it is unused here.
2543
- fix_fields functions has a "dummy" buffer large enough for the
2544
corresponding exec. (Thus we only have to check in fix_fields.)
2545
- Passing to check_stack_overrun() prevents the compiler from removing it.
2547
bool check_stack_overrun(THD *thd, long margin,
2548
unsigned char *buf __attribute__((unused)))
2551
assert(thd == current_thd);
2552
if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
2553
(long) (my_thread_stack_size - margin))
2555
sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2556
stack_used,my_thread_stack_size,margin);
2557
my_message(ER_STACK_OVERRUN_NEED_MORE,errbuff[0],MYF(ME_FATALERROR));
2563
2089
#define MY_YACC_INIT 1000 // Start with big alloc
2564
2090
#define MY_YACC_MAX 32000 // Because of 'short'
2566
2092
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2568
LEX *lex= current_thd->lex;
2094
LEX *lex= current_session->lex;
2569
2095
ulong old_info=0;
2570
2096
if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2572
2098
if (!lex->yacc_yyvs)
2573
2099
old_info= *yystacksize;
2574
2100
*yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2575
if (!(lex->yacc_yyvs= (unsigned char*)
2576
my_realloc(lex->yacc_yyvs,
2577
*yystacksize*sizeof(**yyvs),
2578
MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
2579
!(lex->yacc_yyss= (unsigned char*)
2580
my_realloc(lex->yacc_yyss,
2581
*yystacksize*sizeof(**yyss),
2582
MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
2101
unsigned char *tmpptr= NULL;
2102
if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
2103
*yystacksize* sizeof(**yyvs))))
2105
lex->yacc_yyvs= tmpptr;
2107
if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
2108
*yystacksize* sizeof(**yyss))))
2110
lex->yacc_yyss= tmpptr;
2585
2112
{ // Copy old info from stack
2586
2113
memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2596
Reset THD part responsible for command processing state.
2598
This needs to be called before execution of every statement
2599
(prepared or conventional).
2600
It is not called by substatements of routines.
2603
Make it a method of THD and align its name with the rest of
2604
reset/end/start/init methods.
2606
Call it after we use THD for queries, not before.
2609
void mysql_reset_thd_for_next_command(THD *thd)
2611
assert(! thd->in_sub_stmt);
2613
thd->select_number= 1;
2615
Those two lines below are theoretically unneeded as
2616
THD::cleanup_after_query() should take care of this already.
2618
thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2619
thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2621
thd->query_start_used= 0;
2622
thd->is_fatal_error= thd->time_zone_used= 0;
2623
thd->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS |
2624
SERVER_QUERY_NO_INDEX_USED |
2625
SERVER_QUERY_NO_GOOD_INDEX_USED);
2627
If in autocommit mode and not in a transaction, reset
2628
OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2629
in ha_rollback_trans() about some tables couldn't be rolled back.
2631
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2633
thd->options&= ~OPTION_KEEP_LOG;
2634
thd->transaction.all.modified_non_trans_table= false;
2636
assert(thd->security_ctx== &thd->main_security_ctx);
2637
thd->thread_specific_used= false;
2641
reset_dynamic(&thd->user_var_events);
2642
thd->user_var_events_alloc= thd->mem_root;
2645
thd->main_da.reset_diagnostics_area();
2646
thd->total_warn_count=0; // Warnings for this query
2648
thd->sent_row_count= thd->examined_row_count= 0;
2651
Because we come here only for start of top-statements, binlog format is
2652
constant inside a complex statement (using stored functions) etc.
2654
thd->reset_current_stmt_binlog_row_based();
2661
2123
mysql_init_select(LEX *lex)
2663
SELECT_LEX *select_lex= lex->current_select;
2125
Select_Lex *select_lex= lex->current_select;
2664
2126
select_lex->init_select();
2666
2128
if (select_lex == &lex->select_lex)
2816
2279
- first, call query_cache_send_result_to_client,
2817
2280
- second, if caching failed, initialise the lexical and syntactic parser.
2818
2281
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,
2282
of (among others) lex->safe_to_cache_query and session->server_status,
2820
2283
which are reset respectively in
2822
- mysql_reset_thd_for_next_command()
2285
- mysql_reset_session_for_next_command()
2823
2286
So, initializing the lexical analyser *before* using the query cache
2824
2287
is required for the cache to work properly.
2825
2288
FIXME: cleanup the dependencies in the code to simplify this.
2828
mysql_reset_thd_for_next_command(thd);
2291
session->reset_for_next_command();
2833
Lex_input_stream lip(thd, inBuf, length);
2835
bool err= parse_sql(thd, &lip);
2294
LEX *lex= session->lex;
2296
Lex_input_stream lip(session, inBuf, length);
2298
bool err= parse_sql(session, &lip);
2836
2299
*found_semicolon= lip.found_semicolon;
2841
if (! thd->is_error())
2304
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
2307
Binlog logs a string starting from session->query and having length
2308
session->query_length; so we set session->query_length correctly (to not
2846
2309
log several statements in one event, when we executed only first).
2847
2310
We set it to not see the ';' (otherwise it would get into binlog
2848
2311
and Query_log_event::print() would give ';;' output).
2883
2346
1 can be ignored
2886
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
2349
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
2351
LEX *lex= session->lex;
2891
Lex_input_stream lip(thd, inBuf, length);
2893
mysql_reset_thd_for_next_command(thd);
2354
Lex_input_stream lip(session, inBuf, length);
2356
session->reset_for_next_command();
2895
if (!parse_sql(thd, &lip) &&
2896
all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
2358
if (!parse_sql(session, &lip) &&
2359
all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2897
2360
error= 1; /* Ignore question */
2898
thd->end_statement();
2899
thd->cleanup_after_query();
2361
session->end_statement();
2362
session->cleanup_after_query();
3459
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
2924
bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
3461
SELECT_LEX *first_sl= first_select();
2926
Select_Lex *first_sl= first_select();
3462
2927
assert(!fake_select_lex);
3464
if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
2929
if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
3466
fake_select_lex->include_standalone(this,
3467
(SELECT_LEX_NODE**)&fake_select_lex);
2931
fake_select_lex->include_standalone(this,
2932
(Select_Lex_Node**)&fake_select_lex);
3468
2933
fake_select_lex->select_number= INT_MAX;
3469
fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
2934
fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3470
2935
fake_select_lex->make_empty_select();
3471
2936
fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3472
2937
fake_select_lex->select_limit= 0;
3646
3108
than it would help them)
3648
3110
tmp_write_to_binlog= 0;
3649
if( mysql_bin_log.is_open() )
3651
mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
3653
pthread_mutex_lock(&LOCK_active_mi);
3654
rotate_relay_log(active_mi);
3655
pthread_mutex_unlock(&LOCK_active_mi);
3657
/* flush slow and general logs */
3658
logger.flush_logs(thd);
3660
3112
if (ha_flush_logs(NULL))
3662
if (flush_error_log())
3666
3116
Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
3667
3117
(see sql_yacc.yy)
3669
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3119
if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3671
if ((options & REFRESH_READ_LOCK) && thd)
3121
if ((options & REFRESH_READ_LOCK) && session)
3674
3124
We must not try to aspire a global read lock if we have a write
3675
3125
locked table. This would lead to a deadlock when trying to
3676
3126
reopen (and re-lock) the table after the flush.
3678
if (thd->locked_tables)
3128
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;
3130
THR_LOCK_DATA **lock_p= session->locked_tables->locks;
3131
THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
3683
3133
for (; lock_p < end_p; lock_p++)
3696
3146
tmp_write_to_binlog= 0;
3697
if (lock_global_read_lock(thd))
3147
if (lock_global_read_lock(session))
3698
3148
return 1; // Killed
3699
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3149
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3700
3150
false : true, true);
3701
if (make_global_read_lock_block_commit(thd)) // Killed
3151
if (make_global_read_lock_block_commit(session)) // Killed
3703
3153
/* Don't leave things in a half-locked state */
3704
unlock_global_read_lock(thd);
3154
unlock_global_read_lock(session);
3709
result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3159
result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3710
3160
false : true, false);
3713
if (thd && (options & REFRESH_STATUS))
3714
refresh_status(thd);
3715
if (options & REFRESH_THREADS)
3716
flush_thread_cache();
3717
if (options & REFRESH_MASTER)
3720
tmp_write_to_binlog= 0;
3721
if (reset_master(thd))
3726
if (options & REFRESH_SLAVE)
3728
tmp_write_to_binlog= 0;
3729
pthread_mutex_lock(&LOCK_active_mi);
3730
if (reset_slave(thd, active_mi))
3732
pthread_mutex_unlock(&LOCK_active_mi);
3162
if (session && (options & REFRESH_STATUS))
3163
session->refresh_status();
3734
3164
*write_to_binlog= tmp_write_to_binlog;
3931
bool multi_update_precheck(THD *thd,
3932
TableList *tables __attribute__((unused)))
3326
bool update_precheck(Session *session, TableList *)
3934
3328
const char *msg= 0;
3936
SELECT_LEX *select_lex= &lex->select_lex;
3329
LEX *lex= session->lex;
3330
Select_Lex *select_lex= &lex->select_lex;
3938
if (select_lex->item_list.elements != lex->value_list.elements)
3332
if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
3940
3334
my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
3944
if (select_lex->order_list.elements)
3946
else if (select_lex->select_limit)
3338
if (session->lex->select_lex.table_list.elements > 1)
3950
my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
3340
if (select_lex->order_list.elements)
3342
else if (select_lex->select_limit)
3346
my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
4192
3563
return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
4195
if ((negated= expr->neg_transformer(thd)) != 0)
3566
if ((negated= expr->neg_transformer(session)) != 0)
4196
3567
return negated;
4197
3568
return new Item_func_not(expr);
4202
Check that byte length of a string does not exceed some limit.
4204
@param str string to be checked
4205
@param err_msg error message to be displayed if the string is too long
4206
@param max_length max length
4209
false the passed string is not longer than max_length
4211
true the passed string is longer than max_length
4214
The function is not used in existing code but can be useful later?
4217
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
4218
uint32_t max_byte_length)
4220
if (str->length <= max_byte_length)
4223
my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
4230
3573
Check that char length of a string does not exceed some limit.
4262
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
4263
uint32_t err_code, const char *param_for_err_msg)
3605
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
3606
uint32_t max_char_length,
3607
const char *param_for_err_msg)
4265
#ifdef HAVE_CHARSET_utf8mb3
4267
3610
We don't support non-BMP characters in identifiers at the moment,
4268
3611
so they should be prohibited until such support is done.
4269
3612
This is why we use the 3-byte utf8 to check well-formedness here.
4271
const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
4273
const CHARSET_INFO * const cs= system_charset_info;
3614
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
4275
3616
int well_formed_error;
4276
3617
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4277
3618
max_char_length, &well_formed_error);