1
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
17
/* Basic functions needed by many modules */
102
102
Create a table cache key
105
create_table_def_key()
106
key Create key here (must be of size MAX_DBKEY_LENGTH)
107
table_list Table definition
110
The table cache_key is created from:
114
if the table is a tmp table, we add the following to make each tmp table
117
4 bytes for master thread id
118
4 bytes pseudo thread id
105
create_table_def_key()
106
key Create key here (must be of size MAX_DBKEY_LENGTH)
107
table_list Table definition
110
The table cache_key is created from:
114
if the table is a tmp table, we add the following to make each tmp table
117
4 bytes for master thread id
118
4 bytes pseudo thread id
124
124
uint32_t create_table_def_key(char *key, TableList *table_list)
194
194
error out: Error code from open_table_def()
197
Get a table definition from the table definition cache.
198
If it doesn't exist, create a new from the table definition file.
197
Get a table definition from the table definition cache.
198
If it doesn't exist, create a new from the table definition file.
201
We must have wrlock on LOCK_open when we come here
202
(To be changed later)
201
We must have wrlock on LOCK_open when we come here
202
(To be changed later)
209
209
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
210
uint32_t key_length, uint32_t, int *error)
210
uint32_t key_length, uint32_t, int *error)
212
212
TableShare *share;
313
Mark that we are not using table share anymore.
316
release_table_share()
320
If ref_count goes to zero and (we have done a refresh or if we have
321
already too many open table shares) then delete the definition.
323
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
324
that the table is deleted or the thread is killed.
313
Mark that we are not using table share anymore.
316
release_table_share()
320
If ref_count goes to zero and (we have done a refresh or if we have
321
already too many open table shares) then delete the definition.
323
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
324
that the table is deleted or the thread is killed.
327
327
void release_table_share(TableShare *share)
374
374
Close file handle, but leave the table in the table cache
377
close_handle_and_leave_table_as_lock()
377
close_handle_and_leave_table_as_lock()
381
By leaving the table in the table cache, it disallows any other thread
384
session->killed will be set if we run out of memory
386
If closing a MERGE child, the calling function has to take care for
387
closing the parent too, if necessary.
381
By leaving the table in the table cache, it disallows any other thread
384
session->killed will be set if we run out of memory
386
If closing a MERGE child, the calling function has to take care for
387
closing the parent too, if necessary.
427
427
Create a list for all open tables matching SQL expression
431
wild SQL like expression
431
wild SQL like expression
434
One gets only a list of tables for which one has any kind of privilege.
435
db and table names are allocated in result struct, so one doesn't need
436
a lock on LOCK_open when traversing the return list.
434
One gets only a list of tables for which one has any kind of privilege.
435
db and table names are allocated in result struct, so one doesn't need
436
a lock on LOCK_open when traversing the return list.
439
NULL Error (Probably OOM)
440
# Pointer to list of names of open tables.
439
NULL Error (Probably OOM)
440
# Pointer to list of names of open tables.
443
443
OPEN_TableList *list_open_tables(const char *db, const char *wild)
470
470
for (table= open_list ; table ; table=table->next)
472
472
if (!strcmp(table->table, share->table_name.str) &&
473
!strcmp(table->db, share->db.str))
473
!strcmp(table->db, share->db.str))
477
if (entry->locked_by_name)
477
if (entry->locked_by_name)
484
484
if (!(*start_list = (OPEN_TableList *)
485
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
485
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
487
487
open_list=0; // Out of memory
490
490
strcpy((*start_list)->table=
491
491
strcpy(((*start_list)->db= (char*) ((*start_list)+1)),
492
share->db.str)+share->db.length+1,
492
share->db.str)+share->db.length+1,
493
493
share->table_name.str);
494
494
(*start_list)->in_use= entry->in_use ? 1 : 0;
495
495
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
565
565
@param have_lock If LOCK_open is locked
566
566
@param wait_for_refresh Wait for a impending flush
567
567
@param wait_for_placeholders Wait for tables being reopened so that the GRL
568
won't proceed while write-locked tables are being reopened by other
568
won't proceed while write-locked tables are being reopened by other
571
571
@remark Session can be NULL, but then wait_for_refresh must be false
572
and tables must be NULL.
572
and tables must be NULL.
575
575
bool close_cached_tables(Session *session, TableList *tables, bool have_lock,
680
680
not for placeholders with Table::open_placeholder set. Waiting for
681
681
latter will cause deadlock in the following scenario, for example:
683
conn1: lock table t1 write;
684
conn2: lock table t2 write;
683
conn1: lock table t1 write;
684
conn2: lock table t2 write;
688
It also does not make sense to wait for those of placeholders that
689
are employed by CREATE TABLE as in this case table simply does not
688
It also does not make sense to wait for those of placeholders that
689
are employed by CREATE TABLE as in this case table simply does not
692
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
693
(table->open_placeholder && wait_for_placeholders)))
696
pthread_cond_wait(&COND_refresh,&LOCK_open);
692
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
693
(table->open_placeholder && wait_for_placeholders)))
696
pthread_cond_wait(&COND_refresh,&LOCK_open);
872
872
Find table in list.
876
table Pointer to table list
877
offset Offset to which list in table structure to use
878
db_name Data base name
879
table_name Table name
882
This is called by find_table_in_local_list() and
883
find_table_in_global_list().
887
# Pointer to found table.
876
table Pointer to table list
877
offset Offset to which list in table structure to use
878
db_name Data base name
879
table_name Table name
882
This is called by find_table_in_local_list() and
883
find_table_in_global_list().
887
# Pointer to found table.
890
890
TableList *find_table_in_list(TableList *table,
891
TableList *TableList::*link,
893
const char *table_name)
891
TableList *TableList::*link,
893
const char *table_name)
895
895
for (; table; table= table->*link )
907
907
Test that table is unique (It's only exists once in the table list)
911
session thread handle
912
table table which should be checked
913
table_list list of tables
914
check_alias whether to check tables' aliases
916
NOTE: to exclude derived tables from check we use following mechanism:
917
a) during derived table processing set Session::derived_tables_processing
918
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
919
Session::derived_tables_processing set. (we can't use JOIN::execute
920
because for PS we perform only JOIN::prepare, but we can't set this
921
flag in JOIN::prepare if we are not sure that we are in derived table
922
processing loop, because multi-update call fix_fields() for some its
923
items (which mean JOIN::prepare for subqueries) before unique_table
924
call to detect which tables should be locked for write).
925
c) unique_table skip all tables which belong to SELECT with
926
SELECT::exclude_from_table_unique_test set.
927
Also SELECT::exclude_from_table_unique_test used to exclude from check
928
tables of main SELECT of multi-delete and multi-update
930
We also skip tables with TableList::prelocking_placeholder set,
931
because we want to allow SELECTs from them, and their modification
932
will rise the error anyway.
934
TODO: when we will have table/view change detection we can do this check
911
session thread handle
912
table table which should be checked
913
table_list list of tables
914
check_alias whether to check tables' aliases
916
NOTE: to exclude derived tables from check we use following mechanism:
917
a) during derived table processing set Session::derived_tables_processing
918
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
919
Session::derived_tables_processing set. (we can't use JOIN::execute
920
because for PS we perform only JOIN::prepare, but we can't set this
921
flag in JOIN::prepare if we are not sure that we are in derived table
922
processing loop, because multi-update call fix_fields() for some its
923
items (which mean JOIN::prepare for subqueries) before unique_table
924
call to detect which tables should be locked for write).
925
c) unique_table skip all tables which belong to SELECT with
926
SELECT::exclude_from_table_unique_test set.
927
Also SELECT::exclude_from_table_unique_test used to exclude from check
928
tables of main SELECT of multi-delete and multi-update
930
We also skip tables with TableList::prelocking_placeholder set,
931
because we want to allow SELECTs from them, and their modification
932
will rise the error anyway.
934
TODO: when we will have table/view change detection we can do this check
942
942
TableList* unique_table(Session *session, TableList *table, TableList *table_list,
946
946
const char *d_name, *t_name, *t_alias;
995
995
prevent some update operation
998
update_non_unique_table_error()
999
update table which we try to update
1000
operation name of update operation
1001
duplicate duplicate table which we found
998
update_non_unique_table_error()
999
update table which we try to update
1000
operation name of update operation
1001
duplicate duplicate table which we found
1004
here we hide view underlying tables if we have them
1004
here we hide view underlying tables if we have them
1007
1007
void update_non_unique_table_error(TableList *update,
1045
1045
Try to locate the table in the list of session->temporary_tables.
1046
1046
If the table is found:
1047
- if the table is being used by some outer statement, fail.
1048
- if the table is in session->locked_tables, unlock it and
1049
remove it from the list of locked tables. Currently only transactional
1050
temporary tables are present in the locked_tables list.
1051
- Close the temporary table, remove its .FRM
1052
- remove the table from the list of temporary tables
1047
- if the table is being used by some outer statement, fail.
1048
- if the table is in session->locked_tables, unlock it and
1049
remove it from the list of locked tables. Currently only transactional
1050
temporary tables are present in the locked_tables list.
1051
- Close the temporary table, remove its .FRM
1052
- remove the table from the list of temporary tables
1054
1054
This function is used to drop user temporary tables, as well as
1055
1055
internal tables created in CREATE TEMPORARY TABLE ... SELECT
1194
Remove all instances of table from thread's open list and
1197
@param session Thread context
1198
@param find Table to remove
1199
@param unlock true - free all locks on tables removed that are
1200
done with LOCK TABLES
1203
@note When unlock parameter is false or current thread doesn't have
1204
any tables locked with LOCK TABLES, tables are assumed to be
1205
not locked (for example already unlocked).
1194
Remove all instances of table from thread's open list and
1197
@param session Thread context
1198
@param find Table to remove
1199
@param unlock true - free all locks on tables removed that are
1200
done with LOCK TABLES
1203
@note When unlock parameter is false or current thread doesn't have
1204
any tables locked with LOCK TABLES, tables are assumed to be
1205
not locked (for example already unlocked).
1208
1208
void unlink_open_table(Session *session, Table *find, bool unlock)
1253
Auxiliary routine which closes and drops open table.
1255
@param session Thread handle
1256
@param table Table object for table to be dropped
1257
@param db_name Name of database for this table
1258
@param table_name Name of this table
1260
@note This routine assumes that table to be closed is open only
1261
by calling thread so we needn't wait until other threads
1262
will close the table. Also unless called under implicit or
1263
explicit LOCK TABLES mode it assumes that table to be
1264
dropped is already unlocked. In the former case it will
1265
also remove lock on the table. But one should not rely on
1266
this behaviour as it may change in future.
1267
Currently, however, this function is never called for a
1268
table that was locked with LOCK TABLES.
1253
Auxiliary routine which closes and drops open table.
1255
@param session Thread handle
1256
@param table Table object for table to be dropped
1257
@param db_name Name of database for this table
1258
@param table_name Name of this table
1260
@note This routine assumes that table to be closed is open only
1261
by calling thread so we needn't wait until other threads
1262
will close the table. Also unless called under implicit or
1263
explicit LOCK TABLES mode it assumes that table to be
1264
dropped is already unlocked. In the former case it will
1265
also remove lock on the table. But one should not rely on
1266
this behaviour as it may change in future.
1267
Currently, however, this function is never called for a
1268
table that was locked with LOCK TABLES.
1271
1271
void drop_open_table(Session *session, Table *table, const char *db_name,
1292
Wait for condition but allow the user to send a kill to mysqld
1292
Wait for condition but allow the user to send a kill to mysqld
1295
wait_for_condition()
1296
session Thread handler
1297
mutex mutex that is currently hold that is associated with condition
1298
Will be unlocked on return
1299
cond Condition to wait for
1295
wait_for_condition()
1296
session Thread handler
1297
mutex mutex that is currently hold that is associated with condition
1298
Will be unlocked on return
1299
cond Condition to wait for
1302
1302
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
1368
1368
Open table which is already name-locked by this thread.
1371
reopen_name_locked_table()
1372
session Thread handle
1373
table_list TableList object for table to be open, TableList::table
1374
member should point to Table object which was used for
1376
link_in true - if Table object for table to be opened should be
1377
linked into Session::open_tables list.
1378
false - placeholder used for name-locking is already in
1379
this list so we only need to preserve Table::next
1371
reopen_name_locked_table()
1372
session Thread handle
1373
table_list TableList object for table to be open, TableList::table
1374
member should point to Table object which was used for
1376
link_in true - if Table object for table to be opened should be
1377
linked into Session::open_tables list.
1378
false - placeholder used for name-locking is already in
1379
this list so we only need to preserve Table::next
1383
This function assumes that its caller already acquired LOCK_open mutex.
1383
This function assumes that its caller already acquired LOCK_open mutex.
1390
1390
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in)
1457
Create and insert into table cache placeholder for table
1458
which will prevent its opening (or creation) (a.k.a lock
1461
@param session Thread context
1462
@param key Table cache key for name to be locked
1463
@param key_length Table cache key length
1465
@return Pointer to Table object used for name locking or 0 in
1457
Create and insert into table cache placeholder for table
1458
which will prevent its opening (or creation) (a.k.a lock
1461
@param session Thread context
1462
@param key Table cache key for name to be locked
1463
@param key_length Table cache key length
1465
@return Pointer to Table object used for name locking or 0 in
1469
1469
Table *table_cache_insert_placeholder(Session *session, const char *key,
1507
Obtain an exclusive name lock on the table if it is not cached
1510
@param session Thread context
1511
@param db Name of database
1512
@param table_name Name of table
1513
@param[out] table Out parameter which is either:
1514
- set to NULL if table cache contains record for
1516
- set to point to the Table instance used for
1519
@note This function takes into account all records for table in table
1520
cache, even placeholders used for name-locking. This means that
1521
'table' parameter can be set to NULL for some situations when
1522
table does not really exist.
1524
@retval true Error occured (OOM)
1525
@retval false Success. 'table' parameter set according to above rules.
1507
Obtain an exclusive name lock on the table if it is not cached
1510
@param session Thread context
1511
@param db Name of database
1512
@param table_name Name of table
1513
@param[out] table Out parameter which is either:
1514
- set to NULL if table cache contains record for
1516
- set to point to the Table instance used for
1519
@note This function takes into account all records for table in table
1520
cache, even placeholders used for name-locking. This means that
1521
'table' parameter can be set to NULL for some situations when
1522
table does not really exist.
1524
@retval true Error occured (OOM)
1525
@retval false Success. 'table' parameter set according to above rules.
1528
1528
bool lock_table_name_if_not_cached(Session *session, const char *db,
1564
session Thread context.
1565
table_list Open first table in list.
1566
refresh INOUT Pointer to memory that will be set to 1 if
1567
we need to close all tables and reopen them.
1568
If this is a NULL pointer, then the table is not
1569
put in the thread-open-list.
1570
flags Bitmap of flags to modify how open works:
1571
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1572
someone has done a flush or namelock on it.
1573
No version number checking is done.
1574
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1575
table not the base table or view.
1564
session Thread context.
1565
table_list Open first table in list.
1566
refresh INOUT Pointer to memory that will be set to 1 if
1567
we need to close all tables and reopen them.
1568
If this is a NULL pointer, then the table is not
1569
put in the thread-open-list.
1570
flags Bitmap of flags to modify how open works:
1571
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1572
someone has done a flush or namelock on it.
1573
No version number checking is done.
1574
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1575
table not the base table or view.
1578
Uses a cache of open tables to find a table not in use.
1578
Uses a cache of open tables to find a table not in use.
1580
If table list element for the table to be opened has "create" flag
1581
set and table does not exist, this function will automatically insert
1582
a placeholder for exclusive name lock into the open tables cache and
1583
will return the Table instance that corresponds to this placeholder.
1580
If table list element for the table to be opened has "create" flag
1581
set and table does not exist, this function will automatically insert
1582
a placeholder for exclusive name lock into the open tables cache and
1583
will return the Table instance that corresponds to this placeholder.
1586
NULL Open failed. If refresh is set then one should close
1587
all other tables and retry the open.
1588
# Success. Pointer to Table object for open table.
1586
NULL Open failed. If refresh is set then one should close
1587
all other tables and retry the open.
1588
# Success. Pointer to Table object for open table.
1741
1741
- try to find the table in the table cache.
1742
1742
- if one of the discovered Table instances is name-locked
1743
(table->s->version == 0) or some thread has started FLUSH TABLES
1744
(refresh_version > table->s->version), back off -- we have to wait
1745
until no one holds a name lock on the table.
1743
(table->s->version == 0) or some thread has started FLUSH TABLES
1744
(refresh_version > table->s->version), back off -- we have to wait
1745
until no one holds a name lock on the table.
1746
1746
- if there is no such Table in the name cache, read the table definition
1747
1747
and insert it into the cache.
1748
1748
We perform all of the above under LOCK_open which currently protects
1801
1801
need to back off and re-start opening tables.
1802
1802
If we do not back off now, we may dead lock in case of lock
1803
1803
order mismatch with some other thread:
1804
c1: name lock t1; -- sort of exclusive lock
1805
c2: open t2; -- sort of shared lock
1806
c1: name lock t2; -- blocks
1807
c2: open t1; -- blocks
1804
c1: name lock t1; -- sort of exclusive lock
1805
c2: open t2; -- sort of shared lock
1806
c1: name lock t2; -- blocks
1807
c2: open t1; -- blocks
1809
1809
if (table->needs_reopen_or_name_lock())
2088
2088
broadcast_refresh();
2097
Close all instances of a table open by this thread and replace
2098
them with exclusive name-locks.
2100
@param session Thread context
2101
@param db Database name for the table to be closed
2102
@param table_name Name of the table to be closed
2104
@note This function assumes that if we are not under LOCK TABLES,
2105
then there is only one table open and locked. This means that
2106
the function probably has to be adjusted before it can be used
2107
anywhere outside ALTER Table.
2109
@note Must not use TableShare::table_name/db of the table being closed,
2110
the strings are used in a loop even after the share may be freed.
2097
Close all instances of a table open by this thread and replace
2098
them with exclusive name-locks.
2100
@param session Thread context
2101
@param db Database name for the table to be closed
2102
@param table_name Name of the table to be closed
2104
@note This function assumes that if we are not under LOCK TABLES,
2105
then there is only one table open and locked. This means that
2106
the function probably has to be adjusted before it can be used
2107
anywhere outside ALTER Table.
2109
@note Must not use TableShare::table_name/db of the table being closed,
2110
the strings are used in a loop even after the share may be freed.
2113
2113
void close_data_files_and_morph_locks(Session *session, const char *db,
2153
Reopen all tables with closed data files.
2155
@param session Thread context
2156
@param get_locks Should we get locks after reopening tables ?
2157
@param mark_share_as_old Mark share as old to protect from a impending
2160
@note Since this function can't properly handle prelocking and
2161
create placeholders it should be used in very special
2162
situations like FLUSH TABLES or ALTER Table. In general
2163
case one should just repeat open_tables()/lock_tables()
2164
combination when one needs tables to be reopened (for
2165
example see open_and_lock_tables()).
2167
@note One should have lock on LOCK_open when calling this.
2169
@return false in case of success, true - otherwise.
2153
Reopen all tables with closed data files.
2155
@param session Thread context
2156
@param get_locks Should we get locks after reopening tables ?
2157
@param mark_share_as_old Mark share as old to protect from a impending
2160
@note Since this function can't properly handle prelocking and
2161
create placeholders it should be used in very special
2162
situations like FLUSH TABLES or ALTER Table. In general
2163
case one should just repeat open_tables()/lock_tables()
2164
combination when one needs tables to be reopened (for
2165
example see open_and_lock_tables()).
2167
@note One should have lock on LOCK_open when calling this.
2169
@return false in case of success, true - otherwise.
2172
2172
bool reopen_tables(Session *session, bool get_locks, bool mark_share_as_old)
2261
Close handlers for tables in list, but leave the Table structure
2262
intact so that we can re-open these quickly.
2261
Close handlers for tables in list, but leave the Table structure
2262
intact so that we can re-open these quickly.
2264
@param session Thread context
2265
@param table Head of the list of Table objects
2266
@param morph_locks true - remove locks which we have on tables being closed
2267
but ensure that no DML or DDL will sneak in before
2268
we will re-open the table (i.e. temporarily morph
2269
our table-level locks into name-locks).
2271
@param send_refresh Should we awake waiters even if we didn't close any tables?
2264
@param session Thread context
2265
@param table Head of the list of Table objects
2266
@param morph_locks true - remove locks which we have on tables being closed
2267
but ensure that no DML or DDL will sneak in before
2268
we will re-open the table (i.e. temporarily morph
2269
our table-level locks into name-locks).
2271
@param send_refresh Should we awake waiters even if we didn't close any tables?
2274
2274
static void close_old_data_files(Session *session, Table *table, bool morph_locks,
2416
2416
drop tables from locked list
2419
drop_locked_tables()
2420
session Thread thandler
2422
table_name Table name
2419
drop_locked_tables()
2420
session Thread thandler
2422
table_name Table name
2425
This is only called on drop tables
2425
This is only called on drop tables
2427
The Table object for the dropped table is unlocked but still kept around
2428
as a name lock, which means that the table will be available for other
2429
thread as soon as we call unlock_table_names().
2430
If there is multiple copies of the table locked, all copies except
2431
the first, which acts as a name lock, is removed.
2427
The Table object for the dropped table is unlocked but still kept around
2428
as a name lock, which means that the table will be available for other
2429
thread as soon as we call unlock_table_names().
2430
If there is multiple copies of the table locked, all copies except
2431
the first, which acts as a name lock, is removed.
2434
# If table existed, return table
2435
0 Table was not locked
2434
# If table existed, return table
2435
0 Table was not locked
2515
2515
Load a table definition from file and open unireg table
2519
session Thread handle
2520
entry Store open table definition here
2521
table_list TableList with db, table_name
2523
cache_key Key for share_cache
2524
cache_key_length length of cache_key
2519
session Thread handle
2520
entry Store open table definition here
2521
table_list TableList with db, table_name
2523
cache_key Key for share_cache
2524
cache_key_length length of cache_key
2527
Extra argument for open is taken from session->open_options
2528
One must have a lock on LOCK_open when calling this function
2527
Extra argument for open is taken from session->open_options
2528
One must have a lock on LOCK_open when calling this function
2535
2535
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
2567
Here we should wait until all threads has released the table.
2568
For now we do one retry. This may cause a deadlock if there
2569
is other threads waiting for other tables used by this thread.
2567
Here we should wait until all threads has released the table.
2568
For now we do one retry. This may cause a deadlock if there
2569
is other threads waiting for other tables used by this thread.
2571
Proper fix would be to if the second retry failed:
2572
- Mark that table def changed
2573
- Return from open table
2574
- Close all tables used by this thread
2575
- Start waiting that the share is released
2576
- Retry by opening all tables again
2571
Proper fix would be to if the second retry failed:
2572
- Mark that table def changed
2573
- Return from open table
2574
- Close all tables used by this thread
2575
- Start waiting that the share is released
2576
- Retry by opening all tables again
2596
2596
if (!entry->s || !entry->s->crashed)
2598
// Code below is for repairing a crashed file
2599
if ((error= lock_table_name(session, table_list, true)))
2603
if (wait_for_locked_table_names(session, table_list))
2605
unlock_table_name(table_list);
2609
pthread_mutex_unlock(&LOCK_open);
2610
session->clear_error(); // Clear error message
2612
if (open_table_from_share(session, share, alias,
2613
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2617
ha_open_options | HA_OPEN_FOR_REPAIR,
2618
entry, OTM_OPEN) || ! entry->file ||
2598
// Code below is for repairing a crashed file
2599
if ((error= lock_table_name(session, table_list, true)))
2603
if (wait_for_locked_table_names(session, table_list))
2605
unlock_table_name(table_list);
2609
pthread_mutex_unlock(&LOCK_open);
2610
session->clear_error(); // Clear error message
2612
if (open_table_from_share(session, share, alias,
2613
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2617
ha_open_options | HA_OPEN_FOR_REPAIR,
2618
entry, OTM_OPEN) || ! entry->file ||
2619
2619
(entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
2621
/* Give right error message */
2622
session->clear_error();
2623
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2624
errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2625
share->table_name.str);
2627
entry->closefrm(false);
2631
session->clear_error(); // Clear error message
2632
pthread_mutex_lock(&LOCK_open);
2633
unlock_table_name(table_list);
2621
/* Give right error message */
2622
session->clear_error();
2623
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2624
errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2625
share->table_name.str);
2627
entry->closefrm(false);
2631
session->clear_error(); // Clear error message
2632
pthread_mutex_lock(&LOCK_open);
2633
unlock_table_name(table_list);
2641
2641
If we are here, there was no fatal error (but error may be still
2685
2685
Open all tables in list
2689
session - thread handler
2690
start - list of tables in/out
2691
counter - number of opened tables will be return using this parameter
2692
flags - bitmap of flags to modify how the tables will be open:
2693
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
2694
done a flush or namelock on it.
2689
session - thread handler
2690
start - list of tables in/out
2691
counter - number of opened tables will be return using this parameter
2692
flags - bitmap of flags to modify how the tables will be open:
2693
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
2694
done a flush or namelock on it.
2697
Unless we are already in prelocked mode, this function will also precache
2698
all SP/SFs explicitly or implicitly (via views and triggers) used by the
2699
query and add tables needed for their execution to table list. If resulting
2700
tables list will be non empty it will mark query as requiring precaching.
2701
Prelocked mode will be enabled for such query during lock_tables() call.
2697
Unless we are already in prelocked mode, this function will also precache
2698
all SP/SFs explicitly or implicitly (via views and triggers) used by the
2699
query and add tables needed for their execution to table list. If resulting
2700
tables list will be non empty it will mark query as requiring precaching.
2701
Prelocked mode will be enabled for such query during lock_tables() call.
2703
If query for which we are opening tables is already marked as requiring
2704
prelocking it won't do such precaching and will simply reuse table list
2705
which is already built.
2703
If query for which we are opening tables is already marked as requiring
2704
prelocking it won't do such precaching and will simply reuse table list
2705
which is already built.
2712
2712
int open_tables(Session *session, TableList **start, uint32_t *counter, uint32_t flags)
2864
2864
@param[in] lock_type lock to use for table
2867
@retval != NULL OK, opened table returned
2871
If ok, the following are also set:
2872
table_list->lock_type lock_type
2873
table_list->table table
2876
If table_l is a list, not a single table, the list is temporarily
2867
@retval != NULL OK, opened table returned
2871
If ok, the following are also set:
2872
table_list->lock_type lock_type
2873
table_list->table table
2876
If table_l is a list, not a single table, the list is temporarily
2880
This function is meant as a replacement for open_ltable() when
2881
MERGE tables can be opened. open_ltable() cannot open MERGE tables.
2880
This function is meant as a replacement for open_ltable() when
2881
MERGE tables can be opened. open_ltable() cannot open MERGE tables.
2883
There may be more differences between open_n_lock_single_table() and
2884
open_ltable(). One known difference is that open_ltable() does
2885
neither call decide_logging_format() nor handle some other logging
2886
and locking issues because it does not call lock_tables().
2883
There may be more differences between open_n_lock_single_table() and
2884
open_ltable(). One known difference is that open_ltable() does
2885
neither call decide_logging_format() nor handle some other logging
2886
and locking issues because it does not call lock_tables().
2889
2889
Table *open_n_lock_single_table(Session *session, TableList *table_l,
2914
2914
Open and lock one table
2918
session Thread handler
2919
table_list Table to open is first table in this list
2920
lock_type Lock to use for open
2921
lock_flags Flags passed to mysql_lock_table
2918
session Thread handler
2919
table_list Table to open is first table in this list
2920
lock_type Lock to use for open
2921
lock_flags Flags passed to mysql_lock_table
2924
This function don't do anything like SP/SF/views/triggers analysis done
2925
in open_tables(). It is intended for opening of only one concrete table.
2926
And used only in special contexts.
2924
This function don't do anything like SP/SF/views/triggers analysis done
2925
in open_tables(). It is intended for opening of only one concrete table.
2926
And used only in special contexts.
2932
If ok, the following are also set:
2933
table_list->lock_type lock_type
2934
table_list->table table
2932
If ok, the following are also set:
2933
table_list->lock_type lock_type
2934
table_list->table table
2937
2937
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type lock_type,
2953
2953
if (session->locked_tables)
2955
2955
if (check_lock_and_start_stmt(session, table, lock_type))
2960
2960
assert(session->lock == 0); // You must lock everything at once
2961
2961
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
2962
if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
2963
lock_flags, &refresh)))
2962
if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
2963
lock_flags, &refresh)))
2974
2974
Open all tables in list, locks them and optionally process derived tables.
2977
open_and_lock_tables_derived()
2978
session - thread handler
2979
tables - list of tables for open&locking
2980
derived - if to handle derived tables
2977
open_and_lock_tables_derived()
2978
session - thread handler
2979
tables - list of tables for open&locking
2980
derived - if to handle derived tables
2987
The lock will automaticaly be freed by close_thread_tables()
2990
There are two convenience functions:
2991
- simple_open_n_lock_tables(session, tables) without derived handling
2992
- open_and_lock_tables(session, tables) with derived handling
2993
Both inline functions call open_and_lock_tables_derived() with
2994
the third argument set appropriately.
2987
The lock will automaticaly be freed by close_thread_tables()
2990
There are two convenience functions:
2991
- simple_open_n_lock_tables(session, tables) without derived handling
2992
- open_and_lock_tables(session, tables) with derived handling
2993
Both inline functions call open_and_lock_tables_derived() with
2994
the third argument set appropriately.
2997
2997
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3023
3023
Open all tables in list and process derived tables
3026
open_normal_and_derived_tables
3027
session - thread handler
3028
tables - list of tables for open
3029
flags - bitmap of flags to modify how the tables will be open:
3030
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3031
done a flush or namelock on it.
3026
open_normal_and_derived_tables
3027
session - thread handler
3028
tables - list of tables for open
3029
flags - bitmap of flags to modify how the tables will be open:
3030
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3031
done a flush or namelock on it.
3038
This is to be used on prepare stage when you don't read any
3039
data from the tables.
3038
This is to be used on prepare stage when you don't read any
3039
data from the tables.
3042
3042
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
3053
3053
Lock all tables in list
3057
session Thread handler
3058
tables Tables to lock
3059
count Number of opened tables
3060
need_reopen Out parameter which if true indicates that some
3061
tables were dropped or altered during this call
3062
and therefore invoker should reopen tables and
3063
try to lock them once again (in this case
3064
lock_tables() will also return error).
3057
session Thread handler
3058
tables Tables to lock
3059
count Number of opened tables
3060
need_reopen Out parameter which if true indicates that some
3061
tables were dropped or altered during this call
3062
and therefore invoker should reopen tables and
3063
try to lock them once again (in this case
3064
lock_tables() will also return error).
3067
You can't call lock_tables twice, as this would break the dead-lock-free
3068
handling thr_lock gives us. You most always get all needed locks at
3067
You can't call lock_tables twice, as this would break the dead-lock-free
3068
handling thr_lock gives us. You most always get all needed locks at
3071
If query for which we are calling this function marked as requring
3072
prelocking, this function will do implicit LOCK TABLES and change
3073
session::prelocked_mode accordingly.
3071
If query for which we are calling this function marked as requring
3072
prelocking, this function will do implicit LOCK TABLES and change
3073
session::prelocked_mode accordingly.
3080
3080
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
3167
3167
Open a single table without table caching and don't set it in open_list
3170
open_temporary_table()
3171
session Thread object
3172
path Path (without .frm)
3174
table_name Table name
3175
link_in_list 1 if table should be linked into session->temporary_tables
3178
Used by alter_table to open a temporary table and when creating
3179
a temporary table with CREATE TEMPORARY ...
3170
open_temporary_table()
3171
session Thread object
3172
path Path (without .frm)
3174
table_name Table name
3175
link_in_list 1 if table should be linked into session->temporary_tables
3178
Used by alter_table to open a temporary table and when creating
3179
a temporary table with CREATE TEMPORARY ...
3186
3186
Table *open_temporary_table(Session *session, const char *path, const char *db,
3187
const char *table_name, bool link_in_list,
3187
const char *table_name, bool link_in_list,
3188
3188
open_table_mode open_mode)
3190
3190
Table *tmp_table;
3281
3281
/*****************************************************************************
3282
* The following find_field_in_XXX procedures implement the core of the
3283
* name resolution functionality. The entry point to resolve a column name in a
3284
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3285
* for each table reference. In turn, depending on the type of table reference,
3286
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3287
* below specific for the type of table reference.
3288
******************************************************************************/
3282
* The following find_field_in_XXX procedures implement the core of the
3283
* name resolution functionality. The entry point to resolve a column name in a
3284
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3285
* for each table reference. In turn, depending on the type of table reference,
3286
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3287
* below specific for the type of table reference.
3288
******************************************************************************/
3290
3290
/* Special Field pointers as return values of find_field_in_XXX functions. */
3291
3291
Field *not_found_field= (Field*) 0x1;
3338
3338
Find field by name in a NATURAL/USING join table reference.
3341
find_field_in_natural_join()
3342
session [in] thread handler
3343
table_ref [in] table reference to search
3344
name [in] name of field
3345
length [in] length of name
3346
ref [in/out] if 'name' is resolved to a view field, ref is
3347
set to point to the found view field
3348
register_tree_change [in] true if ref is not stack variable and we
3349
need register changes in item tree
3350
actual_table [out] the original table reference where the field
3351
belongs - differs from 'table_list' only for
3341
find_field_in_natural_join()
3342
session [in] thread handler
3343
table_ref [in] table reference to search
3344
name [in] name of field
3345
length [in] length of name
3346
ref [in/out] if 'name' is resolved to a view field, ref is
3347
set to point to the found view field
3348
register_tree_change [in] true if ref is not stack variable and we
3349
need register changes in item tree
3350
actual_table [out] the original table reference where the field
3351
belongs - differs from 'table_list' only for
3355
Search for a field among the result fields of a NATURAL/USING join.
3356
Notice that this procedure is called only for non-qualified field
3357
names. In the case of qualified fields, we search directly the base
3358
tables of a natural join.
3355
Search for a field among the result fields of a NATURAL/USING join.
3356
Notice that this procedure is called only for non-qualified field
3357
names. In the case of qualified fields, we search directly the base
3358
tables of a natural join.
3361
NULL if the field was not found
3362
WRONG_GRANT if no access rights to the found field
3363
# Pointer to the found Field
3361
NULL if the field was not found
3362
WRONG_GRANT if no access rights to the found field
3363
# Pointer to the found Field
3408
3408
Find field by name in a base table or a view with temp table algorithm.
3411
find_field_in_table()
3412
session thread handler
3413
table table where to search for the field
3415
length length of name
3416
allow_rowid do allow finding of "_rowid" field?
3417
cached_field_index_ptr cached position in field list (used to speedup
3418
lookup for fields in prepared tables)
3411
find_field_in_table()
3412
session thread handler
3413
table table where to search for the field
3415
length length of name
3416
allow_rowid do allow finding of "_rowid" field?
3417
cached_field_index_ptr cached position in field list (used to speedup
3418
lookup for fields in prepared tables)
3421
0 field is not found
3421
0 field is not found
3480
3480
Find field in a table reference.
3483
find_field_in_table_ref()
3484
session [in] thread handler
3485
table_list [in] table reference to search
3486
name [in] name of field
3487
length [in] field length of name
3488
item_name [in] name of item if it will be created (VIEW)
3489
db_name [in] optional database name that qualifies the
3490
table_name [in] optional table name that qualifies the field
3491
ref [in/out] if 'name' is resolved to a view field, ref
3492
is set to point to the found view field
3493
check_privileges [in] check privileges
3494
allow_rowid [in] do allow finding of "_rowid" field?
3495
cached_field_index_ptr [in] cached position in field list (used to
3496
speedup lookup for fields in prepared tables)
3497
register_tree_change [in] true if ref is not stack variable and we
3498
need register changes in item tree
3499
actual_table [out] the original table reference where the field
3500
belongs - differs from 'table_list' only for
3501
NATURAL_USING joins.
3483
find_field_in_table_ref()
3484
session [in] thread handler
3485
table_list [in] table reference to search
3486
name [in] name of field
3487
length [in] field length of name
3488
item_name [in] name of item if it will be created (VIEW)
3489
db_name [in] optional database name that qualifies the
3490
table_name [in] optional table name that qualifies the field
3491
ref [in/out] if 'name' is resolved to a view field, ref
3492
is set to point to the found view field
3493
check_privileges [in] check privileges
3494
allow_rowid [in] do allow finding of "_rowid" field?
3495
cached_field_index_ptr [in] cached position in field list (used to
3496
speedup lookup for fields in prepared tables)
3497
register_tree_change [in] true if ref is not stack variable and we
3498
need register changes in item tree
3499
actual_table [out] the original table reference where the field
3500
belongs - differs from 'table_list' only for
3501
NATURAL_USING joins.
3504
Find a field in a table reference depending on the type of table
3505
reference. There are three types of table references with respect
3506
to the representation of their result columns:
3507
- an array of Field_translator objects for MERGE views and some
3508
information_schema tables,
3509
- an array of Field objects (and possibly a name hash) for stored
3511
- a list of Natural_join_column objects for NATURAL/USING joins.
3512
This procedure detects the type of the table reference 'table_list'
3513
and calls the corresponding search routine.
3504
Find a field in a table reference depending on the type of table
3505
reference. There are three types of table references with respect
3506
to the representation of their result columns:
3507
- an array of Field_translator objects for MERGE views and some
3508
information_schema tables,
3509
- an array of Field objects (and possibly a name hash) for stored
3511
- a list of Natural_join_column objects for NATURAL/USING joins.
3512
This procedure detects the type of the table reference 'table_list'
3513
and calls the corresponding search routine.
3516
0 field is not found
3517
view_ref_found found value in VIEW (real result is in *ref)
3516
0 field is not found
3517
view_ref_found found value in VIEW (real result is in *ref)
3547
3547
inside the view, but we want to search directly in the view columns
3548
3548
which are represented as a 'field_translation'.
3550
TODO: Ensure that table_name, db_name and tables->db always points to
3550
TODO: Ensure that table_name, db_name and tables->db always points to
3553
3553
if (/* Exclude nested joins. */
3554
3554
(!table_list->nested_join) &&
3555
/* Include merge views and information schema tables. */
3555
/* Include merge views and information schema tables. */
3557
3557
Test if the field qualifiers match the table reference we plan
3613
if (session->mark_used_columns != MARK_COLUMNS_NONE)
3613
if (session->mark_used_columns != MARK_COLUMNS_NONE)
3616
Get rw_set correct for this field so that the handler
3617
knows that this field is involved in the query and gets
3620
Field *field_to_set= NULL;
3621
if (fld == view_ref_found)
3616
Get rw_set correct for this field so that the handler
3617
knows that this field is involved in the query and gets
3620
Field *field_to_set= NULL;
3621
if (fld == view_ref_found)
3623
Item *it= (*ref)->real_item();
3624
if (it->type() == Item::FIELD_ITEM)
3625
field_to_set= ((Item_field*)it)->field;
3628
if (session->mark_used_columns == MARK_COLUMNS_READ)
3629
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3623
Item *it= (*ref)->real_item();
3624
if (it->type() == Item::FIELD_ITEM)
3625
field_to_set= ((Item_field*)it)->field;
3636
Table *table= field_to_set->table;
3637
3628
if (session->mark_used_columns == MARK_COLUMNS_READ)
3638
table->setReadSet(field_to_set->field_index);
3640
table->setWriteSet(field_to_set->field_index);
3629
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3636
Table *table= field_to_set->table;
3637
if (session->mark_used_columns == MARK_COLUMNS_READ)
3638
table->setReadSet(field_to_set->field_index);
3640
table->setWriteSet(field_to_set->field_index);
3695
3695
Find field in table list.
3698
find_field_in_tables()
3699
session pointer to current thread structure
3700
item field item that should be found
3701
first_table list of tables to be searched for item
3702
last_table end of the list of tables to search for item. If NULL
3703
then search to the end of the list 'first_table'.
3704
ref if 'item' is resolved to a view field, ref is set to
3705
point to the found view field
3706
report_error Degree of error reporting:
3707
- IGNORE_ERRORS then do not report any error
3708
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
3709
fields, suppress all other errors
3710
- REPORT_EXCEPT_NON_UNIQUE report all other errors
3711
except when non-unique fields were found
3713
check_privileges need to check privileges
3714
register_tree_change true if ref is not a stack variable and we
3715
to need register changes in item tree
3698
find_field_in_tables()
3699
session pointer to current thread structure
3700
item field item that should be found
3701
first_table list of tables to be searched for item
3702
last_table end of the list of tables to search for item. If NULL
3703
then search to the end of the list 'first_table'.
3704
ref if 'item' is resolved to a view field, ref is set to
3705
point to the found view field
3706
report_error Degree of error reporting:
3707
- IGNORE_ERRORS then do not report any error
3708
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
3709
fields, suppress all other errors
3710
- REPORT_EXCEPT_NON_UNIQUE report all other errors
3711
except when non-unique fields were found
3713
check_privileges need to check privileges
3714
register_tree_change true if ref is not a stack variable and we
3715
to need register changes in item tree
3718
0 If error: the found field is not unique, or there are
3719
no sufficient access priviliges for the found field,
3720
or the field is qualified with non-existing table.
3721
not_found_field The function was called with report_error ==
3722
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
3723
field was not found.
3724
view_ref_found View field is found, item passed through ref parameter
3725
found field If a item was resolved to some field
3718
0 If error: the found field is not unique, or there are
3719
no sufficient access priviliges for the found field,
3720
or the field is qualified with non-existing table.
3721
not_found_field The function was called with report_error ==
3722
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
3723
field was not found.
3724
view_ref_found View field is found, item passed through ref parameter
3725
found field If a item was resolved to some field
3729
3729
find_field_in_tables(Session *session, Item_ident *item,
3730
3730
TableList *first_table, TableList *last_table,
3731
Item **ref, find_item_error_report_type report_error,
3731
Item **ref, find_item_error_report_type report_error,
3732
3732
bool check_privileges, bool register_tree_change)
3734
3734
Field *found=0;
3921
3921
Find Item in list of items (find_field_in_tables analog)
3924
is it better return only counter?
3924
is it better return only counter?
3930
counter To return number of found item
3932
REPORT_ALL_ERRORS report errors, return 0 if error
3933
REPORT_EXCEPT_NOT_FOUND Do not report 'not found' error and
3934
return not_found_item, report other errors,
3936
IGNORE_ERRORS Do not report errors, return 0 if error
3937
resolution Set to the resolution type if the item is found
3938
(it says whether the item is resolved
3939
against an alias name,
3940
or as a field name without alias,
3941
or as a field hidden by alias,
3930
counter To return number of found item
3932
REPORT_ALL_ERRORS report errors, return 0 if error
3933
REPORT_EXCEPT_NOT_FOUND Do not report 'not found' error and
3934
return not_found_item, report other errors,
3936
IGNORE_ERRORS Do not report errors, return 0 if error
3937
resolution Set to the resolution type if the item is found
3938
(it says whether the item is resolved
3939
against an alias name,
3940
or as a field name without alias,
3941
or as a field hidden by alias,
3945
0 Item is not found or item is not unique,
3946
error message is reported
3947
not_found_item Function was called with
3948
report_error == REPORT_EXCEPT_NOT_FOUND and
3949
item was not found. No error message was reported
3945
0 Item is not found or item is not unique,
3946
error message is reported
3947
not_found_item Function was called with
3948
report_error == REPORT_EXCEPT_NOT_FOUND and
3949
item was not found. No error message was reported
3953
3953
/* Special Item pointer to serve as a return value from find_item_in_list(). */
3990
3990
Item_ident *item_field= (Item_ident*) item;
3993
In case of group_concat() with ORDER BY condition in the QUERY
3994
item_field can be field of temporary table without item name
3995
(if this field created from expression argument of group_concat()),
3996
=> we have to check presence of name before compare
3993
In case of group_concat() with ORDER BY condition in the QUERY
3994
item_field can be field of temporary table without item name
3995
(if this field created from expression argument of group_concat()),
3996
=> we have to check presence of name before compare
3998
3998
if (!item_field->name)
4013
4013
case sensitive. In cases where they are not case sensitive, they
4014
4014
are always in lower case.
4016
item_field->field_name and item_field->table_name can be 0x0 if
4017
item is not fix_field()'ed yet.
4016
item_field->field_name and item_field->table_name can be 0x0 if
4017
item is not fix_field()'ed yet.
4019
4019
if (item_field->field_name && item_field->table_name &&
4020
!my_strcasecmp(system_charset_info, item_field->field_name,
4020
!my_strcasecmp(system_charset_info, item_field->field_name,
4022
4022
!my_strcasecmp(table_alias_charset, item_field->table_name,
4146
4146
Test if a string is a member of a list of strings.
4149
test_if_string_in_list()
4150
find the string to look for
4151
str_list a list of strings to be searched
4149
test_if_string_in_list()
4150
find the string to look for
4151
str_list a list of strings to be searched
4154
Sequentially search a list of strings for a string, and test whether
4155
the list contains the same string.
4154
Sequentially search a list of strings for a string, and test whether
4155
the list contains the same string.
4158
true if find is in str_list
4158
true if find is in str_list
4181
4181
being resolved in a specific table reference.
4184
set_new_item_local_context()
4185
session pointer to current thread
4186
item item for which new context is created and set
4187
table_ref table ref where an item showld be resolved
4184
set_new_item_local_context()
4185
session pointer to current thread
4186
item item for which new context is created and set
4187
table_ref table ref where an item showld be resolved
4190
Create a new name resolution context for an item, so that the item
4191
is resolved only the supplied 'table_ref'.
4190
Create a new name resolution context for an item, so that the item
4191
is resolved only the supplied 'table_ref'.
4213
4213
Find and mark the common columns of two table references.
4216
mark_common_columns()
4217
session [in] current thread
4218
table_ref_1 [in] the first (left) join operand
4219
table_ref_2 [in] the second (right) join operand
4220
using_fields [in] if the join is JOIN...USING - the join columns,
4221
if NATURAL join, then NULL
4222
found_using_fields [out] number of fields from the USING clause that were
4223
found among the common fields
4216
mark_common_columns()
4217
session [in] current thread
4218
table_ref_1 [in] the first (left) join operand
4219
table_ref_2 [in] the second (right) join operand
4220
using_fields [in] if the join is JOIN...USING - the join columns,
4221
if NATURAL join, then NULL
4222
found_using_fields [out] number of fields from the USING clause that were
4223
found among the common fields
4226
The procedure finds the common columns of two relations (either
4227
tables or intermediate join results), and adds an equi-join condition
4228
to the ON clause of 'table_ref_2' for each pair of matching columns.
4229
If some of table_ref_XXX represents a base table or view, then we
4230
create new 'Natural_join_column' instances for each column
4231
reference and store them in the 'join_columns' of the table
4226
The procedure finds the common columns of two relations (either
4227
tables or intermediate join results), and adds an equi-join condition
4228
to the ON clause of 'table_ref_2' for each pair of matching columns.
4229
If some of table_ref_XXX represents a base table or view, then we
4230
create new 'Natural_join_column' instances for each column
4231
reference and store them in the 'join_columns' of the table
4235
The procedure assumes that store_natural_using_join_columns() was
4236
called for the previous level of NATURAL/USING joins.
4235
The procedure assumes that store_natural_using_join_columns() was
4236
called for the previous level of NATURAL/USING joins.
4239
true error when some common column is non-unique, or out of memory
4239
true error when some common column is non-unique, or out of memory
4423
4423
Materialize and store the row type of NATURAL/USING join.
4426
store_natural_using_join_columns()
4427
session current thread
4428
natural_using_join the table reference of the NATURAL/USING join
4429
table_ref_1 the first (left) operand (of a NATURAL/USING join).
4430
table_ref_2 the second (right) operand (of a NATURAL/USING join).
4431
using_fields if the join is JOIN...USING - the join columns,
4432
if NATURAL join, then NULL
4433
found_using_fields number of fields from the USING clause that were
4434
found among the common fields
4426
store_natural_using_join_columns()
4427
session current thread
4428
natural_using_join the table reference of the NATURAL/USING join
4429
table_ref_1 the first (left) operand (of a NATURAL/USING join).
4430
table_ref_2 the second (right) operand (of a NATURAL/USING join).
4431
using_fields if the join is JOIN...USING - the join columns,
4432
if NATURAL join, then NULL
4433
found_using_fields number of fields from the USING clause that were
4434
found among the common fields
4437
Iterate over the columns of both join operands and sort and store
4438
all columns into the 'join_columns' list of natural_using_join
4439
where the list is formed by three parts:
4440
part1: The coalesced columns of table_ref_1 and table_ref_2,
4441
sorted according to the column order of the first table.
4442
part2: The other columns of the first table, in the order in
4443
which they were defined in CREATE TABLE.
4444
part3: The other columns of the second table, in the order in
4445
which they were defined in CREATE TABLE.
4446
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
4449
The procedure assumes that mark_common_columns() has been called
4450
for the join that is being processed.
4453
true error: Some common column is ambiguous
4437
Iterate over the columns of both join operands and sort and store
4438
all columns into the 'join_columns' list of natural_using_join
4439
where the list is formed by three parts:
4440
part1: The coalesced columns of table_ref_1 and table_ref_2,
4441
sorted according to the column order of the first table.
4442
part2: The other columns of the first table, in the order in
4443
which they were defined in CREATE TABLE.
4444
part3: The other columns of the second table, in the order in
4445
which they were defined in CREATE TABLE.
4446
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
4449
The procedure assumes that mark_common_columns() has been called
4450
for the join that is being processed.
4453
true error: Some common column is ambiguous
4547
4547
Precompute and store the row types of the top-most NATURAL/USING joins.
4550
store_top_level_join_columns()
4551
session current thread
4552
table_ref nested join or table in a FROM clause
4553
left_neighbor neighbor table reference to the left of table_ref at the
4554
same level in the join tree
4555
right_neighbor neighbor table reference to the right of table_ref at the
4556
same level in the join tree
4550
store_top_level_join_columns()
4551
session current thread
4552
table_ref nested join or table in a FROM clause
4553
left_neighbor neighbor table reference to the left of table_ref at the
4554
same level in the join tree
4555
right_neighbor neighbor table reference to the right of table_ref at the
4556
same level in the join tree
4559
The procedure performs a post-order traversal of a nested join tree
4560
and materializes the row types of NATURAL/USING joins in a
4561
bottom-up manner until it reaches the TableList elements that
4562
represent the top-most NATURAL/USING joins. The procedure should be
4563
applied to each element of Select_Lex::top_join_list (i.e. to each
4564
top-level element of the FROM clause).
4559
The procedure performs a post-order traversal of a nested join tree
4560
and materializes the row types of NATURAL/USING joins in a
4561
bottom-up manner until it reaches the TableList elements that
4562
represent the top-most NATURAL/USING joins. The procedure should be
4563
applied to each element of Select_Lex::top_join_list (i.e. to each
4564
top-level element of the FROM clause).
4567
Notice that the table references in the list nested_join->join_list
4568
are in reverse order, thus when we iterate over it, we are moving
4569
from the right to the left in the FROM clause.
4567
Notice that the table references in the list nested_join->join_list
4568
are in reverse order, thus when we iterate over it, we are moving
4569
from the right to the left in the FROM clause.
4617
4617
neighbors at the same level.
4619
4619
real_left_neighbor= (same_level_left_neighbor) ?
4620
same_level_left_neighbor : left_neighbor;
4620
same_level_left_neighbor : left_neighbor;
4621
4621
real_right_neighbor= (same_level_right_neighbor) ?
4622
same_level_right_neighbor : right_neighbor;
4622
same_level_right_neighbor : right_neighbor;
4624
4624
if (cur_table_ref->nested_join &&
4625
4625
store_top_level_join_columns(session, cur_table_ref,
4636
4636
if (table_ref->is_natural_join)
4638
4638
assert(table_ref->nested_join &&
4639
table_ref->nested_join->join_list.elements == 2);
4639
table_ref->nested_join->join_list.elements == 2);
4640
4640
List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
4642
4642
Notice that the order of join operands depends on whether table_ref
4643
4643
represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
4644
4644
in inverted order.
4646
4646
TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
4647
4647
TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
4648
4648
List<String> *using_fields= table_ref->join_using_fields;
4711
4711
in a FROM clause.
4714
setup_natural_join_row_types()
4715
session current thread
4716
from_clause list of top-level table references in a FROM clause
4714
setup_natural_join_row_types()
4715
session current thread
4716
from_clause list of top-level table references in a FROM clause
4719
Apply the procedure 'store_top_level_join_columns' to each of the
4720
top-level table referencs of the FROM clause. Adjust the list of tables
4721
for name resolution - context->first_name_resolution_table to the
4722
top-most, lef-most NATURAL/USING join.
4719
Apply the procedure 'store_top_level_join_columns' to each of the
4720
top-level table referencs of the FROM clause. Adjust the list of tables
4721
for name resolution - context->first_name_resolution_table to the
4722
top-most, lef-most NATURAL/USING join.
4725
Notice that the table references in 'from_clause' are in reverse
4726
order, thus when we iterate over it, we are moving from the right
4727
to the left in the FROM clause.
4725
Notice that the table references in 'from_clause' are in reverse
4726
order, thus when we iterate over it, we are moving from the right
4727
to the left in the FROM clause.
4733
4733
static bool setup_natural_join_row_types(Session *session,
4734
4734
List<TableList> *from_clause,
4817
4817
((Item_field*) item)->table_name, &it,
4818
4818
any_privileges))
4822
4822
if (sum_func_list)
4825
sum_func_list is a list that has the fields list as a tail.
4826
Because of this we have to update the element count also for this
4827
list after expanding the '*' entry.
4829
sum_func_list->elements+= fields.elements - elem;
4825
sum_func_list is a list that has the fields list as a tail.
4826
Because of this we have to update the element count also for this
4827
list after expanding the '*' entry.
4829
sum_func_list->elements+= fields.elements - elem;
4840
4840
/****************************************************************************
4841
** Check that all given fields exists and fill struct with current data
4842
****************************************************************************/
4841
** Check that all given fields exists and fill struct with current data
4842
****************************************************************************/
4844
4844
bool setup_fields(Session *session, Item **ref_pointer_array,
4845
4845
List<Item> &fields, enum_mark_columns mark_used_columns,
4931
session Thread handler
4932
context name resolution contest to setup table list there
4933
from_clause Top-level list of table references in the FROM clause
4934
tables Table list (select_lex->table_list)
4935
leaves List of join table leaves list (select_lex->leaf_tables)
4936
refresh It is onle refresh for subquery
4937
select_insert It is SELECT ... INSERT command
4931
session Thread handler
4932
context name resolution contest to setup table list there
4933
from_clause Top-level list of table references in the FROM clause
4934
tables Table list (select_lex->table_list)
4935
leaves List of join table leaves list (select_lex->leaf_tables)
4936
refresh It is onle refresh for subquery
4937
select_insert It is SELECT ... INSERT command
4940
Check also that the 'used keys' and 'ignored keys' exists and set up the
4941
table structure accordingly.
4942
Create a list of leaf tables. For queries with NATURAL/USING JOINs,
4943
compute the row types of the top most natural/using join table references
4944
and link these into a list of table references for name resolution.
4940
Check also that the 'used keys' and 'ignored keys' exists and set up the
4941
table structure accordingly.
4942
Create a list of leaf tables. For queries with NATURAL/USING JOINs,
4943
compute the row types of the top most natural/using join table references
4944
and link these into a list of table references for name resolution.
4946
This has to be called for all tables that are used by items, as otherwise
4947
table->map is not set and all Item_field will be regarded as const items.
4946
This has to be called for all tables that are used by items, as otherwise
4947
table->map is not set and all Item_field will be regarded as const items.
4950
false ok; In this case *map will includes the chosen index
4950
false ok; In this case *map will includes the chosen index
4954
4954
bool setup_tables(Session *session, Name_resolution_context *context,
5004
5004
prepare tables and check access for the view tables
5007
setup_tables_and_check_view_access()
5008
session Thread handler
5009
context name resolution contest to setup table list there
5010
from_clause Top-level list of table references in the FROM clause
5011
tables Table list (select_lex->table_list)
5012
conds Condition of current SELECT (can be changed by VIEW)
5013
leaves List of join table leaves list (select_lex->leaf_tables)
5014
refresh It is onle refresh for subquery
5015
select_insert It is SELECT ... INSERT command
5016
want_access what access is needed
5007
setup_tables_and_check_view_access()
5008
session Thread handler
5009
context name resolution contest to setup table list there
5010
from_clause Top-level list of table references in the FROM clause
5011
tables Table list (select_lex->table_list)
5012
conds Condition of current SELECT (can be changed by VIEW)
5013
leaves List of join table leaves list (select_lex->leaf_tables)
5014
refresh It is onle refresh for subquery
5015
select_insert It is SELECT ... INSERT command
5016
want_access what access is needed
5019
a wrapper for check_tables that will also check the resulting
5020
table leaves list for access to all the tables that belong to a view
5019
a wrapper for check_tables that will also check the resulting
5020
table leaves list for access to all the tables that belong to a view
5023
false ok; In this case *map will include the chosen index
5023
false ok; In this case *map will include the chosen index
5026
5026
bool setup_tables_and_check_access(Session *session,
5027
5027
Name_resolution_context *context,
5052
Create a key_map from a list of index names
5055
get_key_map_from_key_list()
5056
map key_map to fill in
5058
index_list List of index names
5061
0 ok; In this case *map will includes the choosed index
5052
Create a key_map from a list of index names
5055
get_key_map_from_key_list()
5056
map key_map to fill in
5058
index_list List of index names
5061
0 ok; In this case *map will includes the choosed index
5065
5065
bool get_key_map_from_key_list(key_map *map, Table *table,
5092
5092
Drops in all fields instead of current '*' field
5096
session Thread handler
5097
context Context for name resolution
5098
db_name Database name in case of 'database_name.table_name.*'
5099
table_name Table name in case of 'table_name.*'
5101
any_privileges 0 If we should ensure that we have SELECT privileges
5103
1 If any privilege is ok
5096
session Thread handler
5097
context Context for name resolution
5098
db_name Database name in case of 'database_name.table_name.*'
5099
table_name Table name in case of 'table_name.*'
5101
any_privileges 0 If we should ensure that we have SELECT privileges
5103
1 If any privilege is ok
5105
0 ok 'it' is updated to point at last inserted
5106
1 error. Error message is generated but not sent to client
5105
0 ok 'it' is updated to point at last inserted
5106
1 error. Error message is generated but not sent to client
5230
TODO: in the case when we skipped all columns because there was a
5231
qualified '*', and all columns were coalesced, we have to give a more
5232
meaningful message than ER_BAD_TABLE_ERROR.
5230
TODO: in the case when we skipped all columns because there was a
5231
qualified '*', and all columns were coalesced, we have to give a more
5232
meaningful message than ER_BAD_TABLE_ERROR.
5234
5234
if (!table_name)
5235
5235
my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
5244
5244
Fix all conditions and outer join expressions.
5248
session thread handler
5249
tables list of tables for name resolving (select_lex->table_list)
5250
leaves list of leaves of join table tree (select_lex->leaf_tables)
5248
session thread handler
5249
tables list of tables for name resolving (select_lex->table_list)
5250
leaves list of leaves of join table tree (select_lex->leaf_tables)
5257
true if some error occured (e.g. out of memory)
5257
true if some error occured (e.g. out of memory)
5261
5261
int setup_conds(Session *session, TableList *leaves, COND **conds)
5267
5267
it_is_update set to true when tables of primary Select_Lex (Select_Lex
5268
5268
which belong to LEX, i.e. most up SELECT) will be updated by
5269
5269
INSERT/UPDATE/LOAD
5270
NOTE: using this condition helps to prevent call of prepare_check_option()
5271
from subquery of VIEW, because tables of subquery belongs to VIEW
5272
(see condition before prepare_check_option() call)
5270
NOTE: using this condition helps to prevent call of prepare_check_option()
5271
from subquery of VIEW, because tables of subquery belongs to VIEW
5272
(see condition before prepare_check_option() call)
5274
5274
bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
5275
5275
select_lex->is_item_list_lookup= 0;
5330
5330
/******************************************************************************
5331
** Fill a record with data (for INSERT or UPDATE)
5332
** Returns : 1 if some field has wrong type
5333
******************************************************************************/
5331
** Fill a record with data (for INSERT or UPDATE)
5332
** Returns : 1 if some field has wrong type
5333
******************************************************************************/
5337
5337
Fill fields with given items.
5341
session thread handler
5342
fields Item_fields list to be filled
5343
values values to fill with
5344
ignore_errors true if we should ignore errors
5341
session thread handler
5342
fields Item_fields list to be filled
5343
values values to fill with
5344
ignore_errors true if we should ignore errors
5347
fill_record() may set table->auto_increment_field_not_null and a
5348
caller should make sure that it is reset after their last call to this
5347
fill_record() may set table->auto_increment_field_not_null and a
5348
caller should make sure that it is reset after their last call to this
5433
5433
Fill field buffer with values from Field list
5437
session thread handler
5438
ptr pointer on pointer to record
5439
values list of fields
5440
ignore_errors true if we should ignore errors
5437
session thread handler
5438
ptr pointer on pointer to record
5439
values list of fields
5440
ignore_errors true if we should ignore errors
5443
fill_record() may set table->auto_increment_field_not_null and a
5444
caller should make sure that it is reset after their last call to this
5443
fill_record() may set table->auto_increment_field_not_null and a
5444
caller should make sure that it is reset after their last call to this
5587
5587
/*****************************************************************************
5588
unireg support functions
5589
*****************************************************************************/
5588
unireg support functions
5589
*****************************************************************************/
5592
5592
Invalidate any cache entries that are for some DB
5595
remove_db_from_cache()
5596
db Database name. This will be in lower case if
5597
lower_case_table_name is set
5595
remove_db_from_cache()
5596
db Database name. This will be in lower case if
5597
lower_case_table_name is set
5600
We can't use hash_delete when looping hash_elements. We mark them first
5601
and afterwards delete those marked unused.
5600
We can't use hash_delete when looping hash_elements. We mark them first
5601
and afterwards delete those marked unused.
5604
5604
void remove_db_from_cache(const char *db)
5642
5642
close_thread_tables() is called.
5648
0 This thread now have exclusive access to this table and no other thread
5649
can access the table until close_thread_tables() is called.
5650
1 Table is in use by another thread
5648
0 This thread now have exclusive access to this table and no other thread
5649
can access the table until close_thread_tables() is called.
5650
1 Table is in use by another thread
5653
5653
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
5692
5692
in_use->some_tables_deleted= 1;
5693
5693
if (table->is_name_opened())
5698
Now we must abort all tables locks used by this thread
5699
as the thread may be waiting to get a lock for another table.
5698
Now we must abort all tables locks used by this thread
5699
as the thread may be waiting to get a lock for another table.
5700
5700
Note that we need to hold LOCK_open while going through the
5701
5701
list. So that the other thread cannot change it. The other
5702
5702
thread must also hold LOCK_open whenever changing the
5704
5704
closed and before the parent is closed would be fatal.
5706
5706
for (Table *session_table= in_use->open_tables;
5708
session_table= session_table->next)
5708
session_table= session_table->next)
5710
5710
/* Do not handle locks of MERGE children. */
5711
if (session_table->db_stat) // If table is open
5712
signalled|= mysql_lock_abort_for_thread(session, session_table);
5711
if (session_table->db_stat) // If table is open
5712
signalled|= mysql_lock_abort_for_thread(session, session_table);