17
17
/* Basic functions needed by many modules */
18
#include <drizzled/server_includes.h>
19
#include <drizzled/sql_select.h>
20
#include <mysys/my_dir.h>
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
19
#include "mysql_priv.h"
20
#include "sql_select.h"
24
25
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
27
28
@defgroup Data_Dictionary Data Dictionary
30
Table *unused_tables; /* Used by mysql_test */
31
TABLE *unused_tables; /* Used by mysql_test */
31
32
HASH open_cache; /* Used by mysql_test */
32
33
static HASH table_def_cache;
33
34
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
34
35
static pthread_mutex_t LOCK_table_share;
35
36
static bool table_def_inited= 0;
37
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
39
char *cache_key, uint32_t cache_key_length);
40
static void free_cache_entry(Table *entry);
41
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
38
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
40
char *cache_key, uint cache_key_length,
41
MEM_ROOT *mem_root, uint flags);
42
static void free_cache_entry(TABLE *entry);
43
static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks,
42
44
bool send_refresh);
45
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
47
extern "C" uchar *table_cache_key(const uchar *record, size_t *length,
46
48
bool not_used __attribute__((unused)))
48
Table *entry=(Table*) record;
50
TABLE *entry=(TABLE*) record;
49
51
*length= entry->s->table_cache_key.length;
50
return (unsigned char*) entry->s->table_cache_key.str;
52
return (uchar*) entry->s->table_cache_key.str;
102
uint32_t create_table_def_key(THD *thd, char *key, TableList *table_list,
104
uint create_table_def_key(THD *thd, char *key, TABLE_LIST *table_list,
105
uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
107
uint key_length= (uint) (strmov(strmov(key, table_list->db)+1,
106
108
table_list->table_name)-key)+1;
119
121
Functions to handle table definition cach (TABLE_SHARE)
120
122
*****************************************************************************/
122
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
124
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
123
125
bool not_used __attribute__((unused)))
125
127
TABLE_SHARE *entry=(TABLE_SHARE*) record;
126
128
*length= entry->table_cache_key.length;
127
return (unsigned char*) entry->table_cache_key.str;
129
return (uchar*) entry->table_cache_key.str;
199
201
# Share for table
202
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
203
uint32_t key_length, uint32_t db_flags, int *error)
204
TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list, char *key,
205
uint key_length, uint db_flags, int *error)
205
207
TABLE_SHARE *share;
209
211
/* Read table definition from cache */
210
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
212
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
304
306
static TABLE_SHARE
305
*get_table_share_with_create(THD *thd, TableList *table_list,
306
char *key, uint32_t key_length,
307
uint32_t db_flags, int *error)
307
*get_table_share_with_create(THD *thd, TABLE_LIST *table_list,
308
char *key, uint key_length,
309
uint db_flags, int *error)
309
311
TABLE_SHARE *share;
336
338
/* Table didn't exist. Check if some engine can provide it */
337
339
if ((tmp= ha_create_table_from_engine(thd, table_list->db,
338
340
table_list->table_name)) < 0)
343
No such table in any engine.
344
Hide "Table doesn't exist" errors if the table belongs to a view.
345
The check for thd->is_error() is necessary to not push an
346
unwanted error in case of pre-locking, which silences
347
"no such table" errors.
348
@todo Rework the alternative ways to deal with ER_NO_SUCH TABLE.
350
if (thd->is_error() && table_list->belong_to_view)
353
my_error(ER_VIEW_INVALID, MYF(0), "", "");
343
359
/* Give right error message */
351
367
/* Table existed in engine. Let's open it */
352
drizzle_reset_errors(thd, 1); // Clear warnings
368
mysql_reset_errors(thd, 1); // Clear warnings
353
369
thd->clear_error(); // Clear error message
354
370
return(get_table_share(thd, table_list, key, key_length,
355
371
db_flags, error));
430
446
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
432
448
char key[NAME_LEN*2+2];
433
TableList table_list;
449
TABLE_LIST table_list;
435
451
safe_mutex_assert_owner(&LOCK_open);
437
453
table_list.db= (char*) db;
438
454
table_list.table_name= (char*) table_name;
439
455
key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
440
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
456
return (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key, key_length);
477
493
&key_buff, old_share->table_cache_key.length,
480
memset(share, 0, sizeof(*share));
496
bzero((char*) share, sizeof(*share));
481
497
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
482
498
old_share->table_cache_key.length);
483
499
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
512
528
# Pointer to list of names of open tables.
515
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
531
OPEN_TABLE_LIST *list_open_tables(THD *thd __attribute__((__unused__)),
516
532
const char *db, const char *wild)
519
OPEN_TableList **start_list, *open_list;
520
TableList table_list;
535
OPEN_TABLE_LIST **start_list, *open_list;
536
TABLE_LIST table_list;
522
pthread_mutex_lock(&LOCK_open);
523
memset(&table_list, 0, sizeof(table_list));
538
VOID(pthread_mutex_lock(&LOCK_open));
539
bzero((char*) &table_list,sizeof(table_list));
524
540
start_list= &open_list;
527
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
543
for (uint idx=0 ; result == 0 && idx < open_cache.records; idx++)
529
OPEN_TableList *table;
530
Table *entry=(Table*) hash_element(&open_cache,idx);
545
OPEN_TABLE_LIST *table;
546
TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
531
547
TABLE_SHARE *share= entry->s;
533
549
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
557
if (!(*start_list = (OPEN_TableList *)
573
if (!(*start_list = (OPEN_TABLE_LIST *)
558
574
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
560
576
open_list=0; // Out of memory
563
my_stpcpy((*start_list)->table=
564
my_stpcpy(((*start_list)->db= (char*) ((*start_list)+1)),
579
strmov((*start_list)->table=
580
strmov(((*start_list)->db= (char*) ((*start_list)+1)),
565
581
share->db.str)+1,
566
582
share->table_name.str);
567
583
(*start_list)->in_use= entry->in_use ? 1 : 0;
614
free((unsigned char*) table);
630
my_free((uchar*) table,MYF(0));
618
634
/* Free resources allocated by filesort() and read_record() */
620
void free_io_cache(Table *table)
636
void free_io_cache(TABLE *table)
622
638
if (table->sort.io_cache)
624
640
close_cached_file(table->sort.io_cache);
625
free((unsigned char*) table->sort.io_cache);
641
my_free((uchar*) table->sort.io_cache,MYF(0));
626
642
table->sort.io_cache=0;
644
660
and tables must be NULL.
647
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
663
bool close_cached_tables(THD *thd, TABLE_LIST *tables, bool have_lock,
648
664
bool wait_for_refresh, bool wait_for_placeholders)
651
667
assert(thd || (!wait_for_refresh && !tables));
654
pthread_mutex_lock(&LOCK_open);
670
VOID(pthread_mutex_lock(&LOCK_open));
657
673
refresh_version++; // Force close of open tables
658
674
while (unused_tables)
660
676
#ifdef EXTRA_DEBUG
661
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
677
if (hash_delete(&open_cache,(uchar*) unused_tables))
662
678
printf("Warning: Couldn't delete open table from hash\n");
664
hash_delete(&open_cache,(unsigned char*) unused_tables);
680
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
667
683
/* Free table shares */
668
684
while (oldest_unused_share->next)
670
686
pthread_mutex_lock(&oldest_unused_share->mutex);
671
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
687
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
673
689
if (wait_for_refresh)
709
725
after the call to close_old_data_files() i.e. after removal of
710
726
current thread locks.
712
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
728
for (uint idx=0 ; idx < open_cache.records ; idx++)
714
Table *table=(Table*) hash_element(&open_cache,idx);
730
TABLE *table=(TABLE*) hash_element(&open_cache,idx);
715
731
if (table->in_use)
716
732
table->in_use->some_tables_deleted= 1;
748
764
while (found && ! thd->killed)
751
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
767
for (uint idx=0 ; idx < open_cache.records ; idx++)
753
Table *table=(Table*) hash_element(&open_cache,idx);
769
TABLE *table=(TABLE*) hash_element(&open_cache,idx);
754
770
/* Avoid a self-deadlock. */
755
771
if (table->in_use == thd)
758
774
Note that we wait here only for tables which are actually open, and
759
not for placeholders with Table::open_placeholder set. Waiting for
775
not for placeholders with TABLE::open_placeholder set. Waiting for
760
776
latter will cause deadlock in the following scenario, for example:
762
778
conn1: lock table t1 write;
818
834
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
819
835
LEX_STRING *connection, bool have_lock)
822
TableList tmp, *tables= NULL;
838
TABLE_LIST tmp, *tables= NULL;
823
839
bool result= false;
826
memset(&tmp, 0, sizeof(TableList));
842
bzero(&tmp, sizeof(TABLE_LIST));
829
pthread_mutex_lock(&LOCK_open);
845
VOID(pthread_mutex_lock(&LOCK_open));
831
847
for (idx= 0; idx < table_def_cache.records; idx++)
851
867
tmp.table_name= share->table_name.str;
852
868
tmp.next_local= tables;
854
tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp,
870
tables= (TABLE_LIST *) memdup_root(thd->mem_root, (char*)&tmp,
859
875
result= close_cached_tables(thd, tables, true, false, false);
862
pthread_mutex_unlock(&LOCK_open);
878
VOID(pthread_mutex_unlock(&LOCK_open));
864
880
if (if_wait_for_refresh)
866
882
pthread_mutex_lock(&thd->mysys_var->mutex);
867
883
thd->mysys_var->current_mutex= 0;
868
884
thd->mysys_var->current_cond= 0;
869
thd->set_proc_info(0);
870
886
pthread_mutex_unlock(&thd->mysys_var->mutex);
956
972
/* Free tables to hold down open files */
957
973
while (open_cache.records > table_cache_size && unused_tables)
958
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
974
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
959
975
if (found_old_table)
961
977
/* Tell threads waiting for refresh that something has happened */
962
978
broadcast_refresh();
965
pthread_mutex_unlock(&LOCK_open);
981
VOID(pthread_mutex_unlock(&LOCK_open));
1078
1094
/* move one table to free list */
1080
bool close_thread_table(THD *thd, Table **table_ptr)
1096
bool close_thread_table(THD *thd, TABLE **table_ptr)
1082
1098
bool found_old_table= 0;
1083
Table *table= *table_ptr;
1099
TABLE *table= *table_ptr;
1085
1101
assert(table->key_read == 0);
1086
1102
assert(!table->file || table->file->inited == handler::NONE);
1090
1106
if (table->needs_reopen_or_name_lock() ||
1091
1107
thd->version != refresh_version || !table->db_stat)
1093
hash_delete(&open_cache,(unsigned char*) table);
1109
VOID(hash_delete(&open_cache,(uchar*) table));
1094
1110
found_old_table=1;
1099
Open placeholders have Table::db_stat set to 0, so they should be
1115
Open placeholders have TABLE::db_stat set to 0, so they should be
1100
1116
handled by the first alternative.
1102
1118
assert(!table->open_placeholder);
1157
1173
/* Better add "if exists", in case a RESET MASTER has been done */
1158
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1159
uint32_t stub_len= sizeof(stub) - 1;
1174
const char stub[]= "DROP /*!40005 TEMPORARY */ TABLE IF EXISTS ";
1175
uint stub_len= sizeof(stub) - 1;
1161
1177
String s_query= String(buf, sizeof(buf), system_charset_info);
1162
1178
bool found_user_tables= false;
1238
1254
close_temporary(table, 1, 1);
1240
1256
thd->clear_error();
1241
const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1257
CHARSET_INFO *cs_save= thd->variables.character_set_client;
1242
1258
thd->variables.character_set_client= system_charset_info;
1243
1259
Query_log_event qinfo(thd, s_query.ptr(),
1244
1260
s_query.length() - 1 /* to remove trailing ',' */,
1328
1344
Also SELECT::exclude_from_table_unique_test used to exclude from check
1329
1345
tables of main SELECT of multi-delete and multi-update
1331
We also skip tables with TableList::prelocking_placeholder set,
1347
We also skip tables with TABLE_LIST::prelocking_placeholder set,
1332
1348
because we want to allow SELECTs from them, and their modification
1333
1349
will rise the error anyway.
1340
1356
0 if table is unique
1343
TableList* unique_table(THD *thd, TableList *table, TableList *table_list,
1359
TABLE_LIST* unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
1344
1360
bool check_alias)
1347
1363
const char *d_name, *t_name, *t_alias;
1350
1366
If this function called for query which update table (INSERT/UPDATE/...)
1351
then we have in table->table pointer to Table object which we are
1352
updating even if it is VIEW so we need TableList of this Table object
1367
then we have in table->table pointer to TABLE object which we are
1368
updating even if it is VIEW so we need TABLE_LIST of this TABLE object
1353
1369
to get right names (even if lower_case_table_names used).
1355
1371
If this function called for CREATE command that we have not opened table
1356
(table->table equal to 0) and right names is in current TableList
1372
(table->table equal to 0) and right names is in current TABLE_LIST
1359
1375
if (table->table)
1407
1423
here we hide view underlying tables if we have them
1410
void update_non_unique_table_error(TableList *update,
1411
const char *operation __attribute__((unused)),
1412
TableList *duplicate __attribute__((unused)))
1426
void update_non_unique_table_error(TABLE_LIST *update,
1427
const char *operation __attribute__((__unused__)),
1428
TABLE_LIST *duplicate __attribute__((__unused__)))
1414
1430
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1418
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
1434
TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name)
1420
TableList table_list;
1436
TABLE_LIST table_list;
1422
1438
table_list.db= (char*) db;
1423
1439
table_list.table_name= (char*) table_name;
1428
Table *find_temporary_table(THD *thd, TableList *table_list)
1444
TABLE *find_temporary_table(THD *thd, TABLE_LIST *table_list)
1430
1446
char key[MAX_DBKEY_LENGTH];
1431
1447
uint key_length;
1434
1450
key_length= create_table_def_key(thd, key, table_list, 1);
1435
1451
for (table=thd->temporary_tables ; table ; table= table->next)
1457
1473
This function is used to drop user temporary tables, as well as
1458
1474
internal tables created in CREATE TEMPORARY TABLE ... SELECT
1459
or ALTER Table. Even though part of the work done by this function
1475
or ALTER TABLE. Even though part of the work done by this function
1460
1476
is redundant when the table is internal, as long as we
1461
1477
link both internal and user temporary tables into the same
1462
1478
thd->temporary_tables list, it's impossible to tell here whether
1552
1568
if (free_share)
1554
1570
free_table_share(table->s);
1555
free((char*) table);
1571
my_free((char*) table,MYF(0));
1562
Used by ALTER Table when the table is a temporary one. It changes something
1578
Used by ALTER TABLE when the table is a temporary one. It changes something
1563
1579
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1565
1581
Prepares a table cache key, which is the concatenation of db, table_name and
1566
1582
thd->slave_proxy_id, separated by '\0'.
1569
bool rename_temporary_table(THD* thd, Table *table, const char *db,
1585
bool rename_temporary_table(THD* thd, TABLE *table, const char *db,
1570
1586
const char *table_name)
1573
uint32_t key_length;
1574
1590
TABLE_SHARE *share= table->s;
1575
TableList table_list;
1591
TABLE_LIST table_list;
1577
1593
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1578
1594
return(1); /* purecov: inspected */
1617
1633
not locked (for example already unlocked).
1620
void unlink_open_table(THD *thd, Table *find, bool unlock)
1636
void unlink_open_table(THD *thd, TABLE *find, bool unlock)
1622
1638
char key[MAX_DBKEY_LENGTH];
1623
uint32_t key_length= find->s->table_cache_key.length;
1624
Table *list, **prev;
1639
uint key_length= find->s->table_cache_key.length;
1640
TABLE *list, **prev;
1626
1642
safe_mutex_assert_owner(&LOCK_open);
1690
1706
handlerton *table_type= table->s->db_type();
1691
pthread_mutex_lock(&LOCK_open);
1707
VOID(pthread_mutex_lock(&LOCK_open));
1693
1709
unlink_open_table() also tells threads waiting for refresh or close
1694
1710
that something has happened.
1696
1712
unlink_open_table(thd, table, false);
1697
1713
quick_rm_table(table_type, db_name, table_name, 0);
1698
pthread_mutex_unlock(&LOCK_open);
1714
VOID(pthread_mutex_unlock(&LOCK_open));
1753
1769
@return false on success, true otherwise.
1756
bool name_lock_locked_table(THD *thd, TableList *tables)
1772
bool name_lock_locked_table(THD *thd, TABLE_LIST *tables)
1758
1774
/* Under LOCK TABLES we must only accept write locked tables. */
1759
1775
tables->table= find_locked_table(thd, tables->db, tables->table_name);
1783
1799
reopen_name_locked_table()
1784
1800
thd Thread handle
1785
table_list TableList object for table to be open, TableList::table
1786
member should point to Table object which was used for
1801
table_list TABLE_LIST object for table to be open, TABLE_LIST::table
1802
member should point to TABLE object which was used for
1788
link_in true - if Table object for table to be opened should be
1804
link_in true - if TABLE object for table to be opened should be
1789
1805
linked into THD::open_tables list.
1790
1806
false - placeholder used for name-locking is already in
1791
this list so we only need to preserve Table::next
1807
this list so we only need to preserve TABLE::next
1802
bool reopen_name_locked_table(THD* thd, TableList* table_list, bool link_in)
1818
bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in)
1804
Table *table= table_list->table;
1820
TABLE *table= table_list->table;
1805
1821
TABLE_SHARE *share;
1806
1822
char *table_name= table_list->table_name;
1809
1825
safe_mutex_assert_owner(&LOCK_open);
1816
1832
if (open_unireg_entry(thd, table, table_list, table_name,
1817
1833
table->s->table_cache_key.str,
1818
table->s->table_cache_key.length))
1834
table->s->table_cache_key.length, thd->mem_root, 0))
1820
1836
intern_close_table(table);
1874
1888
@param key Table cache key for name to be locked
1875
1889
@param key_length Table cache key length
1877
@return Pointer to Table object used for name locking or 0 in
1891
@return Pointer to TABLE object used for name locking or 0 in
1878
1892
case of failure.
1881
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1882
uint32_t key_length)
1895
TABLE *table_cache_insert_placeholder(THD *thd, const char *key,
1885
1899
TABLE_SHARE *share;
1886
1900
char *key_buff;
1940
1954
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1941
const char *table_name, Table **table)
1955
const char *table_name, TABLE **table)
1943
1957
char key[MAX_DBKEY_LENGTH];
1944
uint32_t key_length;
1946
key_length= (uint)(my_stpcpy(my_stpcpy(key, db) + 1, table_name) - key) + 1;
1947
pthread_mutex_lock(&LOCK_open);
1949
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1960
key_length= (uint)(strmov(strmov(key, db) + 1, table_name) - key) + 1;
1961
VOID(pthread_mutex_lock(&LOCK_open));
1963
if (hash_search(&open_cache, (uchar *)key, key_length))
1951
pthread_mutex_unlock(&LOCK_open);
1965
VOID(pthread_mutex_unlock(&LOCK_open));
1955
1969
if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1957
pthread_mutex_unlock(&LOCK_open);
1971
VOID(pthread_mutex_unlock(&LOCK_open));
1960
1974
(*table)->open_placeholder= 1;
1961
1975
(*table)->next= thd->open_tables;
1962
1976
thd->open_tables= *table;
1963
pthread_mutex_unlock(&LOCK_open);
1977
VOID(pthread_mutex_unlock(&LOCK_open));
2040
2054
If this is a NULL pointer, then the table is not
2041
2055
put in the thread-open-list.
2042
2056
flags Bitmap of flags to modify how open works:
2043
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
2057
MYSQL_LOCK_IGNORE_FLUSH - Open table even if
2044
2058
someone has done a flush or namelock on it.
2045
2059
No version number checking is done.
2046
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
2060
MYSQL_OPEN_TEMPORARY_ONLY - Open only temporary
2047
2061
table not the base table or view.
2052
2066
If table list element for the table to be opened has "create" flag
2053
2067
set and table does not exist, this function will automatically insert
2054
2068
a placeholder for exclusive name lock into the open tables cache and
2055
will return the Table instance that corresponds to this placeholder.
2069
will return the TABLE instance that corresponds to this placeholder.
2058
2072
NULL Open failed. If refresh is set then one should close
2059
2073
all other tables and retry the open.
2060
# Success. Pointer to Table object for open table.
2074
# Success. Pointer to TABLE object for open table.
2064
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint32_t flags)
2078
TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
2079
bool *refresh, uint flags)
2066
register Table *table;
2081
register TABLE *table;
2067
2082
char key[MAX_DBKEY_LENGTH];
2068
2083
unsigned int key_length;
2069
2084
char *alias= table_list->alias;
2216
2231
this is the normal use case.
2218
2233
- try to find the table in the table cache.
2219
- if one of the discovered Table instances is name-locked
2234
- if one of the discovered TABLE instances is name-locked
2220
2235
(table->s->version == 0) or some thread has started FLUSH TABLES
2221
2236
(refresh_version > table->s->version), back off -- we have to wait
2222
2237
until no one holds a name lock on the table.
2223
- if there is no such Table in the name cache, read the table definition
2238
- if there is no such TABLE in the name cache, read the table definition
2224
2239
and insert it into the cache.
2225
2240
We perform all of the above under LOCK_open which currently protects
2226
2241
the open cache (also known as table cache) and table definitions stored
2230
pthread_mutex_lock(&LOCK_open);
2245
VOID(pthread_mutex_lock(&LOCK_open));
2233
2248
If it's the first table from a list of tables used in a query,
2269
2284
an implicit "pending locks queue" - see
2270
2285
wait_for_locked_table_names for details.
2272
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
2287
for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
2274
2289
table && table->in_use ;
2275
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
2290
table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
2282
2297
(re-)opened and added to the cache.
2283
2298
If since then we did (or just started) FLUSH TABLES
2284
2299
statement, refresh_version has been increased.
2285
For "name-locked" Table instances, table->s->version is set
2300
For "name-locked" TABLE instances, table->s->version is set
2286
2301
to 0 (see lock_table_name for details).
2287
2302
In case there is a pending FLUSH TABLES or a name lock, we
2288
2303
need to back off and re-start opening tables.
2375
/* Insert a new Table instance into the open cache */
2390
/* Insert a new TABLE instance into the open cache */
2377
2392
/* Free cache if too big */
2378
2393
while (open_cache.records > table_cache_size && unused_tables)
2379
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
2394
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2381
2396
if (table_list->create)
2406
2421
table->open_placeholder= 1;
2407
2422
table->next= thd->open_tables;
2408
2423
thd->open_tables= table;
2409
pthread_mutex_unlock(&LOCK_open);
2424
VOID(pthread_mutex_unlock(&LOCK_open));
2412
2427
/* Table exists. Let us try to open it. */
2415
2430
/* make a new table */
2416
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
2431
if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))))
2418
pthread_mutex_unlock(&LOCK_open);
2433
VOID(pthread_mutex_unlock(&LOCK_open));
2422
error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
2437
error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
2438
mem_root, (flags & OPEN_VIEW_NO_PARSE));
2423
2439
/* Combine the follow two */
2426
free((unsigned char*)table);
2427
pthread_mutex_unlock(&LOCK_open);
2442
my_free((uchar*)table, MYF(0));
2443
VOID(pthread_mutex_unlock(&LOCK_open));
2432
free((unsigned char*)table);
2433
pthread_mutex_unlock(&LOCK_open);
2448
my_free((uchar*)table, MYF(0));
2449
VOID(pthread_mutex_unlock(&LOCK_open));
2434
2450
return(0); // VIEW
2436
my_hash_insert(&open_cache,(unsigned char*) table);
2452
VOID(my_hash_insert(&open_cache,(uchar*) table));
2439
pthread_mutex_unlock(&LOCK_open);
2455
VOID(pthread_mutex_unlock(&LOCK_open));
2442
2458
table->next=thd->open_tables; /* Link into simple list */
2453
2469
/* Fix alias if table name changes */
2454
2470
if (strcmp(table->alias, alias))
2456
uint32_t length=(uint) strlen(alias)+1;
2472
uint length=(uint) strlen(alias)+1;
2457
2473
table->alias= (char*) my_realloc((char*) table->alias, length,
2459
memcpy((void*) table->alias, alias, length);
2475
memcpy((char*) table->alias, alias, length);
2461
2477
/* These variables are also set in reopen_table() */
2462
2478
table->tablenr=thd->current_tablenr++;
2463
2479
table->used_fields=0;
2464
2480
table->const_table=0;
2465
table->null_row= false;
2466
table->maybe_null= false;
2467
table->force_index= false;
2481
table->null_row= table->maybe_null= table->force_index= 0;
2468
2482
table->status=STATUS_NO_RECORD;
2469
2483
table->insert_values= 0;
2470
2484
/* Catch wrong handling of the auto_increment_field_not_null. */
2482
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
2496
TABLE *find_locked_table(THD *thd, const char *db,const char *table_name)
2484
2498
char key[MAX_DBKEY_LENGTH];
2485
uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
2499
uint key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
2487
for (Table *table=thd->open_tables; table ; table=table->next)
2501
for (TABLE *table=thd->open_tables; table ; table=table->next)
2489
2503
if (table->s->table_cache_key.length == key_length &&
2490
2504
!memcmp(table->s->table_cache_key.str, key, key_length))
2525
2539
#ifdef EXTRA_DEBUG
2526
2540
if (table->db_stat)
2527
sql_print_error(_("Table %s had a open data handler in reopen_table"),
2541
sql_print_error("Table %s had a open data handler in reopen_table",
2530
memset(&table_list, 0, sizeof(TableList));
2544
bzero((char*) &table_list, sizeof(TABLE_LIST));
2531
2545
table_list.db= table->s->db.str;
2532
2546
table_list.table_name= table->s->table_name.str;
2533
2547
table_list.table= table;
2602
2617
@note This function assumes that if we are not under LOCK TABLES,
2603
2618
then there is only one table open and locked. This means that
2604
2619
the function probably has to be adjusted before it can be used
2605
anywhere outside ALTER Table.
2620
anywhere outside ALTER TABLE.
2607
2622
@note Must not use TABLE_SHARE::table_name/db of the table being closed,
2608
2623
the strings are used in a loop even after the share may be freed.
2658
2673
@note Since this function can't properly handle prelocking and
2659
2674
create placeholders it should be used in very special
2660
situations like FLUSH TABLES or ALTER Table. In general
2675
situations like FLUSH TABLES or ALTER TABLE. In general
2661
2676
case one should just repeat open_tables()/lock_tables()
2662
2677
combination when one needs tables to be reopened (for
2663
2678
example see open_and_lock_tables()).
2670
2685
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2672
Table *table,*next,**prev;
2673
Table **tables,**tables_ptr; // For locks
2687
TABLE *table,*next,**prev;
2688
TABLE **tables,**tables_ptr; // For locks
2674
2689
bool error=0, not_used;
2675
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2676
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2677
DRIZZLE_LOCK_IGNORE_FLUSH;
2690
const uint flags= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN |
2691
MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK |
2692
MYSQL_LOCK_IGNORE_FLUSH;
2679
2694
if (!thd->open_tables)
2698
2713
prev= &thd->open_tables;
2699
2714
for (table=thd->open_tables; table ; table=next)
2701
uint32_t db_stat=table->db_stat;
2716
uint db_stat=table->db_stat;
2702
2717
next=table->next;
2703
2718
if (!tables || (!db_stat && reopen_table(table)))
2705
2720
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2706
hash_delete(&open_cache,(unsigned char*) table);
2721
VOID(hash_delete(&open_cache,(uchar*) table));
2759
Close handlers for tables in list, but leave the Table structure
2774
Close handlers for tables in list, but leave the TABLE structure
2760
2775
intact so that we can re-open these quickly.
2762
2777
@param thd Thread context
2763
@param table Head of the list of Table objects
2778
@param table Head of the list of TABLE objects
2764
2779
@param morph_locks true - remove locks which we have on tables being closed
2765
2780
but ensure that no DML or DDL will sneak in before
2766
2781
we will re-open the table (i.e. temporarily morph
2824
2839
We come here only in close-for-back-off scenario. So we have to
2825
2840
"close" create placeholder here to avoid deadlocks (for example,
2826
2841
in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
2827
and RENAME Table t2 TO t1). In close-for-re-open scenario we will
2842
and RENAME TABLE t2 TO t1). In close-for-re-open scenario we will
2828
2843
probably want to let it stay.
2830
2845
Note "***": We must not enter this branch if the placeholder
2847
2862
if the table is closed
2850
bool table_is_used(Table *table, bool wait_for_name_lock)
2865
bool table_is_used(TABLE *table, bool wait_for_name_lock)
2854
2869
char *key= table->s->table_cache_key.str;
2855
uint32_t key_length= table->s->table_cache_key.length;
2870
uint key_length= table->s->table_cache_key.length;
2857
2872
HASH_SEARCH_STATE state;
2858
for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
2873
for (TABLE *search= (TABLE*) hash_first(&open_cache, (uchar*) key,
2859
2874
key_length, &state);
2861
search= (Table*) hash_next(&open_cache, (unsigned char*) key,
2876
search= (TABLE*) hash_next(&open_cache, (uchar*) key,
2862
2877
key_length, &state))
2864
2879
if (search->in_use == table->in_use)
2924
2939
This is only called on drop tables
2926
The Table object for the dropped table is unlocked but still kept around
2941
The TABLE object for the dropped table is unlocked but still kept around
2927
2942
as a name lock, which means that the table will be available for other
2928
2943
thread as soon as we call unlock_table_names().
2929
2944
If there is multiple copies of the table locked, all copies except
2938
Table *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2953
TABLE *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2940
Table *table,*next,**prev, *found= 0;
2955
TABLE *table,*next,**prev, *found= 0;
2941
2956
prev= &thd->open_tables;
2993
If we have the table open, which only happens when a LOCK Table has been
3008
If we have the table open, which only happens when a LOCK TABLE has been
2994
3009
done on the table, change the lock type to a lock that will abort all
2995
3010
other threads trying to get the lock.
2998
3013
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
3001
3016
for (table= thd->open_tables; table ; table= table->next)
3003
3018
if (!strcmp(table->s->table_name.str, table_name) &&
3070
3085
open_unireg_entry()
3071
3086
thd Thread handle
3072
3087
entry Store open table definition here
3073
table_list TableList with db, table_name
3088
table_list TABLE_LIST with db, table_name & belong_to_view
3074
3089
alias Alias name
3075
3090
cache_key Key for share_cache
3076
3091
cache_key_length length of cache_key
3092
mem_root temporary mem_root for parsing
3093
flags the OPEN_VIEW_NO_PARSE flag to be passed to
3094
openfrm()/open_new_frm()
3079
3097
Extra argument for open is taken from thd->open_options
3087
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
3105
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
3088
3106
const char *alias,
3089
char *cache_key, uint32_t cache_key_length)
3107
char *cache_key, uint cache_key_length,
3108
MEM_ROOT *mem_root __attribute__((__unused__)),
3109
uint flags __attribute__((__unused__)))
3092
3112
TABLE_SHARE *share;
3093
uint32_t discover_retry_count= 0;
3113
uint discover_retry_count= 0;
3095
3115
safe_mutex_assert_owner(&LOCK_open);
3097
3117
if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3098
3118
cache_key_length,
3099
3120
table_list->i_s_requested_object,
3175
3197
/* Give right error message */
3176
3198
thd->clear_error();
3177
3199
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3178
sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
3200
sql_print_error("Couldn't repair table: %s.%s", share->db.str,
3179
3201
share->table_name.str);
3180
3202
if (entry->file)
3181
3203
closefrm(entry, 0);
3201
3223
if (mysql_bin_log.is_open())
3203
3225
char *query, *end;
3204
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
3226
uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3205
3227
if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3207
3229
/* this DELETE FROM is needed even with row-based binlogging */
3208
end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3209
share->db.str,"`.`",share->table_name.str,"`", NULL);
3230
end = strxmov(strmov(query, "DELETE FROM `"),
3231
share->db.str,"`.`",share->table_name.str,"`", NullS);
3210
3232
thd->binlog_query(THD::STMT_QUERY_TYPE,
3211
3233
query, (ulong)(end-query), false, false);
3234
my_free(query, MYF(0));
3218
3240
DBA on top of warning the client (which will automatically be done
3219
3241
because of MYF(MY_WME) in my_malloc() above).
3221
sql_print_error(_("When opening HEAP table, could not allocate memory "
3222
"to write 'DELETE FROM `%s`.`%s`' to the binary log"),
3243
sql_print_error("When opening HEAP table, could not allocate memory "
3244
"to write 'DELETE FROM `%s`.`%s`' to the binary log",
3223
3245
table_list->db, table_list->table_name);
3224
3246
closefrm(entry, 0);
3439
3464
and locking issues because it does not call lock_tables().
3442
Table *open_n_lock_single_table(THD *thd, TableList *table_l,
3467
TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
3443
3468
thr_lock_type lock_type)
3445
TableList *save_next_global;
3470
TABLE_LIST *save_next_global;
3447
3472
/* Remember old 'next' pointer. */
3448
3473
save_next_global= table_l->next_global;
3487
3514
table_list->table table
3490
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
3491
uint32_t lock_flags)
3517
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type lock_type,
3496
3523
thd_proc_info(thd, "Opening table");
3497
3524
thd->current_tablenr= 0;
3498
while (!(table= open_table(thd, table_list, &refresh, 0)) &&
3525
/* open_ltable can be used only for BASIC TABLEs */
3526
table_list->required_type= FRMTYPE_TABLE;
3527
while (!(table= open_table(thd, table_list, thd->mem_root, &refresh, 0)) &&
3592
3621
data from the tables.
3595
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint32_t flags)
3624
bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags)
3598
3627
assert(!thd->fill_derived_tables());
3599
3628
if (open_tables(thd, &tables, &counter, flags) ||
3600
3629
mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3640
3669
@param tables Tables involved in the query
3643
int decide_logging_format(THD *thd, TableList *tables)
3672
int decide_logging_format(THD *thd, TABLE_LIST *tables)
3645
3674
if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3647
3676
handler::Table_flags flags_some_set= handler::Table_flags();
3648
3677
handler::Table_flags flags_all_set= ~handler::Table_flags();
3649
bool multi_engine= false;
3678
my_bool multi_engine= false;
3650
3679
void* prev_ht= NULL;
3651
for (TableList *table= tables; table; table= table->next_global)
3680
for (TABLE_LIST *table= tables; table; table= table->next_global)
3653
3682
if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3684
3713
" but not allowed by this combination of engines");
3717
If more than one engine is involved in the statement and at
3718
least one is doing it's own logging (is *self-logging*), the
3719
statement cannot be logged atomically, so we generate an error
3720
rather than allowing the binlog to become corrupt.
3723
(flags_some_set & HA_HAS_OWN_BINLOGGING))
3725
error= ER_BINLOG_LOGGING_IMPOSSIBLE;
3726
my_error(error, MYF(0),
3727
"Statement cannot be written atomically since more"
3728
" than one engine involved and at least one engine"
3729
" is self-logging");
3846
Table *open_temporary_table(THD *thd, const char *path, const char *db,
3891
TABLE *open_temporary_table(THD *thd, const char *path, const char *db,
3847
3892
const char *table_name, bool link_in_list,
3848
3893
open_table_mode open_mode)
3851
3896
TABLE_SHARE *share;
3852
3897
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3853
uint32_t key_length;
3854
TableList table_list;
3899
TABLE_LIST table_list;
3856
3901
table_list.db= (char*) db;
3857
3902
table_list.table_name= (char*) table_name;
3858
3903
/* Create the cache_key for temporary tables */
3859
3904
key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3861
if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3906
if (!(tmp_table= (TABLE*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3862
3907
strlen(path)+1 + key_length,
3864
3909
return(0); /* purecov: inspected */
3866
3911
share= (TABLE_SHARE*) (tmp_table+1);
3867
3912
tmp_path= (char*) (share+1);
3868
saved_cache_key= my_stpcpy(tmp_path, path)+1;
3913
saved_cache_key= strmov(tmp_path, path)+1;
3869
3914
memcpy(saved_cache_key, cache_key, key_length);
3871
3916
init_tmp_table_share(thd, share, saved_cache_key, key_length,
3872
strchr(saved_cache_key, '\0')+1, tmp_path);
3917
strend(saved_cache_key)+1, tmp_path);
3874
3919
if (open_table_def(thd, share, 0) ||
3875
3920
open_table_from_share(thd, share, table_name,
3877
3922
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3879
3924
(open_mode == OTM_ALTER) ?
3880
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3925
(READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD |
3927
: (READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD),
3882
3928
ha_open_options,
3883
3929
tmp_table, open_mode))
3885
3931
/* No need to lock share->mutex as this is not needed for tmp tables */
3886
3932
free_table_share(share);
3887
free((char*) tmp_table);
3933
my_free((char*) tmp_table,MYF(0));
4045
Find a field by name in a view that uses merge algorithm.
4048
find_field_in_view()
4050
table_list view to search for 'name'
4052
length length of name
4053
item_name name of item if it will be created (VIEW)
4054
ref expression substituted in VIEW should be passed
4055
using this reference (return view_ref_found)
4056
register_tree_change true if ref is not stack variable and we
4057
need register changes in item tree
4060
0 field is not found
4061
view_ref_found found value in VIEW (real result is in *ref)
4062
# pointer to field - only for schema table fields
4066
find_field_in_view(THD *thd, TABLE_LIST *table_list,
4067
const char *name, uint length __attribute__((__unused__)),
4068
const char *item_name __attribute__((__unused__)),
4070
bool register_tree_change __attribute__((__unused__)))
4072
Field_iterator_view field_it;
4073
field_it.set(table_list);
4075
assert(table_list->schema_table_reformed ||
4076
(ref != 0 && (0) != 0));
4077
for (; !field_it.end_of_fields(); field_it.next())
4079
if (!my_strcasecmp(system_charset_info, field_it.name(), name))
4082
create_item() may, or may not create a new Item, depending on
4083
the column reference. See create_view_field() for details.
4085
Item *item= field_it.create_item(thd);
4090
*ref != NULL means that *ref contains the item that we need to
4091
replace. If the item was aliased by the user, set the alias to
4093
We need to set alias on both ref itself and on ref real item.
4095
if (*ref && !(*ref)->is_autogenerated_name)
4097
item->set_name((*ref)->name, (*ref)->name_length,
4098
system_charset_info);
4099
item->real_item()->set_name((*ref)->name, (*ref)->name_length,
4100
system_charset_info);
4102
if (register_tree_change)
4103
thd->change_item_tree(ref, item);
4106
return((Field*) view_ref_found);
3999
4114
Find field by name in a NATURAL/USING join table reference.
4028
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4029
uint32_t length __attribute__((unused)),
4030
Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4031
TableList **actual_table)
4143
find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
4144
uint length __attribute__((__unused__)),
4145
Item **ref, bool register_tree_change,
4146
TABLE_LIST **actual_table)
4033
4148
List_iterator_fast<Natural_join_column>
4034
4149
field_it(*(table_ref->join_columns));
4172
if (nj_col->view_field)
4176
create_item() may, or may not create a new Item, depending on the
4177
column reference. See create_view_field() for details.
4179
item= nj_col->create_item(thd);
4181
*ref != NULL means that *ref contains the item that we need to
4182
replace. If the item was aliased by the user, set the alias to
4184
We need to set alias on both ref itself and on ref real item.
4186
if (*ref && !(*ref)->is_autogenerated_name)
4188
item->set_name((*ref)->name, (*ref)->name_length,
4189
system_charset_info);
4190
item->real_item()->set_name((*ref)->name, (*ref)->name_length,
4191
system_charset_info);
4196
assert(nj_col->table_field == NULL);
4197
if (nj_col->table_ref->schema_table_reformed)
4200
Translation table items are always Item_fields and fixed
4201
already('mysql_schema_table' function). So we can return
4202
->field. It is used only for 'show & where' commands.
4204
return(((Item_field*) (nj_col->view_field->item))->field);
4206
if (register_tree_change)
4207
thd->change_item_tree(ref, item);
4210
found_field= (Field*) view_ref_found;
4057
4214
/* This is a base table. */
4215
assert(nj_col->view_field == NULL);
4058
4216
assert(nj_col->table_ref->table == nj_col->table_field->table);
4059
4217
found_field= nj_col->table_field;
4060
4218
update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4088
find_field_in_table(THD *thd, Table *table, const char *name, uint32_t length,
4089
bool allow_rowid, uint32_t *cached_field_index_ptr)
4246
find_field_in_table(THD *thd, TABLE *table, const char *name, uint length,
4247
bool allow_rowid, uint *cached_field_index_ptr)
4091
4249
Field **field_ptr, *field;
4092
uint32_t cached_field_index= *cached_field_index_ptr;
4250
uint cached_field_index= *cached_field_index_ptr;
4094
4252
/* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4095
4253
if (cached_field_index < table->s->fields &&
4184
find_field_in_table_ref(THD *thd, TableList *table_list,
4185
const char *name, uint32_t length,
4342
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
4343
const char *name, uint length,
4186
4344
const char *item_name, const char *db_name,
4187
4345
const char *table_name, Item **ref,
4188
4346
bool check_privileges, bool allow_rowid,
4189
uint32_t *cached_field_index_ptr,
4190
bool register_tree_change, TableList **actual_table)
4347
uint *cached_field_index_ptr,
4348
bool register_tree_change, TABLE_LIST **actual_table)
4194
4352
assert(table_list->alias);
4231
4389
if (table_list->field_translation)
4391
/* 'table_list' is a view or an information schema table. */
4392
if ((fld= find_field_in_view(thd, table_list, name, length, item_name, ref,
4393
register_tree_change)))
4394
*actual_table= table_list;
4234
4396
else if (!table_list->nested_join)
4294
4456
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4295
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4457
it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
4299
4461
field_to_set= fld;
4300
4462
if (field_to_set)
4302
Table *table= field_to_set->table;
4464
TABLE *table= field_to_set->table;
4303
4465
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4304
4466
bitmap_set_bit(table->read_set, field_to_set->field_index);
4326
4488
# pointer to field
4329
Field *find_field_in_table_sef(Table *table, const char *name)
4491
Field *find_field_in_table_sef(TABLE *table, const char *name)
4331
4493
Field **field_ptr;
4332
4494
if (table->s->name_hash.records)
4334
field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
4496
field_ptr= (Field**)hash_search(&table->s->name_hash,(uchar*) name,
4395
4557
find_field_in_tables(THD *thd, Item_ident *item,
4396
TableList *first_table, TableList *last_table,
4558
TABLE_LIST *first_table, TABLE_LIST *last_table,
4397
4559
Item **ref, find_item_error_report_type report_error,
4398
4560
bool check_privileges, bool register_tree_change)
4401
4563
const char *db= item->db_name;
4402
4564
const char *table_name= item->table_name;
4403
4565
const char *name= item->field_name;
4404
uint32_t length=(uint) strlen(name);
4566
uint length=(uint) strlen(name);
4405
4567
char name_buff[NAME_LEN+1];
4406
TableList *cur_table= first_table;
4407
TableList *actual_table;
4568
TABLE_LIST *cur_table= first_table;
4569
TABLE_LIST *actual_table;
4408
4570
bool allow_rowid;
4410
4572
if (!table_name || !table_name[0])
4421
4583
This shortcut is used by prepared statements. We assume that
4422
TableList *first_table is not changed during query execution (which
4584
TABLE_LIST *first_table is not changed during query execution (which
4423
4585
is true for all queries except RENAME but luckily RENAME doesn't
4424
4586
use fields...) so we can rely on reusing pointer to its member.
4425
4587
With this optimization we also miss case when addition of one more
4426
4588
field makes some prepared query ambiguous and so erroneous, but we
4427
4589
accept this trade off.
4429
TableList *table_ref= item->cached_table;
4591
TABLE_LIST *table_ref= item->cached_table;
4431
4593
The condition (table_ref->view == NULL) ensures that we will call
4432
4594
find_field_in_table even in the case of information schema tables
4561
4724
char buff[NAME_LEN*2+1];
4562
4725
if (db && db[0])
4564
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NULL);
4727
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
4565
4728
table_name=buff;
4567
4730
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
4619
find_item_in_list(Item *find, List<Item> &items, uint32_t *counter,
4782
find_item_in_list(Item *find, List<Item> &items, uint *counter,
4620
4783
find_item_error_report_type report_error,
4621
4784
enum_resolution_type *resolution)
4934
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4935
List<String> *using_fields, uint32_t *found_using_fields)
5097
mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
5098
List<String> *using_fields, uint *found_using_fields)
4937
5100
Field_iterator_table_ref it_1, it_2;
4938
5101
Natural_join_column *nj_col_1, *nj_col_2;
4942
5105
Leaf table references to which new natural join columns are added
4943
5106
if the leaves are != NULL.
4945
TableList *leaf_1= (table_ref_1->nested_join &&
5108
TABLE_LIST *leaf_1= (table_ref_1->nested_join &&
4946
5109
!table_ref_1->is_natural_join) ?
4947
5110
NULL : table_ref_1;
4948
TableList *leaf_2= (table_ref_2->nested_join &&
5111
TABLE_LIST *leaf_2= (table_ref_2->nested_join &&
4949
5112
!table_ref_2->is_natural_join) ?
4950
5113
NULL : table_ref_2;
5148
store_natural_using_join_columns(THD *thd __attribute__((unused)),
5149
TableList *natural_using_join,
5150
TableList *table_ref_1,
5151
TableList *table_ref_2,
5316
store_natural_using_join_columns(THD *thd __attribute__((__unused__)),
5317
TABLE_LIST *natural_using_join,
5318
TABLE_LIST *table_ref_1,
5319
TABLE_LIST *table_ref_2,
5152
5320
List<String> *using_fields,
5153
uint32_t found_using_fields)
5321
uint found_using_fields)
5155
5323
Field_iterator_table_ref it_1, it_2;
5156
5324
Natural_join_column *nj_col_1, *nj_col_2;
5249
5417
The procedure performs a post-order traversal of a nested join tree
5250
5418
and materializes the row types of NATURAL/USING joins in a
5251
bottom-up manner until it reaches the TableList elements that
5419
bottom-up manner until it reaches the TABLE_LIST elements that
5252
5420
represent the top-most NATURAL/USING joins. The procedure should be
5253
5421
applied to each element of SELECT_LEX::top_join_list (i.e. to each
5254
5422
top-level element of the FROM clause).
5267
store_top_level_join_columns(THD *thd, TableList *table_ref,
5268
TableList *left_neighbor,
5269
TableList *right_neighbor)
5435
store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref,
5436
TABLE_LIST *left_neighbor,
5437
TABLE_LIST *right_neighbor)
5271
5439
bool result= true;
5273
5441
/* Call the procedure recursively for each nested table reference. */
5274
5442
if (table_ref->nested_join)
5276
List_iterator_fast<TableList> nested_it(table_ref->nested_join->join_list);
5277
TableList *same_level_left_neighbor= nested_it++;
5278
TableList *same_level_right_neighbor= NULL;
5444
List_iterator_fast<TABLE_LIST> nested_it(table_ref->nested_join->join_list);
5445
TABLE_LIST *same_level_left_neighbor= nested_it++;
5446
TABLE_LIST *same_level_right_neighbor= NULL;
5279
5447
/* Left/right-most neighbors, possibly at higher levels in the join tree. */
5280
TableList *real_left_neighbor, *real_right_neighbor;
5448
TABLE_LIST *real_left_neighbor, *real_right_neighbor;
5282
5450
while (same_level_left_neighbor)
5284
TableList *cur_table_ref= same_level_left_neighbor;
5452
TABLE_LIST *cur_table_ref= same_level_left_neighbor;
5285
5453
same_level_left_neighbor= nested_it++;
5287
5455
The order of RIGHT JOIN operands is reversed in 'join list' to
5328
5496
assert(table_ref->nested_join &&
5329
5497
table_ref->nested_join->join_list.elements == 2);
5330
List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
5498
List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list);
5332
5500
Notice that the order of join operands depends on whether table_ref
5333
5501
represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
5334
5502
in inverted order.
5336
TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5337
TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
5504
TABLE_LIST *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5505
TABLE_LIST *table_ref_1= operand_it++; /* First NATURAL join operand. */
5338
5506
List<String> *using_fields= table_ref->join_using_fields;
5339
uint32_t found_using_fields;
5507
uint found_using_fields;
5342
5510
The two join operands were interchanged in the parser, change the order
5343
5511
back for 'mark_common_columns'.
5345
5513
if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5346
std::swap(table_ref_1, table_ref_2);
5514
swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5347
5515
if (mark_common_columns(thd, table_ref_1, table_ref_2,
5348
5516
using_fields, &found_using_fields))
5354
5522
same as of an equivalent LEFT JOIN.
5356
5524
if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5357
std::swap(table_ref_1, table_ref_2);
5525
swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5358
5526
if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
5359
5527
table_ref_2, using_fields,
5360
5528
found_using_fields))
5376
5544
/* Change this table reference to become a leaf for name resolution. */
5377
5545
if (left_neighbor)
5379
TableList *last_leaf_on_the_left;
5547
TABLE_LIST *last_leaf_on_the_left;
5380
5548
last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
5381
5549
last_leaf_on_the_left->next_name_resolution_table= table_ref;
5383
5551
if (right_neighbor)
5385
TableList *first_leaf_on_the_right;
5553
TABLE_LIST *first_leaf_on_the_right;
5386
5554
first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
5387
5555
table_ref->next_name_resolution_table= first_leaf_on_the_right;
5423
5591
static bool setup_natural_join_row_types(THD *thd,
5424
List<TableList> *from_clause,
5592
List<TABLE_LIST> *from_clause,
5425
5593
Name_resolution_context *context)
5427
5595
thd->where= "from clause";
5428
5596
if (from_clause->elements == 0)
5429
5597
return false; /* We come here in the case of UNIONs. */
5431
List_iterator_fast<TableList> table_ref_it(*from_clause);
5432
TableList *table_ref; /* Current table reference. */
5599
List_iterator_fast<TABLE_LIST> table_ref_it(*from_clause);
5600
TABLE_LIST *table_ref; /* Current table reference. */
5433
5601
/* Table reference to the left of the current. */
5434
TableList *left_neighbor;
5602
TABLE_LIST *left_neighbor;
5435
5603
/* Table reference to the right of the current. */
5436
TableList *right_neighbor= NULL;
5604
TABLE_LIST *right_neighbor= NULL;
5438
5606
/* Note that tables in the list are in reversed order */
5439
5607
for (left_neighbor= table_ref_it++; left_neighbor ; )
5441
5609
table_ref= left_neighbor;
5442
5610
left_neighbor= table_ref_it++;
5443
if (store_top_level_join_columns(thd, table_ref,
5444
left_neighbor, right_neighbor))
5611
/* For stored procedures do not redo work if already done. */
5612
if (context->select_lex->first_execution)
5448
TableList *first_leaf_on_the_right;
5449
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5450
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5614
if (store_top_level_join_columns(thd, table_ref,
5615
left_neighbor, right_neighbor))
5619
TABLE_LIST *first_leaf_on_the_right;
5620
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5621
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5452
5624
right_neighbor= table_ref;
5646
5818
bool setup_tables(THD *thd, Name_resolution_context *context,
5647
List<TableList> *from_clause, TableList *tables,
5648
TableList **leaves, bool select_insert)
5819
List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
5820
TABLE_LIST **leaves, bool select_insert)
5650
uint32_t tablenr= 0;
5652
5824
assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
5653
5825
(context->table_list && context->first_name_resolution_table));
5655
5827
this is used for INSERT ... SELECT.
5656
5828
For select we setup tables except first (and its underlying tables)
5658
TableList *first_select_table= (select_insert ?
5830
TABLE_LIST *first_select_table= (select_insert ?
5659
5831
tables->next_local:
5661
5833
if (!(*leaves))
5662
5834
make_leaves_list(leaves, tables);
5664
TableList *table_list;
5836
TABLE_LIST *table_list;
5665
5837
for (table_list= *leaves;
5667
5839
table_list= table_list->next_leaf, tablenr++)
5669
Table *table= table_list->table;
5841
TABLE *table= table_list->table;
5670
5842
table->pos_in_table_list= table_list;
5671
5843
if (first_select_table &&
5672
5844
table_list->top_table() == first_select_table)
5719
5891
bool setup_tables_and_check_access(THD *thd,
5720
5892
Name_resolution_context *context,
5721
List<TableList> *from_clause,
5893
List<TABLE_LIST> *from_clause,
5895
TABLE_LIST **leaves,
5724
5896
bool select_insert)
5726
TableList *leaves_tmp= NULL;
5898
TABLE_LIST *leaves_tmp= NULL;
5727
5899
bool first_table= true;
5729
5901
if (setup_tables(thd, context, from_clause, tables,
5803
5979
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
5804
5980
const char *table_name, List_iterator<Item> *it,
5805
bool any_privileges __attribute__((unused)))
5981
bool any_privileges __attribute__((__unused__)))
5807
5983
Field_iterator_table_ref field_iterator;
5951
6127
false if all is OK
5954
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
6130
int setup_conds(THD *thd, TABLE_LIST *tables __attribute__((__unused__)),
5958
6134
SELECT_LEX *select_lex= thd->lex->current_select;
5959
TableList *table= NULL; // For HP compilers
6135
TABLE_LIST *table= NULL; // For HP compilers
5960
6136
void *save_thd_marker= thd->thd_marker;
5962
6138
it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
6194
6370
(file->name[1] == '.' && !file->name[2])))
6197
if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
6373
if (!bcmp((uchar*) file->name, (uchar*) tmp_file_prefix,
6374
tmp_file_prefix_length))
6199
6376
char *ext= fn_ext(file->name);
6200
uint32_t ext_len= strlen(ext);
6201
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
6377
uint ext_len= strlen(ext);
6378
uint filePath_len= snprintf(filePath, sizeof(filePath),
6202
6379
"%s%c%s", tmpdir, FN_LIBCHAR,
6204
if (!memcmp(reg_ext, ext, ext_len))
6381
if (!bcmp((uchar*) reg_ext, (uchar*) ext, ext_len))
6206
6383
handler *handler_file= 0;
6207
6384
/* We should cut file extention before deleting of table */
6254
6431
void remove_db_from_cache(const char *db)
6256
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
6433
for (uint idx=0 ; idx < open_cache.records ; idx++)
6258
Table *table=(Table*) hash_element(&open_cache,idx);
6435
TABLE *table=(TABLE*) hash_element(&open_cache,idx);
6259
6436
if (!strcmp(table->s->db.str, db))
6261
6438
table->s->version= 0L; /* Free when thread is ready */
6303
6480
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
6306
6483
char key[MAX_DBKEY_LENGTH];
6307
uint32_t key_length;
6309
6486
TABLE_SHARE *share;
6310
6487
bool result= 0, signalled= 0;
6312
key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
6489
key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
6315
6492
HASH_SEARCH_STATE state;
6316
6493
result= signalled= 0;
6318
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
6495
for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
6321
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
6498
table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
6362
6539
result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
6364
6541
while (unused_tables && !unused_tables->s->version)
6365
hash_delete(&open_cache,(unsigned char*) unused_tables);
6542
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6367
6544
/* Remove table from table definition cache if it's not in use */
6368
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
6545
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6371
6548
share->version= 0; // Mark for delete
6372
6549
if (share->ref_count == 0)
6374
6551
pthread_mutex_lock(&share->mutex);
6375
hash_delete(&table_def_cache, (unsigned char*) share);
6552
VOID(hash_delete(&table_def_cache, (uchar*) share));
6395
6572
It can happen that another thread has opened the
6396
6573
table but has not yet locked any table at all. Since
6397
6574
it can be locked waiting for a table that our thread
6398
has done LOCK Table x WRITE on previously, we need to
6575
has done LOCK TABLE x WRITE on previously, we need to
6399
6576
ensure that the thread actually hears our signal
6400
6577
before we go to sleep. Thus we wait for a short time
6401
6578
and then we retry another loop in the
6419
6596
return a->length == b->length && !strncmp(a->str, b->str, a->length);
6601
Open and lock system tables for read.
6604
open_system_tables_for_read()
6606
table_list List of tables to open.
6607
backup Pointer to Open_tables_state instance where
6608
information about currently open tables will be
6609
saved, and from which will be restored when we will
6610
end work with system tables.
6613
Thanks to restrictions which we put on opening and locking of
6614
system tables for writing, we can open and lock them for reading
6615
even when we already have some other tables open and locked. One
6616
must call close_system_tables() to close systems tables opened
6625
open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
6626
Open_tables_state *backup)
6628
thd->reset_n_backup_open_tables_state(backup);
6632
for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
6634
TABLE *table= open_table(thd, tables, thd->mem_root, ¬_used,
6635
MYSQL_LOCK_IGNORE_FLUSH);
6639
assert(table->s->table_category == TABLE_CATEGORY_SYSTEM);
6641
table->use_all_columns();
6642
table->reginfo.lock_type= tables->lock_type;
6643
tables->table= table;
6648
TABLE **list= (TABLE**) thd->alloc(sizeof(TABLE*) * count);
6650
for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
6651
*(ptr++)= tables->table;
6653
thd->lock= mysql_lock_tables(thd, list, count,
6654
MYSQL_LOCK_IGNORE_FLUSH, ¬_used);
6660
close_system_tables(thd, backup);
6667
Close system tables, opened with open_system_tables_for_read().
6670
close_system_tables()
6672
backup Pointer to Open_tables_state instance which holds
6673
information about tables which were open before we
6674
decided to access system tables.
6678
close_system_tables(THD *thd, Open_tables_state *backup)
6680
close_thread_tables(thd);
6681
thd->restore_backup_open_tables_state(backup);
6686
Open and lock one system table for update.
6689
open_system_table_for_update()
6691
one_table Table to open.
6694
Table opened with this call should closed using close_thread_tables().
6698
# Pointer to TABLE object of system table
6702
open_system_table_for_update(THD *thd, TABLE_LIST *one_table)
6704
TABLE *table= open_ltable(thd, one_table, one_table->lock_type, 0);
6707
assert(table->s->table_category == TABLE_CATEGORY_SYSTEM);
6708
table->use_all_columns();
6422
6715
@} (end of group Data_Dictionary)