138
178
sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA;
140
180
sql_command_flags[SQLCOM_UPDATE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
181
sql_command_flags[SQLCOM_UPDATE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
141
182
sql_command_flags[SQLCOM_INSERT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
142
183
sql_command_flags[SQLCOM_INSERT_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
143
184
sql_command_flags[SQLCOM_DELETE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
185
sql_command_flags[SQLCOM_DELETE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
144
186
sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
145
187
sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
189
sql_command_flags[SQLCOM_SHOW_STATUS]= CF_STATUS_COMMAND;
190
sql_command_flags[SQLCOM_SHOW_DATABASES]= CF_STATUS_COMMAND;
191
sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND;
192
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
193
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
194
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
195
sql_command_flags[SQLCOM_SHOW_CHARSETS]= CF_STATUS_COMMAND;
196
sql_command_flags[SQLCOM_SHOW_COLLATIONS]= CF_STATUS_COMMAND;
197
sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
198
sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
199
sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
147
200
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
148
201
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
202
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
203
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
149
204
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
150
205
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
206
sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
207
sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
209
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
210
CF_SHOW_TABLE_COMMAND);
211
sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
212
CF_SHOW_TABLE_COMMAND);
153
214
The following admin table operations are allowed
217
sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND;
218
sql_command_flags[SQLCOM_OPTIMIZE]= CF_WRITE_LOGS_COMMAND;
156
219
sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND;
223
bool is_update_query(enum enum_sql_command command)
225
assert(command >= 0 && command <= SQLCOM_END);
226
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
229
void execute_init_command(THD *thd, sys_var_str *init_command_var,
230
rw_lock_t *var_mutex)
233
ulong save_client_capabilities;
235
thd_proc_info(thd, "Execution of init_command");
237
We need to lock init_command_var because
238
during execution of init_command_var query
239
values of init_command_var can't be changed
241
rw_rdlock(var_mutex);
242
save_client_capabilities= thd->client_capabilities;
243
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
245
We don't need return result of execution to client side.
246
To forbid this we should set thd->net.vio to 0.
248
save_vio= thd->net.vio;
250
dispatch_command(COM_QUERY, thd,
251
init_command_var->value,
252
init_command_var->value_length);
253
rw_unlock(var_mutex);
254
thd->client_capabilities= save_client_capabilities;
255
thd->net.vio= save_vio;
259
Ends the current transaction and (maybe) begin the next.
261
@param thd Current thread
262
@param completion Completion type
268
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
273
if (unlikely(thd->in_sub_stmt))
275
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
278
if (thd->transaction.xid_state.xa_state != XA_NOTR)
280
my_error(ER_XAER_RMFAIL, MYF(0),
281
xa_state_names[thd->transaction.xid_state.xa_state]);
284
switch (completion) {
287
We don't use end_active_trans() here to ensure that this works
288
even if there is a problem with the OPTION_AUTO_COMMIT flag
289
(Which of course should never happen...)
291
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
293
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
294
thd->transaction.all.modified_non_trans_table= false;
297
do_release= 1; /* fall through */
298
case COMMIT_AND_CHAIN:
299
res= end_active_trans(thd);
300
if (!res && completion == COMMIT_AND_CHAIN)
301
res= begin_trans(thd);
303
case ROLLBACK_RELEASE:
304
do_release= 1; /* fall through */
306
case ROLLBACK_AND_CHAIN:
308
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
309
if (ha_rollback(thd))
311
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
312
thd->transaction.all.modified_non_trans_table= false;
313
if (!res && (completion == ROLLBACK_AND_CHAIN))
314
res= begin_trans(thd);
319
my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
324
my_error(thd->killed_errno(), MYF(0));
325
else if ((res == 0) && do_release)
326
thd->killed= THD::KILL_CONNECTION;
333
Read one command from connection and execute it (query or simple command).
334
This function is called in loop from thread function.
336
For profiling to work, it must never be called recursively.
341
1 request of thread shutdown (see dispatch_command() description)
344
bool do_command(THD *thd)
350
enum enum_server_command command;
353
indicator of uninitialized lex => normal flow of errors handling
356
thd->lex->current_select= 0;
359
This thread will do a blocking read from the client which
360
will be interrupted when the next command is received from
361
the client, the connection is closed or "net_wait_timeout"
362
number of seconds has passed
364
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
367
XXX: this code is here only to clear possible errors of init_connect.
368
Consider moving to init_connect() instead.
370
thd->clear_error(); // Clear error message
371
thd->main_da.reset_diagnostics_area();
373
net_new_transaction(net);
375
packet_length= my_net_read(net);
376
if (packet_length == packet_error)
378
/* Check if we can continue without closing the connection */
380
/* The error must be set. */
381
assert(thd->is_error());
382
net_end_statement(thd);
386
return_value= true; // We have to close it.
395
packet= (char*) net->read_pos;
397
'packet_length' contains length of data, as it was stored in packet
398
header. In case of malformed header, my_net_read returns zero.
399
If packet_length is not zero, my_net_read ensures that the returned
400
number of bytes was actually read from network.
401
There is also an extra safety measure in my_net_read:
402
it sets packet[packet_length]= 0, but only for non-zero packets.
404
if (packet_length == 0) /* safety */
406
/* Initialize with COM_SLEEP packet */
407
packet[0]= (uchar) COM_SLEEP;
410
/* Do not rely on my_net_read, extra safety against programming errors. */
411
packet[packet_length]= '\0'; /* safety */
413
command= (enum enum_server_command) (uchar) packet[0];
415
if (command >= COM_END)
416
command= COM_END; // Wrong command
418
/* Restore read timeout value */
419
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
421
assert(packet_length);
422
return_value= dispatch_command(command, thd, packet+1, (uint) (packet_length-1));
425
return(return_value);
429
Determine if an attempt to update a non-temporary table while the
430
read-only option was enabled has been made.
432
This is a helper function to mysql_execute_command.
434
@note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
436
@see mysql_execute_command
439
@retval true The statement should be denied.
440
@retval false The statement isn't updating any relevant tables.
443
static bool deny_updates_if_read_only_option(THD *thd,
444
TableList *all_tables)
451
if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
454
/* Multi update is an exception and is dealt with later. */
455
if (lex->sql_command == SQLCOM_UPDATE_MULTI)
458
const bool create_temp_tables=
459
(lex->sql_command == SQLCOM_CREATE_TABLE) &&
460
(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
462
const bool drop_temp_tables=
463
(lex->sql_command == SQLCOM_DROP_TABLE) &&
466
const bool update_real_tables=
467
some_non_temp_table_to_be_updated(thd, all_tables) &&
468
!(create_temp_tables || drop_temp_tables);
471
const bool create_or_drop_databases=
472
(lex->sql_command == SQLCOM_CREATE_DB) ||
473
(lex->sql_command == SQLCOM_DROP_DB);
475
if (update_real_tables || create_or_drop_databases)
478
An attempt was made to modify one or more non-temporary tables.
484
/* Assuming that only temporary tables are modified. */
160
489
Perform one connection-level (COM_XXXX) command.
162
491
@param command type of command to perform
163
@param session connection handle
492
@param thd connection handle
164
493
@param packet data for the command, packet is always null-terminated
165
494
@param packet_length length of packet + 1 (to show that data is
166
495
null-terminated) except for COM_SLEEP, where it
170
set session->lex->sql_command to SQLCOM_END here.
499
set thd->lex->sql_command to SQLCOM_END here.
172
501
The following has to be changed to an 8 byte integer
197
530
/* Increase id and count all other statements. */
199
session->status_var.questions++;
203
/* @todo set session->lex->sql_command to SQLCOM_END here */
205
plugin::Logging::preDo(session);
206
if (unlikely(plugin::EventObserver::beforeStatement(*session)))
208
// We should do something about an error...
211
session->server_status&=
532
statistic_increment(thd->status_var.questions, &LOCK_status);
537
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
538
VOID(pthread_mutex_unlock(&LOCK_thread_count));
212
541
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
213
542
switch (command) {
214
543
case COM_INIT_DB:
216
if (packet_length == 0)
218
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
222
string tmp(packet, packet_length);
224
identifier::Schema identifier(tmp);
226
if (not change_db(session, identifier))
546
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
547
thd->convert_string(&tmp, system_charset_info,
548
packet, packet_length, thd->charset());
549
if (!mysql_change_db(thd, &tmp, false))
551
general_log_write(thd, command, thd->db, thd->db_length);
556
case COM_REGISTER_SLAVE:
558
if (!register_slave(thd, (uchar*)packet, packet_length))
562
case COM_CHANGE_USER:
564
status_var_increment(thd->status_var.com_other);
565
char *user= (char*) packet, *packet_end= packet + packet_length;
566
/* Safe because there is always a trailing \0 at the end of the packet */
567
char *passwd= strchr(user, '\0')+1;
570
thd->clear_error(); // if errors from rollback
573
Old clients send null-terminated string ('\0' for empty string) for
574
password. New clients send the size (1 byte) + string (not null
575
terminated, so also '\0' for empty string).
577
Cast *passwd to an unsigned char, so that it doesn't extend the sign
578
for *passwd > 127 and become 2**32-127 after casting to uint.
580
char db_buff[NAME_LEN+1]; // buffer to store db in utf8
584
If there is no password supplied, the packet must contain '\0',
585
in any type of handshake (4.1 or pre-4.1).
587
if (passwd >= packet_end)
589
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
592
uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
593
(uchar)(*passwd++) : strlen(passwd));
594
uint dummy_errors, save_db_length, db_length;
596
Security_context save_security_ctx= *thd->security_ctx;
597
USER_CONN *save_user_connect;
601
Database name is always NUL-terminated, so in case of empty database
602
the packet must contain at least the trailing '\0'.
604
if (db >= packet_end)
606
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
609
db_length= strlen(db);
611
char *ptr= db + db_length + 1;
614
if (ptr < packet_end)
616
if (ptr + 2 > packet_end)
618
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
622
cs_number= uint2korr(ptr);
625
/* Convert database name to utf8 */
626
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
627
system_charset_info, db, db_length,
628
thd->charset(), &dummy_errors)]= 0;
631
/* Save user and privileges */
632
save_db_length= thd->db_length;
634
save_user_connect= thd->user_connect;
636
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
638
thd->security_ctx->user= save_security_ctx.user;
639
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
643
/* Clear variables that are allocated */
644
thd->user_connect= 0;
645
res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
649
x_free(thd->security_ctx->user);
650
*thd->security_ctx= save_security_ctx;
651
thd->user_connect= save_user_connect;
653
thd->db_length= save_db_length;
658
x_free(save_security_ctx.user);
662
thd_init_client_charset(thd, cs_number);
663
thd->update_charset();
234
if (not session->readAndStoreQuery(packet, packet_length))
670
if (alloc_query(thd, packet, packet_length))
235
671
break; // fatal error is set
236
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
238
const_cast<const char *>(session->schema()->c_str()));
240
parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
672
char *packet_end= thd->query + thd->query_length;
673
const char* end_of_stmt= NULL;
675
general_log_write(thd, command, thd->query, thd->query_length);
677
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
679
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
681
char *beginning_of_next_stmt= (char*) end_of_stmt;
683
net_end_statement(thd);
685
Multiple queries exits, execute them individually
687
close_thread_tables(thd);
688
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
690
log_slow_statement(thd);
692
/* Remove garbage at start of query */
693
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
695
beginning_of_next_stmt++;
699
VOID(pthread_mutex_lock(&LOCK_thread_count));
700
thd->query_length= length;
701
thd->query= beginning_of_next_stmt;
703
Count each statement from the client.
705
statistic_increment(thd->status_var.questions, &LOCK_status);
706
thd->query_id= next_query_id();
707
thd->set_time(); /* Reset the query start time. */
708
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
709
VOID(pthread_mutex_unlock(&LOCK_thread_count));
711
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
715
case COM_FIELD_LIST: // This isn't actually needed
717
char *fields, *packet_end= packet + packet_length, *arg_end;
718
/* Locked closure of all tables */
719
TableList table_list;
720
LEX_STRING conv_name;
722
/* used as fields initializator */
725
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
726
memset(&table_list, 0, sizeof(table_list));
727
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
730
We have name + wildcard in packet, separated by endzero
732
arg_end= strchr(packet, '\0');
733
thd->convert_string(&conv_name, system_charset_info,
734
packet, (uint) (arg_end - packet), thd->charset());
735
table_list.alias= table_list.table_name= conv_name.str;
738
if (!my_strcasecmp(system_charset_info, table_list.db,
739
INFORMATION_SCHEMA_NAME.str))
741
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
743
table_list.schema_table= schema_table;
746
thd->query_length= (uint) (packet_end - packet); // Don't count end \0
747
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
749
general_log_print(thd, command, "%s %s", table_list.table_name, fields);
750
if (lower_case_table_names)
751
my_casedn_str(files_charset_info, table_list.table_name);
753
/* init structures for VIEW processing */
754
table_list.select_lex= &(thd->lex->select_lex);
757
mysql_reset_thd_for_next_command(thd);
760
select_lex.table_list.link_in_list((uchar*) &table_list,
761
(uchar**) &table_list.next_local);
762
thd->lex->add_to_query_tables(&table_list);
764
/* switch on VIEW optimisation: do not fill temporary tables */
765
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
766
mysqld_list_fields(thd,&table_list,fields);
767
thd->lex->unit.cleanup();
768
thd->cleanup_after_query();
245
772
/* We don't calculate statistics for this command */
246
session->main_da.disable_status(); // Don't send anything back
773
general_log_print(thd, command, NullS);
774
net->error=0; // Don't give 'abort' message
775
thd->main_da.disable_status(); // Don't send anything back
247
776
error=true; // End server
778
case COM_BINLOG_DUMP:
782
uint32_t slave_server_id;
784
status_var_increment(thd->status_var.com_other);
785
thd->enable_slow_log= opt_log_slow_admin_statements;
786
/* TODO: The following has to be changed to an 8 byte integer */
787
pos = uint4korr(packet);
788
flags = uint2korr(packet + 4);
789
thd->server_id=0; /* avoid suicide */
790
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
791
kill_zombie_dump_threads(slave_server_id);
792
thd->server_id = slave_server_id;
794
general_log_print(thd, command, "Log: '%s' Pos: %ld", packet+10,
796
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
797
unregister_slave(thd,1,1);
798
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
249
802
case COM_SHUTDOWN:
251
session->status_var.com_other++;
253
session->close_thread_tables(); // Free before kill
804
status_var_increment(thd->status_var.com_other);
806
If the client is < 4.1.3, it is going to send us no argument; then
807
packet_length is 0, packet[0] is the end 0 of the packet. Note that
808
SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
811
enum drizzle_enum_shutdown_level level=
812
(enum drizzle_enum_shutdown_level) (uchar) packet[0];
813
if (level == SHUTDOWN_DEFAULT)
814
level= SHUTDOWN_WAIT_ALL_BUFFERS; // soon default will be configurable
815
else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
817
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "this shutdown level");
820
general_log_print(thd, command, NullS);
822
close_thread_tables(thd); // Free before kill
259
session->status_var.com_other++;
260
session->my_ok(); // Tell client we are alive
828
status_var_increment(thd->status_var.com_other);
829
my_ok(thd); // Tell client we are alive
831
case COM_PROCESS_INFO:
832
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
833
general_log_print(thd, command, NullS);
834
mysqld_list_processes(thd, NullS, 0);
836
case COM_PROCESS_KILL:
838
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
839
ulong id=(ulong) uint4korr(packet);
840
sql_kill(thd,id,false);
845
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
846
uint opt_command= uint2korr(packet);
848
switch (opt_command) {
849
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
850
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
853
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
854
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
858
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
263
864
case COM_CONNECT: // Impossible here
865
case COM_TIME: // Impossible from client
266
868
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
481
1183
variables, but for now this is probably good enough.
482
1184
Don't reset warnings when executing a stored routine.
484
if (all_tables || ! lex->is_single_level_stmt())
486
drizzle_reset_errors(session, 0);
489
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
491
if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
492
&& ! session->inTransaction()
493
&& lex->statement->isTransactional())
495
if (session->startTransaction() == false)
497
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
502
/* now we are ready to execute the statement */
503
res= lex->statement->execute();
504
session->set_proc_info("query end");
1186
if (all_tables || !lex->is_single_level_stmt())
1187
drizzle_reset_errors(thd, 0);
1189
if (unlikely(thd->slave_thread))
1192
Check if statment should be skipped because of slave filtering
1196
- UPDATE MULTI: For this statement, we want to check the filtering
1197
rules later in the code
1198
- SET: we always execute it (Not that many SET commands exists in
1199
the binary log anyway -- only 4.1 masters write SET statements,
1200
in 5.0 there are no SET statements in the binary log)
1201
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1202
have stale files on slave caused by exclusion of one tmp table).
1204
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1205
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1206
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1207
lex->drop_temporary && lex->drop_if_exists) &&
1208
all_tables_not_ok(thd, all_tables))
1210
/* we warn the slave SQL thread */
1211
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1212
if (thd->one_shot_set)
1215
It's ok to check thd->one_shot_set here:
1217
The charsets in a MySQL 5.0 slave can change by both a binlogged
1218
SET ONE_SHOT statement and the event-internal charset setting,
1219
and these two ways to change charsets do not seems to work
1222
At least there seems to be problems in the rli cache for
1223
charsets if we are using ONE_SHOT. Note that this is normally no
1224
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1225
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1227
reset_one_shot_variables(thd);
1235
When option readonly is set deny operations which change non-temporary
1236
tables. Except for the replication thread and the 'super' users.
1238
if (deny_updates_if_read_only_option(thd, all_tables))
1240
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1243
} /* endif unlikely slave */
1244
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1246
assert(thd->transaction.stmt.modified_non_trans_table == false);
1248
switch (lex->sql_command) {
1249
case SQLCOM_SHOW_STATUS:
1251
system_status_var old_status_var= thd->status_var;
1252
thd->initial_status_var= &old_status_var;
1253
res= execute_sqlcom_select(thd, all_tables);
1254
/* Don't log SHOW STATUS commands to slow query log */
1255
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1256
SERVER_QUERY_NO_GOOD_INDEX_USED);
1258
restore status variables, as we don't want 'show status' to cause
1261
pthread_mutex_lock(&LOCK_status);
1262
add_diff_to_status(&global_status_var, &thd->status_var,
1264
thd->status_var= old_status_var;
1265
pthread_mutex_unlock(&LOCK_status);
1268
case SQLCOM_SHOW_DATABASES:
1269
case SQLCOM_SHOW_TABLES:
1270
case SQLCOM_SHOW_TABLE_STATUS:
1271
case SQLCOM_SHOW_OPEN_TABLES:
1272
case SQLCOM_SHOW_FIELDS:
1273
case SQLCOM_SHOW_KEYS:
1274
case SQLCOM_SHOW_VARIABLES:
1275
case SQLCOM_SHOW_CHARSETS:
1276
case SQLCOM_SHOW_COLLATIONS:
1279
thd->status_var.last_query_cost= 0.0;
1280
res= execute_sqlcom_select(thd, all_tables);
1283
case SQLCOM_EMPTY_QUERY:
1289
res = purge_master_logs(thd, lex->to_log);
1292
case SQLCOM_PURGE_BEFORE:
1296
/* PURGE MASTER LOGS BEFORE 'data' */
1297
it= (Item *)lex->value_list.head();
1298
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1301
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1304
it= new Item_func_unix_timestamp(it);
1306
it is OK only emulate fix_fieds, because we need only
1309
it->quick_fix_field();
1310
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1313
case SQLCOM_SHOW_WARNS:
1315
res= mysqld_show_warnings(thd, (uint32_t)
1316
((1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1317
(1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1318
(1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1322
case SQLCOM_SHOW_ERRORS:
1324
res= mysqld_show_warnings(thd, (uint32_t)
1325
(1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1328
case SQLCOM_SHOW_SLAVE_HOSTS:
1330
res = show_slave_hosts(thd);
1333
case SQLCOM_SHOW_BINLOG_EVENTS:
1335
res = mysql_show_binlog_events(thd);
1339
case SQLCOM_ASSIGN_TO_KEYCACHE:
1341
assert(first_table == all_tables && first_table != 0);
1342
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1345
case SQLCOM_CHANGE_MASTER:
1347
pthread_mutex_lock(&LOCK_active_mi);
1348
res = change_master(thd,active_mi);
1349
pthread_mutex_unlock(&LOCK_active_mi);
1352
case SQLCOM_SHOW_SLAVE_STAT:
1354
pthread_mutex_lock(&LOCK_active_mi);
1355
if (active_mi != NULL)
1357
res = show_master_info(thd, active_mi);
1361
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1362
"the master info structure does not exist");
1365
pthread_mutex_unlock(&LOCK_active_mi);
1368
case SQLCOM_SHOW_MASTER_STAT:
1370
res = show_binlog_info(thd);
1374
case SQLCOM_SHOW_ENGINE_STATUS:
1376
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1379
case SQLCOM_CREATE_TABLE:
1381
/* If CREATE TABLE of non-temporary table, do implicit commit */
1382
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1384
if (end_active_trans(thd))
1390
assert(first_table == all_tables && first_table != 0);
1392
// Skip first table, which is the table we are creating
1393
TableList *create_table= lex->unlink_first_table(&link_to_local);
1394
TableList *select_tables= lex->query_tables;
1396
Code below (especially in mysql_create_table() and select_create
1397
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1398
use a copy of this structure to make execution prepared statement-
1399
safe. A shallow copy is enough as this code won't modify any memory
1400
referenced from this structure.
1402
HA_CREATE_INFO create_info(lex->create_info);
1404
We need to copy alter_info for the same reasons of re-execution
1405
safety, only in case of Alter_info we have to do (almost) a deep
1408
Alter_info alter_info(lex->alter_info, thd->mem_root);
1410
if (thd->is_fatal_error)
1412
/* If out of memory when creating a copy of alter_info. */
1414
goto end_with_restore_list;
1417
if ((res= create_table_precheck(thd, select_tables, create_table)))
1418
goto end_with_restore_list;
1420
/* Might have been updated in create_table_precheck */
1421
create_info.alias= create_table->alias;
1423
#ifdef HAVE_READLINK
1424
/* Fix names if symlinked tables */
1425
if (append_file_to_dir(thd, &create_info.data_file_name,
1426
create_table->table_name) ||
1427
append_file_to_dir(thd, &create_info.index_file_name,
1428
create_table->table_name))
1429
goto end_with_restore_list;
1432
If we are using SET CHARSET without DEFAULT, add an implicit
1433
DEFAULT to not confuse old users. (This may change).
1435
if ((create_info.used_fields &
1436
(HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
1437
HA_CREATE_USED_CHARSET)
1439
create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
1440
create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
1441
create_info.default_table_charset= create_info.table_charset;
1442
create_info.table_charset= 0;
1445
The create-select command will open and read-lock the select table
1446
and then create, open and write-lock the new table. If a global
1447
read lock steps in, we get a deadlock. The write lock waits for
1448
the global read lock, while the global read lock waits for the
1449
select table to be closed. So we wait until the global readlock is
1450
gone before starting both steps. Note that
1451
wait_if_global_read_lock() sets a protection against a new global
1452
read lock when it succeeds. This needs to be released by
1453
start_waiting_global_read_lock(). We protect the normal CREATE
1454
TABLE in the same way. That way we avoid that a new table is
1455
created during a gobal read lock.
1457
if (!thd->locked_tables &&
1458
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1461
goto end_with_restore_list;
1463
if (select_lex->item_list.elements) // With select
1465
select_result *result;
1467
select_lex->options|= SELECT_NO_UNLOCK;
1468
unit->set_limit(select_lex);
1470
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1472
lex->link_first_table_back(create_table, link_to_local);
1473
create_table->create= true;
1476
if (!(res= open_and_lock_tables(thd, lex->query_tables)))
1479
Is table which we are changing used somewhere in other parts
1482
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1484
TableList *duplicate;
1485
create_table= lex->unlink_first_table(&link_to_local);
1486
if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1488
update_non_unique_table_error(create_table, "CREATE", duplicate);
1490
goto end_with_restore_list;
1495
select_create is currently not re-execution friendly and
1496
needs to be created for every execution of a PS/SP.
1498
if ((result= new select_create(create_table,
1501
select_lex->item_list,
1507
CREATE from SELECT give its SELECT_LEX for SELECT,
1508
and item_list belong to SELECT
1510
res= handle_select(thd, lex, result, 0);
1514
else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1515
create_table= lex->unlink_first_table(&link_to_local);
1520
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1521
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1522
thd->options|= OPTION_KEEP_LOG;
1523
/* regular create */
1524
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1525
res= mysql_create_like_table(thd, create_table, select_tables,
1529
res= mysql_create_table(thd, create_table->db,
1530
create_table->table_name, &create_info,
1537
/* put tables back for PS rexecuting */
1538
end_with_restore_list:
1539
lex->link_first_table_back(create_table, link_to_local);
1542
case SQLCOM_CREATE_INDEX:
1544
case SQLCOM_DROP_INDEX:
1546
CREATE INDEX and DROP INDEX are implemented by calling ALTER
1547
TABLE with proper arguments.
1549
In the future ALTER TABLE will notice that the request is to
1550
only add indexes and create these one by one for the existing
1551
table without having to do a full rebuild.
1554
/* Prepare stack copies to be re-execution safe */
1555
HA_CREATE_INFO create_info;
1556
Alter_info alter_info(lex->alter_info, thd->mem_root);
1558
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1561
assert(first_table == all_tables && first_table != 0);
1562
if (end_active_trans(thd))
1565
Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1566
and thus classify as slow administrative statements just like
1569
thd->enable_slow_log= opt_log_slow_admin_statements;
1571
memset(&create_info, 0, sizeof(create_info));
1572
create_info.db_type= 0;
1573
create_info.row_type= ROW_TYPE_NOT_USED;
1574
create_info.default_table_charset= thd->variables.collation_database;
1576
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1577
&create_info, first_table, &alter_info,
1578
0, (order_st*) 0, 0);
1581
case SQLCOM_SLAVE_START:
1583
pthread_mutex_lock(&LOCK_active_mi);
1584
start_slave(thd,active_mi,1 /* net report*/);
1585
pthread_mutex_unlock(&LOCK_active_mi);
1588
case SQLCOM_SLAVE_STOP:
1590
If the client thread has locked tables, a deadlock is possible.
1592
- the client thread does LOCK TABLE t READ.
1593
- then the master updates t.
1594
- then the SQL slave thread wants to update t,
1595
so it waits for the client thread because t is locked by it.
1596
- then the client thread does SLAVE STOP.
1597
SLAVE STOP waits for the SQL slave thread to terminate its
1598
update t, which waits for the client thread because t is locked by it.
1599
To prevent that, refuse SLAVE STOP if the
1600
client thread has locked tables
1602
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1604
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1605
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1609
pthread_mutex_lock(&LOCK_active_mi);
1610
stop_slave(thd,active_mi,1/* net report*/);
1611
pthread_mutex_unlock(&LOCK_active_mi);
1615
case SQLCOM_ALTER_TABLE:
1616
assert(first_table == all_tables && first_table != 0);
1619
Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
1620
so we have to use a copy of this structure to make execution
1621
prepared statement- safe. A shallow copy is enough as no memory
1622
referenced from this structure will be modified.
1624
HA_CREATE_INFO create_info(lex->create_info);
1625
Alter_info alter_info(lex->alter_info, thd->mem_root);
1627
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1632
/* Must be set in the parser */
1633
assert(select_lex->db);
1635
{ // Rename of table
1636
TableList tmp_table;
1637
memset(&tmp_table, 0, sizeof(tmp_table));
1638
tmp_table.table_name= lex->name.str;
1639
tmp_table.db=select_lex->db;
1642
/* Don't yet allow changing of symlinks with ALTER TABLE */
1643
if (create_info.data_file_name)
1644
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1645
"DATA DIRECTORY option ignored");
1646
if (create_info.index_file_name)
1647
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1648
"INDEX DIRECTORY option ignored");
1649
create_info.data_file_name= create_info.index_file_name= NULL;
1650
/* ALTER TABLE ends previous transaction */
1651
if (end_active_trans(thd))
1654
if (!thd->locked_tables &&
1655
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1661
thd->enable_slow_log= opt_log_slow_admin_statements;
1662
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1666
select_lex->order_list.elements,
1667
(order_st *) select_lex->order_list.first,
1671
case SQLCOM_RENAME_TABLE:
1673
assert(first_table == all_tables && first_table != 0);
1675
for (table= first_table; table; table= table->next_local->next_local)
1677
TableList old_list, new_list;
1679
we do not need initialize old_list and new_list because we will
1680
come table[0] and table->next[0] there
1683
new_list= table->next_local[0];
1686
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1692
case SQLCOM_SHOW_BINLOGS:
1694
res = show_binlogs(thd);
1697
case SQLCOM_SHOW_CREATE:
1698
assert(first_table == all_tables && first_table != 0);
1700
res= mysqld_show_create(thd, first_table);
1703
case SQLCOM_CHECKSUM:
1705
assert(first_table == all_tables && first_table != 0);
1706
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1711
assert(first_table == all_tables && first_table != 0);
1712
thd->enable_slow_log= opt_log_slow_admin_statements;
1713
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1714
/* ! we write after unlocking the table */
1716
Presumably, REPAIR and binlog writing doesn't require synchronization
1718
write_bin_log(thd, true, thd->query, thd->query_length);
1719
select_lex->table_list.first= (uchar*) first_table;
1720
lex->query_tables=all_tables;
1725
assert(first_table == all_tables && first_table != 0);
1726
thd->enable_slow_log= opt_log_slow_admin_statements;
1727
res = mysql_check_table(thd, first_table, &lex->check_opt);
1728
select_lex->table_list.first= (uchar*) first_table;
1729
lex->query_tables=all_tables;
1732
case SQLCOM_ANALYZE:
1734
assert(first_table == all_tables && first_table != 0);
1735
thd->enable_slow_log= opt_log_slow_admin_statements;
1736
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1737
/* ! we write after unlocking the table */
1738
write_bin_log(thd, true, thd->query, thd->query_length);
1739
select_lex->table_list.first= (uchar*) first_table;
1740
lex->query_tables=all_tables;
1744
case SQLCOM_OPTIMIZE:
1746
assert(first_table == all_tables && first_table != 0);
1747
thd->enable_slow_log= opt_log_slow_admin_statements;
1748
res= mysql_optimize_table(thd, first_table, &lex->check_opt);
1749
/* ! we write after unlocking the table */
1750
write_bin_log(thd, true, thd->query, thd->query_length);
1751
select_lex->table_list.first= (uchar*) first_table;
1752
lex->query_tables=all_tables;
1756
assert(first_table == all_tables && first_table != 0);
1757
if (update_precheck(thd, all_tables))
1759
assert(select_lex->offset_limit == 0);
1760
unit->set_limit(select_lex);
1761
res= (up_result= mysql_update(thd, all_tables,
1762
select_lex->item_list,
1765
select_lex->order_list.elements,
1766
(order_st *) select_lex->order_list.first,
1767
unit->select_limit_cnt,
1768
lex->duplicates, lex->ignore));
1769
/* mysql_update return 2 if we need to switch to multi-update */
1773
case SQLCOM_UPDATE_MULTI:
1775
assert(first_table == all_tables && first_table != 0);
1776
/* if we switched from normal update, rights are checked */
1779
if ((res= multi_update_precheck(thd, all_tables)))
1785
res= mysql_multi_update_prepare(thd);
1787
/* Check slave filtering rules */
1788
if (unlikely(thd->slave_thread))
1790
if (all_tables_not_ok(thd, all_tables))
1794
res= 0; /* don't care of prev failure */
1795
thd->clear_error(); /* filters are of highest prior */
1797
/* we warn the slave SQL thread */
1798
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1809
some_non_temp_table_to_be_updated(thd, all_tables))
1811
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1816
res= mysql_multi_update(thd, all_tables,
1817
&select_lex->item_list,
1820
select_lex->options,
1821
lex->duplicates, lex->ignore, unit, select_lex);
1824
case SQLCOM_REPLACE:
1827
assert(first_table == all_tables && first_table != 0);
1828
if ((res= insert_precheck(thd, all_tables)))
1831
if (!thd->locked_tables &&
1832
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1838
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1839
lex->update_list, lex->value_list,
1840
lex->duplicates, lex->ignore);
1844
case SQLCOM_REPLACE_SELECT:
1845
case SQLCOM_INSERT_SELECT:
1847
select_result *sel_result;
1848
assert(first_table == all_tables && first_table != 0);
1849
if ((res= insert_precheck(thd, all_tables)))
1852
/* Fix lock for first table */
1853
if (first_table->lock_type == TL_WRITE_DELAYED)
1854
first_table->lock_type= TL_WRITE;
1856
/* Don't unlock tables until command is written to binary log */
1857
select_lex->options|= SELECT_NO_UNLOCK;
1859
unit->set_limit(select_lex);
1861
if (! thd->locked_tables &&
1862
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1868
if (!(res= open_and_lock_tables(thd, all_tables)))
1870
/* Skip first table, which is the table we are inserting in */
1871
TableList *second_table= first_table->next_local;
1872
select_lex->table_list.first= (uchar*) second_table;
1873
select_lex->context.table_list=
1874
select_lex->context.first_name_resolution_table= second_table;
1875
res= mysql_insert_select_prepare(thd);
1876
if (!res && (sel_result= new select_insert(first_table,
1884
res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1886
Invalidate the table in the query cache if something changed
1887
after unlocking when changes become visible.
1888
TODO: this is workaround. right way will be move invalidating in
1889
the unlock procedure.
1891
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
1894
/* INSERT ... SELECT should invalidate only the very first table */
1895
TableList *save_table= first_table->next_local;
1896
first_table->next_local= 0;
1897
first_table->next_local= save_table;
1901
/* revert changes for SP */
1902
select_lex->table_list.first= (uchar*) first_table;
1907
case SQLCOM_TRUNCATE:
1908
if (end_active_trans(thd))
1913
assert(first_table == all_tables && first_table != 0);
1915
Don't allow this within a transaction because we want to use
1918
if (thd->locked_tables || thd->active_transaction())
1920
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1921
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1925
res= mysql_truncate(thd, first_table, 0);
1930
assert(first_table == all_tables && first_table != 0);
1931
assert(select_lex->offset_limit == 0);
1932
unit->set_limit(select_lex);
1934
if (!thd->locked_tables &&
1935
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1941
res = mysql_delete(thd, all_tables, select_lex->where,
1942
&select_lex->order_list,
1943
unit->select_limit_cnt, select_lex->options,
1947
case SQLCOM_DELETE_MULTI:
1949
assert(first_table == all_tables && first_table != 0);
1950
TableList *aux_tables=
1951
(TableList *)thd->lex->auxiliary_table_list.first;
1952
multi_delete *del_result;
1954
if (!thd->locked_tables &&
1955
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1961
if ((res= multi_delete_precheck(thd, all_tables)))
1964
/* condition will be true on SP re-excuting */
1965
if (select_lex->item_list.elements != 0)
1966
select_lex->item_list.empty();
1967
if (add_item_to_list(thd, new Item_null()))
1970
thd_proc_info(thd, "init");
1971
if ((res= open_and_lock_tables(thd, all_tables)))
1974
if ((res= mysql_multi_delete_prepare(thd)))
1977
if (!thd->is_fatal_error &&
1978
(del_result= new multi_delete(aux_tables, lex->table_count)))
1980
res= mysql_select(thd, &select_lex->ref_pointer_array,
1981
select_lex->get_table_list(),
1982
select_lex->with_wild,
1983
select_lex->item_list,
1985
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1987
select_lex->options | thd->options |
1988
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1989
OPTION_SETUP_TABLES_DONE,
1990
del_result, unit, select_lex);
1991
res|= thd->is_error();
1993
del_result->abort();
2000
case SQLCOM_DROP_TABLE:
2002
assert(first_table == all_tables && first_table != 0);
2003
if (!lex->drop_temporary)
2005
if (end_active_trans(thd))
2011
If this is a slave thread, we may sometimes execute some
2012
DROP / * 40005 TEMPORARY * / TABLE
2013
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
2014
MASTER TO), while the temporary table has already been dropped.
2015
To not generate such irrelevant "table does not exist errors",
2016
we silently add IF EXISTS if TEMPORARY was used.
2018
if (thd->slave_thread)
2019
lex->drop_if_exists= 1;
2021
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
2022
thd->options|= OPTION_KEEP_LOG;
2024
/* DDL and binlog write order protected by LOCK_open */
2025
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
2028
case SQLCOM_SHOW_PROCESSLIST:
2029
mysqld_list_processes(thd, NullS, lex->verbose);
2031
case SQLCOM_SHOW_ENGINE_LOGS:
2033
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
2036
case SQLCOM_CHANGE_DB:
2038
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2040
if (!mysql_change_db(thd, &db_str, false))
2048
assert(first_table == all_tables && first_table != 0);
2049
if (lex->local_file)
2051
if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
2054
my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2059
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
2060
lex->update_list, lex->value_list, lex->duplicates,
2061
lex->ignore, (bool) lex->local_file);
2065
case SQLCOM_SET_OPTION:
2067
List<set_var_base> *lex_var_list= &lex->var_list;
2069
if (lex->autocommit && end_active_trans(thd))
2072
if (open_and_lock_tables(thd, all_tables))
2074
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2076
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2079
if (!(res= sql_set_variables(thd, lex_var_list)))
2082
If the previous command was a SET ONE_SHOT, we don't want to forget
2083
about the ONE_SHOT property of that SET. So we use a |= instead of = .
2085
thd->one_shot_set|= lex->one_shot_set;
2091
We encountered some sort of error, but no message was sent.
2092
Send something semi-generic here since we don't know which
2093
assignment in the list caused the error.
2095
if (!thd->is_error())
2096
my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2103
case SQLCOM_UNLOCK_TABLES:
2105
It is critical for mysqldump --single-transaction --master-data that
2106
UNLOCK TABLES does not implicitely commit a connection which has only
2107
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2108
false, mysqldump will not work.
2110
unlock_locked_tables(thd);
2111
if (thd->options & OPTION_TABLE_LOCK)
2113
end_active_trans(thd);
2114
thd->options&= ~(OPTION_TABLE_LOCK);
2116
if (thd->global_read_lock)
2117
unlock_global_read_lock(thd);
2120
case SQLCOM_LOCK_TABLES:
2122
We try to take transactional locks if
2123
- only transactional locks are requested (lex->lock_transactional) and
2124
- no non-transactional locks exist (!thd->locked_tables).
2126
if (lex->lock_transactional && !thd->locked_tables)
2130
All requested locks are transactional and no non-transactional
2133
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2141
Non-transactional locking has been requested or
2142
non-transactional locks exist already or transactional locks are
2143
not supported by all storage engines. Take non-transactional
2148
One or more requested locks are non-transactional and/or
2149
non-transactional locks exist or a storage engine does not support
2150
transactional locks. Check if at least one transactional lock is
2151
requested. If yes, warn about the conversion to non-transactional
2152
locks or abort in strict mode.
2154
if (check_transactional_lock(thd, all_tables))
2156
unlock_locked_tables(thd);
2157
/* we must end the trasaction first, regardless of anything */
2158
if (end_active_trans(thd))
2160
thd->in_lock_tables=1;
2161
thd->options|= OPTION_TABLE_LOCK;
2163
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2165
thd->locked_tables=thd->lock;
2167
(void) set_handler_table_locks(thd, all_tables, false);
2173
Need to end the current transaction, so the storage engine (InnoDB)
2174
can free its locks if LOCK TABLES locked some tables before finding
2175
that it can't lock a table in its list
2177
ha_autocommit_or_rollback(thd, 1);
2178
end_active_trans(thd);
2179
thd->options&= ~(OPTION_TABLE_LOCK);
2181
thd->in_lock_tables=0;
2183
case SQLCOM_CREATE_DB:
2186
As mysql_create_db() may modify HA_CREATE_INFO structure passed to
2187
it, we need to use a copy of LEX::create_info to make execution
2188
prepared statement- safe.
2190
HA_CREATE_INFO create_info(lex->create_info);
2191
if (end_active_trans(thd))
2197
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2198
check_db_name(&lex->name))
2200
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2204
If in a slave thread :
2205
CREATE DATABASE DB was certainly not preceded by USE DB.
2206
For that reason, db_ok() in sql/slave.cc did not check the
2207
do_db/ignore_db. And as this query involves no tables, tables_ok()
2208
above was not called. So we have to check rules again here.
2210
if (thd->slave_thread &&
2211
(!rpl_filter->db_ok(lex->name.str) ||
2212
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2214
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2217
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2218
lex->name.str), &create_info, 0);
2221
case SQLCOM_DROP_DB:
2223
if (end_active_trans(thd))
2228
if (check_db_name(&lex->name))
2230
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2234
If in a slave thread :
2235
DROP DATABASE DB may not be preceded by USE DB.
2236
For that reason, maybe db_ok() in sql/slave.cc did not check the
2237
do_db/ignore_db. And as this query involves no tables, tables_ok()
2238
above was not called. So we have to check rules again here.
2240
if (thd->slave_thread &&
2241
(!rpl_filter->db_ok(lex->name.str) ||
2242
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2244
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2247
if (thd->locked_tables || thd->active_transaction())
2249
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2250
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2253
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2256
case SQLCOM_ALTER_DB:
2258
LEX_STRING *db= &lex->name;
2259
HA_CREATE_INFO create_info(lex->create_info);
2260
if (check_db_name(db))
2262
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2266
If in a slave thread :
2267
ALTER DATABASE DB may not be preceded by USE DB.
2268
For that reason, maybe db_ok() in sql/slave.cc did not check the
2269
do_db/ignore_db. And as this query involves no tables, tables_ok()
2270
above was not called. So we have to check rules again here.
2272
if (thd->slave_thread &&
2273
(!rpl_filter->db_ok(db->str) ||
2274
!rpl_filter->db_ok_with_wild_table(db->str)))
2276
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2279
if (thd->locked_tables || thd->active_transaction())
2281
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2282
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2285
res= mysql_alter_db(thd, db->str, &create_info);
2288
case SQLCOM_SHOW_CREATE_DB:
2290
if (check_db_name(&lex->name))
2292
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2295
res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
2301
bool write_to_binlog;
2304
reload_cache() will tell us if we are allowed to write to the
2307
if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
2310
We WANT to write and we CAN write.
2311
! we write after unlocking the table.
2314
Presumably, RESET and binlog writing doesn't require synchronization
2316
write_bin_log(thd, false, thd->query, thd->query_length);
2324
Item *it= (Item *)lex->value_list.head();
2326
if (lex->table_or_sp_used())
2328
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2329
"function calls as part of this statement");
2333
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2335
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2339
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2343
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2345
my_error(ER_XAER_RMFAIL, MYF(0),
2346
xa_state_names[thd->transaction.xid_state.xa_state]);
2350
Breakpoints for backup testing.
2352
if (begin_trans(thd))
2357
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2358
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2362
case SQLCOM_ROLLBACK:
2363
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2364
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2368
case SQLCOM_RELEASE_SAVEPOINT:
2371
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2373
if (my_strnncoll(system_charset_info,
2374
(uchar *)lex->ident.str, lex->ident.length,
2375
(uchar *)sv->name, sv->length) == 0)
2380
if (ha_release_savepoint(thd, sv))
2381
res= true; // cannot happen
2384
thd->transaction.savepoints=sv->prev;
2387
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2390
case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2393
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2395
if (my_strnncoll(system_charset_info,
2396
(uchar *)lex->ident.str, lex->ident.length,
2397
(uchar *)sv->name, sv->length) == 0)
2402
if (ha_rollback_to_savepoint(thd, sv))
2403
res= true; // cannot happen
2406
if (((thd->options & OPTION_KEEP_LOG) ||
2407
thd->transaction.all.modified_non_trans_table) &&
2409
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2410
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2411
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2414
thd->transaction.savepoints=sv;
2417
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2420
case SQLCOM_SAVEPOINT:
2421
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2422
thd->in_sub_stmt) || !opt_using_transactions)
2426
SAVEPOINT **sv, *newsv;
2427
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2429
if (my_strnncoll(system_charset_info,
2430
(uchar *)lex->ident.str, lex->ident.length,
2431
(uchar *)(*sv)->name, (*sv)->length) == 0)
2434
if (*sv) /* old savepoint of the same name exists */
2437
ha_release_savepoint(thd, *sv); // it cannot fail
2440
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2441
savepoint_alloc_size)) == 0)
2443
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2446
newsv->name=strmake_root(&thd->transaction.mem_root,
2447
lex->ident.str, lex->ident.length);
2448
newsv->length=lex->ident.length;
2450
if we'll get an error here, don't add new savepoint to the list.
2451
we'll lose a little bit of memory in transaction mem_root, but it'll
2452
be free'd when transaction ends anyway
2454
if (ha_savepoint(thd, newsv))
2458
newsv->prev=thd->transaction.savepoints;
2459
thd->transaction.savepoints=newsv;
2464
case SQLCOM_BINLOG_BASE64_EVENT:
2466
mysql_client_binlog_statement(thd);
2470
assert(0); /* Impossible */
2474
thd_proc_info(thd, "query end");
2477
Binlog-related cleanup:
2478
Reset system variables temporarily modified by SET ONE SHOT.
2480
Exception: If this is a SET, do nothing. This is to allow
2481
mysqlbinlog to print many SET commands (in this case we want the
2482
charset temp setting to live until the real query). This is also
2483
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2486
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2487
reset_one_shot_variables(thd);
506
2490
The return value for ROW_COUNT() is "implementation dependent" if the
507
2491
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
508
2492
wants. We also keep the last value in case of SQLCOM_CALL or
511
if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
2495
if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2496
thd->row_count_func= -1;
2504
if (need_start_waiting)
513
session->row_count_func= -1;
2507
Release the protection against the global read lock and wake
2508
everyone, who might want to set a global read lock.
2510
start_waiting_global_read_lock(thd);
516
return (res || session->is_error());
2512
return(res || thd->is_error());
518
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2515
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
520
LEX *lex= session->lex;
521
2518
select_result *result=lex->result;
523
2520
/* assign global limit variable if limit is not given */
525
Select_Lex *param= lex->unit.global_parameters;
2522
SELECT_LEX *param= lex->unit.global_parameters;
526
2523
if (!param->explicit_limit)
527
2524
param->select_limit=
528
new Item_int((uint64_t) session->variables.select_limit);
532
&& ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
533
&& ! session->inTransaction()
534
&& ! lex->statement->isShow())
536
if (session->startTransaction() == false)
538
my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
543
if (not (res= session->openTablesLock(all_tables)))
2525
new Item_int((uint64_t) thd->variables.select_limit);
2527
if (!(res= open_and_lock_tables(thd, all_tables)))
545
2529
if (lex->describe)