13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
#include <drizzled/server_includes.h>
17
#include "mysql_priv.h"
18
18
#include "sql_repl.h"
19
19
#include "rpl_filter.h"
20
20
#include "repl_failsafe.h"
22
#include <drizzled/drizzled_error_messages.h>
25
26
@defgroup Runtime_Environment Runtime Environment
30
static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables);
30
32
const char *any_db="*any*"; // Special symbol for check_access
32
const LEX_STRING command_name[COM_END+1]={
34
const LEX_STRING command_name[]={
33
35
{ C_STRING_WITH_LEN("Sleep") },
34
36
{ C_STRING_WITH_LEN("Quit") },
35
37
{ C_STRING_WITH_LEN("Init DB") },
39
41
{ C_STRING_WITH_LEN("Drop DB") },
40
42
{ C_STRING_WITH_LEN("Refresh") },
41
43
{ C_STRING_WITH_LEN("Shutdown") },
44
{ C_STRING_WITH_LEN("Statistics") },
42
45
{ C_STRING_WITH_LEN("Processlist") },
43
46
{ C_STRING_WITH_LEN("Connect") },
44
47
{ C_STRING_WITH_LEN("Kill") },
48
{ C_STRING_WITH_LEN("Debug") },
45
49
{ C_STRING_WITH_LEN("Ping") },
46
50
{ C_STRING_WITH_LEN("Time") },
51
{ C_STRING_WITH_LEN("Delayed insert") },
47
52
{ C_STRING_WITH_LEN("Change user") },
48
53
{ C_STRING_WITH_LEN("Binlog Dump") },
54
{ C_STRING_WITH_LEN("Table Dump") },
49
55
{ C_STRING_WITH_LEN("Connect Out") },
50
56
{ C_STRING_WITH_LEN("Register Slave") },
57
{ C_STRING_WITH_LEN("Prepare") },
58
{ C_STRING_WITH_LEN("Execute") },
59
{ C_STRING_WITH_LEN("Long Data") },
60
{ C_STRING_WITH_LEN("Close stmt") },
61
{ C_STRING_WITH_LEN("Reset stmt") },
51
62
{ C_STRING_WITH_LEN("Set option") },
63
{ C_STRING_WITH_LEN("Fetch") },
52
64
{ C_STRING_WITH_LEN("Daemon") },
53
65
{ C_STRING_WITH_LEN("Error") } // Last command number
129
141
Returns true if all tables should be ignored.
131
inline bool all_tables_not_ok(THD *thd, TableList *tables)
143
inline bool all_tables_not_ok(THD *thd, TABLE_LIST *tables)
133
145
return rpl_filter->is_on() && tables &&
134
146
!rpl_filter->tables_ok(thd->db, tables);
138
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
150
static bool some_non_temp_table_to_be_updated(THD *thd, TABLE_LIST *tables)
140
for (TableList *table= tables; table; table= table->next_global)
152
for (TABLE_LIST *table= tables; table; table= table->next_global)
142
154
assert(table->db && table->table_name);
143
155
if (table->updating &&
161
173
a number of modified rows
164
uint32_t sql_command_flags[SQLCOM_END+1];
176
uint sql_command_flags[SQLCOM_END+1];
166
178
void init_update_queries(void)
168
memset(&sql_command_flags, 0, sizeof(sql_command_flags));
180
bzero((uchar*) &sql_command_flags, sizeof(sql_command_flags));
170
182
sql_command_flags[SQLCOM_CREATE_TABLE]= CF_CHANGES_DATA;
171
183
sql_command_flags[SQLCOM_CREATE_INDEX]= CF_CHANGES_DATA;
405
417
if (packet_length == 0) /* safety */
407
419
/* Initialize with COM_SLEEP packet */
408
packet[0]= (unsigned char) COM_SLEEP;
420
packet[0]= (uchar) COM_SLEEP;
409
421
packet_length= 1;
411
423
/* Do not rely on my_net_read, extra safety against programming errors. */
412
424
packet[packet_length]= '\0'; /* safety */
414
command= (enum enum_server_command) (unsigned char) packet[0];
426
command= (enum enum_server_command) (uchar) packet[0];
416
428
if (command >= COM_END)
417
429
command= COM_END; // Wrong command
521
533
thd->enable_slow_log= true;
522
534
thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
524
pthread_mutex_lock(&LOCK_thread_count);
536
VOID(pthread_mutex_lock(&LOCK_thread_count));
525
537
thd->query_id= global_query_id;
527
539
switch( command ) {
528
540
/* Ignore these statements. */
531
544
/* Increase id and count all other statements. */
551
562
packet, packet_length, thd->charset());
552
563
if (!mysql_change_db(thd, &tmp, false))
565
general_log_write(thd, command, thd->db, thd->db_length);
558
570
case COM_REGISTER_SLAVE:
560
if (!register_slave(thd, (unsigned char*)packet, packet_length))
572
if (!register_slave(thd, (uchar*)packet, packet_length))
566
578
status_var_increment(thd->status_var.com_other);
567
579
char *user= (char*) packet, *packet_end= packet + packet_length;
568
580
/* Safe because there is always a trailing \0 at the end of the packet */
569
char *passwd= strchr(user, '\0')+1;
581
char *passwd= strend(user)+1;
572
583
thd->clear_error(); // if errors from rollback
591
602
my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
594
uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
595
(unsigned char)(*passwd++) : strlen(passwd));
596
uint32_t dummy_errors, save_db_length, db_length;
605
uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
606
(uchar)(*passwd++) : strlen(passwd));
607
uint dummy_errors, save_db_length, db_length;
598
609
Security_context save_security_ctx= *thd->security_ctx;
599
610
USER_CONN *save_user_connect;
645
656
/* Clear variables that are allocated */
646
657
thd->user_connect= 0;
647
res= check_user(thd, passwd, passwd_len, db, false);
658
thd->security_ctx->priv_user= thd->security_ctx->user;
659
res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
651
if (thd->security_ctx->user)
652
free(thd->security_ctx->user);
663
x_free(thd->security_ctx->user);
653
664
*thd->security_ctx= save_security_ctx;
654
665
thd->user_connect= save_user_connect;
655
666
thd->db= save_db;
677
686
char *packet_end= thd->query + thd->query_length;
678
687
const char* end_of_stmt= NULL;
689
general_log_write(thd, command, thd->query, thd->query_length);
680
691
mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
682
693
while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
709
720
thd->query_id= next_query_id();
710
721
thd->set_time(); /* Reset the query start time. */
711
722
/* TODO: set thd->lex->sql_command to SQLCOM_END here */
712
pthread_mutex_unlock(&LOCK_thread_count);
723
VOID(pthread_mutex_unlock(&LOCK_thread_count));
714
725
mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
720
731
char *fields, *packet_end= packet + packet_length, *arg_end;
721
732
/* Locked closure of all tables */
722
TableList table_list;
733
TABLE_LIST table_list;
723
734
LEX_STRING conv_name;
725
736
/* used as fields initializator */
728
739
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
memset(&table_list, 0, sizeof(table_list));
740
bzero((char*) &table_list,sizeof(table_list));
730
741
if (thd->copy_db_to(&table_list.db, &table_list.db_length))
733
744
We have name + wildcard in packet, separated by endzero
735
arg_end= strchr(packet, '\0');
746
arg_end= strend(packet);
736
747
thd->convert_string(&conv_name, system_charset_info,
737
packet, (uint32_t) (arg_end - packet), thd->charset());
748
packet, (uint) (arg_end - packet), thd->charset());
738
749
table_list.alias= table_list.table_name= conv_name.str;
739
750
packet= arg_end + 1;
746
757
table_list.schema_table= schema_table;
749
thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
760
thd->query_length= (uint) (packet_end - packet); // Don't count end \0
750
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);
752
764
if (lower_case_table_names)
753
765
my_casedn_str(files_charset_info, table_list.table_name);
759
771
mysql_reset_thd_for_next_command(thd);
762
select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
(unsigned char**) &table_list.next_local);
774
select_lex.table_list.link_in_list((uchar*) &table_list,
775
(uchar**) &table_list.next_local);
764
776
thd->lex->add_to_query_tables(&table_list);
766
778
/* switch on VIEW optimisation: do not fill temporary tables */
792
805
kill_zombie_dump_threads(slave_server_id);
793
806
thd->server_id = slave_server_id;
808
general_log_print(thd, command, "Log: '%s' Pos: %ld", packet+10,
795
810
mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
796
811
unregister_slave(thd,1,1);
797
812
/* fake COM_QUIT -- if we get here, the thread needs to terminate */
801
816
case COM_SHUTDOWN:
803
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 mysql_enum_shutdown_level level=
826
(enum mysql_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);
805
836
close_thread_tables(thd); // 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();
811
878
status_var_increment(thd->status_var.com_other);
812
879
my_ok(thd); // Tell client we are alive
814
881
case COM_PROCESS_INFO:
815
882
status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
mysqld_list_processes(thd, NULL, 0);
883
general_log_print(thd, command, NullS);
884
mysqld_list_processes(thd, NullS, 0);
818
886
case COM_PROCESS_KILL:
825
893
case COM_SET_OPTION:
827
895
status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
828
uint32_t opt_command= uint2korr(packet);
896
uint opt_command= uint2korr(packet);
830
898
switch (opt_command) {
831
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
899
case (int) MYSQL_OPTION_MULTI_STATEMENTS_ON:
832
900
thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
835
case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
903
case (int) MYSQL_OPTION_MULTI_STATEMENTS_OFF:
836
904
thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
874
942
net_end_statement(thd);
876
thd->set_proc_info("closing tables");
944
thd->proc_info= "closing tables";
877
945
/* Free tables */
878
946
close_thread_tables(thd);
880
948
log_slow_statement(thd);
882
950
thd_proc_info(thd, "cleaning up");
883
pthread_mutex_lock(&LOCK_thread_count); // For process list
951
VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list
884
952
thd_proc_info(thd, 0);
885
953
thd->command=COM_SLEEP;
887
955
thd->query_length=0;
888
956
thread_running--;
889
pthread_mutex_unlock(&LOCK_thread_count);
957
VOID(pthread_mutex_unlock(&LOCK_thread_count));
890
958
thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
891
959
free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
903
971
if (unlikely(thd->in_sub_stmt))
904
972
return; // Don't set time for sub stmt
906
logging_post_do(thd);
975
Do not log administrative statements unless the appropriate option is
976
set; do not log into slow log if reading from backup.
978
if (thd->enable_slow_log && !thd->user_time)
980
thd_proc_info(thd, "logging slow query");
981
uint64_t end_utime_of_query= thd->current_utime();
983
if (((end_utime_of_query - thd->utime_after_lock) >
984
thd->variables.long_query_time ||
985
((thd->server_status &
986
(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
987
opt_log_queries_not_using_indexes &&
988
!(sql_command_flags[thd->lex->sql_command] & CF_STATUS_COMMAND))) &&
989
thd->examined_row_count >= thd->variables.min_examined_row_limit)
991
thd_proc_info(thd, "logging slow query");
992
thd->status_var.long_query_count++;
993
slow_log_print(thd, thd->query, thd->query_length, end_utime_of_query);
913
Create a TableList object for an INFORMATION_SCHEMA table.
1001
Create a TABLE_LIST object for an INFORMATION_SCHEMA table.
915
1003
This function is used in the parser to convert a SHOW or DESCRIBE
916
1004
table_name command to a SELECT from INFORMATION_SCHEMA.
917
It prepares a SELECT_LEX and a TableList object to represent the
1005
It prepares a SELECT_LEX and a TABLE_LIST object to represent the
918
1006
given command as a SELECT parse tree.
920
1008
@param thd thread handle
1019
1107
true error; In this case thd->fatal_error is set
1022
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
1110
bool alloc_query(THD *thd, const char *packet, uint packet_length)
1024
1112
/* Remove garbage at start and end of query */
1025
1113
while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
1108
1196
/* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1109
1197
SELECT_LEX *select_lex= &lex->select_lex;
1110
1198
/* first table of first SELECT_LEX */
1111
TableList *first_table= (TableList*) select_lex->table_list.first;
1199
TABLE_LIST *first_table= (TABLE_LIST*) select_lex->table_list.first;
1112
1200
/* list of all tables in query */
1113
TableList *all_tables;
1201
TABLE_LIST *all_tables;
1114
1202
/* most outer SELECT_LEX_UNIT of query */
1115
1203
SELECT_LEX_UNIT *unit= &lex->unit;
1116
1204
/* Saved variable value */
1275
1363
case SQLCOM_SHOW_WARNS:
1277
res= mysqld_show_warnings(thd, (uint32_t)
1278
((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1279
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1280
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
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)
1284
1372
case SQLCOM_SHOW_ERRORS:
1286
res= mysqld_show_warnings(thd, (uint32_t)
1287
(1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1374
res= mysqld_show_warnings(thd, (ulong)
1375
(1L << (uint) MYSQL_ERROR::WARN_LEVEL_ERROR));
1290
1378
case SQLCOM_SHOW_SLAVE_HOSTS:
1352
1440
assert(first_table == all_tables && first_table != 0);
1353
1441
bool link_to_local;
1354
1442
// Skip first table, which is the table we are creating
1355
TableList *create_table= lex->unlink_first_table(&link_to_local);
1356
TableList *select_tables= lex->query_tables;
1443
TABLE_LIST *create_table= lex->unlink_first_table(&link_to_local);
1444
TABLE_LIST *select_tables= lex->query_tables;
1358
1446
Code below (especially in mysql_create_table() and select_create
1359
1447
methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1429
1517
select_lex->options|= SELECT_NO_UNLOCK;
1430
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;
1432
1533
if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1434
1535
lex->link_first_table_back(create_table, link_to_local);
1452
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;
1457
1575
select_create is currently not re-execution friendly and
1531
1649
thd->enable_slow_log= opt_log_slow_admin_statements;
1533
memset(&create_info, 0, sizeof(create_info));
1651
bzero((char*) &create_info, sizeof(create_info));
1534
1652
create_info.db_type= 0;
1535
1653
create_info.row_type= ROW_TYPE_NOT_USED;
1536
1654
create_info.default_table_charset= thd->variables.collation_database;
1538
1656
res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1539
1657
&create_info, first_table, &alter_info,
1540
0, (order_st*) 0, 0);
1543
1661
case SQLCOM_SLAVE_START:
1595
1713
assert(select_lex->db);
1597
1715
{ // Rename of table
1598
TableList tmp_table;
1599
memset(&tmp_table, 0, sizeof(tmp_table));
1716
TABLE_LIST tmp_table;
1717
bzero((char*) &tmp_table,sizeof(tmp_table));
1600
1718
tmp_table.table_name= lex->name.str;
1601
1719
tmp_table.db=select_lex->db;
1604
1722
/* Don't yet allow changing of symlinks with ALTER TABLE */
1605
1723
if (create_info.data_file_name)
1606
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1724
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1607
1725
"DATA DIRECTORY option ignored");
1608
1726
if (create_info.index_file_name)
1609
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1727
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, 0,
1610
1728
"INDEX DIRECTORY option ignored");
1611
1729
create_info.data_file_name= create_info.index_file_name= NULL;
1612
1730
/* ALTER TABLE ends previous transaction */
1628
1746
select_lex->order_list.elements,
1629
(order_st *) select_lex->order_list.first,
1747
(ORDER *) select_lex->order_list.first,
1633
1751
case SQLCOM_RENAME_TABLE:
1635
1753
assert(first_table == all_tables && first_table != 0);
1637
1755
for (table= first_table; table; table= table->next_local->next_local)
1639
TableList old_list, new_list;
1757
TABLE_LIST old_list, new_list;
1641
1759
we do not need initialize old_list and new_list because we will
1642
1760
come table[0] and table->next[0] there
1674
1792
thd->enable_slow_log= opt_log_slow_admin_statements;
1675
1793
res= mysql_repair_table(thd, first_table, &lex->check_opt);
1676
1794
/* ! we write after unlocking the table */
1678
Presumably, REPAIR and binlog writing doesn't require synchronization
1680
write_bin_log(thd, true, thd->query, thd->query_length);
1681
select_lex->table_list.first= (unsigned char*) first_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;
1682
1803
lex->query_tables=all_tables;
1687
1808
assert(first_table == all_tables && first_table != 0);
1688
1809
thd->enable_slow_log= opt_log_slow_admin_statements;
1689
1810
res = mysql_check_table(thd, first_table, &lex->check_opt);
1690
select_lex->table_list.first= (unsigned char*) first_table;
1811
select_lex->table_list.first= (uchar*) first_table;
1691
1812
lex->query_tables=all_tables;
1697
1818
thd->enable_slow_log= opt_log_slow_admin_statements;
1698
1819
res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1699
1820
/* ! we write after unlocking the table */
1700
write_bin_log(thd, true, thd->query, thd->query_length);
1701
select_lex->table_list.first= (unsigned char*) first_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;
1702
1829
lex->query_tables=all_tables;
1708
1835
assert(first_table == all_tables && first_table != 0);
1709
1836
thd->enable_slow_log= opt_log_slow_admin_statements;
1710
res= mysql_optimize_table(thd, first_table, &lex->check_opt);
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);
1711
1840
/* ! we write after unlocking the table */
1712
write_bin_log(thd, true, thd->query, thd->query_length);
1713
select_lex->table_list.first= (unsigned char*) first_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;
1714
1849
lex->query_tables=all_tables;
1830
1965
if (!(res= open_and_lock_tables(thd, all_tables)))
1832
1967
/* Skip first table, which is the table we are inserting in */
1833
TableList *second_table= first_table->next_local;
1834
select_lex->table_list.first= (unsigned char*) second_table;
1968
TABLE_LIST *second_table= first_table->next_local;
1969
select_lex->table_list.first= (uchar*) second_table;
1835
1970
select_lex->context.table_list=
1836
1971
select_lex->context.first_name_resolution_table= second_table;
1837
1972
res= mysql_insert_select_prepare(thd);
1856
1991
/* INSERT ... SELECT should invalidate only the very first table */
1857
TableList *save_table= first_table->next_local;
1992
TABLE_LIST *save_table= first_table->next_local;
1858
1993
first_table->next_local= 0;
1859
1994
first_table->next_local= save_table;
1861
1996
delete sel_result;
1863
1998
/* revert changes for SP */
1864
select_lex->table_list.first= (unsigned char*) first_table;
1999
select_lex->table_list.first= (uchar*) first_table;
1944
2079
select_lex->with_wild,
1945
2080
select_lex->item_list,
1946
2081
select_lex->where,
1947
0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
2082
0, (ORDER *)NULL, (ORDER *)NULL, (Item *)NULL,
1949
2084
select_lex->options | thd->options |
1950
2085
SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
2086
OPTION_SETUP_TABLES_DONE,
2215
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);
2218
2387
case SQLCOM_ALTER_DB:
2220
2389
LEX_STRING *db= &lex->name;
2276
2450
Presumably, RESET and binlog writing doesn't require synchronization
2278
write_bin_log(thd, false, thd->query, thd->query_length);
2452
if (!lex->no_write_to_binlog && write_to_binlog)
2454
write_bin_log(thd, false, thd->query, thd->query_length);
2333
2510
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2335
2512
if (my_strnncoll(system_charset_info,
2336
(unsigned char *)lex->ident.str, lex->ident.length,
2337
(unsigned char *)sv->name, sv->length) == 0)
2513
(uchar *)lex->ident.str, lex->ident.length,
2514
(uchar *)sv->name, sv->length) == 0)
2355
2532
for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2357
2534
if (my_strnncoll(system_charset_info,
2358
(unsigned char *)lex->ident.str, lex->ident.length,
2359
(unsigned char *)sv->name, sv->length) == 0)
2535
(uchar *)lex->ident.str, lex->ident.length,
2536
(uchar *)sv->name, sv->length) == 0)
2368
2545
if (((thd->options & OPTION_KEEP_LOG) ||
2369
2546
thd->transaction.all.modified_non_trans_table) &&
2370
2547
!thd->slave_thread)
2371
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2548
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2372
2549
ER_WARNING_NOT_COMPLETE_ROLLBACK,
2373
2550
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2389
2566
for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2391
2568
if (my_strnncoll(system_charset_info,
2392
(unsigned char *)lex->ident.str, lex->ident.length,
2393
(unsigned char *)(*sv)->name, (*sv)->length) == 0)
2569
(uchar *)lex->ident.str, lex->ident.length,
2570
(uchar *)(*sv)->name, (*sv)->length) == 0)
2396
2573
if (*sv) /* old savepoint of the same name exists */
2503
2681
if (lex->describe & DESCRIBE_EXTENDED)
2505
2683
char buff[1024];
2506
String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2684
String str(buff,(uint32) sizeof(buff), system_charset_info);
2508
2686
thd->lex->unit.print(&str, QT_ORDINARY);
2509
2687
str.append('\0');
2510
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2688
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
2511
2689
ER_YES, str.ptr());
2568
2746
LEX *lex= current_thd->lex;
2569
2747
ulong old_info=0;
2570
if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2748
if ((uint) *yystacksize >= MY_YACC_MAX)
2572
2750
if (!lex->yacc_yyvs)
2573
2751
old_info= *yystacksize;
2574
2752
*yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2575
if (!(lex->yacc_yyvs= (unsigned char*)
2753
if (!(lex->yacc_yyvs= (uchar*)
2576
2754
my_realloc(lex->yacc_yyvs,
2577
2755
*yystacksize*sizeof(**yyvs),
2578
2756
MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
2579
!(lex->yacc_yyss= (unsigned char*)
2757
!(lex->yacc_yyss= (uchar*)
2580
2758
my_realloc(lex->yacc_yyss,
2581
2759
*yystacksize*sizeof(**yyss),
2582
2760
MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
2585
2763
{ // Copy old info from stack
2586
memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2587
memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
2764
memcpy(lex->yacc_yyss, (uchar*) *yyss, old_info*sizeof(**yyss));
2765
memcpy(lex->yacc_yyvs, (uchar*) *yyvs, old_info*sizeof(**yyvs));
2589
2767
*yyss=(short*) lex->yacc_yyss;
2590
2768
*yyvs=(YYSTYPE*) lex->yacc_yyvs;
2717
2895
if (lex->current_select->order_list.first && !lex->current_select->braces)
2719
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
2897
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY");
2722
2900
select_lex->include_neighbour(lex->current_select);
2762
2940
lex->sql_command= SQLCOM_SELECT;
2763
2941
tmp.str= (char*) var_name;
2764
2942
tmp.length=strlen(var_name);
2765
memset(&null_lex_string.str, 0, sizeof(null_lex_string));
2943
bzero((char*) &null_lex_string.str, sizeof(null_lex_string));
2767
2945
We set the name of Item to @@session.var_name because that then is used
2768
2946
as the column name in the output.
2770
2948
if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
2772
end= strxmov(buff, "@@session.", var_name, NULL);
2950
end= strxmov(buff, "@@session.", var_name, NullS);
2773
2951
var->set_name(buff, end-buff, system_charset_info);
2774
2952
add_item_to_list(thd, var);
2892
3070
lex_start(thd);
2893
3071
mysql_reset_thd_for_next_command(thd);
2895
if (!parse_sql(thd, &lip) &&
2896
all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
3073
if (!parse_sql(thd, &lip, NULL) &&
3074
all_tables_not_ok(thd,(TABLE_LIST*) lex->select_lex.table_list.first))
2897
3075
error= 1; /* Ignore question */
2898
3076
thd->end_statement();
2899
3077
thd->cleanup_after_query();
2912
3090
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
2913
3091
char *length, char *decimals,
2914
uint32_t type_modifier,
2915
3093
enum column_format_type column_format,
2916
3094
Item *default_value, Item *on_update_value,
2917
3095
LEX_STRING *comment,
2919
List<String> *interval_list, const CHARSET_INFO * const cs)
3097
List<String> *interval_list, CHARSET_INFO *cs)
2921
3099
register Create_field *new_field;
2922
3100
LEX *lex= thd->lex;
3001
3179
void store_position_for_column(const char *name)
3003
current_thd->lex->last_field->after=const_cast<char*> (name);
3181
current_thd->lex->last_field->after=my_const_cast(char*) (name);
3007
3185
add_proc_to_list(THD* thd, Item *item)
3010
3188
Item **item_ptr;
3012
if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
3190
if (!(order = (ORDER *) thd->alloc(sizeof(ORDER)+sizeof(Item*))))
3014
3192
item_ptr = (Item**) (order+1);
3015
3193
*item_ptr= item;
3016
3194
order->item=item_ptr;
3017
3195
order->free_me=0;
3018
thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
3196
thd->lex->proc_list.link_in_list((uchar*) order,(uchar**) &order->next);
3153
3331
/* check that used name is unique */
3154
3332
if (lock_type != TL_IGNORE)
3156
TableList *first_table= (TableList*) table_list.first;
3157
for (TableList *tables= first_table ;
3334
TABLE_LIST *first_table= (TABLE_LIST*) table_list.first;
3335
for (TABLE_LIST *tables= first_table ;
3159
3337
tables=tables->next_local)
3170
3348
if (table_list.elements > 0)
3173
table_list.next points to the last inserted TableList->next_local'
3351
table_list.next points to the last inserted TABLE_LIST->next_local'
3175
3353
We don't use the offsetof() macro here to avoid warnings from gcc
3177
previous_table_ref= (TableList*) ((char*) table_list.next -
3355
previous_table_ref= (TABLE_LIST*) ((char*) table_list.next -
3178
3356
((char*) &(ptr->next_local) -
3181
3359
Set next_name_resolution_table of the previous table reference to point
3182
3360
to the current table reference. In effect the list
3183
TableList::next_name_resolution_table coincides with
3184
TableList::next_local. Later this may be changed in
3361
TABLE_LIST::next_name_resolution_table coincides with
3362
TABLE_LIST::next_local. Later this may be changed in
3185
3363
store_top_level_join_columns() for NATURAL/USING joins.
3187
3365
previous_table_ref->next_name_resolution_table= ptr;
3193
3371
previous table reference to 'ptr'. Here we also add one element to the
3194
3372
list 'table_list'.
3196
table_list.link_in_list((unsigned char*) ptr, (unsigned char**) &ptr->next_local);
3374
table_list.link_in_list((uchar*) ptr, (uchar**) &ptr->next_local);
3197
3375
ptr->next_name_resolution_table= NULL;
3198
3376
/* Link table in global list (all used tables) */
3199
3377
lex->add_to_query_tables(ptr);
3223
3401
bool st_select_lex::init_nested_join(THD *thd)
3226
nested_join_st *nested_join;
3404
NESTED_JOIN *nested_join;
3228
if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3229
sizeof(nested_join_st))))
3406
if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
3407
sizeof(NESTED_JOIN))))
3231
3409
nested_join= ptr->nested_join=
3232
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
3410
((NESTED_JOIN*) ((uchar*) ptr + ALIGN_SIZE(sizeof(TABLE_LIST))));
3234
3412
join_list->push_front(ptr);
3235
3413
ptr->embedding= embedding;
3252
3430
@param thd current thread
3255
- Pointer to TableList element added to the total table list, if success
3433
- Pointer to TABLE_LIST element added to the total table list, if success
3259
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
3437
TABLE_LIST *st_select_lex::end_nested_join(THD *thd __attribute__((__unused__)))
3262
nested_join_st *nested_join;
3440
NESTED_JOIN *nested_join;
3264
3442
assert(embedding);
3265
3443
ptr= embedding;
3297
\# Pointer to TableList element created for the new nested join
3475
\# Pointer to TABLE_LIST element created for the new nested join
3300
TableList *st_select_lex::nest_last_join(THD *thd)
3478
TABLE_LIST *st_select_lex::nest_last_join(THD *thd)
3303
nested_join_st *nested_join;
3304
List<TableList> *embedded_list;
3481
NESTED_JOIN *nested_join;
3482
List<TABLE_LIST> *embedded_list;
3306
if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3307
sizeof(nested_join_st))))
3484
if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
3485
sizeof(NESTED_JOIN))))
3309
3487
nested_join= ptr->nested_join=
3310
((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
3488
((NESTED_JOIN*) ((uchar*) ptr + ALIGN_SIZE(sizeof(TABLE_LIST))));
3312
3490
ptr->embedding= embedding;
3313
3491
ptr->join_list= join_list;
3315
3493
embedded_list= &nested_join->join_list;
3316
3494
embedded_list->empty();
3318
for (uint32_t i=0; i < 2; i++)
3496
for (uint i=0; i < 2; i++)
3320
TableList *table= join_list->pop();
3498
TABLE_LIST *table= join_list->pop();
3321
3499
table->join_list= embedded_list;
3322
3500
table->embedding= ptr;
3323
3501
embedded_list->push_back(table);
3395
TableList *st_select_lex::convert_right_join()
3573
TABLE_LIST *st_select_lex::convert_right_join()
3397
TableList *tab2= join_list->pop();
3398
TableList *tab1= join_list->pop();
3575
TABLE_LIST *tab2= join_list->pop();
3576
TABLE_LIST *tab1= join_list->pop();
3400
3578
join_list->push_front(tab2);
3401
3579
join_list->push_front(tab1);
3437
3615
This object is created for any union construct containing a union
3438
3616
operation and also for any single select union construct of the form
3440
(SELECT ... order_st BY order_list [LIMIT n]) order_st BY ...
3618
(SELECT ... ORDER BY order_list [LIMIT n]) ORDER BY ...
3444
(SELECT ... order_st BY LIMIT n) order_st BY ...
3622
(SELECT ... ORDER BY LIMIT n) ORDER BY ...
3447
3625
@param thd_arg thread handle
3482
3660
This works only for
3483
(SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
3484
(SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
3661
(SELECT ... ORDER BY list [LIMIT n]) ORDER BY order_list [LIMIT m],
3662
(SELECT ... LIMIT n) ORDER BY order_list [LIMIT m]
3485
3663
just before the parser starts processing order_list
3487
3665
global_parameters= fake_select_lex;
3747
3925
This is written such that we have a short lock on LOCK_thread_count
3751
kill_one_thread(THD *thd __attribute__((unused)),
3752
ulong id, bool only_kill_query)
3928
uint kill_one_thread(THD *thd __attribute__((__unused__)),
3929
ulong id, bool only_kill_query)
3755
uint32_t error=ER_NO_SUCH_THREAD;
3756
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
3932
uint error=ER_NO_SUCH_THREAD;
3933
VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
3757
3934
I_List_iterator<THD> it(threads);
3758
3935
while ((tmp=it++))
3815
3992
/* Fix is using unix filename format on dos */
3816
my_stpcpy(buff,*filename_ptr);
3817
end=convert_dirname(buff, *filename_ptr, NULL);
3993
strmov(buff,*filename_ptr);
3994
end=convert_dirname(buff, *filename_ptr, NullS);
3818
3995
if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3819
3996
return 1; // End of memory
3820
3997
*filename_ptr=ptr;
3821
strxmov(ptr,buff,table_name,NULL);
3998
strxmov(ptr,buff,table_name,NullS);
3841
4018
char command[80];
3842
4019
Lex_input_stream *lip= thd->m_lip;
3843
4020
strmake(command, lip->yylval->symbol.str,
3844
cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
4021
min(lip->yylval->symbol.length, sizeof(command)-1));
3845
4022
my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3968
4145
bool multi_delete_precheck(THD *thd,
3969
TableList *tables __attribute__((unused)))
4146
TABLE_LIST *tables __attribute__((__unused__)))
3971
4148
SELECT_LEX *select_lex= &thd->lex->select_lex;
3972
TableList **save_query_tables_own_last= thd->lex->query_tables_own_last;
4149
TABLE_LIST **save_query_tables_own_last= thd->lex->query_tables_own_last;
3974
4151
thd->lex->query_tables_own_last= 0;
3975
4152
thd->lex->query_tables_own_last= save_query_tables_own_last;
4056
4233
bool multi_delete_set_locks_and_link_aux_tables(LEX *lex)
4058
TableList *tables= (TableList*)lex->select_lex.table_list.first;
4059
TableList *target_tbl;
4235
TABLE_LIST *tables= (TABLE_LIST*)lex->select_lex.table_list.first;
4236
TABLE_LIST *target_tbl;
4061
4238
lex->table_count= 0;
4063
for (target_tbl= (TableList *)lex->auxiliary_table_list.first;
4240
for (target_tbl= (TABLE_LIST *)lex->auxiliary_table_list.first;
4064
4241
target_tbl; target_tbl= target_tbl->next_local)
4066
4243
lex->table_count++;
4067
4244
/* All tables in aux_tables must be found in FROM PART */
4068
TableList *walk= multi_delete_table_match(lex, target_tbl, tables);
4245
TABLE_LIST *walk= multi_delete_table_match(lex, target_tbl, tables);
4071
4248
if (!walk->derived)
4149
bool create_table_precheck(THD *thd __attribute__((unused)),
4150
TableList *tables __attribute__((unused)),
4151
TableList *create_table)
4326
bool create_table_precheck(THD *thd,
4327
TABLE_LIST *tables __attribute__((__unused__)),
4328
TABLE_LIST *create_table)
4331
SELECT_LEX *select_lex= &lex->select_lex;
4153
4332
bool error= true; // Error message is given
4155
4334
if (create_table && (strcmp(create_table->db, "information_schema") == 0))
4340
if (select_lex->item_list.elements)
4342
/* Check permissions for used tables in CREATE TABLE ... SELECT */
4344
#ifdef NOT_NECESSARY_TO_CHECK_CREATE_TABLE_EXIST_WHEN_PREPARING_STATEMENT
4345
/* This code throws an ill error for CREATE TABLE t1 SELECT * FROM t1 */
4347
Only do the check for PS, because we on execute we have to check that
4348
against the opened tables to ensure we don't use a table that is part
4349
of the view (which can only be done after the table has been opened).
4351
if (thd->stmt_arena->is_stmt_prepare_or_first_sp_execute())
4354
For temporary tables we don't have to check if the created table exists
4356
if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) &&
4357
find_table_in_global_list(tables, create_table->db,
4358
create_table->table_name))
4245
4450
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
4246
uint32_t max_char_length, const CHARSET_INFO * const cs,
4451
uint max_char_length, CHARSET_INFO *cs,
4249
4454
int well_formed_error;
4250
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4455
uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4251
4456
max_char_length, &well_formed_error);
4253
4458
if (!well_formed_error && str->length == res)
4262
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
4263
uint32_t err_code, const char *param_for_err_msg)
4467
bool check_identifier_name(LEX_STRING *str, uint max_char_length,
4468
uint err_code, const char *param_for_err_msg)
4265
4470
#ifdef HAVE_CHARSET_utf8mb3
4268
4473
so they should be prohibited until such support is done.
4269
4474
This is why we use the 3-byte utf8 to check well-formedness here.
4271
const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
4476
CHARSET_INFO *cs= &my_charset_utf8mb3_general_ci;
4273
const CHARSET_INFO * const cs= system_charset_info;
4478
CHARSET_INFO *cs= system_charset_info;
4275
4480
int well_formed_error;
4276
uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4481
uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4277
4482
max_char_length, &well_formed_error);
4279
4484
if (well_formed_error)
4331
4536
if (home_dir_len < dir_len)
4333
if (!my_strnncoll(character_set_filesystem,
4334
(const unsigned char*) conv_path, home_dir_len,
4335
(const unsigned char*) mysql_unpacked_real_data_home,
4538
if (lower_case_file_system)
4540
if (!my_strnncoll(character_set_filesystem,
4541
(const uchar*) conv_path, home_dir_len,
4542
(const uchar*) mysql_unpacked_real_data_home,
4546
else if (!memcmp(conv_path, mysql_unpacked_real_data_home, home_dir_len))
4350
4560
@param thd Thread context.
4351
4561
@param lip Lexer context.
4562
@param creation_ctx Object creation context.
4353
4564
@return Error status.
4354
4565
@retval false on success.
4355
4566
@retval true on parsing error.
4358
bool parse_sql(THD *thd, Lex_input_stream *lip)
4569
bool parse_sql(THD *thd,
4570
Lex_input_stream *lip,
4571
Object_creation_ctx *creation_ctx)
4360
4573
assert(thd->m_lip == NULL);
4575
/* Backup creation context. */
4577
Object_creation_ctx *backup_ctx= NULL;
4580
backup_ctx= creation_ctx->set_n_backup(thd);
4362
4582
/* Set Lex_input_stream. */
4364
4584
thd->m_lip= lip;