139
191
sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA;
141
193
sql_command_flags[SQLCOM_UPDATE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
194
sql_command_flags[SQLCOM_UPDATE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
142
195
sql_command_flags[SQLCOM_INSERT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
143
196
sql_command_flags[SQLCOM_INSERT_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
144
197
sql_command_flags[SQLCOM_DELETE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
198
sql_command_flags[SQLCOM_DELETE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
145
199
sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
146
200
sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
202
sql_command_flags[SQLCOM_SHOW_STATUS]= CF_STATUS_COMMAND;
203
sql_command_flags[SQLCOM_SHOW_DATABASES]= CF_STATUS_COMMAND;
204
sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND;
205
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
206
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
207
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
208
sql_command_flags[SQLCOM_SHOW_CHARSETS]= CF_STATUS_COMMAND;
209
sql_command_flags[SQLCOM_SHOW_COLLATIONS]= CF_STATUS_COMMAND;
210
sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
211
sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
212
sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
148
213
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
149
214
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
215
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
216
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
150
217
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
151
218
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
219
sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
220
sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
222
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
223
CF_SHOW_TABLE_COMMAND);
224
sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
225
CF_SHOW_TABLE_COMMAND);
154
227
The following admin table operations are allowed
230
sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND;
231
sql_command_flags[SQLCOM_OPTIMIZE]= CF_WRITE_LOGS_COMMAND;
157
232
sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND;
236
bool is_update_query(enum enum_sql_command command)
238
assert(command >= 0 && command <= SQLCOM_END);
239
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
242
void execute_init_command(THD *thd, sys_var_str *init_command_var,
243
rw_lock_t *var_mutex)
246
ulong save_client_capabilities;
248
thd_proc_info(thd, "Execution of init_command");
250
We need to lock init_command_var because
251
during execution of init_command_var query
252
values of init_command_var can't be changed
254
rw_rdlock(var_mutex);
255
save_client_capabilities= thd->client_capabilities;
256
thd->client_capabilities|= CLIENT_MULTI_QUERIES;
258
We don't need return result of execution to client side.
259
To forbid this we should set thd->net.vio to 0.
261
save_vio= thd->net.vio;
263
dispatch_command(COM_QUERY, thd,
264
init_command_var->value,
265
init_command_var->value_length);
266
rw_unlock(var_mutex);
267
thd->client_capabilities= save_client_capabilities;
268
thd->net.vio= save_vio;
272
Ends the current transaction and (maybe) begin the next.
274
@param thd Current thread
275
@param completion Completion type
281
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
286
if (unlikely(thd->in_sub_stmt))
288
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
291
if (thd->transaction.xid_state.xa_state != XA_NOTR)
293
my_error(ER_XAER_RMFAIL, MYF(0),
294
xa_state_names[thd->transaction.xid_state.xa_state]);
297
switch (completion) {
300
We don't use end_active_trans() here to ensure that this works
301
even if there is a problem with the OPTION_AUTO_COMMIT flag
302
(Which of course should never happen...)
304
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
306
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
307
thd->transaction.all.modified_non_trans_table= false;
310
do_release= 1; /* fall through */
311
case COMMIT_AND_CHAIN:
312
res= end_active_trans(thd);
313
if (!res && completion == COMMIT_AND_CHAIN)
314
res= begin_trans(thd);
316
case ROLLBACK_RELEASE:
317
do_release= 1; /* fall through */
319
case ROLLBACK_AND_CHAIN:
321
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
322
if (ha_rollback(thd))
324
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
325
thd->transaction.all.modified_non_trans_table= false;
326
if (!res && (completion == ROLLBACK_AND_CHAIN))
327
res= begin_trans(thd);
332
my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
337
my_error(thd->killed_errno(), MYF(0));
338
else if ((res == 0) && do_release)
339
thd->killed= THD::KILL_CONNECTION;
346
Read one command from connection and execute it (query or simple command).
347
This function is called in loop from thread function.
349
For profiling to work, it must never be called recursively.
354
1 request of thread shutdown (see dispatch_command() description)
357
bool do_command(THD *thd)
363
enum enum_server_command command;
366
indicator of uninitialized lex => normal flow of errors handling
369
thd->lex->current_select= 0;
372
This thread will do a blocking read from the client which
373
will be interrupted when the next command is received from
374
the client, the connection is closed or "net_wait_timeout"
375
number of seconds has passed
377
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
380
XXX: this code is here only to clear possible errors of init_connect.
381
Consider moving to init_connect() instead.
383
thd->clear_error(); // Clear error message
384
thd->main_da.reset_diagnostics_area();
386
net_new_transaction(net);
388
packet_length= my_net_read(net);
389
if (packet_length == packet_error)
391
/* Check if we can continue without closing the connection */
393
/* The error must be set. */
394
assert(thd->is_error());
395
net_end_statement(thd);
399
return_value= true; // We have to close it.
408
packet= (char*) net->read_pos;
410
'packet_length' contains length of data, as it was stored in packet
411
header. In case of malformed header, my_net_read returns zero.
412
If packet_length is not zero, my_net_read ensures that the returned
413
number of bytes was actually read from network.
414
There is also an extra safety measure in my_net_read:
415
it sets packet[packet_length]= 0, but only for non-zero packets.
417
if (packet_length == 0) /* safety */
419
/* Initialize with COM_SLEEP packet */
420
packet[0]= (uchar) COM_SLEEP;
423
/* Do not rely on my_net_read, extra safety against programming errors. */
424
packet[packet_length]= '\0'; /* safety */
426
command= (enum enum_server_command) (uchar) packet[0];
428
if (command >= COM_END)
429
command= COM_END; // Wrong command
431
/* Restore read timeout value */
432
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
434
assert(packet_length);
435
return_value= dispatch_command(command, thd, packet+1, (uint) (packet_length-1));
438
return(return_value);
442
Determine if an attempt to update a non-temporary table while the
443
read-only option was enabled has been made.
445
This is a helper function to mysql_execute_command.
447
@note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
449
@see mysql_execute_command
452
@retval true The statement should be denied.
453
@retval false The statement isn't updating any relevant tables.
456
static my_bool deny_updates_if_read_only_option(THD *thd,
457
TABLE_LIST *all_tables)
464
if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
467
/* Multi update is an exception and is dealt with later. */
468
if (lex->sql_command == SQLCOM_UPDATE_MULTI)
471
const my_bool create_temp_tables=
472
(lex->sql_command == SQLCOM_CREATE_TABLE) &&
473
(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
475
const my_bool drop_temp_tables=
476
(lex->sql_command == SQLCOM_DROP_TABLE) &&
479
const my_bool update_real_tables=
480
some_non_temp_table_to_be_updated(thd, all_tables) &&
481
!(create_temp_tables || drop_temp_tables);
484
const my_bool create_or_drop_databases=
485
(lex->sql_command == SQLCOM_CREATE_DB) ||
486
(lex->sql_command == SQLCOM_DROP_DB);
488
if (update_real_tables || create_or_drop_databases)
491
An attempt was made to modify one or more non-temporary tables.
497
/* Assuming that only temporary tables are modified. */
161
502
Perform one connection-level (COM_XXXX) command.
163
504
@param command type of command to perform
164
@param session connection handle
505
@param thd connection handle
165
506
@param packet data for the command, packet is always null-terminated
166
507
@param packet_length length of packet + 1 (to show that data is
167
508
null-terminated) except for COM_SLEEP, where it
171
set session->getLex()->sql_command to SQLCOM_END here.
512
set thd->lex->sql_command to SQLCOM_END here.
173
514
The following has to be changed to an 8 byte integer
178
519
1 request of thread shutdown, i. e. if command is
179
520
COM_QUIT/COM_SHUTDOWN
181
bool dispatch_command(enum enum_server_command command, Session *session,
182
char* packet, uint32_t packet_length)
522
bool dispatch_command(enum enum_server_command command, THD *thd,
523
char* packet, uint packet_length)
185
Query_id &query_id= Query_id::get_query_id();
187
DRIZZLE_COMMAND_START(session->thread_id, command);
189
session->command= command;
190
session->getLex()->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
192
session->setQueryId(query_id.value());
528
thd->command=command;
530
Commands which always take a long time are logged into
531
the slow log only if opt_log_slow_admin_statements is set.
533
thd->enable_slow_log= true;
534
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
536
VOID(pthread_mutex_lock(&LOCK_thread_count));
537
thd->query_id= global_query_id;
194
539
switch( command ) {
195
540
/* Ignore these statements. */
544
/* Only increase id on these statements but don't count them. */
545
case COM_STMT_PREPARE:
198
550
/* Increase id and count all other statements. */
200
session->status_var.questions++;
204
/* @todo set session->getLex()->sql_command to SQLCOM_END here */
206
plugin::Logging::preDo(session);
207
if (unlikely(plugin::EventObserver::beforeStatement(*session)))
209
// We should do something about an error...
212
session->server_status&=
552
statistic_increment(thd->status_var.questions, &LOCK_status);
557
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
558
VOID(pthread_mutex_unlock(&LOCK_thread_count));
213
561
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
214
562
switch (command) {
215
563
case COM_INIT_DB:
217
if (packet_length == 0)
219
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
223
string tmp(packet, packet_length);
225
identifier::Schema identifier(tmp);
227
if (not schema::change(*session, identifier))
566
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
567
thd->convert_string(&tmp, system_charset_info,
568
packet, packet_length, thd->charset());
569
if (!mysql_change_db(thd, &tmp, false))
571
general_log_write(thd, command, thd->db, thd->db_length);
576
case COM_REGISTER_SLAVE:
578
if (!register_slave(thd, (uchar*)packet, packet_length))
582
case COM_CHANGE_USER:
584
status_var_increment(thd->status_var.com_other);
585
char *user= (char*) packet, *packet_end= packet + packet_length;
586
/* Safe because there is always a trailing \0 at the end of the packet */
587
char *passwd= strend(user)+1;
589
thd->clear_error(); // if errors from rollback
592
Old clients send null-terminated string ('\0' for empty string) for
593
password. New clients send the size (1 byte) + string (not null
594
terminated, so also '\0' for empty string).
596
Cast *passwd to an unsigned char, so that it doesn't extend the sign
597
for *passwd > 127 and become 2**32-127 after casting to uint.
599
char db_buff[NAME_LEN+1]; // buffer to store db in utf8
603
If there is no password supplied, the packet must contain '\0',
604
in any type of handshake (4.1 or pre-4.1).
606
if (passwd >= packet_end)
608
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
611
uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
612
(uchar)(*passwd++) : strlen(passwd));
613
uint dummy_errors, save_db_length, db_length;
615
Security_context save_security_ctx= *thd->security_ctx;
616
USER_CONN *save_user_connect;
620
Database name is always NUL-terminated, so in case of empty database
621
the packet must contain at least the trailing '\0'.
623
if (db >= packet_end)
625
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
628
db_length= strlen(db);
630
char *ptr= db + db_length + 1;
633
if (ptr < packet_end)
635
if (ptr + 2 > packet_end)
637
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
641
cs_number= uint2korr(ptr);
644
/* Convert database name to utf8 */
645
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
646
system_charset_info, db, db_length,
647
thd->charset(), &dummy_errors)]= 0;
650
/* Save user and privileges */
651
save_db_length= thd->db_length;
653
save_user_connect= thd->user_connect;
655
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
657
thd->security_ctx->user= save_security_ctx.user;
658
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
662
/* Clear variables that are allocated */
663
thd->user_connect= 0;
664
thd->security_ctx->priv_user= thd->security_ctx->user;
665
res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
669
x_free(thd->security_ctx->user);
670
*thd->security_ctx= save_security_ctx;
671
thd->user_connect= save_user_connect;
673
thd->db_length= save_db_length;
678
x_free(save_security_ctx.user);
682
thd_init_client_charset(thd, cs_number);
683
thd->update_charset();
688
case COM_STMT_EXECUTE:
690
case COM_STMT_SEND_LONG_DATA:
691
case COM_STMT_PREPARE:
695
/* We should toss an error here */
235
if (not session->readAndStoreQuery(packet, packet_length))
700
if (alloc_query(thd, packet, packet_length))
236
701
break; // fatal error is set
237
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
239
const_cast<const char *>(session->schema()->c_str()));
241
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
702
char *packet_end= thd->query + thd->query_length;
703
const char* end_of_stmt= NULL;
705
general_log_write(thd, command, thd->query, thd->query_length);
707
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
709
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
711
char *beginning_of_next_stmt= (char*) end_of_stmt;
713
net_end_statement(thd);
715
Multiple queries exits, execute them individually
717
close_thread_tables(thd);
718
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
720
log_slow_statement(thd);
722
/* Remove garbage at start of query */
723
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
725
beginning_of_next_stmt++;
729
VOID(pthread_mutex_lock(&LOCK_thread_count));
730
thd->query_length= length;
731
thd->query= beginning_of_next_stmt;
733
Count each statement from the client.
735
statistic_increment(thd->status_var.questions, &LOCK_status);
736
thd->query_id= next_query_id();
737
thd->set_time(); /* Reset the query start time. */
738
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
739
VOID(pthread_mutex_unlock(&LOCK_thread_count));
741
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
745
case COM_FIELD_LIST: // This isn't actually needed
747
char *fields, *packet_end= packet + packet_length, *arg_end;
748
/* Locked closure of all tables */
749
TABLE_LIST table_list;
750
LEX_STRING conv_name;
752
/* used as fields initializator */
755
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
756
bzero((char*) &table_list,sizeof(table_list));
757
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
760
We have name + wildcard in packet, separated by endzero
762
arg_end= strend(packet);
763
thd->convert_string(&conv_name, system_charset_info,
764
packet, (uint) (arg_end - packet), thd->charset());
765
table_list.alias= table_list.table_name= conv_name.str;
768
if (!my_strcasecmp(system_charset_info, table_list.db,
769
INFORMATION_SCHEMA_NAME.str))
771
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
773
table_list.schema_table= schema_table;
776
thd->query_length= (uint) (packet_end - packet); // Don't count end \0
777
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
779
general_log_print(thd, command, "%s %s", table_list.table_name, fields);
780
if (lower_case_table_names)
781
my_casedn_str(files_charset_info, table_list.table_name);
783
/* init structures for VIEW processing */
784
table_list.select_lex= &(thd->lex->select_lex);
787
mysql_reset_thd_for_next_command(thd);
790
select_lex.table_list.link_in_list((uchar*) &table_list,
791
(uchar**) &table_list.next_local);
792
thd->lex->add_to_query_tables(&table_list);
794
/* switch on VIEW optimisation: do not fill temporary tables */
795
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
796
mysqld_list_fields(thd,&table_list,fields);
797
thd->lex->unit.cleanup();
798
thd->cleanup_after_query();
246
802
/* We don't calculate statistics for this command */
247
session->main_da.disable_status(); // Don't send anything back
803
general_log_print(thd, command, NullS);
804
net->error=0; // Don't give 'abort' message
805
thd->main_da.disable_status(); // Don't send anything back
248
806
error=true; // End server
808
case COM_BINLOG_DUMP:
812
uint32 slave_server_id;
814
status_var_increment(thd->status_var.com_other);
815
thd->enable_slow_log= opt_log_slow_admin_statements;
816
/* TODO: The following has to be changed to an 8 byte integer */
817
pos = uint4korr(packet);
818
flags = uint2korr(packet + 4);
819
thd->server_id=0; /* avoid suicide */
820
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
821
kill_zombie_dump_threads(slave_server_id);
822
thd->server_id = slave_server_id;
824
general_log_print(thd, command, "Log: '%s' Pos: %ld", packet+10,
826
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
827
unregister_slave(thd,1,1);
828
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
250
832
case COM_SHUTDOWN:
252
session->status_var.com_other++;
254
session->close_thread_tables(); // Free before kill
834
status_var_increment(thd->status_var.com_other);
836
If the client is < 4.1.3, it is going to send us no argument; then
837
packet_length is 0, packet[0] is the end 0 of the packet. Note that
838
SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
841
enum mysql_enum_shutdown_level level=
842
(enum mysql_enum_shutdown_level) (uchar) packet[0];
843
if (level == SHUTDOWN_DEFAULT)
844
level= SHUTDOWN_WAIT_ALL_BUFFERS; // soon default will be configurable
845
else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
847
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "this shutdown level");
850
general_log_print(thd, command, NullS);
852
close_thread_tables(thd); // Free before kill
859
STATUS_VAR current_global_status_var;
862
uint64_t queries_per_second1000;
864
uint buff_len= sizeof(buff);
866
general_log_print(thd, command, NullS);
867
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_STATUS]);
868
calc_sum_of_all_status(¤t_global_status_var);
869
if (!(uptime= (ulong) (thd->start_time - server_start_time)))
870
queries_per_second1000= 0;
872
queries_per_second1000= thd->query_id * 1000LL / uptime;
874
length= snprintf((char*) buff, buff_len - 1,
875
"Uptime: %lu Threads: %d Questions: %lu "
876
"Slow queries: %lu Opens: %lu Flush tables: %lu "
877
"Open tables: %u Queries per second avg: %u.%u",
879
(int) thread_count, (ulong) thd->query_id,
880
current_global_status_var.long_query_count,
881
current_global_status_var.opened_tables,
883
cached_open_tables(),
884
(uint) (queries_per_second1000 / 1000),
885
(uint) (queries_per_second1000 % 1000));
886
/* Store the buffer in permanent memory */
887
my_ok(thd, 0, 0, buff);
888
VOID(my_net_write(net, (uchar*) buff, length));
889
VOID(net_flush(net));
890
thd->main_da.disable_status();
260
session->status_var.com_other++;
261
session->my_ok(); // Tell client we are alive
894
status_var_increment(thd->status_var.com_other);
895
my_ok(thd); // Tell client we are alive
897
case COM_PROCESS_INFO:
898
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
899
general_log_print(thd, command, NullS);
900
mysqld_list_processes(thd, NullS, 0);
902
case COM_PROCESS_KILL:
904
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
905
ulong id=(ulong) uint4korr(packet);
906
sql_kill(thd,id,false);
911
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
912
uint opt_command= uint2korr(packet);
914
switch (opt_command) {
915
case (int) MYSQL_OPTION_MULTI_STATEMENTS_ON:
916
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
919
case (int) MYSQL_OPTION_MULTI_STATEMENTS_OFF:
920
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
924
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
264
930
case COM_CONNECT: // Impossible here
931
case COM_TIME: // Impossible from client
267
934
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
483
1249
variables, but for now this is probably good enough.
484
1250
Don't reset warnings when executing a stored routine.
486
if (all_tables || ! session->getLex()->is_single_level_stmt())
488
drizzle_reset_errors(session, 0);
491
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
493
if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
494
&& ! session->inTransaction()
495
&& session->getLex()->statement->isTransactional())
497
if (session->startTransaction() == false)
499
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
504
/* now we are ready to execute the statement */
505
res= session->getLex()->statement->execute();
506
session->set_proc_info("query end");
1252
if (all_tables || !lex->is_single_level_stmt())
1253
mysql_reset_errors(thd, 0);
1255
if (unlikely(thd->slave_thread))
1258
Check if statment should be skipped because of slave filtering
1262
- UPDATE MULTI: For this statement, we want to check the filtering
1263
rules later in the code
1264
- SET: we always execute it (Not that many SET commands exists in
1265
the binary log anyway -- only 4.1 masters write SET statements,
1266
in 5.0 there are no SET statements in the binary log)
1267
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1268
have stale files on slave caused by exclusion of one tmp table).
1270
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1271
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1272
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1273
lex->drop_temporary && lex->drop_if_exists) &&
1274
all_tables_not_ok(thd, all_tables))
1276
/* we warn the slave SQL thread */
1277
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1278
if (thd->one_shot_set)
1281
It's ok to check thd->one_shot_set here:
1283
The charsets in a MySQL 5.0 slave can change by both a binlogged
1284
SET ONE_SHOT statement and the event-internal charset setting,
1285
and these two ways to change charsets do not seems to work
1288
At least there seems to be problems in the rli cache for
1289
charsets if we are using ONE_SHOT. Note that this is normally no
1290
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1291
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1293
reset_one_shot_variables(thd);
1301
When option readonly is set deny operations which change non-temporary
1302
tables. Except for the replication thread and the 'super' users.
1304
if (deny_updates_if_read_only_option(thd, all_tables))
1306
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1309
} /* endif unlikely slave */
1310
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1312
assert(thd->transaction.stmt.modified_non_trans_table == false);
1314
switch (lex->sql_command) {
1315
case SQLCOM_SHOW_STATUS:
1317
system_status_var old_status_var= thd->status_var;
1318
thd->initial_status_var= &old_status_var;
1319
res= execute_sqlcom_select(thd, all_tables);
1320
/* Don't log SHOW STATUS commands to slow query log */
1321
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1322
SERVER_QUERY_NO_GOOD_INDEX_USED);
1324
restore status variables, as we don't want 'show status' to cause
1327
pthread_mutex_lock(&LOCK_status);
1328
add_diff_to_status(&global_status_var, &thd->status_var,
1330
thd->status_var= old_status_var;
1331
pthread_mutex_unlock(&LOCK_status);
1334
case SQLCOM_SHOW_DATABASES:
1335
case SQLCOM_SHOW_TABLES:
1336
case SQLCOM_SHOW_TABLE_STATUS:
1337
case SQLCOM_SHOW_OPEN_TABLES:
1338
case SQLCOM_SHOW_FIELDS:
1339
case SQLCOM_SHOW_KEYS:
1340
case SQLCOM_SHOW_VARIABLES:
1341
case SQLCOM_SHOW_CHARSETS:
1342
case SQLCOM_SHOW_COLLATIONS:
1345
thd->status_var.last_query_cost= 0.0;
1346
res= execute_sqlcom_select(thd, all_tables);
1349
case SQLCOM_EMPTY_QUERY:
1355
res = purge_master_logs(thd, lex->to_log);
1358
case SQLCOM_PURGE_BEFORE:
1362
/* PURGE MASTER LOGS BEFORE 'data' */
1363
it= (Item *)lex->value_list.head();
1364
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1367
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1370
it= new Item_func_unix_timestamp(it);
1372
it is OK only emulate fix_fieds, because we need only
1375
it->quick_fix_field();
1376
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1379
case SQLCOM_SHOW_WARNS:
1381
res= mysqld_show_warnings(thd, (ulong)
1382
((1L << (uint) MYSQL_ERROR::WARN_LEVEL_NOTE) |
1383
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_WARN) |
1384
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR)
1388
case SQLCOM_SHOW_ERRORS:
1390
res= mysqld_show_warnings(thd, (ulong)
1391
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR));
1394
case SQLCOM_SHOW_SLAVE_HOSTS:
1396
res = show_slave_hosts(thd);
1399
case SQLCOM_SHOW_BINLOG_EVENTS:
1401
res = mysql_show_binlog_events(thd);
1405
case SQLCOM_ASSIGN_TO_KEYCACHE:
1407
assert(first_table == all_tables && first_table != 0);
1408
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1411
case SQLCOM_CHANGE_MASTER:
1413
pthread_mutex_lock(&LOCK_active_mi);
1414
res = change_master(thd,active_mi);
1415
pthread_mutex_unlock(&LOCK_active_mi);
1418
case SQLCOM_SHOW_SLAVE_STAT:
1420
pthread_mutex_lock(&LOCK_active_mi);
1421
if (active_mi != NULL)
1423
res = show_master_info(thd, active_mi);
1427
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1428
"the master info structure does not exist");
1431
pthread_mutex_unlock(&LOCK_active_mi);
1434
case SQLCOM_SHOW_MASTER_STAT:
1436
res = show_binlog_info(thd);
1440
case SQLCOM_SHOW_ENGINE_STATUS:
1442
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1445
case SQLCOM_CREATE_TABLE:
1447
/* If CREATE TABLE of non-temporary table, do implicit commit */
1448
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1450
if (end_active_trans(thd))
1456
assert(first_table == all_tables && first_table != 0);
1458
// Skip first table, which is the table we are creating
1459
TABLE_LIST *create_table= lex->unlink_first_table(&link_to_local);
1460
TABLE_LIST *select_tables= lex->query_tables;
1462
Code below (especially in mysql_create_table() and select_create
1463
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1464
use a copy of this structure to make execution prepared statement-
1465
safe. A shallow copy is enough as this code won't modify any memory
1466
referenced from this structure.
1468
HA_CREATE_INFO create_info(lex->create_info);
1470
We need to copy alter_info for the same reasons of re-execution
1471
safety, only in case of Alter_info we have to do (almost) a deep
1474
Alter_info alter_info(lex->alter_info, thd->mem_root);
1476
if (thd->is_fatal_error)
1478
/* If out of memory when creating a copy of alter_info. */
1480
goto end_with_restore_list;
1483
if ((res= create_table_precheck(thd, select_tables, create_table)))
1484
goto end_with_restore_list;
1486
/* Might have been updated in create_table_precheck */
1487
create_info.alias= create_table->alias;
1489
#ifdef HAVE_READLINK
1490
/* Fix names if symlinked tables */
1491
if (append_file_to_dir(thd, &create_info.data_file_name,
1492
create_table->table_name) ||
1493
append_file_to_dir(thd, &create_info.index_file_name,
1494
create_table->table_name))
1495
goto end_with_restore_list;
1498
If we are using SET CHARSET without DEFAULT, add an implicit
1499
DEFAULT to not confuse old users. (This may change).
1501
if ((create_info.used_fields &
1502
(HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
1503
HA_CREATE_USED_CHARSET)
1505
create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
1506
create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
1507
create_info.default_table_charset= create_info.table_charset;
1508
create_info.table_charset= 0;
1511
The create-select command will open and read-lock the select table
1512
and then create, open and write-lock the new table. If a global
1513
read lock steps in, we get a deadlock. The write lock waits for
1514
the global read lock, while the global read lock waits for the
1515
select table to be closed. So we wait until the global readlock is
1516
gone before starting both steps. Note that
1517
wait_if_global_read_lock() sets a protection against a new global
1518
read lock when it succeeds. This needs to be released by
1519
start_waiting_global_read_lock(). We protect the normal CREATE
1520
TABLE in the same way. That way we avoid that a new table is
1521
created during a gobal read lock.
1523
if (!thd->locked_tables &&
1524
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1527
goto end_with_restore_list;
1529
if (select_lex->item_list.elements) // With select
1531
select_result *result;
1533
select_lex->options|= SELECT_NO_UNLOCK;
1534
unit->set_limit(select_lex);
1537
Disable non-empty MERGE tables with CREATE...SELECT. Too
1538
complicated. See Bug #26379. Empty MERGE tables are read-only
1539
and don't allow CREATE...SELECT anyway.
1541
if (create_info.used_fields & HA_CREATE_USED_UNION)
1543
my_error(ER_WRONG_OBJECT, MYF(0), create_table->db,
1544
create_table->table_name, "BASE TABLE");
1546
goto end_with_restore_list;
1549
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1551
lex->link_first_table_back(create_table, link_to_local);
1552
create_table->create= true;
1555
if (!(res= open_and_lock_tables(thd, lex->query_tables)))
1558
Is table which we are changing used somewhere in other parts
1561
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1563
TABLE_LIST *duplicate;
1564
create_table= lex->unlink_first_table(&link_to_local);
1565
if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1567
update_non_unique_table_error(create_table, "CREATE", duplicate);
1569
goto end_with_restore_list;
1572
/* If we create merge table, we have to test tables in merge, too */
1573
if (create_info.used_fields & HA_CREATE_USED_UNION)
1576
for (tab= (TABLE_LIST*) create_info.merge_list.first;
1578
tab= tab->next_local)
1580
TABLE_LIST *duplicate;
1581
if ((duplicate= unique_table(thd, tab, select_tables, 0)))
1583
update_non_unique_table_error(tab, "CREATE", duplicate);
1585
goto end_with_restore_list;
1591
select_create is currently not re-execution friendly and
1592
needs to be created for every execution of a PS/SP.
1594
if ((result= new select_create(create_table,
1597
select_lex->item_list,
1603
CREATE from SELECT give its SELECT_LEX for SELECT,
1604
and item_list belong to SELECT
1606
res= handle_select(thd, lex, result, 0);
1610
else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1611
create_table= lex->unlink_first_table(&link_to_local);
1616
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1617
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1618
thd->options|= OPTION_KEEP_LOG;
1619
/* regular create */
1620
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1621
res= mysql_create_like_table(thd, create_table, select_tables,
1625
res= mysql_create_table(thd, create_table->db,
1626
create_table->table_name, &create_info,
1633
/* put tables back for PS rexecuting */
1634
end_with_restore_list:
1635
lex->link_first_table_back(create_table, link_to_local);
1638
case SQLCOM_CREATE_INDEX:
1640
case SQLCOM_DROP_INDEX:
1642
CREATE INDEX and DROP INDEX are implemented by calling ALTER
1643
TABLE with proper arguments.
1645
In the future ALTER TABLE will notice that the request is to
1646
only add indexes and create these one by one for the existing
1647
table without having to do a full rebuild.
1650
/* Prepare stack copies to be re-execution safe */
1651
HA_CREATE_INFO create_info;
1652
Alter_info alter_info(lex->alter_info, thd->mem_root);
1654
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1657
assert(first_table == all_tables && first_table != 0);
1658
if (end_active_trans(thd))
1661
Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1662
and thus classify as slow administrative statements just like
1665
thd->enable_slow_log= opt_log_slow_admin_statements;
1667
bzero((char*) &create_info, sizeof(create_info));
1668
create_info.db_type= 0;
1669
create_info.row_type= ROW_TYPE_NOT_USED;
1670
create_info.default_table_charset= thd->variables.collation_database;
1672
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1673
&create_info, first_table, &alter_info,
1677
#ifdef HAVE_REPLICATION
1678
case SQLCOM_SLAVE_START:
1680
pthread_mutex_lock(&LOCK_active_mi);
1681
start_slave(thd,active_mi,1 /* net report*/);
1682
pthread_mutex_unlock(&LOCK_active_mi);
1685
case SQLCOM_SLAVE_STOP:
1687
If the client thread has locked tables, a deadlock is possible.
1689
- the client thread does LOCK TABLE t READ.
1690
- then the master updates t.
1691
- then the SQL slave thread wants to update t,
1692
so it waits for the client thread because t is locked by it.
1693
- then the client thread does SLAVE STOP.
1694
SLAVE STOP waits for the SQL slave thread to terminate its
1695
update t, which waits for the client thread because t is locked by it.
1696
To prevent that, refuse SLAVE STOP if the
1697
client thread has locked tables
1699
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1701
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1702
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1706
pthread_mutex_lock(&LOCK_active_mi);
1707
stop_slave(thd,active_mi,1/* net report*/);
1708
pthread_mutex_unlock(&LOCK_active_mi);
1711
#endif /* HAVE_REPLICATION */
1713
case SQLCOM_ALTER_TABLE:
1714
assert(first_table == all_tables && first_table != 0);
1717
Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
1718
so we have to use a copy of this structure to make execution
1719
prepared statement- safe. A shallow copy is enough as no memory
1720
referenced from this structure will be modified.
1722
HA_CREATE_INFO create_info(lex->create_info);
1723
Alter_info alter_info(lex->alter_info, thd->mem_root);
1725
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1730
/* Must be set in the parser */
1731
assert(select_lex->db);
1733
{ // Rename of table
1734
TABLE_LIST tmp_table;
1735
bzero((char*) &tmp_table,sizeof(tmp_table));
1736
tmp_table.table_name= lex->name.str;
1737
tmp_table.db=select_lex->db;
1740
/* Don't yet allow changing of symlinks with ALTER TABLE */
1741
if (create_info.data_file_name)
1742
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1743
"DATA DIRECTORY option ignored");
1744
if (create_info.index_file_name)
1745
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1746
"INDEX DIRECTORY option ignored");
1747
create_info.data_file_name= create_info.index_file_name= NULL;
1748
/* ALTER TABLE ends previous transaction */
1749
if (end_active_trans(thd))
1752
if (!thd->locked_tables &&
1753
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1759
thd->enable_slow_log= opt_log_slow_admin_statements;
1760
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1764
select_lex->order_list.elements,
1765
(ORDER *) select_lex->order_list.first,
1769
case SQLCOM_RENAME_TABLE:
1771
assert(first_table == all_tables && first_table != 0);
1773
for (table= first_table; table; table= table->next_local->next_local)
1775
TABLE_LIST old_list, new_list;
1777
we do not need initialize old_list and new_list because we will
1778
come table[0] and table->next[0] there
1781
new_list= table->next_local[0];
1784
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1790
case SQLCOM_SHOW_BINLOGS:
1792
res = show_binlogs(thd);
1795
case SQLCOM_SHOW_CREATE:
1796
assert(first_table == all_tables && first_table != 0);
1798
res= mysqld_show_create(thd, first_table);
1801
case SQLCOM_CHECKSUM:
1803
assert(first_table == all_tables && first_table != 0);
1804
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1809
assert(first_table == all_tables && first_table != 0);
1810
thd->enable_slow_log= opt_log_slow_admin_statements;
1811
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1812
/* ! we write after unlocking the table */
1813
if (!res && !lex->no_write_to_binlog)
1816
Presumably, REPAIR and binlog writing doesn't require synchronization
1818
write_bin_log(thd, true, thd->query, thd->query_length);
1820
select_lex->table_list.first= (uchar*) first_table;
1821
lex->query_tables=all_tables;
1826
assert(first_table == all_tables && first_table != 0);
1827
thd->enable_slow_log= opt_log_slow_admin_statements;
1828
res = mysql_check_table(thd, first_table, &lex->check_opt);
1829
select_lex->table_list.first= (uchar*) first_table;
1830
lex->query_tables=all_tables;
1833
case SQLCOM_ANALYZE:
1835
assert(first_table == all_tables && first_table != 0);
1836
thd->enable_slow_log= opt_log_slow_admin_statements;
1837
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1838
/* ! we write after unlocking the table */
1839
if (!res && !lex->no_write_to_binlog)
1842
Presumably, ANALYZE and binlog writing doesn't require synchronization
1844
write_bin_log(thd, true, thd->query, thd->query_length);
1846
select_lex->table_list.first= (uchar*) first_table;
1847
lex->query_tables=all_tables;
1851
case SQLCOM_OPTIMIZE:
1853
assert(first_table == all_tables && first_table != 0);
1854
thd->enable_slow_log= opt_log_slow_admin_statements;
1855
res= (specialflag & (SPECIAL_SAFE_MODE | SPECIAL_NO_NEW_FUNC)) ?
1856
mysql_recreate_table(thd, first_table) :
1857
mysql_optimize_table(thd, first_table, &lex->check_opt);
1858
/* ! we write after unlocking the table */
1859
if (!res && !lex->no_write_to_binlog)
1862
Presumably, OPTIMIZE and binlog writing doesn't require synchronization
1864
write_bin_log(thd, true, thd->query, thd->query_length);
1866
select_lex->table_list.first= (uchar*) first_table;
1867
lex->query_tables=all_tables;
1871
assert(first_table == all_tables && first_table != 0);
1872
if (update_precheck(thd, all_tables))
1874
assert(select_lex->offset_limit == 0);
1875
unit->set_limit(select_lex);
1876
res= (up_result= mysql_update(thd, all_tables,
1877
select_lex->item_list,
1880
select_lex->order_list.elements,
1881
(ORDER *) select_lex->order_list.first,
1882
unit->select_limit_cnt,
1883
lex->duplicates, lex->ignore));
1884
/* mysql_update return 2 if we need to switch to multi-update */
1888
case SQLCOM_UPDATE_MULTI:
1890
assert(first_table == all_tables && first_table != 0);
1891
/* if we switched from normal update, rights are checked */
1894
if ((res= multi_update_precheck(thd, all_tables)))
1900
res= mysql_multi_update_prepare(thd);
1902
#ifdef HAVE_REPLICATION
1903
/* Check slave filtering rules */
1904
if (unlikely(thd->slave_thread))
1906
if (all_tables_not_ok(thd, all_tables))
1910
res= 0; /* don't care of prev failure */
1911
thd->clear_error(); /* filters are of highest prior */
1913
/* we warn the slave SQL thread */
1914
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1922
#endif /* HAVE_REPLICATION */
1926
some_non_temp_table_to_be_updated(thd, all_tables))
1928
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1931
#ifdef HAVE_REPLICATION
1935
res= mysql_multi_update(thd, all_tables,
1936
&select_lex->item_list,
1939
select_lex->options,
1940
lex->duplicates, lex->ignore, unit, select_lex);
1943
case SQLCOM_REPLACE:
1946
assert(first_table == all_tables && first_table != 0);
1947
if ((res= insert_precheck(thd, all_tables)))
1950
if (!thd->locked_tables &&
1951
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1957
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1958
lex->update_list, lex->value_list,
1959
lex->duplicates, lex->ignore);
1963
case SQLCOM_REPLACE_SELECT:
1964
case SQLCOM_INSERT_SELECT:
1966
select_result *sel_result;
1967
assert(first_table == all_tables && first_table != 0);
1968
if ((res= insert_precheck(thd, all_tables)))
1971
/* Fix lock for first table */
1972
if (first_table->lock_type == TL_WRITE_DELAYED)
1973
first_table->lock_type= TL_WRITE;
1975
/* Don't unlock tables until command is written to binary log */
1976
select_lex->options|= SELECT_NO_UNLOCK;
1978
unit->set_limit(select_lex);
1980
if (! thd->locked_tables &&
1981
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1987
if (!(res= open_and_lock_tables(thd, all_tables)))
1989
/* Skip first table, which is the table we are inserting in */
1990
TABLE_LIST *second_table= first_table->next_local;
1991
select_lex->table_list.first= (uchar*) second_table;
1992
select_lex->context.table_list=
1993
select_lex->context.first_name_resolution_table= second_table;
1994
res= mysql_insert_select_prepare(thd);
1995
if (!res && (sel_result= new select_insert(first_table,
2003
res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
2005
Invalidate the table in the query cache if something changed
2006
after unlocking when changes become visible.
2007
TODO: this is workaround. right way will be move invalidating in
2008
the unlock procedure.
2010
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
2013
/* INSERT ... SELECT should invalidate only the very first table */
2014
TABLE_LIST *save_table= first_table->next_local;
2015
first_table->next_local= 0;
2016
first_table->next_local= save_table;
2020
/* revert changes for SP */
2021
select_lex->table_list.first= (uchar*) first_table;
2026
case SQLCOM_TRUNCATE:
2027
if (end_active_trans(thd))
2032
assert(first_table == all_tables && first_table != 0);
2034
Don't allow this within a transaction because we want to use
2037
if (thd->locked_tables || thd->active_transaction())
2039
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2040
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2044
res= mysql_truncate(thd, first_table, 0);
2049
assert(first_table == all_tables && first_table != 0);
2050
assert(select_lex->offset_limit == 0);
2051
unit->set_limit(select_lex);
2053
if (!thd->locked_tables &&
2054
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2060
res = mysql_delete(thd, all_tables, select_lex->where,
2061
&select_lex->order_list,
2062
unit->select_limit_cnt, select_lex->options,
2066
case SQLCOM_DELETE_MULTI:
2068
assert(first_table == all_tables && first_table != 0);
2069
TABLE_LIST *aux_tables=
2070
(TABLE_LIST *)thd->lex->auxiliary_table_list.first;
2071
multi_delete *del_result;
2073
if (!thd->locked_tables &&
2074
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2080
if ((res= multi_delete_precheck(thd, all_tables)))
2083
/* condition will be true on SP re-excuting */
2084
if (select_lex->item_list.elements != 0)
2085
select_lex->item_list.empty();
2086
if (add_item_to_list(thd, new Item_null()))
2089
thd_proc_info(thd, "init");
2090
if ((res= open_and_lock_tables(thd, all_tables)))
2093
if ((res= mysql_multi_delete_prepare(thd)))
2096
if (!thd->is_fatal_error &&
2097
(del_result= new multi_delete(aux_tables, lex->table_count)))
2099
res= mysql_select(thd, &select_lex->ref_pointer_array,
2100
select_lex->get_table_list(),
2101
select_lex->with_wild,
2102
select_lex->item_list,
2104
0, (ORDER *)NULL, (ORDER *)NULL, (Item *)NULL,
2106
select_lex->options | thd->options |
2107
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
2108
OPTION_SETUP_TABLES_DONE,
2109
del_result, unit, select_lex);
2110
res|= thd->is_error();
2112
del_result->abort();
2119
case SQLCOM_DROP_TABLE:
2121
assert(first_table == all_tables && first_table != 0);
2122
if (!lex->drop_temporary)
2124
if (end_active_trans(thd))
2130
If this is a slave thread, we may sometimes execute some
2131
DROP / * 40005 TEMPORARY * / TABLE
2132
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
2133
MASTER TO), while the temporary table has already been dropped.
2134
To not generate such irrelevant "table does not exist errors",
2135
we silently add IF EXISTS if TEMPORARY was used.
2137
if (thd->slave_thread)
2138
lex->drop_if_exists= 1;
2140
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
2141
thd->options|= OPTION_KEEP_LOG;
2143
/* DDL and binlog write order protected by LOCK_open */
2144
res= mysql_rm_table(thd, first_table, lex->drop_if_exists,
2145
lex->drop_temporary);
2148
case SQLCOM_SHOW_PROCESSLIST:
2149
mysqld_list_processes(thd, NullS, lex->verbose);
2151
case SQLCOM_SHOW_ENGINE_LOGS:
2153
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
2156
case SQLCOM_CHANGE_DB:
2158
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2160
if (!mysql_change_db(thd, &db_str, false))
2168
assert(first_table == all_tables && first_table != 0);
2169
if (lex->local_file)
2171
if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
2174
my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2179
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
2180
lex->update_list, lex->value_list, lex->duplicates,
2181
lex->ignore, (bool) lex->local_file);
2185
case SQLCOM_SET_OPTION:
2187
List<set_var_base> *lex_var_list= &lex->var_list;
2189
if (lex->autocommit && end_active_trans(thd))
2192
if (open_and_lock_tables(thd, all_tables))
2194
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2196
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2199
if (!(res= sql_set_variables(thd, lex_var_list)))
2202
If the previous command was a SET ONE_SHOT, we don't want to forget
2203
about the ONE_SHOT property of that SET. So we use a |= instead of = .
2205
thd->one_shot_set|= lex->one_shot_set;
2211
We encountered some sort of error, but no message was sent.
2212
Send something semi-generic here since we don't know which
2213
assignment in the list caused the error.
2215
if (!thd->is_error())
2216
my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2223
case SQLCOM_UNLOCK_TABLES:
2225
It is critical for mysqldump --single-transaction --master-data that
2226
UNLOCK TABLES does not implicitely commit a connection which has only
2227
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2228
false, mysqldump will not work.
2230
unlock_locked_tables(thd);
2231
if (thd->options & OPTION_TABLE_LOCK)
2233
end_active_trans(thd);
2234
thd->options&= ~(OPTION_TABLE_LOCK);
2236
if (thd->global_read_lock)
2237
unlock_global_read_lock(thd);
2240
case SQLCOM_LOCK_TABLES:
2242
We try to take transactional locks if
2243
- only transactional locks are requested (lex->lock_transactional) and
2244
- no non-transactional locks exist (!thd->locked_tables).
2246
if (lex->lock_transactional && !thd->locked_tables)
2250
All requested locks are transactional and no non-transactional
2253
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2261
Non-transactional locking has been requested or
2262
non-transactional locks exist already or transactional locks are
2263
not supported by all storage engines. Take non-transactional
2268
One or more requested locks are non-transactional and/or
2269
non-transactional locks exist or a storage engine does not support
2270
transactional locks. Check if at least one transactional lock is
2271
requested. If yes, warn about the conversion to non-transactional
2272
locks or abort in strict mode.
2274
if (check_transactional_lock(thd, all_tables))
2276
unlock_locked_tables(thd);
2277
/* we must end the trasaction first, regardless of anything */
2278
if (end_active_trans(thd))
2280
thd->in_lock_tables=1;
2281
thd->options|= OPTION_TABLE_LOCK;
2283
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2285
thd->locked_tables=thd->lock;
2287
(void) set_handler_table_locks(thd, all_tables, false);
2293
Need to end the current transaction, so the storage engine (InnoDB)
2294
can free its locks if LOCK TABLES locked some tables before finding
2295
that it can't lock a table in its list
2297
ha_autocommit_or_rollback(thd, 1);
2298
end_active_trans(thd);
2299
thd->options&= ~(OPTION_TABLE_LOCK);
2301
thd->in_lock_tables=0;
2303
case SQLCOM_CREATE_DB:
2306
As mysql_create_db() may modify HA_CREATE_INFO structure passed to
2307
it, we need to use a copy of LEX::create_info to make execution
2308
prepared statement- safe.
2310
HA_CREATE_INFO create_info(lex->create_info);
2311
if (end_active_trans(thd))
2317
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2318
check_db_name(&lex->name))
2320
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2324
If in a slave thread :
2325
CREATE DATABASE DB was certainly not preceded by USE DB.
2326
For that reason, db_ok() in sql/slave.cc did not check the
2327
do_db/ignore_db. And as this query involves no tables, tables_ok()
2328
above was not called. So we have to check rules again here.
2330
if (thd->slave_thread &&
2331
(!rpl_filter->db_ok(lex->name.str) ||
2332
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2334
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2337
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2338
lex->name.str), &create_info, 0);
2341
case SQLCOM_DROP_DB:
2343
if (end_active_trans(thd))
2348
if (check_db_name(&lex->name))
2350
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2354
If in a slave thread :
2355
DROP DATABASE DB may not be preceded by USE DB.
2356
For that reason, maybe db_ok() in sql/slave.cc did not check the
2357
do_db/ignore_db. And as this query involves no tables, tables_ok()
2358
above was not called. So we have to check rules again here.
2360
#ifdef HAVE_REPLICATION
2361
if (thd->slave_thread &&
2362
(!rpl_filter->db_ok(lex->name.str) ||
2363
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2365
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2369
if (thd->locked_tables || thd->active_transaction())
2371
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2372
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2375
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2378
case SQLCOM_ALTER_DB_UPGRADE:
2380
LEX_STRING *db= & lex->name;
2381
if (end_active_trans(thd))
2386
#ifdef HAVE_REPLICATION
2387
if (thd->slave_thread &&
2388
(!rpl_filter->db_ok(db->str) ||
2389
!rpl_filter->db_ok_with_wild_table(db->str)))
2392
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2396
if (check_db_name(db))
2398
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2401
if (thd->locked_tables || thd->active_transaction())
2404
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2405
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2409
res= mysql_upgrade_db(thd, db);
2414
case SQLCOM_ALTER_DB:
2416
LEX_STRING *db= &lex->name;
2417
HA_CREATE_INFO create_info(lex->create_info);
2418
if (check_db_name(db))
2420
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2424
If in a slave thread :
2425
ALTER DATABASE DB may not be preceded by USE DB.
2426
For that reason, maybe db_ok() in sql/slave.cc did not check the
2427
do_db/ignore_db. And as this query involves no tables, tables_ok()
2428
above was not called. So we have to check rules again here.
2430
#ifdef HAVE_REPLICATION
2431
if (thd->slave_thread &&
2432
(!rpl_filter->db_ok(db->str) ||
2433
!rpl_filter->db_ok_with_wild_table(db->str)))
2435
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2439
if (thd->locked_tables || thd->active_transaction())
2441
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2442
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2445
res= mysql_alter_db(thd, db->str, &create_info);
2448
case SQLCOM_SHOW_CREATE_DB:
2450
if (check_db_name(&lex->name))
2452
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2455
res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
2460
RESET commands are never written to the binary log, so we have to
2461
initialize this variable because RESET shares the same code as FLUSH
2463
lex->no_write_to_binlog= 1;
2466
bool write_to_binlog;
2469
reload_cache() will tell us if we are allowed to write to the
2472
if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
2475
We WANT to write and we CAN write.
2476
! we write after unlocking the table.
2479
Presumably, RESET and binlog writing doesn't require synchronization
2481
if (!lex->no_write_to_binlog && write_to_binlog)
2483
write_bin_log(thd, false, thd->query, thd->query_length);
2492
Item *it= (Item *)lex->value_list.head();
2494
if (lex->table_or_sp_used())
2496
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2497
"function calls as part of this statement");
2501
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2503
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2507
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2511
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2513
my_error(ER_XAER_RMFAIL, MYF(0),
2514
xa_state_names[thd->transaction.xid_state.xa_state]);
2518
Breakpoints for backup testing.
2520
if (begin_trans(thd))
2525
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2526
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2530
case SQLCOM_ROLLBACK:
2531
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2532
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2536
case SQLCOM_RELEASE_SAVEPOINT:
2539
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2541
if (my_strnncoll(system_charset_info,
2542
(uchar *)lex->ident.str, lex->ident.length,
2543
(uchar *)sv->name, sv->length) == 0)
2548
if (ha_release_savepoint(thd, sv))
2549
res= true; // cannot happen
2552
thd->transaction.savepoints=sv->prev;
2555
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2558
case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2561
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2563
if (my_strnncoll(system_charset_info,
2564
(uchar *)lex->ident.str, lex->ident.length,
2565
(uchar *)sv->name, sv->length) == 0)
2570
if (ha_rollback_to_savepoint(thd, sv))
2571
res= true; // cannot happen
2574
if (((thd->options & OPTION_KEEP_LOG) ||
2575
thd->transaction.all.modified_non_trans_table) &&
2577
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2578
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2579
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2582
thd->transaction.savepoints=sv;
2585
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2588
case SQLCOM_SAVEPOINT:
2589
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2590
thd->in_sub_stmt) || !opt_using_transactions)
2594
SAVEPOINT **sv, *newsv;
2595
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2597
if (my_strnncoll(system_charset_info,
2598
(uchar *)lex->ident.str, lex->ident.length,
2599
(uchar *)(*sv)->name, (*sv)->length) == 0)
2602
if (*sv) /* old savepoint of the same name exists */
2605
ha_release_savepoint(thd, *sv); // it cannot fail
2608
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2609
savepoint_alloc_size)) == 0)
2611
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2614
newsv->name=strmake_root(&thd->transaction.mem_root,
2615
lex->ident.str, lex->ident.length);
2616
newsv->length=lex->ident.length;
2618
if we'll get an error here, don't add new savepoint to the list.
2619
we'll lose a little bit of memory in transaction mem_root, but it'll
2620
be free'd when transaction ends anyway
2622
if (ha_savepoint(thd, newsv))
2626
newsv->prev=thd->transaction.savepoints;
2627
thd->transaction.savepoints=newsv;
2632
case SQLCOM_BINLOG_BASE64_EVENT:
2634
mysql_client_binlog_statement(thd);
2638
assert(0); /* Impossible */
2642
thd_proc_info(thd, "query end");
2645
Binlog-related cleanup:
2646
Reset system variables temporarily modified by SET ONE SHOT.
2648
Exception: If this is a SET, do nothing. This is to allow
2649
mysqlbinlog to print many SET commands (in this case we want the
2650
charset temp setting to live until the real query). This is also
2651
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2654
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2655
reset_one_shot_variables(thd);
508
2658
The return value for ROW_COUNT() is "implementation dependent" if the
509
2659
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
510
2660
wants. We also keep the last value in case of SQLCOM_CALL or
513
if (! (sql_command_flags[session->getLex()->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
2663
if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2664
thd->row_count_func= -1;
2672
if (need_start_waiting)
515
session->row_count_func= -1;
2675
Release the protection against the global read lock and wake
2676
everyone, who might want to set a global read lock.
2678
start_waiting_global_read_lock(thd);
518
return (res || session->is_error());
2680
return(res || thd->is_error());
520
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2684
static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
522
LEX *lex= session->getLex();
523
2687
select_result *result=lex->result;
525
2689
/* assign global limit variable if limit is not given */
527
Select_Lex *param= lex->unit.global_parameters;
2691
SELECT_LEX *param= lex->unit.global_parameters;
528
2692
if (!param->explicit_limit)
529
2693
param->select_limit=
530
new Item_int((uint64_t) session->variables.select_limit);
534
&& ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
535
&& ! session->inTransaction()
536
&& ! lex->statement->isShow())
538
if (session->startTransaction() == false)
540
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
545
if (not (res= session->openTablesLock(all_tables)))
2694
new Item_int((uint64_t) thd->variables.select_limit);
2696
if (!(res= open_and_lock_tables(thd, all_tables)))
547
2698
if (lex->describe)