125
169
sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA;
127
171
sql_command_flags[SQLCOM_UPDATE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
172
sql_command_flags[SQLCOM_UPDATE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
128
173
sql_command_flags[SQLCOM_INSERT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
129
174
sql_command_flags[SQLCOM_INSERT_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
130
175
sql_command_flags[SQLCOM_DELETE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
176
sql_command_flags[SQLCOM_DELETE_MULTI]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
131
177
sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
132
178
sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
180
sql_command_flags[SQLCOM_SHOW_STATUS]= CF_STATUS_COMMAND;
181
sql_command_flags[SQLCOM_SHOW_DATABASES]= CF_STATUS_COMMAND;
182
sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND;
183
sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND;
184
sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND;
185
sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND;
186
sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
134
187
sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
135
188
sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
189
sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
190
sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
136
191
sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
137
192
sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
193
sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
194
sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
196
sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
197
CF_SHOW_TABLE_COMMAND);
198
sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
199
CF_SHOW_TABLE_COMMAND);
140
201
The following admin table operations are allowed
204
sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND;
205
sql_command_flags[SQLCOM_OPTIMIZE]= CF_WRITE_LOGS_COMMAND;
143
206
sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND;
210
bool is_update_query(enum enum_sql_command command)
212
assert(command >= 0 && command <= SQLCOM_END);
213
return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
216
void execute_init_command(THD *thd, sys_var_str *init_command_var,
217
rw_lock_t *var_mutex)
220
ulong save_client_capabilities;
222
thd->set_proc_info("Execution of init_command");
224
We need to lock init_command_var because
225
during execution of init_command_var query
226
values of init_command_var can't be changed
228
rw_rdlock(var_mutex);
229
save_client_capabilities= thd->client_capabilities;
230
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
232
We don't need return result of execution to client side.
233
To forbid this we should set thd->net.vio to 0.
235
save_vio= thd->net.vio;
237
dispatch_command(COM_QUERY, thd,
238
init_command_var->value,
239
init_command_var->value_length);
240
rw_unlock(var_mutex);
241
thd->client_capabilities= save_client_capabilities;
242
thd->net.vio= save_vio;
246
Ends the current transaction and (maybe) begin the next.
248
@param thd Current thread
249
@param completion Completion type
255
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
260
if (thd->transaction.xid_state.xa_state != XA_NOTR)
262
my_error(ER_XAER_RMFAIL, MYF(0),
263
xa_state_names[thd->transaction.xid_state.xa_state]);
266
switch (completion) {
269
We don't use end_active_trans() here to ensure that this works
270
even if there is a problem with the OPTION_AUTO_COMMIT flag
271
(Which of course should never happen...)
273
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
275
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
276
thd->transaction.all.modified_non_trans_table= false;
279
do_release= 1; /* fall through */
280
case COMMIT_AND_CHAIN:
281
res= end_active_trans(thd);
282
if (!res && completion == COMMIT_AND_CHAIN)
283
res= begin_trans(thd);
285
case ROLLBACK_RELEASE:
286
do_release= 1; /* fall through */
288
case ROLLBACK_AND_CHAIN:
290
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
291
if (ha_rollback(thd))
293
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
294
thd->transaction.all.modified_non_trans_table= false;
295
if (!res && (completion == ROLLBACK_AND_CHAIN))
296
res= begin_trans(thd);
301
my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
306
my_error(thd->killed_errno(), MYF(0));
307
else if ((res == 0) && do_release)
308
thd->killed= THD::KILL_CONNECTION;
315
Read one command from connection and execute it (query or simple command).
316
This function is called in loop from thread function.
318
For profiling to work, it must never be called recursively.
323
1 request of thread shutdown (see dispatch_command() description)
326
bool do_command(THD *thd)
332
enum enum_server_command command;
335
indicator of uninitialized lex => normal flow of errors handling
338
thd->lex->current_select= 0;
341
This thread will do a blocking read from the client which
342
will be interrupted when the next command is received from
343
the client, the connection is closed or "net_wait_timeout"
344
number of seconds has passed
346
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
349
XXX: this code is here only to clear possible errors of init_connect.
350
Consider moving to init_connect() instead.
352
thd->clear_error(); // Clear error message
353
thd->main_da.reset_diagnostics_area();
355
net_new_transaction(net);
357
packet_length= my_net_read(net);
358
if (packet_length == packet_error)
360
/* Check if we can continue without closing the connection */
362
/* The error must be set. */
363
assert(thd->is_error());
364
net_end_statement(thd);
368
return_value= true; // We have to close it.
377
packet= (char*) net->read_pos;
379
'packet_length' contains length of data, as it was stored in packet
380
header. In case of malformed header, my_net_read returns zero.
381
If packet_length is not zero, my_net_read ensures that the returned
382
number of bytes was actually read from network.
383
There is also an extra safety measure in my_net_read:
384
it sets packet[packet_length]= 0, but only for non-zero packets.
386
if (packet_length == 0) /* safety */
388
/* Initialize with COM_SLEEP packet */
389
packet[0]= (unsigned char) COM_SLEEP;
392
/* Do not rely on my_net_read, extra safety against programming errors. */
393
packet[packet_length]= '\0'; /* safety */
395
command= (enum enum_server_command) (unsigned char) packet[0];
397
if (command >= COM_END)
398
command= COM_END; // Wrong command
400
/* Restore read timeout value */
401
my_net_set_read_timeout(net, thd->variables.net_read_timeout);
403
assert(packet_length);
404
return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
407
return(return_value);
411
Determine if an attempt to update a non-temporary table while the
412
read-only option was enabled has been made.
414
This is a helper function to mysql_execute_command.
416
@note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
418
@see mysql_execute_command
421
@retval true The statement should be denied.
422
@retval false The statement isn't updating any relevant tables.
425
static bool deny_updates_if_read_only_option(THD *thd,
426
TableList *all_tables)
433
if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
436
/* Multi update is an exception and is dealt with later. */
437
if (lex->sql_command == SQLCOM_UPDATE_MULTI)
440
const bool create_temp_tables=
441
(lex->sql_command == SQLCOM_CREATE_TABLE) &&
442
(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
444
const bool drop_temp_tables=
445
(lex->sql_command == SQLCOM_DROP_TABLE) &&
448
const bool update_real_tables=
449
some_non_temp_table_to_be_updated(thd, all_tables) &&
450
!(create_temp_tables || drop_temp_tables);
453
const bool create_or_drop_databases=
454
(lex->sql_command == SQLCOM_CREATE_DB) ||
455
(lex->sql_command == SQLCOM_DROP_DB);
457
if (update_real_tables || create_or_drop_databases)
460
An attempt was made to modify one or more non-temporary tables.
466
/* Assuming that only temporary tables are modified. */
147
471
Perform one connection-level (COM_XXXX) command.
149
473
@param command type of command to perform
150
@param session connection handle
474
@param thd connection handle
151
475
@param packet data for the command, packet is always null-terminated
152
476
@param packet_length length of packet + 1 (to show that data is
153
477
null-terminated) except for COM_SLEEP, where it
157
set session->lex->sql_command to SQLCOM_END here.
481
set thd->lex->sql_command to SQLCOM_END here.
159
483
The following has to be changed to an 8 byte integer
184
507
/* Increase id and count all other statements. */
186
session->status_var.questions++;
190
/* TODO: set session->lex->sql_command to SQLCOM_END here */
192
plugin::Logging::preDo(session);
193
if (unlikely(plugin::EventObserver::beforeStatement(*session)))
195
// We should do something about an error...
198
session->server_status&=
509
statistic_increment(thd->status_var.questions, &LOCK_status);
514
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
515
pthread_mutex_unlock(&LOCK_thread_count);
199
520
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
200
521
switch (command) {
201
522
case COM_INIT_DB:
203
if (packet_length == 0)
205
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
209
string tmp(packet, packet_length);
211
SchemaIdentifier identifier(tmp);
213
if (not mysql_change_db(session, identifier))
525
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
526
thd->convert_string(&tmp, system_charset_info,
527
packet, packet_length, thd->charset());
528
if (!mysql_change_db(thd, &tmp, false))
534
case COM_CHANGE_USER:
536
status_var_increment(thd->status_var.com_other);
537
char *user= (char*) packet, *packet_end= packet + packet_length;
538
/* Safe because there is always a trailing \0 at the end of the packet */
539
char *passwd= strchr(user, '\0')+1;
542
thd->clear_error(); // if errors from rollback
545
Old clients send null-terminated string ('\0' for empty string) for
546
password. New clients send the size (1 byte) + string (not null
547
terminated, so also '\0' for empty string).
549
Cast *passwd to an unsigned char, so that it doesn't extend the sign
550
for *passwd > 127 and become 2**32-127 after casting to uint32_t.
552
char db_buff[NAME_LEN+1]; // buffer to store db in utf8
556
If there is no password supplied, the packet must contain '\0',
557
in any type of handshake (4.1 or pre-4.1).
559
if (passwd >= packet_end)
561
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
564
uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
565
(unsigned char)(*passwd++) : strlen(passwd));
566
uint32_t dummy_errors, save_db_length, db_length;
568
Security_context save_security_ctx= *thd->security_ctx;
569
USER_CONN *save_user_connect;
573
Database name is always NUL-terminated, so in case of empty database
574
the packet must contain at least the trailing '\0'.
576
if (db >= packet_end)
578
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
581
db_length= strlen(db);
583
char *ptr= db + db_length + 1;
584
uint32_t cs_number= 0;
586
if (ptr < packet_end)
588
if (ptr + 2 > packet_end)
590
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
594
cs_number= uint2korr(ptr);
597
/* Convert database name to utf8 */
598
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
599
system_charset_info, db, db_length,
600
thd->charset(), &dummy_errors)]= 0;
603
/* Save user and privileges */
604
save_db_length= thd->db_length;
606
save_user_connect= thd->user_connect;
608
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
610
thd->security_ctx->user= save_security_ctx.user;
611
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
615
/* Clear variables that are allocated */
616
thd->user_connect= 0;
617
res= check_user(thd, passwd, passwd_len, db, false);
621
if (thd->security_ctx->user)
622
free(thd->security_ctx->user);
623
*thd->security_ctx= save_security_ctx;
624
thd->user_connect= save_user_connect;
626
thd->db_length= save_db_length;
632
if (save_security_ctx.user)
633
free(save_security_ctx.user);
637
thd_init_client_charset(thd, cs_number);
638
thd->update_charset();
221
if (not session->readAndStoreQuery(packet, packet_length))
645
if (alloc_query(thd, packet, packet_length))
222
646
break; // fatal error is set
223
DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
225
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
227
plugin::QueryRewriter::rewriteQuery(session->getSchema(), session->getQueryString());
228
mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
647
char *packet_end= thd->query + thd->query_length;
648
const char* end_of_stmt= NULL;
650
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
652
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
654
char *beginning_of_next_stmt= (char*) end_of_stmt;
656
net_end_statement(thd);
658
Multiple queries exits, execute them individually
660
close_thread_tables(thd);
661
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
663
log_slow_statement(thd);
665
/* Remove garbage at start of query */
666
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
668
beginning_of_next_stmt++;
672
pthread_mutex_lock(&LOCK_thread_count);
673
thd->query_length= length;
674
thd->query= beginning_of_next_stmt;
676
Count each statement from the client.
678
statistic_increment(thd->status_var.questions, &LOCK_status);
679
thd->query_id= next_query_id();
680
thd->set_time(); /* Reset the query start time. */
681
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
682
pthread_mutex_unlock(&LOCK_thread_count);
684
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
688
case COM_FIELD_LIST: // This isn't actually needed
690
char *fields, *packet_end= packet + packet_length, *arg_end;
691
/* Locked closure of all tables */
692
TableList table_list;
693
LEX_STRING conv_name;
695
/* used as fields initializator */
698
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
699
memset(&table_list, 0, sizeof(table_list));
700
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
703
We have name + wildcard in packet, separated by endzero
705
arg_end= strchr(packet, '\0');
706
thd->convert_string(&conv_name, system_charset_info,
707
packet, (uint32_t) (arg_end - packet), thd->charset());
708
table_list.alias= table_list.table_name= conv_name.str;
711
if (!my_strcasecmp(system_charset_info, table_list.db,
712
INFORMATION_SCHEMA_NAME.str))
714
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
716
table_list.schema_table= schema_table;
719
thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
720
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
722
if (lower_case_table_names)
723
my_casedn_str(files_charset_info, table_list.table_name);
725
/* init structures for VIEW processing */
726
table_list.select_lex= &(thd->lex->select_lex);
729
mysql_reset_thd_for_next_command(thd);
732
select_lex.table_list.link_in_list((unsigned char*) &table_list,
733
(unsigned char**) &table_list.next_local);
734
thd->lex->add_to_query_tables(&table_list);
736
/* switch on VIEW optimisation: do not fill temporary tables */
737
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
738
mysqld_list_fields(thd,&table_list,fields);
739
thd->lex->unit.cleanup();
740
thd->cleanup_after_query();
233
744
/* We don't calculate statistics for this command */
234
session->main_da.disable_status(); // Don't send anything back
745
net->error=0; // Don't give 'abort' message
746
thd->main_da.disable_status(); // Don't send anything back
235
747
error=true; // End server
749
case COM_BINLOG_DUMP:
753
uint32_t slave_server_id;
755
status_var_increment(thd->status_var.com_other);
756
/* TODO: The following has to be changed to an 8 byte integer */
757
pos = uint4korr(packet);
758
flags = uint2korr(packet + 4);
759
thd->server_id=0; /* avoid suicide */
760
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
761
kill_zombie_dump_threads(slave_server_id);
762
thd->server_id = slave_server_id;
764
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
765
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
237
769
case COM_SHUTDOWN:
239
session->status_var.com_other++;
241
session->close_thread_tables(); // Free before kill
771
status_var_increment(thd->status_var.com_other);
773
close_thread_tables(thd); // Free before kill
247
session->status_var.com_other++;
248
session->my_ok(); // Tell client we are alive
779
status_var_increment(thd->status_var.com_other);
780
my_ok(thd); // Tell client we are alive
782
case COM_PROCESS_INFO:
783
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
784
mysqld_list_processes(thd, NULL, 0);
786
case COM_PROCESS_KILL:
788
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
789
ulong id=(ulong) uint4korr(packet);
790
sql_kill(thd,id,false);
795
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
796
uint32_t opt_command= uint2korr(packet);
798
switch (opt_command) {
799
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
800
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
803
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
804
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
808
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
251
814
case COM_CONNECT: // Impossible here
815
case COM_TIME: // Impossible from client
254
818
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
469
1105
variables, but for now this is probably good enough.
470
1106
Don't reset warnings when executing a stored routine.
472
if (all_tables || ! lex->is_single_level_stmt())
474
drizzle_reset_errors(session, 0);
477
assert(session->transaction.stmt.hasModifiedNonTransData() == false);
479
/* now we are ready to execute the statement */
480
res= lex->statement->execute();
481
session->set_proc_info("query end");
1108
if (all_tables || !lex->is_single_level_stmt())
1109
drizzle_reset_errors(thd, 0);
1111
if (unlikely(thd->slave_thread))
1114
Check if statment should be skipped because of slave filtering
1118
- UPDATE MULTI: For this statement, we want to check the filtering
1119
rules later in the code
1120
- SET: we always execute it (Not that many SET commands exists in
1121
the binary log anyway -- only 4.1 masters write SET statements,
1122
in 5.0 there are no SET statements in the binary log)
1123
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1124
have stale files on slave caused by exclusion of one tmp table).
1126
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1127
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1128
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1129
lex->drop_temporary && lex->drop_if_exists) &&
1130
all_tables_not_ok(thd, all_tables))
1132
/* we warn the slave SQL thread */
1133
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1134
if (thd->one_shot_set)
1137
It's ok to check thd->one_shot_set here:
1139
The charsets in a MySQL 5.0 slave can change by both a binlogged
1140
SET ONE_SHOT statement and the event-internal charset setting,
1141
and these two ways to change charsets do not seems to work
1144
At least there seems to be problems in the rli cache for
1145
charsets if we are using ONE_SHOT. Note that this is normally no
1146
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1147
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1149
reset_one_shot_variables(thd);
1157
When option readonly is set deny operations which change non-temporary
1158
tables. Except for the replication thread and the 'super' users.
1160
if (deny_updates_if_read_only_option(thd, all_tables))
1162
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1165
} /* endif unlikely slave */
1166
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1168
assert(thd->transaction.stmt.modified_non_trans_table == false);
1170
switch (lex->sql_command) {
1171
case SQLCOM_SHOW_STATUS:
1173
system_status_var old_status_var= thd->status_var;
1174
thd->initial_status_var= &old_status_var;
1175
res= execute_sqlcom_select(thd, all_tables);
1176
/* Don't log SHOW STATUS commands to slow query log */
1177
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1178
SERVER_QUERY_NO_GOOD_INDEX_USED);
1180
restore status variables, as we don't want 'show status' to cause
1183
pthread_mutex_lock(&LOCK_status);
1184
add_diff_to_status(&global_status_var, &thd->status_var,
1186
thd->status_var= old_status_var;
1187
pthread_mutex_unlock(&LOCK_status);
1190
case SQLCOM_SHOW_DATABASES:
1191
case SQLCOM_SHOW_TABLES:
1192
case SQLCOM_SHOW_TABLE_STATUS:
1193
case SQLCOM_SHOW_OPEN_TABLES:
1194
case SQLCOM_SHOW_FIELDS:
1195
case SQLCOM_SHOW_KEYS:
1196
case SQLCOM_SHOW_VARIABLES:
1199
thd->status_var.last_query_cost= 0.0;
1200
res= execute_sqlcom_select(thd, all_tables);
1203
case SQLCOM_EMPTY_QUERY:
1209
res = purge_master_logs(thd, lex->to_log);
1212
case SQLCOM_PURGE_BEFORE:
1216
/* PURGE MASTER LOGS BEFORE 'data' */
1217
it= (Item *)lex->value_list.head();
1218
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1221
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1224
it= new Item_func_unix_timestamp(it);
1226
it is OK only emulate fix_fieds, because we need only
1229
it->quick_fix_field();
1230
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1233
case SQLCOM_SHOW_WARNS:
1235
res= mysqld_show_warnings(thd, (uint32_t)
1236
((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1237
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1238
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1242
case SQLCOM_SHOW_ERRORS:
1244
res= mysqld_show_warnings(thd, (uint32_t)
1245
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1248
case SQLCOM_ASSIGN_TO_KEYCACHE:
1250
assert(first_table == all_tables && first_table != 0);
1251
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1254
case SQLCOM_CHANGE_MASTER:
1256
pthread_mutex_lock(&LOCK_active_mi);
1257
res = change_master(thd,active_mi);
1258
pthread_mutex_unlock(&LOCK_active_mi);
1261
case SQLCOM_SHOW_SLAVE_STAT:
1263
pthread_mutex_lock(&LOCK_active_mi);
1264
if (active_mi != NULL)
1266
res = show_master_info(thd, active_mi);
1270
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1271
"the master info structure does not exist");
1274
pthread_mutex_unlock(&LOCK_active_mi);
1277
case SQLCOM_SHOW_MASTER_STAT:
1279
res = show_binlog_info(thd);
1283
case SQLCOM_SHOW_ENGINE_STATUS:
1285
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1288
case SQLCOM_CREATE_TABLE:
1290
/* If CREATE TABLE of non-temporary table, do implicit commit */
1291
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1293
if (end_active_trans(thd))
1299
assert(first_table == all_tables && first_table != 0);
1301
// Skip first table, which is the table we are creating
1302
TableList *create_table= lex->unlink_first_table(&link_to_local);
1303
TableList *select_tables= lex->query_tables;
1305
Code below (especially in mysql_create_table() and select_create
1306
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1307
use a copy of this structure to make execution prepared statement-
1308
safe. A shallow copy is enough as this code won't modify any memory
1309
referenced from this structure.
1311
HA_CREATE_INFO create_info(lex->create_info);
1313
We need to copy alter_info for the same reasons of re-execution
1314
safety, only in case of Alter_info we have to do (almost) a deep
1317
Alter_info alter_info(lex->alter_info, thd->mem_root);
1319
if (thd->is_fatal_error)
1321
/* If out of memory when creating a copy of alter_info. */
1323
goto end_with_restore_list;
1326
if ((res= create_table_precheck(thd, select_tables, create_table)))
1327
goto end_with_restore_list;
1329
/* Might have been updated in create_table_precheck */
1330
create_info.alias= create_table->alias;
1332
#ifdef HAVE_READLINK
1333
/* Fix names if symlinked tables */
1334
if (append_file_to_dir(thd, &create_info.data_file_name,
1335
create_table->table_name) ||
1336
append_file_to_dir(thd, &create_info.index_file_name,
1337
create_table->table_name))
1338
goto end_with_restore_list;
1341
If we are using SET CHARSET without DEFAULT, add an implicit
1342
DEFAULT to not confuse old users. (This may change).
1344
if ((create_info.used_fields &
1345
(HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
1346
HA_CREATE_USED_CHARSET)
1348
create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
1349
create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
1350
create_info.default_table_charset= create_info.table_charset;
1351
create_info.table_charset= 0;
1354
The create-select command will open and read-lock the select table
1355
and then create, open and write-lock the new table. If a global
1356
read lock steps in, we get a deadlock. The write lock waits for
1357
the global read lock, while the global read lock waits for the
1358
select table to be closed. So we wait until the global readlock is
1359
gone before starting both steps. Note that
1360
wait_if_global_read_lock() sets a protection against a new global
1361
read lock when it succeeds. This needs to be released by
1362
start_waiting_global_read_lock(). We protect the normal CREATE
1363
TABLE in the same way. That way we avoid that a new table is
1364
created during a gobal read lock.
1366
if (!thd->locked_tables &&
1367
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1370
goto end_with_restore_list;
1372
if (select_lex->item_list.elements) // With select
1374
select_result *result;
1376
select_lex->options|= SELECT_NO_UNLOCK;
1377
unit->set_limit(select_lex);
1379
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1381
lex->link_first_table_back(create_table, link_to_local);
1382
create_table->create= true;
1385
if (!(res= open_and_lock_tables(thd, lex->query_tables)))
1388
Is table which we are changing used somewhere in other parts
1391
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1393
TableList *duplicate;
1394
create_table= lex->unlink_first_table(&link_to_local);
1395
if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1397
update_non_unique_table_error(create_table, "CREATE", duplicate);
1399
goto end_with_restore_list;
1404
select_create is currently not re-execution friendly and
1405
needs to be created for every execution of a PS/SP.
1407
if ((result= new select_create(create_table,
1410
select_lex->item_list,
1416
CREATE from SELECT give its SELECT_LEX for SELECT,
1417
and item_list belong to SELECT
1419
res= handle_select(thd, lex, result, 0);
1423
else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1424
create_table= lex->unlink_first_table(&link_to_local);
1429
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1430
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1431
thd->options|= OPTION_KEEP_LOG;
1432
/* regular create */
1433
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1434
res= mysql_create_like_table(thd, create_table, select_tables,
1438
res= mysql_create_table(thd, create_table->db,
1439
create_table->table_name, &create_info,
1446
/* put tables back for PS rexecuting */
1447
end_with_restore_list:
1448
lex->link_first_table_back(create_table, link_to_local);
1451
case SQLCOM_CREATE_INDEX:
1453
case SQLCOM_DROP_INDEX:
1455
CREATE INDEX and DROP INDEX are implemented by calling ALTER
1456
TABLE with proper arguments.
1458
In the future ALTER TABLE will notice that the request is to
1459
only add indexes and create these one by one for the existing
1460
table without having to do a full rebuild.
1463
/* Prepare stack copies to be re-execution safe */
1464
HA_CREATE_INFO create_info;
1465
Alter_info alter_info(lex->alter_info, thd->mem_root);
1467
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1470
assert(first_table == all_tables && first_table != 0);
1471
if (end_active_trans(thd))
1474
memset(&create_info, 0, sizeof(create_info));
1475
create_info.db_type= 0;
1476
create_info.row_type= ROW_TYPE_NOT_USED;
1477
create_info.default_table_charset= thd->variables.collation_database;
1479
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1480
&create_info, first_table, &alter_info,
1481
0, (order_st*) 0, 0);
1484
case SQLCOM_SLAVE_START:
1486
pthread_mutex_lock(&LOCK_active_mi);
1487
start_slave(thd,active_mi,1 /* net report*/);
1488
pthread_mutex_unlock(&LOCK_active_mi);
1491
case SQLCOM_SLAVE_STOP:
1493
If the client thread has locked tables, a deadlock is possible.
1495
- the client thread does LOCK TABLE t READ.
1496
- then the master updates t.
1497
- then the SQL slave thread wants to update t,
1498
so it waits for the client thread because t is locked by it.
1499
- then the client thread does SLAVE STOP.
1500
SLAVE STOP waits for the SQL slave thread to terminate its
1501
update t, which waits for the client thread because t is locked by it.
1502
To prevent that, refuse SLAVE STOP if the
1503
client thread has locked tables
1505
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1507
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1508
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1512
pthread_mutex_lock(&LOCK_active_mi);
1513
stop_slave(thd,active_mi,1/* net report*/);
1514
pthread_mutex_unlock(&LOCK_active_mi);
1518
case SQLCOM_ALTER_TABLE:
1519
assert(first_table == all_tables && first_table != 0);
1522
Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
1523
so we have to use a copy of this structure to make execution
1524
prepared statement- safe. A shallow copy is enough as no memory
1525
referenced from this structure will be modified.
1527
HA_CREATE_INFO create_info(lex->create_info);
1528
Alter_info alter_info(lex->alter_info, thd->mem_root);
1530
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1535
/* Must be set in the parser */
1536
assert(select_lex->db);
1538
{ // Rename of table
1539
TableList tmp_table;
1540
memset(&tmp_table, 0, sizeof(tmp_table));
1541
tmp_table.table_name= lex->name.str;
1542
tmp_table.db=select_lex->db;
1545
/* Don't yet allow changing of symlinks with ALTER TABLE */
1546
if (create_info.data_file_name)
1547
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1548
"DATA DIRECTORY option ignored");
1549
if (create_info.index_file_name)
1550
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1551
"INDEX DIRECTORY option ignored");
1552
create_info.data_file_name= create_info.index_file_name= NULL;
1553
/* ALTER TABLE ends previous transaction */
1554
if (end_active_trans(thd))
1557
if (!thd->locked_tables &&
1558
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1564
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1568
select_lex->order_list.elements,
1569
(order_st *) select_lex->order_list.first,
1573
case SQLCOM_RENAME_TABLE:
1575
assert(first_table == all_tables && first_table != 0);
1577
for (table= first_table; table; table= table->next_local->next_local)
1579
TableList old_list, new_list;
1581
we do not need initialize old_list and new_list because we will
1582
come table[0] and table->next[0] there
1585
new_list= table->next_local[0];
1588
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1594
case SQLCOM_SHOW_BINLOGS:
1596
res = show_binlogs(thd);
1599
case SQLCOM_SHOW_CREATE:
1600
assert(first_table == all_tables && first_table != 0);
1602
res= mysqld_show_create(thd, first_table);
1605
case SQLCOM_CHECKSUM:
1607
assert(first_table == all_tables && first_table != 0);
1608
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1613
assert(first_table == all_tables && first_table != 0);
1614
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1615
/* ! we write after unlocking the table */
1617
Presumably, REPAIR and binlog writing doesn't require synchronization
1619
write_bin_log(thd, true, thd->query, thd->query_length);
1620
select_lex->table_list.first= (unsigned char*) first_table;
1621
lex->query_tables=all_tables;
1626
assert(first_table == all_tables && first_table != 0);
1627
res = mysql_check_table(thd, first_table, &lex->check_opt);
1628
select_lex->table_list.first= (unsigned char*) first_table;
1629
lex->query_tables=all_tables;
1632
case SQLCOM_ANALYZE:
1634
assert(first_table == all_tables && first_table != 0);
1635
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1636
/* ! we write after unlocking the table */
1637
write_bin_log(thd, true, thd->query, thd->query_length);
1638
select_lex->table_list.first= (unsigned char*) first_table;
1639
lex->query_tables=all_tables;
1643
case SQLCOM_OPTIMIZE:
1645
assert(first_table == all_tables && first_table != 0);
1646
res= mysql_optimize_table(thd, first_table, &lex->check_opt);
1647
/* ! we write after unlocking the table */
1648
write_bin_log(thd, true, thd->query, thd->query_length);
1649
select_lex->table_list.first= (unsigned char*) first_table;
1650
lex->query_tables=all_tables;
1654
assert(first_table == all_tables && first_table != 0);
1655
if (update_precheck(thd, all_tables))
1657
assert(select_lex->offset_limit == 0);
1658
unit->set_limit(select_lex);
1659
res= (up_result= mysql_update(thd, all_tables,
1660
select_lex->item_list,
1663
select_lex->order_list.elements,
1664
(order_st *) select_lex->order_list.first,
1665
unit->select_limit_cnt,
1666
lex->duplicates, lex->ignore));
1667
/* mysql_update return 2 if we need to switch to multi-update */
1671
case SQLCOM_UPDATE_MULTI:
1673
assert(first_table == all_tables && first_table != 0);
1674
/* if we switched from normal update, rights are checked */
1677
if ((res= multi_update_precheck(thd, all_tables)))
1683
res= mysql_multi_update_prepare(thd);
1685
/* Check slave filtering rules */
1686
if (unlikely(thd->slave_thread))
1688
if (all_tables_not_ok(thd, all_tables))
1692
res= 0; /* don't care of prev failure */
1693
thd->clear_error(); /* filters are of highest prior */
1695
/* we warn the slave SQL thread */
1696
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1707
some_non_temp_table_to_be_updated(thd, all_tables))
1709
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1714
res= mysql_multi_update(thd, all_tables,
1715
&select_lex->item_list,
1718
select_lex->options,
1719
lex->duplicates, lex->ignore, unit, select_lex);
1722
case SQLCOM_REPLACE:
1725
assert(first_table == all_tables && first_table != 0);
1726
if ((res= insert_precheck(thd, all_tables)))
1729
if (!thd->locked_tables &&
1730
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1736
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1737
lex->update_list, lex->value_list,
1738
lex->duplicates, lex->ignore);
1742
case SQLCOM_REPLACE_SELECT:
1743
case SQLCOM_INSERT_SELECT:
1745
select_result *sel_result;
1746
assert(first_table == all_tables && first_table != 0);
1747
if ((res= insert_precheck(thd, all_tables)))
1750
/* Fix lock for first table */
1751
if (first_table->lock_type == TL_WRITE_DELAYED)
1752
first_table->lock_type= TL_WRITE;
1754
/* Don't unlock tables until command is written to binary log */
1755
select_lex->options|= SELECT_NO_UNLOCK;
1757
unit->set_limit(select_lex);
1759
if (! thd->locked_tables &&
1760
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1766
if (!(res= open_and_lock_tables(thd, all_tables)))
1768
/* Skip first table, which is the table we are inserting in */
1769
TableList *second_table= first_table->next_local;
1770
select_lex->table_list.first= (unsigned char*) second_table;
1771
select_lex->context.table_list=
1772
select_lex->context.first_name_resolution_table= second_table;
1773
res= mysql_insert_select_prepare(thd);
1774
if (!res && (sel_result= new select_insert(first_table,
1782
res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1784
Invalidate the table in the query cache if something changed
1785
after unlocking when changes become visible.
1786
TODO: this is workaround. right way will be move invalidating in
1787
the unlock procedure.
1789
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
1792
/* INSERT ... SELECT should invalidate only the very first table */
1793
TableList *save_table= first_table->next_local;
1794
first_table->next_local= 0;
1795
first_table->next_local= save_table;
1799
/* revert changes for SP */
1800
select_lex->table_list.first= (unsigned char*) first_table;
1805
case SQLCOM_TRUNCATE:
1806
if (end_active_trans(thd))
1811
assert(first_table == all_tables && first_table != 0);
1813
Don't allow this within a transaction because we want to use
1816
if (thd->locked_tables || thd->active_transaction())
1818
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1819
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1823
res= mysql_truncate(thd, first_table, 0);
1828
assert(first_table == all_tables && first_table != 0);
1829
assert(select_lex->offset_limit == 0);
1830
unit->set_limit(select_lex);
1832
if (!thd->locked_tables &&
1833
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1839
res = mysql_delete(thd, all_tables, select_lex->where,
1840
&select_lex->order_list,
1841
unit->select_limit_cnt, select_lex->options,
1845
case SQLCOM_DELETE_MULTI:
1847
assert(first_table == all_tables && first_table != 0);
1848
TableList *aux_tables=
1849
(TableList *)thd->lex->auxiliary_table_list.first;
1850
multi_delete *del_result;
1852
if (!thd->locked_tables &&
1853
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1859
if ((res= multi_delete_precheck(thd, all_tables)))
1862
/* condition will be true on SP re-excuting */
1863
if (select_lex->item_list.elements != 0)
1864
select_lex->item_list.empty();
1865
if (add_item_to_list(thd, new Item_null()))
1868
thd->set_proc_info("init");
1869
if ((res= open_and_lock_tables(thd, all_tables)))
1872
if ((res= mysql_multi_delete_prepare(thd)))
1875
if (!thd->is_fatal_error &&
1876
(del_result= new multi_delete(aux_tables, lex->table_count)))
1878
res= mysql_select(thd, &select_lex->ref_pointer_array,
1879
select_lex->get_table_list(),
1880
select_lex->with_wild,
1881
select_lex->item_list,
1883
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1885
select_lex->options | thd->options |
1886
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1887
OPTION_SETUP_TABLES_DONE,
1888
del_result, unit, select_lex);
1889
res|= thd->is_error();
1891
del_result->abort();
1898
case SQLCOM_DROP_TABLE:
1900
assert(first_table == all_tables && first_table != 0);
1901
if (!lex->drop_temporary)
1903
if (end_active_trans(thd))
1909
If this is a slave thread, we may sometimes execute some
1910
DROP / * 40005 TEMPORARY * / TABLE
1911
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
1912
MASTER TO), while the temporary table has already been dropped.
1913
To not generate such irrelevant "table does not exist errors",
1914
we silently add IF EXISTS if TEMPORARY was used.
1916
if (thd->slave_thread)
1917
lex->drop_if_exists= 1;
1919
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1920
thd->options|= OPTION_KEEP_LOG;
1922
/* DDL and binlog write order protected by LOCK_open */
1923
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
1926
case SQLCOM_SHOW_PROCESSLIST:
1927
mysqld_list_processes(thd, NULL, lex->verbose);
1929
case SQLCOM_SHOW_ENGINE_LOGS:
1931
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
1934
case SQLCOM_CHANGE_DB:
1936
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
1938
if (!mysql_change_db(thd, &db_str, false))
1946
assert(first_table == all_tables && first_table != 0);
1947
if (lex->local_file)
1949
if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
1952
my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
1957
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
1958
lex->update_list, lex->value_list, lex->duplicates,
1959
lex->ignore, (bool) lex->local_file);
1963
case SQLCOM_SET_OPTION:
1965
List<set_var_base> *lex_var_list= &lex->var_list;
1967
if (lex->autocommit && end_active_trans(thd))
1970
if (open_and_lock_tables(thd, all_tables))
1972
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
1974
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
1977
if (!(res= sql_set_variables(thd, lex_var_list)))
1980
If the previous command was a SET ONE_SHOT, we don't want to forget
1981
about the ONE_SHOT property of that SET. So we use a |= instead of = .
1983
thd->one_shot_set|= lex->one_shot_set;
1989
We encountered some sort of error, but no message was sent.
1990
Send something semi-generic here since we don't know which
1991
assignment in the list caused the error.
1993
if (!thd->is_error())
1994
my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2001
case SQLCOM_UNLOCK_TABLES:
2003
It is critical for mysqldump --single-transaction --master-data that
2004
UNLOCK TABLES does not implicitely commit a connection which has only
2005
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2006
false, mysqldump will not work.
2008
unlock_locked_tables(thd);
2009
if (thd->options & OPTION_TABLE_LOCK)
2011
end_active_trans(thd);
2012
thd->options&= ~(OPTION_TABLE_LOCK);
2014
if (thd->global_read_lock)
2015
unlock_global_read_lock(thd);
2018
case SQLCOM_LOCK_TABLES:
2020
We try to take transactional locks if
2021
- only transactional locks are requested (lex->lock_transactional) and
2022
- no non-transactional locks exist (!thd->locked_tables).
2024
if (lex->lock_transactional && !thd->locked_tables)
2028
All requested locks are transactional and no non-transactional
2031
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2039
Non-transactional locking has been requested or
2040
non-transactional locks exist already or transactional locks are
2041
not supported by all storage engines. Take non-transactional
2046
One or more requested locks are non-transactional and/or
2047
non-transactional locks exist or a storage engine does not support
2048
transactional locks. Check if at least one transactional lock is
2049
requested. If yes, warn about the conversion to non-transactional
2050
locks or abort in strict mode.
2052
if (check_transactional_lock(thd, all_tables))
2054
unlock_locked_tables(thd);
2055
/* we must end the trasaction first, regardless of anything */
2056
if (end_active_trans(thd))
2058
thd->in_lock_tables=1;
2059
thd->options|= OPTION_TABLE_LOCK;
2061
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2063
thd->locked_tables=thd->lock;
2065
(void) set_handler_table_locks(thd, all_tables, false);
2071
Need to end the current transaction, so the storage engine (InnoDB)
2072
can free its locks if LOCK TABLES locked some tables before finding
2073
that it can't lock a table in its list
2075
ha_autocommit_or_rollback(thd, 1);
2076
end_active_trans(thd);
2077
thd->options&= ~(OPTION_TABLE_LOCK);
2079
thd->in_lock_tables=0;
2081
case SQLCOM_CREATE_DB:
2084
As mysql_create_db() may modify HA_CREATE_INFO structure passed to
2085
it, we need to use a copy of LEX::create_info to make execution
2086
prepared statement- safe.
2088
HA_CREATE_INFO create_info(lex->create_info);
2089
if (end_active_trans(thd))
2095
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2096
check_db_name(&lex->name))
2098
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2102
If in a slave thread :
2103
CREATE DATABASE DB was certainly not preceded by USE DB.
2104
For that reason, db_ok() in sql/slave.cc did not check the
2105
do_db/ignore_db. And as this query involves no tables, tables_ok()
2106
above was not called. So we have to check rules again here.
2108
if (thd->slave_thread &&
2109
(!rpl_filter->db_ok(lex->name.str) ||
2110
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2112
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2115
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2116
lex->name.str), &create_info, 0);
2119
case SQLCOM_DROP_DB:
2121
if (end_active_trans(thd))
2126
if (check_db_name(&lex->name))
2128
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2132
If in a slave thread :
2133
DROP DATABASE DB may not be preceded by USE DB.
2134
For that reason, maybe db_ok() in sql/slave.cc did not check the
2135
do_db/ignore_db. And as this query involves no tables, tables_ok()
2136
above was not called. So we have to check rules again here.
2138
if (thd->slave_thread &&
2139
(!rpl_filter->db_ok(lex->name.str) ||
2140
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2142
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2145
if (thd->locked_tables || thd->active_transaction())
2147
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2148
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2151
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2154
case SQLCOM_ALTER_DB:
2156
LEX_STRING *db= &lex->name;
2157
HA_CREATE_INFO create_info(lex->create_info);
2158
if (check_db_name(db))
2160
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2164
If in a slave thread :
2165
ALTER DATABASE DB may not be preceded by USE DB.
2166
For that reason, maybe db_ok() in sql/slave.cc did not check the
2167
do_db/ignore_db. And as this query involves no tables, tables_ok()
2168
above was not called. So we have to check rules again here.
2170
if (thd->slave_thread &&
2171
(!rpl_filter->db_ok(db->str) ||
2172
!rpl_filter->db_ok_with_wild_table(db->str)))
2174
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2177
if (thd->locked_tables || thd->active_transaction())
2179
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2180
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2183
res= mysql_alter_db(thd, db->str, &create_info);
2186
case SQLCOM_SHOW_CREATE_DB:
2188
if (check_db_name(&lex->name))
2190
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2193
res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
2199
bool write_to_binlog;
2202
reload_cache() will tell us if we are allowed to write to the
2205
if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
2208
We WANT to write and we CAN write.
2209
! we write after unlocking the table.
2212
Presumably, RESET and binlog writing doesn't require synchronization
2214
write_bin_log(thd, false, thd->query, thd->query_length);
2222
Item *it= (Item *)lex->value_list.head();
2224
if (lex->table_or_sp_used())
2226
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2227
"function calls as part of this statement");
2231
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2233
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2237
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2241
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2243
my_error(ER_XAER_RMFAIL, MYF(0),
2244
xa_state_names[thd->transaction.xid_state.xa_state]);
2248
Breakpoints for backup testing.
2250
if (begin_trans(thd))
2255
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2256
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2260
case SQLCOM_ROLLBACK:
2261
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2262
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2266
case SQLCOM_RELEASE_SAVEPOINT:
2269
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2271
if (my_strnncoll(system_charset_info,
2272
(unsigned char *)lex->ident.str, lex->ident.length,
2273
(unsigned char *)sv->name, sv->length) == 0)
2278
if (ha_release_savepoint(thd, sv))
2279
res= true; // cannot happen
2282
thd->transaction.savepoints=sv->prev;
2285
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2288
case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2291
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2293
if (my_strnncoll(system_charset_info,
2294
(unsigned char *)lex->ident.str, lex->ident.length,
2295
(unsigned char *)sv->name, sv->length) == 0)
2300
if (ha_rollback_to_savepoint(thd, sv))
2301
res= true; // cannot happen
2304
if (((thd->options & OPTION_KEEP_LOG) ||
2305
thd->transaction.all.modified_non_trans_table) &&
2307
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2308
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2309
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2312
thd->transaction.savepoints=sv;
2315
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2318
case SQLCOM_SAVEPOINT:
2319
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2323
SAVEPOINT **sv, *newsv;
2324
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2326
if (my_strnncoll(system_charset_info,
2327
(unsigned char *)lex->ident.str, lex->ident.length,
2328
(unsigned char *)(*sv)->name, (*sv)->length) == 0)
2331
if (*sv) /* old savepoint of the same name exists */
2334
ha_release_savepoint(thd, *sv); // it cannot fail
2337
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2338
savepoint_alloc_size)) == 0)
2340
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2343
newsv->name=strmake_root(&thd->transaction.mem_root,
2344
lex->ident.str, lex->ident.length);
2345
newsv->length=lex->ident.length;
2347
if we'll get an error here, don't add new savepoint to the list.
2348
we'll lose a little bit of memory in transaction mem_root, but it'll
2349
be free'd when transaction ends anyway
2351
if (ha_savepoint(thd, newsv))
2355
newsv->prev=thd->transaction.savepoints;
2356
thd->transaction.savepoints=newsv;
2361
case SQLCOM_BINLOG_BASE64_EVENT:
2363
mysql_client_binlog_statement(thd);
2367
assert(0); /* Impossible */
2371
thd->set_proc_info("query end");
2374
Binlog-related cleanup:
2375
Reset system variables temporarily modified by SET ONE SHOT.
2377
Exception: If this is a SET, do nothing. This is to allow
2378
mysqlbinlog to print many SET commands (in this case we want the
2379
charset temp setting to live until the real query). This is also
2380
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2383
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2384
reset_one_shot_variables(thd);
483
2387
The return value for ROW_COUNT() is "implementation dependent" if the
484
2388
statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
485
2389
wants. We also keep the last value in case of SQLCOM_CALL or
488
if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
2392
if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2393
thd->row_count_func= -1;
2401
if (need_start_waiting)
490
session->row_count_func= -1;
2404
Release the protection against the global read lock and wake
2405
everyone, who might want to set a global read lock.
2407
start_waiting_global_read_lock(thd);
493
return (res || session->is_error());
2409
return(res || thd->is_error());
495
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2412
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
497
LEX *lex= session->lex;
498
2415
select_result *result=lex->result;
500
2417
/* assign global limit variable if limit is not given */
502
Select_Lex *param= lex->unit.global_parameters;
2419
SELECT_LEX *param= lex->unit.global_parameters;
503
2420
if (!param->explicit_limit)
504
2421
param->select_limit=
505
new Item_int((uint64_t) session->variables.select_limit);
2422
new Item_int((uint64_t) thd->variables.select_limit);
507
if (not (res= session->openTablesLock(all_tables)))
2424
if (!(res= open_and_lock_tables(thd, all_tables)))
509
2426
if (lex->describe)