519
169
1 request of thread shutdown, i. e. if command is
520
170
COM_QUIT/COM_SHUTDOWN
522
bool dispatch_command(enum enum_server_command command, THD *thd,
523
char* packet, uint packet_length)
172
bool dispatch_command(enum enum_server_command command, Session *session,
173
char* packet, uint32_t packet_length)
528
thd->command=command;
530
Commands which always take a long time are logged into
531
the slow log only if opt_log_slow_admin_statements is set.
533
thd->enable_slow_log= true;
534
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
536
VOID(pthread_mutex_lock(&LOCK_thread_count));
537
thd->query_id= global_query_id;
176
Query_id &query_id= Query_id::get_query_id();
178
DRIZZLE_COMMAND_START(session->thread_id,
181
session->command= command;
182
session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
184
session->query_id= query_id.value();
539
186
switch( command ) {
540
187
/* Ignore these statements. */
544
190
/* Increase id and count all other statements. */
546
statistic_increment(thd->status_var.questions, &LOCK_status);
192
statistic_increment(session->status_var.questions, &LOCK_status);
551
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
552
VOID(pthread_mutex_unlock(&LOCK_thread_count));
196
/* TODO: set session->lex->sql_command to SQLCOM_END here */
198
plugin::Logging::preDo(session);
200
session->server_status&=
555
201
~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
556
202
switch (command) {
557
203
case COM_INIT_DB:
560
status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
561
thd->convert_string(&tmp, system_charset_info,
562
packet, packet_length, thd->charset());
563
if (!mysql_change_db(thd, &tmp, false))
565
general_log_write(thd, command, thd->db, thd->db_length);
570
case COM_REGISTER_SLAVE:
572
if (!register_slave(thd, (uchar*)packet, packet_length))
576
case COM_CHANGE_USER:
578
status_var_increment(thd->status_var.com_other);
579
char *user= (char*) packet, *packet_end= packet + packet_length;
580
/* Safe because there is always a trailing \0 at the end of the packet */
581
char *passwd= strend(user)+1;
583
thd->clear_error(); // if errors from rollback
586
Old clients send null-terminated string ('\0' for empty string) for
587
password. New clients send the size (1 byte) + string (not null
588
terminated, so also '\0' for empty string).
590
Cast *passwd to an unsigned char, so that it doesn't extend the sign
591
for *passwd > 127 and become 2**32-127 after casting to uint.
593
char db_buff[NAME_LEN+1]; // buffer to store db in utf8
597
If there is no password supplied, the packet must contain '\0',
598
in any type of handshake (4.1 or pre-4.1).
600
if (passwd >= packet_end)
602
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
605
uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
606
(uchar)(*passwd++) : strlen(passwd));
607
uint dummy_errors, save_db_length, db_length;
609
Security_context save_security_ctx= *thd->security_ctx;
610
USER_CONN *save_user_connect;
614
Database name is always NUL-terminated, so in case of empty database
615
the packet must contain at least the trailing '\0'.
617
if (db >= packet_end)
619
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
622
db_length= strlen(db);
624
char *ptr= db + db_length + 1;
627
if (ptr < packet_end)
629
if (ptr + 2 > packet_end)
631
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
635
cs_number= uint2korr(ptr);
638
/* Convert database name to utf8 */
639
db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
640
system_charset_info, db, db_length,
641
thd->charset(), &dummy_errors)]= 0;
644
/* Save user and privileges */
645
save_db_length= thd->db_length;
647
save_user_connect= thd->user_connect;
649
if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
651
thd->security_ctx->user= save_security_ctx.user;
652
my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
656
/* Clear variables that are allocated */
657
thd->user_connect= 0;
658
thd->security_ctx->priv_user= thd->security_ctx->user;
659
res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
663
x_free(thd->security_ctx->user);
664
*thd->security_ctx= save_security_ctx;
665
thd->user_connect= save_user_connect;
667
thd->db_length= save_db_length;
672
x_free(save_security_ctx.user);
676
thd_init_client_charset(thd, cs_number);
677
thd->update_charset();
205
string database_name(packet);
206
NonNormalisedDatabaseName non_normalised_database_name(database_name);
207
NormalisedDatabaseName normalised_database_name(non_normalised_database_name);
209
status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
211
if (! mysql_change_db(session, normalised_database_name, false))
684
if (alloc_query(thd, packet, packet_length))
219
if (! session->readAndStoreQuery(packet, packet_length))
685
220
break; // fatal error is set
686
char *packet_end= thd->query + thd->query_length;
221
DRIZZLE_QUERY_START(session->query,
223
const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
687
224
const char* end_of_stmt= NULL;
689
general_log_write(thd, command, thd->query, thd->query_length);
691
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
693
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
695
char *beginning_of_next_stmt= (char*) end_of_stmt;
697
net_end_statement(thd);
699
Multiple queries exits, execute them individually
701
close_thread_tables(thd);
702
ulong length= (ulong)(packet_end - beginning_of_next_stmt);
704
log_slow_statement(thd);
706
/* Remove garbage at start of query */
707
while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
709
beginning_of_next_stmt++;
713
VOID(pthread_mutex_lock(&LOCK_thread_count));
714
thd->query_length= length;
715
thd->query= beginning_of_next_stmt;
717
Count each statement from the client.
719
statistic_increment(thd->status_var.questions, &LOCK_status);
720
thd->query_id= next_query_id();
721
thd->set_time(); /* Reset the query start time. */
722
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
723
VOID(pthread_mutex_unlock(&LOCK_thread_count));
725
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
729
case COM_FIELD_LIST: // This isn't actually needed
731
char *fields, *packet_end= packet + packet_length, *arg_end;
732
/* Locked closure of all tables */
733
TABLE_LIST table_list;
734
LEX_STRING conv_name;
736
/* used as fields initializator */
739
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
740
bzero((char*) &table_list,sizeof(table_list));
741
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
744
We have name + wildcard in packet, separated by endzero
746
arg_end= strend(packet);
747
thd->convert_string(&conv_name, system_charset_info,
748
packet, (uint) (arg_end - packet), thd->charset());
749
table_list.alias= table_list.table_name= conv_name.str;
752
if (!my_strcasecmp(system_charset_info, table_list.db,
753
INFORMATION_SCHEMA_NAME.str))
755
ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
757
table_list.schema_table= schema_table;
760
thd->query_length= (uint) (packet_end - packet); // Don't count end \0
761
if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
763
general_log_print(thd, command, "%s %s", table_list.table_name, fields);
764
if (lower_case_table_names)
765
my_casedn_str(files_charset_info, table_list.table_name);
767
/* init structures for VIEW processing */
768
table_list.select_lex= &(thd->lex->select_lex);
771
mysql_reset_thd_for_next_command(thd);
774
select_lex.table_list.link_in_list((uchar*) &table_list,
775
(uchar**) &table_list.next_local);
776
thd->lex->add_to_query_tables(&table_list);
778
/* switch on VIEW optimisation: do not fill temporary tables */
779
thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
780
mysqld_list_fields(thd,&table_list,fields);
781
thd->lex->unit.cleanup();
782
thd->cleanup_after_query();
226
mysql_parse(session, session->query, session->query_length, &end_of_stmt);
786
231
/* We don't calculate statistics for this command */
787
general_log_print(thd, command, NullS);
788
net->error=0; // Don't give 'abort' message
789
thd->main_da.disable_status(); // Don't send anything back
232
session->main_da.disable_status(); // Don't send anything back
790
233
error=true; // End server
792
case COM_BINLOG_DUMP:
796
uint32_t slave_server_id;
798
status_var_increment(thd->status_var.com_other);
799
thd->enable_slow_log= opt_log_slow_admin_statements;
800
/* TODO: The following has to be changed to an 8 byte integer */
801
pos = uint4korr(packet);
802
flags = uint2korr(packet + 4);
803
thd->server_id=0; /* avoid suicide */
804
if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
805
kill_zombie_dump_threads(slave_server_id);
806
thd->server_id = slave_server_id;
808
general_log_print(thd, command, "Log: '%s' Pos: %ld", packet+10,
810
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
811
unregister_slave(thd,1,1);
812
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
816
235
case COM_SHUTDOWN:
818
status_var_increment(thd->status_var.com_other);
820
If the client is < 4.1.3, it is going to send us no argument; then
821
packet_length is 0, packet[0] is the end 0 of the packet. Note that
822
SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
825
enum drizzle_enum_shutdown_level level=
826
(enum drizzle_enum_shutdown_level) (uchar) packet[0];
827
if (level == SHUTDOWN_DEFAULT)
828
level= SHUTDOWN_WAIT_ALL_BUFFERS; // soon default will be configurable
829
else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
831
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "this shutdown level");
834
general_log_print(thd, command, NullS);
836
close_thread_tables(thd); // Free before kill
237
status_var_increment(session->status_var.com_other);
239
session->close_thread_tables(); // Free before kill
843
STATUS_VAR current_global_status_var;
846
uint64_t queries_per_second1000;
848
uint buff_len= sizeof(buff);
850
general_log_print(thd, command, NullS);
851
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_STATUS]);
852
calc_sum_of_all_status(¤t_global_status_var);
853
if (!(uptime= (ulong) (thd->start_time - server_start_time)))
854
queries_per_second1000= 0;
856
queries_per_second1000= thd->query_id * 1000LL / uptime;
858
length= snprintf((char*) buff, buff_len - 1,
859
"Uptime: %lu Threads: %d Questions: %lu "
860
"Slow queries: %lu Opens: %lu Flush tables: %lu "
861
"Open tables: %u Queries per second avg: %u.%u",
863
(int) thread_count, (ulong) thd->query_id,
864
current_global_status_var.long_query_count,
865
current_global_status_var.opened_tables,
867
cached_open_tables(),
868
(uint) (queries_per_second1000 / 1000),
869
(uint) (queries_per_second1000 % 1000));
870
/* Store the buffer in permanent memory */
871
my_ok(thd, 0, 0, buff);
872
VOID(my_net_write(net, (uchar*) buff, length));
873
VOID(net_flush(net));
874
thd->main_da.disable_status();
878
status_var_increment(thd->status_var.com_other);
879
my_ok(thd); // Tell client we are alive
881
case COM_PROCESS_INFO:
882
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
883
general_log_print(thd, command, NullS);
884
mysqld_list_processes(thd, NullS, 0);
886
case COM_PROCESS_KILL:
888
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
889
ulong id=(ulong) uint4korr(packet);
890
sql_kill(thd,id,false);
895
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
896
uint opt_command= uint2korr(packet);
898
switch (opt_command) {
899
case (int) MYSQL_OPTION_MULTI_STATEMENTS_ON:
900
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
903
case (int) MYSQL_OPTION_MULTI_STATEMENTS_OFF:
904
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
908
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
245
status_var_increment(session->status_var.com_other);
246
session->my_ok(); // Tell client we are alive
914
249
case COM_CONNECT: // Impossible here
915
case COM_TIME: // Impossible from client
918
252
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
1233
494
variables, but for now this is probably good enough.
1234
495
Don't reset warnings when executing a stored routine.
1236
if (all_tables || !lex->is_single_level_stmt())
1237
mysql_reset_errors(thd, 0);
1239
if (unlikely(thd->slave_thread))
1242
Check if statment should be skipped because of slave filtering
1246
- UPDATE MULTI: For this statement, we want to check the filtering
1247
rules later in the code
1248
- SET: we always execute it (Not that many SET commands exists in
1249
the binary log anyway -- only 4.1 masters write SET statements,
1250
in 5.0 there are no SET statements in the binary log)
1251
- DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1252
have stale files on slave caused by exclusion of one tmp table).
1254
if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1255
!(lex->sql_command == SQLCOM_SET_OPTION) &&
1256
!(lex->sql_command == SQLCOM_DROP_TABLE &&
1257
lex->drop_temporary && lex->drop_if_exists) &&
1258
all_tables_not_ok(thd, all_tables))
1260
/* we warn the slave SQL thread */
1261
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1262
if (thd->one_shot_set)
1265
It's ok to check thd->one_shot_set here:
1267
The charsets in a MySQL 5.0 slave can change by both a binlogged
1268
SET ONE_SHOT statement and the event-internal charset setting,
1269
and these two ways to change charsets do not seems to work
1272
At least there seems to be problems in the rli cache for
1273
charsets if we are using ONE_SHOT. Note that this is normally no
1274
problem because either the >= 5.0 slave reads a 4.1 binlog (with
1275
ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1277
reset_one_shot_variables(thd);
1285
When option readonly is set deny operations which change non-temporary
1286
tables. Except for the replication thread and the 'super' users.
1288
if (deny_updates_if_read_only_option(thd, all_tables))
1290
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1293
} /* endif unlikely slave */
1294
status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1296
assert(thd->transaction.stmt.modified_non_trans_table == false);
1298
switch (lex->sql_command) {
1299
case SQLCOM_SHOW_STATUS:
1301
system_status_var old_status_var= thd->status_var;
1302
thd->initial_status_var= &old_status_var;
1303
res= execute_sqlcom_select(thd, all_tables);
1304
/* Don't log SHOW STATUS commands to slow query log */
1305
thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1306
SERVER_QUERY_NO_GOOD_INDEX_USED);
1308
restore status variables, as we don't want 'show status' to cause
1311
pthread_mutex_lock(&LOCK_status);
1312
add_diff_to_status(&global_status_var, &thd->status_var,
1314
thd->status_var= old_status_var;
1315
pthread_mutex_unlock(&LOCK_status);
1318
case SQLCOM_SHOW_DATABASES:
1319
case SQLCOM_SHOW_TABLES:
1320
case SQLCOM_SHOW_TABLE_STATUS:
1321
case SQLCOM_SHOW_OPEN_TABLES:
1322
case SQLCOM_SHOW_FIELDS:
1323
case SQLCOM_SHOW_KEYS:
1324
case SQLCOM_SHOW_VARIABLES:
1325
case SQLCOM_SHOW_CHARSETS:
1326
case SQLCOM_SHOW_COLLATIONS:
1329
thd->status_var.last_query_cost= 0.0;
1330
res= execute_sqlcom_select(thd, all_tables);
1333
case SQLCOM_EMPTY_QUERY:
1339
res = purge_master_logs(thd, lex->to_log);
1342
case SQLCOM_PURGE_BEFORE:
1346
/* PURGE MASTER LOGS BEFORE 'data' */
1347
it= (Item *)lex->value_list.head();
1348
if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1351
my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1354
it= new Item_func_unix_timestamp(it);
1356
it is OK only emulate fix_fieds, because we need only
1359
it->quick_fix_field();
1360
res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1363
case SQLCOM_SHOW_WARNS:
1365
res= mysqld_show_warnings(thd, (ulong)
1366
((1L << (uint) MYSQL_ERROR::WARN_LEVEL_NOTE) |
1367
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_WARN) |
1368
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR)
1372
case SQLCOM_SHOW_ERRORS:
1374
res= mysqld_show_warnings(thd, (ulong)
1375
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR));
1378
case SQLCOM_SHOW_SLAVE_HOSTS:
1380
res = show_slave_hosts(thd);
1383
case SQLCOM_SHOW_BINLOG_EVENTS:
1385
res = mysql_show_binlog_events(thd);
1389
case SQLCOM_ASSIGN_TO_KEYCACHE:
1391
assert(first_table == all_tables && first_table != 0);
1392
res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1395
case SQLCOM_CHANGE_MASTER:
1397
pthread_mutex_lock(&LOCK_active_mi);
1398
res = change_master(thd,active_mi);
1399
pthread_mutex_unlock(&LOCK_active_mi);
1402
case SQLCOM_SHOW_SLAVE_STAT:
1404
pthread_mutex_lock(&LOCK_active_mi);
1405
if (active_mi != NULL)
1407
res = show_master_info(thd, active_mi);
1411
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1412
"the master info structure does not exist");
1415
pthread_mutex_unlock(&LOCK_active_mi);
1418
case SQLCOM_SHOW_MASTER_STAT:
1420
res = show_binlog_info(thd);
1424
case SQLCOM_SHOW_ENGINE_STATUS:
1426
res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1429
case SQLCOM_CREATE_TABLE:
1431
/* If CREATE TABLE of non-temporary table, do implicit commit */
1432
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1434
if (end_active_trans(thd))
1440
assert(first_table == all_tables && first_table != 0);
1442
// Skip first table, which is the table we are creating
1443
TABLE_LIST *create_table= lex->unlink_first_table(&link_to_local);
1444
TABLE_LIST *select_tables= lex->query_tables;
1446
Code below (especially in mysql_create_table() and select_create
1447
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1448
use a copy of this structure to make execution prepared statement-
1449
safe. A shallow copy is enough as this code won't modify any memory
1450
referenced from this structure.
1452
HA_CREATE_INFO create_info(lex->create_info);
1454
We need to copy alter_info for the same reasons of re-execution
1455
safety, only in case of Alter_info we have to do (almost) a deep
1458
Alter_info alter_info(lex->alter_info, thd->mem_root);
1460
if (thd->is_fatal_error)
1462
/* If out of memory when creating a copy of alter_info. */
1464
goto end_with_restore_list;
1467
if ((res= create_table_precheck(thd, select_tables, create_table)))
1468
goto end_with_restore_list;
1470
/* Might have been updated in create_table_precheck */
1471
create_info.alias= create_table->alias;
1473
#ifdef HAVE_READLINK
1474
/* Fix names if symlinked tables */
1475
if (append_file_to_dir(thd, &create_info.data_file_name,
1476
create_table->table_name) ||
1477
append_file_to_dir(thd, &create_info.index_file_name,
1478
create_table->table_name))
1479
goto end_with_restore_list;
1482
If we are using SET CHARSET without DEFAULT, add an implicit
1483
DEFAULT to not confuse old users. (This may change).
1485
if ((create_info.used_fields &
1486
(HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
1487
HA_CREATE_USED_CHARSET)
1489
create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
1490
create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
1491
create_info.default_table_charset= create_info.table_charset;
1492
create_info.table_charset= 0;
1495
The create-select command will open and read-lock the select table
1496
and then create, open and write-lock the new table. If a global
1497
read lock steps in, we get a deadlock. The write lock waits for
1498
the global read lock, while the global read lock waits for the
1499
select table to be closed. So we wait until the global readlock is
1500
gone before starting both steps. Note that
1501
wait_if_global_read_lock() sets a protection against a new global
1502
read lock when it succeeds. This needs to be released by
1503
start_waiting_global_read_lock(). We protect the normal CREATE
1504
TABLE in the same way. That way we avoid that a new table is
1505
created during a gobal read lock.
1507
if (!thd->locked_tables &&
1508
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1511
goto end_with_restore_list;
1513
if (select_lex->item_list.elements) // With select
1515
select_result *result;
1517
select_lex->options|= SELECT_NO_UNLOCK;
1518
unit->set_limit(select_lex);
1521
Disable non-empty MERGE tables with CREATE...SELECT. Too
1522
complicated. See Bug #26379. Empty MERGE tables are read-only
1523
and don't allow CREATE...SELECT anyway.
1525
if (create_info.used_fields & HA_CREATE_USED_UNION)
1527
my_error(ER_WRONG_OBJECT, MYF(0), create_table->db,
1528
create_table->table_name, "BASE TABLE");
1530
goto end_with_restore_list;
1533
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1535
lex->link_first_table_back(create_table, link_to_local);
1536
create_table->create= true;
1539
if (!(res= open_and_lock_tables(thd, lex->query_tables)))
1542
Is table which we are changing used somewhere in other parts
1545
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1547
TABLE_LIST *duplicate;
1548
create_table= lex->unlink_first_table(&link_to_local);
1549
if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1551
update_non_unique_table_error(create_table, "CREATE", duplicate);
1553
goto end_with_restore_list;
1556
/* If we create merge table, we have to test tables in merge, too */
1557
if (create_info.used_fields & HA_CREATE_USED_UNION)
1560
for (tab= (TABLE_LIST*) create_info.merge_list.first;
1562
tab= tab->next_local)
1564
TABLE_LIST *duplicate;
1565
if ((duplicate= unique_table(thd, tab, select_tables, 0)))
1567
update_non_unique_table_error(tab, "CREATE", duplicate);
1569
goto end_with_restore_list;
1575
select_create is currently not re-execution friendly and
1576
needs to be created for every execution of a PS/SP.
1578
if ((result= new select_create(create_table,
1581
select_lex->item_list,
1587
CREATE from SELECT give its SELECT_LEX for SELECT,
1588
and item_list belong to SELECT
1590
res= handle_select(thd, lex, result, 0);
1594
else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1595
create_table= lex->unlink_first_table(&link_to_local);
1600
/* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1601
if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1602
thd->options|= OPTION_KEEP_LOG;
1603
/* regular create */
1604
if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1605
res= mysql_create_like_table(thd, create_table, select_tables,
1609
res= mysql_create_table(thd, create_table->db,
1610
create_table->table_name, &create_info,
1617
/* put tables back for PS rexecuting */
1618
end_with_restore_list:
1619
lex->link_first_table_back(create_table, link_to_local);
1622
case SQLCOM_CREATE_INDEX:
1624
case SQLCOM_DROP_INDEX:
1626
CREATE INDEX and DROP INDEX are implemented by calling ALTER
1627
TABLE with proper arguments.
1629
In the future ALTER TABLE will notice that the request is to
1630
only add indexes and create these one by one for the existing
1631
table without having to do a full rebuild.
1634
/* Prepare stack copies to be re-execution safe */
1635
HA_CREATE_INFO create_info;
1636
Alter_info alter_info(lex->alter_info, thd->mem_root);
1638
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1641
assert(first_table == all_tables && first_table != 0);
1642
if (end_active_trans(thd))
1645
Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1646
and thus classify as slow administrative statements just like
1649
thd->enable_slow_log= opt_log_slow_admin_statements;
1651
bzero((char*) &create_info, sizeof(create_info));
1652
create_info.db_type= 0;
1653
create_info.row_type= ROW_TYPE_NOT_USED;
1654
create_info.default_table_charset= thd->variables.collation_database;
1656
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1657
&create_info, first_table, &alter_info,
1661
case SQLCOM_SLAVE_START:
1663
pthread_mutex_lock(&LOCK_active_mi);
1664
start_slave(thd,active_mi,1 /* net report*/);
1665
pthread_mutex_unlock(&LOCK_active_mi);
1668
case SQLCOM_SLAVE_STOP:
1670
If the client thread has locked tables, a deadlock is possible.
1672
- the client thread does LOCK TABLE t READ.
1673
- then the master updates t.
1674
- then the SQL slave thread wants to update t,
1675
so it waits for the client thread because t is locked by it.
1676
- then the client thread does SLAVE STOP.
1677
SLAVE STOP waits for the SQL slave thread to terminate its
1678
update t, which waits for the client thread because t is locked by it.
1679
To prevent that, refuse SLAVE STOP if the
1680
client thread has locked tables
1682
if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1684
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1685
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1689
pthread_mutex_lock(&LOCK_active_mi);
1690
stop_slave(thd,active_mi,1/* net report*/);
1691
pthread_mutex_unlock(&LOCK_active_mi);
1695
case SQLCOM_ALTER_TABLE:
1696
assert(first_table == all_tables && first_table != 0);
1699
Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
1700
so we have to use a copy of this structure to make execution
1701
prepared statement- safe. A shallow copy is enough as no memory
1702
referenced from this structure will be modified.
1704
HA_CREATE_INFO create_info(lex->create_info);
1705
Alter_info alter_info(lex->alter_info, thd->mem_root);
1707
if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1712
/* Must be set in the parser */
1713
assert(select_lex->db);
1715
{ // Rename of table
1716
TABLE_LIST tmp_table;
1717
bzero((char*) &tmp_table,sizeof(tmp_table));
1718
tmp_table.table_name= lex->name.str;
1719
tmp_table.db=select_lex->db;
1722
/* Don't yet allow changing of symlinks with ALTER TABLE */
1723
if (create_info.data_file_name)
1724
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1725
"DATA DIRECTORY option ignored");
1726
if (create_info.index_file_name)
1727
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1728
"INDEX DIRECTORY option ignored");
1729
create_info.data_file_name= create_info.index_file_name= NULL;
1730
/* ALTER TABLE ends previous transaction */
1731
if (end_active_trans(thd))
1734
if (!thd->locked_tables &&
1735
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1741
thd->enable_slow_log= opt_log_slow_admin_statements;
1742
res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1746
select_lex->order_list.elements,
1747
(ORDER *) select_lex->order_list.first,
1751
case SQLCOM_RENAME_TABLE:
1753
assert(first_table == all_tables && first_table != 0);
1755
for (table= first_table; table; table= table->next_local->next_local)
1757
TABLE_LIST old_list, new_list;
1759
we do not need initialize old_list and new_list because we will
1760
come table[0] and table->next[0] there
1763
new_list= table->next_local[0];
1766
if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1772
case SQLCOM_SHOW_BINLOGS:
1774
res = show_binlogs(thd);
1777
case SQLCOM_SHOW_CREATE:
1778
assert(first_table == all_tables && first_table != 0);
1780
res= mysqld_show_create(thd, first_table);
1783
case SQLCOM_CHECKSUM:
1785
assert(first_table == all_tables && first_table != 0);
1786
res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1791
assert(first_table == all_tables && first_table != 0);
1792
thd->enable_slow_log= opt_log_slow_admin_statements;
1793
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1794
/* ! we write after unlocking the table */
1795
if (!res && !lex->no_write_to_binlog)
1798
Presumably, REPAIR and binlog writing doesn't require synchronization
1800
write_bin_log(thd, true, thd->query, thd->query_length);
1802
select_lex->table_list.first= (uchar*) first_table;
1803
lex->query_tables=all_tables;
1808
assert(first_table == all_tables && first_table != 0);
1809
thd->enable_slow_log= opt_log_slow_admin_statements;
1810
res = mysql_check_table(thd, first_table, &lex->check_opt);
1811
select_lex->table_list.first= (uchar*) first_table;
1812
lex->query_tables=all_tables;
1815
case SQLCOM_ANALYZE:
1817
assert(first_table == all_tables && first_table != 0);
1818
thd->enable_slow_log= opt_log_slow_admin_statements;
1819
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1820
/* ! we write after unlocking the table */
1821
if (!res && !lex->no_write_to_binlog)
1824
Presumably, ANALYZE and binlog writing doesn't require synchronization
1826
write_bin_log(thd, true, thd->query, thd->query_length);
1828
select_lex->table_list.first= (uchar*) first_table;
1829
lex->query_tables=all_tables;
1833
case SQLCOM_OPTIMIZE:
1835
assert(first_table == all_tables && first_table != 0);
1836
thd->enable_slow_log= opt_log_slow_admin_statements;
1837
res= (specialflag & (SPECIAL_SAFE_MODE | SPECIAL_NO_NEW_FUNC)) ?
1838
mysql_recreate_table(thd, first_table) :
1839
mysql_optimize_table(thd, first_table, &lex->check_opt);
1840
/* ! we write after unlocking the table */
1841
if (!res && !lex->no_write_to_binlog)
1844
Presumably, OPTIMIZE and binlog writing doesn't require synchronization
1846
write_bin_log(thd, true, thd->query, thd->query_length);
1848
select_lex->table_list.first= (uchar*) first_table;
1849
lex->query_tables=all_tables;
1853
assert(first_table == all_tables && first_table != 0);
1854
if (update_precheck(thd, all_tables))
1856
assert(select_lex->offset_limit == 0);
1857
unit->set_limit(select_lex);
1858
res= (up_result= mysql_update(thd, all_tables,
1859
select_lex->item_list,
1862
select_lex->order_list.elements,
1863
(ORDER *) select_lex->order_list.first,
1864
unit->select_limit_cnt,
1865
lex->duplicates, lex->ignore));
1866
/* mysql_update return 2 if we need to switch to multi-update */
1870
case SQLCOM_UPDATE_MULTI:
1872
assert(first_table == all_tables && first_table != 0);
1873
/* if we switched from normal update, rights are checked */
1876
if ((res= multi_update_precheck(thd, all_tables)))
1882
res= mysql_multi_update_prepare(thd);
1884
/* Check slave filtering rules */
1885
if (unlikely(thd->slave_thread))
1887
if (all_tables_not_ok(thd, all_tables))
1891
res= 0; /* don't care of prev failure */
1892
thd->clear_error(); /* filters are of highest prior */
1894
/* we warn the slave SQL thread */
1895
my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1906
some_non_temp_table_to_be_updated(thd, all_tables))
1908
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1913
res= mysql_multi_update(thd, all_tables,
1914
&select_lex->item_list,
1917
select_lex->options,
1918
lex->duplicates, lex->ignore, unit, select_lex);
1921
case SQLCOM_REPLACE:
1924
assert(first_table == all_tables && first_table != 0);
1925
if ((res= insert_precheck(thd, all_tables)))
1928
if (!thd->locked_tables &&
1929
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1935
res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1936
lex->update_list, lex->value_list,
1937
lex->duplicates, lex->ignore);
1941
case SQLCOM_REPLACE_SELECT:
1942
case SQLCOM_INSERT_SELECT:
1944
select_result *sel_result;
1945
assert(first_table == all_tables && first_table != 0);
1946
if ((res= insert_precheck(thd, all_tables)))
1949
/* Fix lock for first table */
1950
if (first_table->lock_type == TL_WRITE_DELAYED)
1951
first_table->lock_type= TL_WRITE;
1953
/* Don't unlock tables until command is written to binary log */
1954
select_lex->options|= SELECT_NO_UNLOCK;
1956
unit->set_limit(select_lex);
1958
if (! thd->locked_tables &&
1959
! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1965
if (!(res= open_and_lock_tables(thd, all_tables)))
1967
/* Skip first table, which is the table we are inserting in */
1968
TABLE_LIST *second_table= first_table->next_local;
1969
select_lex->table_list.first= (uchar*) second_table;
1970
select_lex->context.table_list=
1971
select_lex->context.first_name_resolution_table= second_table;
1972
res= mysql_insert_select_prepare(thd);
1973
if (!res && (sel_result= new select_insert(first_table,
1981
res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1983
Invalidate the table in the query cache if something changed
1984
after unlocking when changes become visible.
1985
TODO: this is workaround. right way will be move invalidating in
1986
the unlock procedure.
1988
if (first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
1991
/* INSERT ... SELECT should invalidate only the very first table */
1992
TABLE_LIST *save_table= first_table->next_local;
1993
first_table->next_local= 0;
1994
first_table->next_local= save_table;
1998
/* revert changes for SP */
1999
select_lex->table_list.first= (uchar*) first_table;
2004
case SQLCOM_TRUNCATE:
2005
if (end_active_trans(thd))
2010
assert(first_table == all_tables && first_table != 0);
2012
Don't allow this within a transaction because we want to use
2015
if (thd->locked_tables || thd->active_transaction())
2017
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2018
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2022
res= mysql_truncate(thd, first_table, 0);
2027
assert(first_table == all_tables && first_table != 0);
2028
assert(select_lex->offset_limit == 0);
2029
unit->set_limit(select_lex);
2031
if (!thd->locked_tables &&
2032
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2038
res = mysql_delete(thd, all_tables, select_lex->where,
2039
&select_lex->order_list,
2040
unit->select_limit_cnt, select_lex->options,
2044
case SQLCOM_DELETE_MULTI:
2046
assert(first_table == all_tables && first_table != 0);
2047
TABLE_LIST *aux_tables=
2048
(TABLE_LIST *)thd->lex->auxiliary_table_list.first;
2049
multi_delete *del_result;
2051
if (!thd->locked_tables &&
2052
!(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
2058
if ((res= multi_delete_precheck(thd, all_tables)))
2061
/* condition will be true on SP re-excuting */
2062
if (select_lex->item_list.elements != 0)
2063
select_lex->item_list.empty();
2064
if (add_item_to_list(thd, new Item_null()))
2067
thd_proc_info(thd, "init");
2068
if ((res= open_and_lock_tables(thd, all_tables)))
2071
if ((res= mysql_multi_delete_prepare(thd)))
2074
if (!thd->is_fatal_error &&
2075
(del_result= new multi_delete(aux_tables, lex->table_count)))
2077
res= mysql_select(thd, &select_lex->ref_pointer_array,
2078
select_lex->get_table_list(),
2079
select_lex->with_wild,
2080
select_lex->item_list,
2082
0, (ORDER *)NULL, (ORDER *)NULL, (Item *)NULL,
2084
select_lex->options | thd->options |
2085
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
2086
OPTION_SETUP_TABLES_DONE,
2087
del_result, unit, select_lex);
2088
res|= thd->is_error();
2090
del_result->abort();
2097
case SQLCOM_DROP_TABLE:
2099
assert(first_table == all_tables && first_table != 0);
2100
if (!lex->drop_temporary)
2102
if (end_active_trans(thd))
2108
If this is a slave thread, we may sometimes execute some
2109
DROP / * 40005 TEMPORARY * / TABLE
2110
that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
2111
MASTER TO), while the temporary table has already been dropped.
2112
To not generate such irrelevant "table does not exist errors",
2113
we silently add IF EXISTS if TEMPORARY was used.
2115
if (thd->slave_thread)
2116
lex->drop_if_exists= 1;
2118
/* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
2119
thd->options|= OPTION_KEEP_LOG;
2121
/* DDL and binlog write order protected by LOCK_open */
2122
res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
2125
case SQLCOM_SHOW_PROCESSLIST:
2126
mysqld_list_processes(thd, NullS, lex->verbose);
2128
case SQLCOM_SHOW_ENGINE_LOGS:
2130
res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
2133
case SQLCOM_CHANGE_DB:
2135
LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2137
if (!mysql_change_db(thd, &db_str, false))
2145
assert(first_table == all_tables && first_table != 0);
2146
if (lex->local_file)
2148
if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
2151
my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2156
res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
2157
lex->update_list, lex->value_list, lex->duplicates,
2158
lex->ignore, (bool) lex->local_file);
2162
case SQLCOM_SET_OPTION:
2164
List<set_var_base> *lex_var_list= &lex->var_list;
2166
if (lex->autocommit && end_active_trans(thd))
2169
if (open_and_lock_tables(thd, all_tables))
2171
if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2173
my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2176
if (!(res= sql_set_variables(thd, lex_var_list)))
2179
If the previous command was a SET ONE_SHOT, we don't want to forget
2180
about the ONE_SHOT property of that SET. So we use a |= instead of = .
2182
thd->one_shot_set|= lex->one_shot_set;
2188
We encountered some sort of error, but no message was sent.
2189
Send something semi-generic here since we don't know which
2190
assignment in the list caused the error.
2192
if (!thd->is_error())
2193
my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2200
case SQLCOM_UNLOCK_TABLES:
2202
It is critical for mysqldump --single-transaction --master-data that
2203
UNLOCK TABLES does not implicitely commit a connection which has only
2204
done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2205
false, mysqldump will not work.
2207
unlock_locked_tables(thd);
2208
if (thd->options & OPTION_TABLE_LOCK)
2210
end_active_trans(thd);
2211
thd->options&= ~(OPTION_TABLE_LOCK);
2213
if (thd->global_read_lock)
2214
unlock_global_read_lock(thd);
2217
case SQLCOM_LOCK_TABLES:
2219
We try to take transactional locks if
2220
- only transactional locks are requested (lex->lock_transactional) and
2221
- no non-transactional locks exist (!thd->locked_tables).
2223
if (lex->lock_transactional && !thd->locked_tables)
2227
All requested locks are transactional and no non-transactional
2230
if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2238
Non-transactional locking has been requested or
2239
non-transactional locks exist already or transactional locks are
2240
not supported by all storage engines. Take non-transactional
2245
One or more requested locks are non-transactional and/or
2246
non-transactional locks exist or a storage engine does not support
2247
transactional locks. Check if at least one transactional lock is
2248
requested. If yes, warn about the conversion to non-transactional
2249
locks or abort in strict mode.
2251
if (check_transactional_lock(thd, all_tables))
2253
unlock_locked_tables(thd);
2254
/* we must end the trasaction first, regardless of anything */
2255
if (end_active_trans(thd))
2257
thd->in_lock_tables=1;
2258
thd->options|= OPTION_TABLE_LOCK;
2260
if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2262
thd->locked_tables=thd->lock;
2264
(void) set_handler_table_locks(thd, all_tables, false);
2270
Need to end the current transaction, so the storage engine (InnoDB)
2271
can free its locks if LOCK TABLES locked some tables before finding
2272
that it can't lock a table in its list
2274
ha_autocommit_or_rollback(thd, 1);
2275
end_active_trans(thd);
2276
thd->options&= ~(OPTION_TABLE_LOCK);
2278
thd->in_lock_tables=0;
2280
case SQLCOM_CREATE_DB:
2283
As mysql_create_db() may modify HA_CREATE_INFO structure passed to
2284
it, we need to use a copy of LEX::create_info to make execution
2285
prepared statement- safe.
2287
HA_CREATE_INFO create_info(lex->create_info);
2288
if (end_active_trans(thd))
2294
if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2295
check_db_name(&lex->name))
2297
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2301
If in a slave thread :
2302
CREATE DATABASE DB was certainly not preceded by USE DB.
2303
For that reason, db_ok() in sql/slave.cc did not check the
2304
do_db/ignore_db. And as this query involves no tables, tables_ok()
2305
above was not called. So we have to check rules again here.
2307
if (thd->slave_thread &&
2308
(!rpl_filter->db_ok(lex->name.str) ||
2309
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2311
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2314
res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2315
lex->name.str), &create_info, 0);
2318
case SQLCOM_DROP_DB:
2320
if (end_active_trans(thd))
2325
if (check_db_name(&lex->name))
2327
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2331
If in a slave thread :
2332
DROP DATABASE DB may not be preceded by USE DB.
2333
For that reason, maybe db_ok() in sql/slave.cc did not check the
2334
do_db/ignore_db. And as this query involves no tables, tables_ok()
2335
above was not called. So we have to check rules again here.
2337
if (thd->slave_thread &&
2338
(!rpl_filter->db_ok(lex->name.str) ||
2339
!rpl_filter->db_ok_with_wild_table(lex->name.str)))
2341
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2344
if (thd->locked_tables || thd->active_transaction())
2346
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2347
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2350
res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2353
case SQLCOM_ALTER_DB_UPGRADE:
2355
LEX_STRING *db= & lex->name;
2356
if (end_active_trans(thd))
2361
if (thd->slave_thread &&
2362
(!rpl_filter->db_ok(db->str) ||
2363
!rpl_filter->db_ok_with_wild_table(db->str)))
2366
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2369
if (check_db_name(db))
2371
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2374
if (thd->locked_tables || thd->active_transaction())
2377
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2378
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2382
res= mysql_upgrade_db(thd, db);
2387
case SQLCOM_ALTER_DB:
2389
LEX_STRING *db= &lex->name;
2390
HA_CREATE_INFO create_info(lex->create_info);
2391
if (check_db_name(db))
2393
my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2397
If in a slave thread :
2398
ALTER DATABASE DB may not be preceded by USE DB.
2399
For that reason, maybe db_ok() in sql/slave.cc did not check the
2400
do_db/ignore_db. And as this query involves no tables, tables_ok()
2401
above was not called. So we have to check rules again here.
2403
if (thd->slave_thread &&
2404
(!rpl_filter->db_ok(db->str) ||
2405
!rpl_filter->db_ok_with_wild_table(db->str)))
2407
my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2410
if (thd->locked_tables || thd->active_transaction())
2412
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2413
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2416
res= mysql_alter_db(thd, db->str, &create_info);
2419
case SQLCOM_SHOW_CREATE_DB:
2421
if (check_db_name(&lex->name))
2423
my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2426
res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
2431
RESET commands are never written to the binary log, so we have to
2432
initialize this variable because RESET shares the same code as FLUSH
2434
lex->no_write_to_binlog= 1;
2437
bool write_to_binlog;
2440
reload_cache() will tell us if we are allowed to write to the
2443
if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
2446
We WANT to write and we CAN write.
2447
! we write after unlocking the table.
2450
Presumably, RESET and binlog writing doesn't require synchronization
2452
if (!lex->no_write_to_binlog && write_to_binlog)
2454
write_bin_log(thd, false, thd->query, thd->query_length);
2463
Item *it= (Item *)lex->value_list.head();
2465
if (lex->table_or_sp_used())
2467
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2468
"function calls as part of this statement");
2472
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2474
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2478
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2482
if (thd->transaction.xid_state.xa_state != XA_NOTR)
2484
my_error(ER_XAER_RMFAIL, MYF(0),
2485
xa_state_names[thd->transaction.xid_state.xa_state]);
2489
Breakpoints for backup testing.
2491
if (begin_trans(thd))
2496
if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2497
lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2501
case SQLCOM_ROLLBACK:
2502
if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2503
lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2507
case SQLCOM_RELEASE_SAVEPOINT:
2510
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2512
if (my_strnncoll(system_charset_info,
2513
(uchar *)lex->ident.str, lex->ident.length,
2514
(uchar *)sv->name, sv->length) == 0)
2519
if (ha_release_savepoint(thd, sv))
2520
res= true; // cannot happen
2523
thd->transaction.savepoints=sv->prev;
2526
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2529
case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2532
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2534
if (my_strnncoll(system_charset_info,
2535
(uchar *)lex->ident.str, lex->ident.length,
2536
(uchar *)sv->name, sv->length) == 0)
2541
if (ha_rollback_to_savepoint(thd, sv))
2542
res= true; // cannot happen
2545
if (((thd->options & OPTION_KEEP_LOG) ||
2546
thd->transaction.all.modified_non_trans_table) &&
2548
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2549
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2550
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2553
thd->transaction.savepoints=sv;
2556
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2559
case SQLCOM_SAVEPOINT:
2560
if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2561
thd->in_sub_stmt) || !opt_using_transactions)
2565
SAVEPOINT **sv, *newsv;
2566
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2568
if (my_strnncoll(system_charset_info,
2569
(uchar *)lex->ident.str, lex->ident.length,
2570
(uchar *)(*sv)->name, (*sv)->length) == 0)
2573
if (*sv) /* old savepoint of the same name exists */
2576
ha_release_savepoint(thd, *sv); // it cannot fail
2579
else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2580
savepoint_alloc_size)) == 0)
2582
my_error(ER_OUT_OF_RESOURCES, MYF(0));
2585
newsv->name=strmake_root(&thd->transaction.mem_root,
2586
lex->ident.str, lex->ident.length);
2587
newsv->length=lex->ident.length;
2589
if we'll get an error here, don't add new savepoint to the list.
2590
we'll lose a little bit of memory in transaction mem_root, but it'll
2591
be free'd when transaction ends anyway
2593
if (ha_savepoint(thd, newsv))
2597
newsv->prev=thd->transaction.savepoints;
2598
thd->transaction.savepoints=newsv;
2603
case SQLCOM_BINLOG_BASE64_EVENT:
2605
mysql_client_binlog_statement(thd);
2609
assert(0); /* Impossible */
2613
thd_proc_info(thd, "query end");
2616
Binlog-related cleanup:
2617
Reset system variables temporarily modified by SET ONE SHOT.
2619
Exception: If this is a SET, do nothing. This is to allow
2620
mysqlbinlog to print many SET commands (in this case we want the
2621
charset temp setting to live until the real query). This is also
2622
needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2625
if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2626
reset_one_shot_variables(thd);
497
if (all_tables || ! lex->is_single_level_stmt())
499
drizzle_reset_errors(session, 0);
502
status_var_increment(session->status_var.com_stat[lex->sql_command]);
504
assert(session->transaction.stmt.modified_non_trans_table == false);
506
/* now we are ready to execute the statement */
507
res= lex->statement->execute();
509
session->set_proc_info("query end");
2629
512
The return value for ROW_COUNT() is "implementation dependent" if the