116
98
return open_cache.records;
121
Close cursor handle, but leave the table in the table cache
124
close_handle_and_leave_table_as_lock()
128
By leaving the table in the table cache, it disallows any other thread
131
session->killed will be set if we run out of memory
133
If closing a MERGE child, the calling function has to take care for
134
closing the parent too, if necessary.
102
Create a table cache key
105
create_table_def_key()
106
session Thread handler
107
key Create key here (must be of size MAX_DBKEY_LENGTH)
108
table_list Table definition
109
tmp_table Set if table is a tmp table
112
The table cache_key is created from:
116
if the table is a tmp table, we add the following to make each tmp table
119
4 bytes for master thread id
120
4 bytes pseudo thread id
126
uint32_t create_table_def_key(Session *session, char *key, TableList *table_list,
131
key_pos= strcpy(key_pos, table_list->db) + strlen(table_list->db);
132
key_pos= strcpy(key_pos+1, table_list->table_name) +
133
strlen(table_list->table_name);
134
key_length= (uint32_t)(key_pos-key)+1;
138
int4store(key + key_length, session->server_id);
139
int4store(key + key_length + 4, session->variables.pseudo_thread_id);
140
key_length+= TMP_TABLE_KEY_EXTRA;
147
/*****************************************************************************
148
Functions to handle table definition cach (TABLE_SHARE)
149
*****************************************************************************/
151
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
152
bool not_used __attribute__((unused)))
154
TABLE_SHARE *entry=(TABLE_SHARE*) record;
155
*length= entry->table_cache_key.length;
156
return (unsigned char*) entry->table_cache_key.str;
160
static void table_def_free_entry(TABLE_SHARE *share)
164
/* remove from old_unused_share list */
165
pthread_mutex_lock(&LOCK_table_share);
166
*share->prev= share->next;
167
share->next->prev= share->prev;
168
pthread_mutex_unlock(&LOCK_table_share);
170
free_table_share(share);
175
bool table_def_init(void)
178
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
179
oldest_unused_share= &end_of_unused_share;
180
end_of_unused_share.prev= &oldest_unused_share;
182
return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
184
(hash_free_key) table_def_free_entry, 0);
188
void table_def_free(void)
190
if (table_def_inited)
193
pthread_mutex_destroy(&LOCK_table_share);
194
hash_free(&table_def_cache);
200
uint32_t cached_table_definitions(void)
202
return table_def_cache.records;
207
Get TABLE_SHARE for a table.
210
session Thread handle
211
table_list Table that should be opened
213
key_length Length of key
214
db_flags Flags to open_table_def():
216
error out: Error code from open_table_def()
219
Get a table definition from the table definition cache.
220
If it doesn't exist, create a new from the table definition file.
223
We must have wrlock on LOCK_open when we come here
224
(To be changed later)
231
TABLE_SHARE *get_table_share(Session *session, TableList *table_list, char *key,
232
uint32_t key_length, uint32_t db_flags, int *error)
238
/* Read table definition from cache */
239
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
243
if (!(share= alloc_table_share(table_list, key, key_length)))
249
Lock mutex to be able to read table definition from file without
252
(void) pthread_mutex_lock(&share->mutex);
255
We assign a new table id under the protection of the LOCK_open and
256
the share's own mutex. We do this insted of creating a new mutex
257
and using it for the sole purpose of serializing accesses to a
258
static variable, we assign the table id here. We assign it to the
259
share before inserting it into the table_def_cache to be really
260
sure that it cannot be read from the cache without having a table
263
CAVEAT. This means that the table cannot be used for
264
binlogging/replication purposes, unless get_table_share() has been
265
called directly or indirectly.
267
assign_new_table_id(share);
269
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
271
free_table_share(share);
272
return(0); // return error
274
if (open_table_def(session, share, db_flags))
276
*error= share->error;
277
(void) hash_delete(&table_def_cache, (unsigned char*) share);
280
share->ref_count++; // Mark in use
281
(void) pthread_mutex_unlock(&share->mutex);
286
We found an existing table definition. Return it if we didn't get
287
an error when reading the table definition from file.
290
/* We must do a lock to ensure that the structure is initialized */
291
(void) pthread_mutex_lock(&share->mutex);
294
/* Table definition contained an error */
295
open_table_error(share, share->error, share->open_errno, share->errarg);
296
(void) pthread_mutex_unlock(&share->mutex);
300
if (!share->ref_count++ && share->prev)
303
Share was not used before and it was in the old_unused_share list
304
Unlink share from this list
306
pthread_mutex_lock(&LOCK_table_share);
307
*share->prev= share->next;
308
share->next->prev= share->prev;
311
pthread_mutex_unlock(&LOCK_table_share);
313
(void) pthread_mutex_unlock(&share->mutex);
315
/* Free cache if too big */
316
while (table_def_cache.records > table_def_size &&
317
oldest_unused_share->next)
319
pthread_mutex_lock(&oldest_unused_share->mutex);
320
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
328
Get a table share. If it didn't exist, try creating it from engine
330
For arguments and return values, see get_table_from_share()
334
*get_table_share_with_create(Session *session, TableList *table_list,
335
char *key, uint32_t key_length,
336
uint32_t db_flags, int *error)
340
share= get_table_share(session, table_list, key, key_length, db_flags, error);
342
If share is not NULL, we found an existing share.
344
If share is NULL, and there is no error, we're inside
345
pre-locking, which silences 'ER_NO_SUCH_TABLE' errors
346
with the intention to silently drop non-existing tables
347
from the pre-locking list. In this case we still need to try
348
auto-discover before returning a NULL share.
350
If share is NULL and the error is ER_NO_SUCH_TABLE, this is
351
the same as above, only that the error was not silenced by
352
pre-locking. Once again, we need to try to auto-discover
355
Finally, if share is still NULL, it's a real error and we need
358
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
360
if (share || (session->is_error() && (session->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
369
Mark that we are not using table share anymore.
372
release_table_share()
374
release_type How the release should be done:
376
- Release without checking
377
RELEASE_WAIT_FOR_DROP
378
- Don't return until we get a signal that the
379
table is deleted or the thread is killed.
382
If ref_count goes to zero and (we have done a refresh or if we have
383
already too many open table shares) then delete the definition.
385
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
386
that the table is deleted or the thread is killed.
389
void release_table_share(TABLE_SHARE *share,
390
enum release_type type __attribute__((unused)))
392
bool to_be_deleted= 0;
394
safe_mutex_assert_owner(&LOCK_open);
396
pthread_mutex_lock(&share->mutex);
397
if (!--share->ref_count)
399
if (share->version != refresh_version)
403
/* Link share last in used_table_share list */
404
assert(share->next == 0);
405
pthread_mutex_lock(&LOCK_table_share);
406
share->prev= end_of_unused_share.prev;
407
*end_of_unused_share.prev= share;
408
end_of_unused_share.prev= &share->next;
409
share->next= &end_of_unused_share;
410
pthread_mutex_unlock(&LOCK_table_share);
412
to_be_deleted= (table_def_cache.records > table_def_size);
418
hash_delete(&table_def_cache, (unsigned char*) share);
421
pthread_mutex_unlock(&share->mutex);
427
Check if table definition exits in cache
430
get_cached_table_share()
432
table_name Table name
436
# TABLE_SHARE for table
439
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
441
char key[NAME_LEN*2+2];
442
TableList table_list;
444
safe_mutex_assert_owner(&LOCK_open);
446
table_list.db= (char*) db;
447
table_list.table_name= (char*) table_name;
448
key_length= create_table_def_key((Session*) 0, key, &table_list, 0);
449
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
454
Close file handle, but leave the table in the table cache
457
close_handle_and_leave_table_as_lock()
461
By leaving the table in the table cache, it disallows any other thread
464
session->killed will be set if we run out of memory
466
If closing a MERGE child, the calling function has to take care for
467
closing the parent too, if necessary.
138
471
void close_handle_and_leave_table_as_lock(Table *table)
140
TableShare *share, *old_share= table->s;
473
TABLE_SHARE *share, *old_share= table->s;
142
memory::Root *mem_root= &table->mem_root;
475
MEM_ROOT *mem_root= &table->mem_root;
144
477
assert(table->db_stat);
156
489
memset(share, 0, sizeof(*share));
157
490
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
158
491
old_share->table_cache_key.length);
159
share->tmp_table= message::Table::INTERNAL; // for intern_close_table()
492
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
162
table->cursor->close();
163
table->db_stat= 0; // Mark cursor closed
164
TableShare::release(table->s);
495
table->file->close();
496
table->db_stat= 0; // Mark file closed
497
release_table_share(table->s, RELEASE_NORMAL);
166
table->cursor->change_table_ptr(table, table->s);
499
table->file->change_table_ptr(table, table->s);
507
Create a list for all open tables matching SQL expression
511
wild SQL like expression
514
One gets only a list of tables for which one has any kind of privilege.
515
db and table names are allocated in result struct, so one doesn't need
516
a lock on LOCK_open when traversing the return list.
519
NULL Error (Probably OOM)
520
# Pointer to list of names of open tables.
523
OPEN_TableList *list_open_tables(const char *db, const char *wild)
526
OPEN_TableList **start_list, *open_list;
527
TableList table_list;
529
pthread_mutex_lock(&LOCK_open);
530
memset(&table_list, 0, sizeof(table_list));
531
start_list= &open_list;
534
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
536
OPEN_TableList *table;
537
Table *entry=(Table*) hash_element(&open_cache,idx);
538
TABLE_SHARE *share= entry->s;
540
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
542
if (wild && wild_compare(share->table_name.str, wild, 0))
545
/* Check if user has SELECT privilege for any column in the table */
546
table_list.db= share->db.str;
547
table_list.table_name= share->table_name.str;
549
/* need to check if we haven't already listed it */
550
for (table= open_list ; table ; table=table->next)
552
if (!strcmp(table->table, share->table_name.str) &&
553
!strcmp(table->db, share->db.str))
557
if (entry->locked_by_name)
564
if (!(*start_list = (OPEN_TableList *)
565
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
567
open_list=0; // Out of memory
570
strcpy((*start_list)->table=
571
strcpy(((*start_list)->db= (char*) ((*start_list)+1)),
572
share->db.str)+share->db.length+1,
573
share->table_name.str);
574
(*start_list)->in_use= entry->in_use ? 1 : 0;
575
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
576
start_list= &(*start_list)->next;
579
pthread_mutex_unlock(&LOCK_open);
170
583
/*****************************************************************************
171
584
* Functions to free open table cache
172
585
****************************************************************************/
175
void Table::intern_close_table()
588
void intern_close_table(Table *table)
176
589
{ // Free all structures
178
if (cursor) // Not true if name lock
179
closefrm(true); // close cursor
590
free_io_cache(table);
591
if (table->file) // Not true if name lock
592
table->closefrm(true); // close file
183
597
Remove table from the open table cache
187
entry Table to remove
601
entry Table to remove
190
We need to have a lock on LOCK_open when calling this
604
We need to have a lock on LOCK_open when calling this
193
void free_cache_entry(void *entry)
607
static void free_cache_entry(void *entry)
195
609
Table *table= static_cast<Table *>(entry);
196
table->intern_close_table();
197
if (not table->in_use)
610
intern_close_table(table);
199
613
table->next->prev=table->prev; /* remove from used chain */
200
614
table->prev->next=table->next;
373
804
table->s->version= refresh_version;
808
pthread_mutex_unlock(&LOCK_open);
809
if (wait_for_refresh)
811
pthread_mutex_lock(&session->mysys_var->mutex);
812
session->mysys_var->current_mutex= 0;
813
session->mysys_var->current_cond= 0;
814
session->set_proc_info(0);
815
pthread_mutex_unlock(&session->mysys_var->mutex);
822
Close all tables which match specified connection string or
823
if specified string is NULL, then any table with a connection string.
826
bool close_cached_connection_tables(Session *session, bool if_wait_for_refresh,
827
LEX_STRING *connection, bool have_lock)
830
TableList tmp, *tables= NULL;
834
memset(&tmp, 0, sizeof(TableList));
837
pthread_mutex_lock(&LOCK_open);
839
for (idx= 0; idx < table_def_cache.records; idx++)
841
TABLE_SHARE *share= (TABLE_SHARE *) hash_element(&table_def_cache, idx);
843
/* Ignore if table is not open or does not have a connect_string */
844
if (!share->connect_string.length || !share->ref_count)
847
/* Compare the connection string */
849
(connection->length > share->connect_string.length ||
850
(connection->length < share->connect_string.length &&
851
(share->connect_string.str[connection->length] != '/' &&
852
share->connect_string.str[connection->length] != '\\')) ||
853
strncasecmp(connection->str, share->connect_string.str,
854
connection->length)))
857
/* close_cached_tables() only uses these elements */
858
tmp.db= share->db.str;
859
tmp.table_name= share->table_name.str;
860
tmp.next_local= tables;
862
tables= (TableList *) memdup_root(session->mem_root, (char*)&tmp,
867
result= close_cached_tables(session, tables, true, false, false);
870
pthread_mutex_unlock(&LOCK_open);
872
if (if_wait_for_refresh)
874
pthread_mutex_lock(&session->mysys_var->mutex);
875
session->mysys_var->current_mutex= 0;
876
session->mysys_var->current_cond= 0;
877
session->set_proc_info(0);
878
pthread_mutex_unlock(&session->mysys_var->mutex);
886
Mark all temporary tables which were used by the current statement or
887
substatement as free for reuse, but only if the query_id can be cleared.
889
@param session thread context
891
@remark For temp tables associated with a open SQL HANDLER the query_id
892
is not reset until the HANDLER is closed.
895
static void mark_temp_tables_as_free_for_reuse(Session *session)
897
for (Table *table= session->temporary_tables ; table ; table= table->next)
899
if ((table->query_id == session->query_id) && ! table->open_by_handler)
902
table->file->ha_reset();
909
Mark all tables in the list which were used by current substatement
913
mark_used_tables_as_free_for_reuse()
914
session - thread context
915
table - head of the list of tables
918
Marks all tables in the list which were used by current substatement
919
(they are marked by its query_id) as free for reuse.
922
The reason we reset query_id is that it's not enough to just test
923
if table->query_id != session->query_id to know if a table is in use.
926
SELECT f1_that_uses_t1() FROM t1;
927
In f1_that_uses_t1() we will see one instance of t1 where query_id is
928
set to query_id of original query.
931
static void mark_used_tables_as_free_for_reuse(Session *session, Table *table)
933
for (; table ; table= table->next)
935
if (table->query_id == session->query_id)
938
table->file->ha_reset();
945
Auxiliary function to close all tables in the open_tables list.
947
@param session Thread context.
949
@remark It should not ordinarily be called directly.
952
static void close_open_tables(Session *session)
954
bool found_old_table= 0;
956
safe_mutex_assert_not_owner(&LOCK_open);
958
pthread_mutex_lock(&LOCK_open);
960
while (session->open_tables)
961
found_old_table|= close_thread_table(session, &session->open_tables);
962
session->some_tables_deleted= 0;
964
/* Free tables to hold down open files */
965
while (open_cache.records > table_cache_size && unused_tables)
966
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
969
/* Tell threads waiting for refresh that something has happened */
377
973
pthread_mutex_unlock(&LOCK_open);
379
if (wait_for_refresh)
381
pthread_mutex_lock(&session->mysys_var->mutex);
382
session->mysys_var->current_mutex= 0;
383
session->mysys_var->current_cond= 0;
384
session->set_proc_info(0);
385
pthread_mutex_unlock(&session->mysys_var->mutex);
393
move one table to free list
978
Close all tables used by the current substatement, or all tables
979
used by this thread if we are on the upper level.
982
close_thread_tables()
983
session Thread handler
986
Unlocks tables and frees derived tables.
987
Put all normal tables used by thread in free list.
989
It will only close/mark as free for reuse tables opened by this
990
substatement, it will also check if we are closing tables after
991
execution of complete query (i.e. we are on upper level) and will
992
leave prelocked mode if needed.
396
bool Session::free_cached_table()
398
bool found_old_table= false;
399
Table *table= open_tables;
401
safe_mutex_assert_owner(&LOCK_open);
995
void close_thread_tables(Session *session)
1000
We are assuming here that session->derived_tables contains ONLY derived
1001
tables for this substatement. i.e. instead of approach which uses
1002
query_id matching for determining which of the derived tables belong
1003
to this substatement we rely on the ability of substatements to
1004
save/restore session->derived_tables during their execution.
1006
TODO: Probably even better approach is to simply associate list of
1007
derived tables with (sub-)statement instead of thread and destroy
1008
them at the end of its execution.
1010
if (session->derived_tables)
1014
Close all derived tables generated in queries like
1015
SELECT * FROM (SELECT * FROM t1)
1017
for (table= session->derived_tables ; table ; table= next)
1020
table->free_tmp_table(session);
1022
session->derived_tables= 0;
1026
Mark all temporary tables used by this statement as free for reuse.
1028
mark_temp_tables_as_free_for_reuse(session);
1030
Let us commit transaction for statement. Since in 5.0 we only have
1031
one statement transaction and don't allow several nested statement
1032
transactions this call will do nothing if we are inside of stored
1033
function or trigger (i.e. statement transaction is already active and
1034
does not belong to statement for which we do close_thread_tables()).
1035
TODO: This should be fixed in later releases.
1037
if (!(session->state_flags & Open_tables_state::BACKUPS_AVAIL))
1039
session->main_da.can_overwrite_status= true;
1040
ha_autocommit_or_rollback(session, session->is_error());
1041
session->main_da.can_overwrite_status= false;
1042
session->transaction.stmt.reset();
1045
if (session->locked_tables)
1048
/* Ensure we are calling ha_reset() for all used tables */
1049
mark_used_tables_as_free_for_reuse(session, session->open_tables);
1052
We are under simple LOCK TABLES so should not do anything else.
1060
For RBR we flush the pending event just before we unlock all the
1061
tables. This means that we are at the end of a topmost
1062
statement, so we ensure that the STMT_END_F flag is set on the
1063
pending event. For statements that are *inside* stored
1064
functions, the pending event will not be flushed: that will be
1065
handled either before writing a query log event (inside
1066
binlog_query()) or when preparing a pending event.
1068
mysql_unlock_tables(session, session->lock);
1072
Note that we need to hold LOCK_open while changing the
1073
open_tables list. Another thread may work on it.
1074
(See: remove_table_from_cache(), mysql_wait_completed_table())
1075
Closing a MERGE child before the parent would be fatal if the
1076
other thread tries to abort the MERGE lock in between.
1078
if (session->open_tables)
1079
close_open_tables(session);
1085
/* move one table to free list */
1087
bool close_thread_table(Session *session, Table **table_ptr)
1089
bool found_old_table= 0;
1090
Table *table= *table_ptr;
402
1092
assert(table->key_read == 0);
403
assert(!table->cursor || table->cursor->inited == Cursor::NONE);
1093
assert(!table->file || table->file->inited == handler::NONE);
405
open_tables= table->next;
1095
*table_ptr=table->next;
407
1097
if (table->needs_reopen_or_name_lock() ||
408
version != refresh_version || !table->db_stat)
1098
session->version != refresh_version || !table->db_stat)
410
1100
hash_delete(&open_cache,(unsigned char*) table);
411
found_old_table= true;
504
1163
Test that table is unique (It's only exists once in the table list)
508
session thread handle
509
table table which should be checked
510
table_list list of tables
511
check_alias whether to check tables' aliases
513
NOTE: to exclude derived tables from check we use following mechanism:
514
a) during derived table processing set Session::derived_tables_processing
515
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
516
Session::derived_tables_processing set. (we can't use JOIN::execute
517
because for PS we perform only JOIN::prepare, but we can't set this
518
flag in JOIN::prepare if we are not sure that we are in derived table
519
processing loop, because multi-update call fix_fields() for some its
520
items (which mean JOIN::prepare for subqueries) before unique_table
521
call to detect which tables should be locked for write).
522
c) unique_table skip all tables which belong to SELECT with
523
SELECT::exclude_from_table_unique_test set.
524
Also SELECT::exclude_from_table_unique_test used to exclude from check
525
tables of main SELECT of multi-delete and multi-update
527
We also skip tables with TableList::prelocking_placeholder set,
528
because we want to allow SELECTs from them, and their modification
529
will rise the error anyway.
531
TODO: when we will have table/view change detection we can do this check
1167
session thread handle
1168
table table which should be checked
1169
table_list list of tables
1170
check_alias whether to check tables' aliases
1172
NOTE: to exclude derived tables from check we use following mechanism:
1173
a) during derived table processing set Session::derived_tables_processing
1174
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
1175
Session::derived_tables_processing set. (we can't use JOIN::execute
1176
because for PS we perform only JOIN::prepare, but we can't set this
1177
flag in JOIN::prepare if we are not sure that we are in derived table
1178
processing loop, because multi-update call fix_fields() for some its
1179
items (which mean JOIN::prepare for subqueries) before unique_table
1180
call to detect which tables should be locked for write).
1181
c) unique_table skip all tables which belong to SELECT with
1182
SELECT::exclude_from_table_unique_test set.
1183
Also SELECT::exclude_from_table_unique_test used to exclude from check
1184
tables of main SELECT of multi-delete and multi-update
1186
We also skip tables with TableList::prelocking_placeholder set,
1187
because we want to allow SELECTs from them, and their modification
1188
will rise the error anyway.
1190
TODO: when we will have table/view change detection we can do this check
1195
0 if table is unique
539
TableList* unique_table(TableList *table, TableList *table_list,
1198
TableList* unique_table(Session *session, TableList *table, TableList *table_list,
543
1202
const char *d_name, *t_name, *t_alias;
589
void Session::doGetTableNames(SchemaIdentifier &schema_identifier,
590
std::set<std::string>& set_of_names)
592
for (Table *table= temporary_tables ; table ; table= table->next)
594
if (schema_identifier.compare(table->s->getSchemaName()))
596
set_of_names.insert(table->s->table_name.str);
601
void Session::doGetTableNames(CachedDirectory &,
602
SchemaIdentifier &schema_identifier,
603
std::set<std::string> &set_of_names)
605
doGetTableNames(schema_identifier, set_of_names);
608
void Session::doGetTableIdentifiers(SchemaIdentifier &schema_identifier,
609
TableIdentifiers &set_of_identifiers)
611
for (Table *table= temporary_tables ; table ; table= table->next)
613
if (schema_identifier.compare(table->s->getSchemaName()))
615
set_of_identifiers.push_back(TableIdentifier(table->getShare()->getSchemaName(),
616
table->getShare()->getTableName(),
617
table->getShare()->getPath()));
622
void Session::doGetTableIdentifiers(CachedDirectory &,
623
SchemaIdentifier &schema_identifier,
624
TableIdentifiers &set_of_identifiers)
626
doGetTableIdentifiers(schema_identifier, set_of_identifiers);
629
bool Session::doDoesTableExist(TableIdentifier &identifier)
631
for (Table *table= temporary_tables ; table ; table= table->next)
633
if (table->s->tmp_table == message::Table::TEMPORARY)
635
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
645
int Session::doGetTableDefinition(TableIdentifier &identifier,
646
message::Table &table_proto)
648
for (Table *table= temporary_tables ; table ; table= table->next)
650
if (table->s->tmp_table == message::Table::TEMPORARY)
652
if (identifier.compare(table->s->getSchemaName(), table->s->table_name.str))
654
table_proto.CopyFrom(*(table->s->getTableProto()));
664
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
666
char key[MAX_DBKEY_LENGTH];
669
key_length= TableShare::createKey(key, new_db, table_name);
671
for (Table *table= temporary_tables ; table ; table= table->next)
673
if (table->s->table_cache_key.length == key_length &&
674
not memcmp(table->s->table_cache_key.str, key, key_length))
679
return NULL; // Not a temporary table
682
Table *Session::find_temporary_table(TableList *table_list)
684
return find_temporary_table(table_list->db, table_list->table_name);
687
Table *Session::find_temporary_table(TableIdentifier &identifier)
689
char key[MAX_DBKEY_LENGTH];
692
key_length= TableShare::createKey(key, identifier);
694
for (Table *table= temporary_tables ; table ; table= table->next)
696
if (table->s->table_cache_key.length == key_length &&
697
not memcmp(table->s->table_cache_key.str, key, key_length))
702
return NULL; // Not a temporary table
1252
Issue correct error message in case we found 2 duplicate tables which
1253
prevent some update operation
1256
update_non_unique_table_error()
1257
update table which we try to update
1258
operation name of update operation
1259
duplicate duplicate table which we found
1262
here we hide view underlying tables if we have them
1265
void update_non_unique_table_error(TableList *update,
1266
const char *operation __attribute__((unused)),
1267
TableList *duplicate __attribute__((unused)))
1269
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1273
Table *find_temporary_table(Session *session, const char *db, const char *table_name)
1275
TableList table_list;
1277
table_list.db= (char*) db;
1278
table_list.table_name= (char*) table_name;
1279
return find_temporary_table(session, &table_list);
1283
Table *find_temporary_table(Session *session, TableList *table_list)
1285
char key[MAX_DBKEY_LENGTH];
1289
key_length= create_table_def_key(session, key, table_list, 1);
1290
for (table=session->temporary_tables ; table ; table= table->next)
1292
if (table->s->table_cache_key.length == key_length &&
1293
!memcmp(table->s->table_cache_key.str, key, key_length))
1296
return(0); // Not a temporary table
726
1320
@retval 0 the table was found and dropped successfully.
727
1321
@retval 1 the table was not found in the list of temporary tables
729
1323
@retval -1 the table is in use by a outer query
732
int Session::drop_temporary_table(TableList *table_list)
1326
int drop_temporary_table(Session *session, TableList *table_list)
736
if (not (table= find_temporary_table(table_list)))
1330
if (!(table= find_temporary_table(session, table_list)))
739
1333
/* Table might be in use by some outer statement. */
740
if (table->query_id && table->query_id != query_id)
1334
if (table->query_id && table->query_id != session->query_id)
742
1336
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
746
close_temporary_table(table);
752
/* move table first in unused links */
1341
If LOCK TABLES list is not empty and contains this table,
1342
unlock the table and remove the table from this list.
1344
mysql_lock_remove(session, session->locked_tables, table, false);
1345
close_temporary_table(session, table, 1, 1);
1350
unlink from session->temporary tables and close temporary table
1353
void close_temporary_table(Session *session, Table *table,
1354
bool free_share, bool delete_table)
1358
table->prev->next= table->next;
1359
if (table->prev->next)
1360
table->next->prev= table->prev;
1364
/* removing the item from the list */
1365
assert(table == session->temporary_tables);
1367
slave must reset its temporary list pointer to zero to exclude
1368
passing non-zero value to end_slave via rli->save_temporary_tables
1369
when no temp tables opened, see an invariant below.
1371
session->temporary_tables= table->next;
1372
if (session->temporary_tables)
1373
table->next->prev= 0;
1375
if (session->slave_thread)
1377
/* natural invariant of temporary_tables */
1378
assert(slave_open_temp_tables || !session->temporary_tables);
1379
slave_open_temp_tables--;
1381
close_temporary(table, free_share, delete_table);
1387
Close and delete a temporary table
1390
This dosn't unlink table from session->temporary
1391
If this is needed, use close_temporary_table()
1394
void close_temporary(Table *table, bool free_share, bool delete_table)
1396
handlerton *table_type= table->s->db_type();
1398
free_io_cache(table);
1399
table->closefrm(false);
1402
rm_temporary_table(table_type, table->s->path.str);
1406
free_table_share(table->s);
1407
free((char*) table);
1414
Used by ALTER Table when the table is a temporary one. It changes something
1415
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1417
Prepares a table cache key, which is the concatenation of db, table_name and
1418
session->slave_proxy_id, separated by '\0'.
1421
bool rename_temporary_table(Session* session, Table *table, const char *db,
1422
const char *table_name)
1425
uint32_t key_length;
1426
TABLE_SHARE *share= table->s;
1427
TableList table_list;
1429
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1430
return true; /* purecov: inspected */
1432
table_list.db= (char*) db;
1433
table_list.table_name= (char*) table_name;
1434
key_length= create_table_def_key(session, key, &table_list, 1);
1435
share->set_table_cache_key(key, key_length);
1441
/* move table first in unused links */
754
1443
static void relink_unused(Table *table)
812
1511
// Notify any 'refresh' threads
813
1512
broadcast_refresh();
818
Auxiliary routine which closes and drops open table.
820
@param session Thread handle
821
@param table Table object for table to be dropped
822
@param db_name Name of database for this table
823
@param table_name Name of this table
825
@note This routine assumes that table to be closed is open only
826
by calling thread so we needn't wait until other threads
827
will close the table. Also unless called under implicit or
828
explicit LOCK TABLES mode it assumes that table to be
829
dropped is already unlocked. In the former case it will
830
also remove lock on the table. But one should not rely on
831
this behaviour as it may change in future.
832
Currently, however, this function is never called for a
833
table that was locked with LOCK TABLES.
1518
Auxiliary routine which closes and drops open table.
1520
@param session Thread handle
1521
@param table Table object for table to be dropped
1522
@param db_name Name of database for this table
1523
@param table_name Name of this table
1525
@note This routine assumes that table to be closed is open only
1526
by calling thread so we needn't wait until other threads
1527
will close the table. Also unless called under implicit or
1528
explicit LOCK TABLES mode it assumes that table to be
1529
dropped is already unlocked. In the former case it will
1530
also remove lock on the table. But one should not rely on
1531
this behaviour as it may change in future.
1532
Currently, however, this function is never called for a
1533
table that was locked with LOCK TABLES.
836
void Session::drop_open_table(Table *table, TableIdentifier &identifier)
1536
void drop_open_table(Session *session, Table *table, const char *db_name,
1537
const char *table_name)
838
1539
if (table->s->tmp_table)
840
close_temporary_table(table);
1540
close_temporary_table(session, table, 1, 1);
844
pthread_mutex_lock(&LOCK_open); /* Close and drop a table (AUX routine) */
1543
handlerton *table_type= table->s->db_type();
1544
pthread_mutex_lock(&LOCK_open);
846
1546
unlink_open_table() also tells threads waiting for refresh or close
847
1547
that something has happened.
849
unlink_open_table(table);
850
quick_rm_table(*this, identifier);
1549
unlink_open_table(session, table, false);
1550
quick_rm_table(table_type, db_name, table_name, 0);
851
1551
pthread_mutex_unlock(&LOCK_open);
857
Wait for condition but allow the user to send a kill to mysqld
1557
Wait for condition but allow the user to send a kill to mysqld
861
session Thread Cursor
862
mutex mutex that is currently hold that is associated with condition
863
Will be unlocked on return
864
cond Condition to wait for
1560
wait_for_condition()
1561
session Thread handler
1562
mutex mutex that is currently hold that is associated with condition
1563
Will be unlocked on return
1564
cond Condition to wait for
867
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
1567
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
869
1569
/* Wait until the current table is up to date */
870
const char *saved_proc_info;
871
mysys_var->current_mutex= mutex;
872
mysys_var->current_cond= cond;
873
saved_proc_info= get_proc_info();
874
set_proc_info("Waiting for table");
1570
const char *proc_info;
1571
session->mysys_var->current_mutex= mutex;
1572
session->mysys_var->current_cond= cond;
1573
proc_info=session->get_proc_info();
1574
session->set_proc_info("Waiting for table");
1575
if (!session->killed)
876
1576
(void) pthread_cond_wait(cond, mutex);
973
1707
table->next= orig_table.next;
976
table->tablenr= current_tablenr++;
977
table->used_fields= 0;
978
table->const_table= 0;
1710
table->tablenr=session->current_tablenr++;
1711
table->used_fields=0;
1712
table->const_table=0;
979
1713
table->null_row= false;
980
1714
table->maybe_null= false;
981
1715
table->force_index= false;
982
table->status= STATUS_NO_RECORD;
1716
table->status=STATUS_NO_RECORD;
989
Create and insert into table cache placeholder for table
990
which will prevent its opening (or creation) (a.k.a lock
993
@param session Thread context
994
@param key Table cache key for name to be locked
995
@param key_length Table cache key length
997
@return Pointer to Table object used for name locking or 0 in
1722
Create and insert into table cache placeholder for table
1723
which will prevent its opening (or creation) (a.k.a lock
1726
@param session Thread context
1727
@param key Table cache key for name to be locked
1728
@param key_length Table cache key length
1730
@return Pointer to Table object used for name locking or 0 in
1001
Table *Session::table_cache_insert_placeholder(const char *key, uint32_t key_length)
1734
Table *table_cache_insert_placeholder(Session *session, const char *key,
1735
uint32_t key_length)
1005
1739
char *key_buff;
1007
1741
safe_mutex_assert_owner(&LOCK_open);
1010
1744
Create a table entry with the right key and with an old refresh version
1011
Note that we must use multi_malloc() here as this is freed by the
1745
Note that we must use my_multi_malloc() here as this is freed by the
1014
if (! memory::multi_malloc(true,
1015
&table, sizeof(*table),
1016
&share, sizeof(*share),
1017
&key_buff, key_length,
1748
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
1749
&table, sizeof(*table),
1750
&share, sizeof(*share),
1751
&key_buff, key_length,
1021
1755
table->s= share;
1022
1756
share->set_table_cache_key(key_buff, key, key_length);
1023
share->tmp_table= message::Table::INTERNAL; // for intern_close_table
1024
table->in_use= this;
1757
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1758
table->in_use= session;
1025
1759
table->locked_by_name=1;
1027
1761
if (my_hash_insert(&open_cache, (unsigned char*)table))
1029
1763
free((unsigned char*) table);
1038
Obtain an exclusive name lock on the table if it is not cached
1041
@param session Thread context
1042
@param db Name of database
1043
@param table_name Name of table
1044
@param[out] table Out parameter which is either:
1045
- set to NULL if table cache contains record for
1047
- set to point to the Table instance used for
1050
@note This function takes into account all records for table in table
1051
cache, even placeholders used for name-locking. This means that
1052
'table' parameter can be set to NULL for some situations when
1053
table does not really exist.
1055
@retval true Error occured (OOM)
1056
@retval false Success. 'table' parameter set according to above rules.
1772
Obtain an exclusive name lock on the table if it is not cached
1775
@param session Thread context
1776
@param db Name of database
1777
@param table_name Name of table
1778
@param[out] table Out parameter which is either:
1779
- set to NULL if table cache contains record for
1781
- set to point to the Table instance used for
1784
@note This function takes into account all records for table in table
1785
cache, even placeholders used for name-locking. This means that
1786
'table' parameter can be set to NULL for some situations when
1787
table does not really exist.
1789
@retval true Error occured (OOM)
1790
@retval false Success. 'table' parameter set according to above rules.
1058
bool Session::lock_table_name_if_not_cached(TableIdentifier &identifier, Table **table)
1060
return lock_table_name_if_not_cached(identifier.getSchemaName().c_str(), identifier.getTableName().c_str(), table);
1063
bool Session::lock_table_name_if_not_cached(const char *new_db,
1064
const char *table_name, Table **table)
1793
bool lock_table_name_if_not_cached(Session *session, const char *db,
1794
const char *table_name, Table **table)
1066
1796
char key[MAX_DBKEY_LENGTH];
1067
1797
char *key_pos= key;
1068
1798
uint32_t key_length;
1070
key_pos= strcpy(key_pos, new_db) + strlen(new_db);
1800
key_pos= strcpy(key_pos, db) + strlen(db);
1071
1801
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1072
1802
key_length= (uint32_t) (key_pos-key)+1;
1074
pthread_mutex_lock(&LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1804
pthread_mutex_lock(&LOCK_open);
1076
1806
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1078
1808
pthread_mutex_unlock(&LOCK_open);
1082
if (not (*table= table_cache_insert_placeholder(key, key_length)))
1812
if (!(*table= table_cache_insert_placeholder(session, key, key_length)))
1084
1814
pthread_mutex_unlock(&LOCK_open);
1087
(*table)->open_placeholder= true;
1088
(*table)->next= open_tables;
1089
open_tables= *table;
1817
(*table)->open_placeholder= 1;
1818
(*table)->next= session->open_tables;
1819
session->open_tables= *table;
1090
1820
pthread_mutex_unlock(&LOCK_open);
1100
session Thread context.
1101
table_list Open first table in list.
1102
refresh INOUT Pointer to memory that will be set to 1 if
1103
we need to close all tables and reopen them.
1104
If this is a NULL pointer, then the table is not
1105
put in the thread-open-list.
1106
flags Bitmap of flags to modify how open works:
1107
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1108
someone has done a flush or namelock on it.
1109
No version number checking is done.
1110
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1111
table not the base table or view.
1829
session Thread context.
1830
table_list Open first table in list.
1831
refresh INOUT Pointer to memory that will be set to 1 if
1832
we need to close all tables and reopen them.
1833
If this is a NULL pointer, then the table is not
1834
put in the thread-open-list.
1835
flags Bitmap of flags to modify how open works:
1836
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1837
someone has done a flush or namelock on it.
1838
No version number checking is done.
1839
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1840
table not the base table or view.
1114
Uses a cache of open tables to find a table not in use.
1843
Uses a cache of open tables to find a table not in use.
1116
If table list element for the table to be opened has "create" flag
1117
set and table does not exist, this function will automatically insert
1118
a placeholder for exclusive name lock into the open tables cache and
1119
will return the Table instance that corresponds to this placeholder.
1845
If table list element for the table to be opened has "create" flag
1846
set and table does not exist, this function will automatically insert
1847
a placeholder for exclusive name lock into the open tables cache and
1848
will return the Table instance that corresponds to this placeholder.
1122
NULL Open failed. If refresh is set then one should close
1123
all other tables and retry the open.
1124
# Success. Pointer to Table object for open table.
1851
NULL Open failed. If refresh is set then one should close
1852
all other tables and retry the open.
1853
# Success. Pointer to Table object for open table.
1128
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
1857
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags)
1859
register Table *table;
1131
1860
char key[MAX_DBKEY_LENGTH];
1132
1861
unsigned int key_length;
1133
const char *alias= table_list->alias;
1862
char *alias= table_list->alias;
1134
1863
HASH_SEARCH_STATE state;
1136
1865
/* Parsing of partitioning information from .frm needs session->lex set up. */
1137
assert(lex->is_lex_started);
1866
assert(session->lex->is_lex_started);
1139
1868
/* find a unused table in the open table cache */
1143
1872
/* an open table operation needs a lot of the stack space */
1144
if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1150
key_length= table_list->create_table_def_key(key);
1873
if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1876
if (session->killed)
1879
key_length= (create_table_def_key(session, key, table_list, 1) -
1880
TMP_TABLE_KEY_EXTRA);
1153
1883
Unless requested otherwise, try to resolve this table in the list
1154
1884
of temporary tables of this thread. In MySQL temporary tables
1155
1885
are always thread-local and "shadow" possible base tables with the
1156
1886
same name. This block implements the behaviour.
1157
TODO -> move this block into a separate function.
1887
TODO: move this block into a separate function.
1159
for (table= temporary_tables; table ; table=table->next)
1161
if (table->s->table_cache_key.length == key_length && !memcmp(table->s->table_cache_key.str, key, key_length))
1890
for (table= session->temporary_tables; table ; table=table->next)
1164
We're trying to use the same temporary table twice in a query.
1165
Right now we don't support this because a temporary table
1166
is always represented by only one Table object in Session, and
1167
it can not be cloned. Emit an error for an unsupported behaviour.
1169
if (table->query_id)
1892
if (table->s->table_cache_key.length == key_length +
1893
TMP_TABLE_KEY_EXTRA &&
1894
!memcmp(table->s->table_cache_key.str, key,
1895
key_length + TMP_TABLE_KEY_EXTRA))
1171
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1898
We're trying to use the same temporary table twice in a query.
1899
Right now we don't support this because a temporary table
1900
is always represented by only one Table object in Session, and
1901
it can not be cloned. Emit an error for an unsupported behaviour.
1903
if (table->query_id)
1905
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1908
table->query_id= session->query_id;
1909
session->thread_specific_used= true;
1174
table->query_id= getQueryId();
1179
1915
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1181
1917
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
1186
If it's the first table from a list of tables used in a query,
1187
remember refresh_version (the version of open_cache state).
1188
If the version changes while we're opening the remaining tables,
1189
we will have to back off, close all the tables opened-so-far,
1190
and try to reopen them.
1192
Note-> refresh_version is currently changed only during FLUSH TABLES.
1195
version= refresh_version;
1196
else if ((version != refresh_version) &&
1197
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
1199
/* Someone did a refresh while thread was opening tables */
1207
Before we test the global cache, we test our local session cache.
1211
assert(false); /* Not implemented yet */
1922
The table is not temporary - if we're in pre-locked or LOCK TABLES
1923
mode, let's try to find the requested table in the list of pre-opened
1924
and locked tables. If the table is not there, return an error - we can't
1925
open not pre-opened tables in pre-locked/LOCK TABLES mode.
1926
TODO: move this block into a separate function.
1928
if (session->locked_tables)
1929
{ // Using table locks
1930
Table *best_table= 0;
1931
int best_distance= INT_MIN;
1932
bool check_if_used= false;
1933
for (table=session->open_tables; table ; table=table->next)
1935
if (table->s->table_cache_key.length == key_length &&
1936
!memcmp(table->s->table_cache_key.str, key, key_length))
1938
if (check_if_used && table->query_id &&
1939
table->query_id != session->query_id)
1942
If we are in stored function or trigger we should ensure that
1943
we won't change table that is already used by calling statement.
1944
So if we are opening table for writing, we should check that it
1945
is not already open by some calling stamement.
1947
my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
1948
table->s->table_name.str);
1952
When looking for a usable Table, ignore MERGE children, as they
1953
belong to their parent and cannot be used explicitly.
1955
if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
1956
table->query_id != session->query_id) /* skip tables already used */
1958
int distance= ((int) table->reginfo.lock_type -
1959
(int) table_list->lock_type);
1961
Find a table that either has the exact lock type requested,
1962
or has the best suitable lock. In case there is no locked
1963
table that has an equal or higher lock than requested,
1964
we us the closest matching lock to be able to produce an error
1965
message about wrong lock mode on the table. The best_table
1966
is changed if bd < 0 <= d or bd < d < 0 or 0 <= d < bd.
1968
distance < 0 - No suitable lock found
1969
distance > 0 - we have lock mode higher then we require
1970
distance == 0 - we have lock mode exactly which we need
1972
if ((best_distance < 0 && distance > best_distance) || (distance >= 0 && distance < best_distance))
1974
best_distance= distance;
1976
if (best_distance == 0 && !check_if_used)
1979
If we have found perfect match and we don't need to check that
1980
table is not used by one of calling statements (assuming that
1981
we are inside of function or trigger) we can finish iterating
1982
through open tables list.
1993
table->query_id= session->query_id;
1997
No table in the locked tables list. In case of explicit LOCK TABLES
1998
this can happen if a user did not include the able into the list.
1999
In case of pre-locked mode locked tables list is generated automatically,
2000
so we may only end up here if the table did not exist when
2001
locked tables list was created.
2003
my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
1343
2170
/* Free cache if too big */
1344
2171
while (open_cache.records > table_cache_size && unused_tables)
1345
hash_delete(&open_cache,(unsigned char*) unused_tables);
2172
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
1347
2174
if (table_list->create)
1349
TableIdentifier lock_table_identifier(table_list->db, table_list->table_name, message::Table::STANDARD);
1351
if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
2176
if(ha_table_exists_in_engine(session, table_list->db,
2177
table_list->table_name)
2178
== HA_ERR_TABLE_EXIST)
2180
pthread_mutex_unlock(&LOCK_open);
1354
2186
Table to be created, so we need to create placeholder in table-cache.
1356
if (!(table= table_cache_insert_placeholder(key, key_length)))
2188
if (!(table= table_cache_insert_placeholder(session, key, key_length)))
1358
2190
pthread_mutex_unlock(&LOCK_open);
1362
2194
Link placeholder to the open tables list so it will be automatically
1363
2195
removed once tables are closed. Also mark it so it won't be ignored
1364
2196
by other trying to take name-lock.
1366
table->open_placeholder= true;
1367
table->next= open_tables;
2198
table->open_placeholder= 1;
2199
table->next= session->open_tables;
2200
session->open_tables= table;
1369
2201
pthread_mutex_unlock(&LOCK_open);
1373
2204
/* Table exists. Let us try to open it. */
1376
2207
/* make a new table */
1377
table= (Table *)malloc(sizeof(Table));
2208
table= (Table *) malloc(sizeof(*table));
1378
2209
if (table == NULL)
1380
2211
pthread_mutex_unlock(&LOCK_open);
1384
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
2215
error= open_unireg_entry(session, table, table_list, alias, key, key_length);
1385
2216
if (error != 0)
1388
2219
pthread_mutex_unlock(&LOCK_open);
1391
my_hash_insert(&open_cache, (unsigned char*) table);
2222
my_hash_insert(&open_cache,(unsigned char*) table);
1394
2225
pthread_mutex_unlock(&LOCK_open);
1397
table->next= open_tables; /* Link into simple list */
2228
table->next=session->open_tables; /* Link into simple list */
2229
session->open_tables=table;
1400
table->reginfo.lock_type= TL_READ; /* Assume read */
1403
assert(table->s->ref_count > 0 || table->s->tmp_table != message::Table::STANDARD);
1405
if (lex->need_correct_ident())
2231
table->reginfo.lock_type=TL_READ; /* Assume read */
2234
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2236
if (session->lex->need_correct_ident())
1406
2237
table->alias_name_used= my_strcasecmp(table_alias_charset,
1407
2238
table->s->table_name.str, alias);
1408
2239
/* Fix alias if table name changes */
1409
2240
if (strcmp(table->alias, alias))
1411
uint32_t length=(uint32_t) strlen(alias)+1;
2242
uint32_t length=(uint) strlen(alias)+1;
1412
2243
table->alias= (char*) realloc((char*) table->alias, length);
1413
2244
memcpy((void*) table->alias, alias, length);
1416
2246
/* These variables are also set in reopen_table() */
1417
table->tablenr= current_tablenr++;
1418
table->used_fields= 0;
1419
table->const_table= 0;
2247
table->tablenr=session->current_tablenr++;
2248
table->used_fields=0;
2249
table->const_table=0;
1420
2250
table->null_row= false;
1421
2251
table->maybe_null= false;
1422
2252
table->force_index= false;
1567
2420
for target table name if we process ALTER Table ... RENAME.
1568
2421
So loop below makes sense even if we are not under LOCK TABLES.
1570
for (table= open_tables; table ; table=table->next)
2423
for (table=session->open_tables; table ; table=table->next)
1572
if (!strcmp(table->s->table_name.str, new_table_name) &&
1573
!strcasecmp(table->s->getSchemaName(), new_db))
2425
if (!strcmp(table->s->table_name.str, table_name) &&
2426
!strcmp(table->s->db.str, db))
1575
table->open_placeholder= true;
2428
if (session->locked_tables)
2430
mysql_lock_remove(session, session->locked_tables, table, true);
2432
table->open_placeholder= 1;
1576
2433
close_handle_and_leave_table_as_lock(table);
1583
Reopen all tables with closed data files.
1585
@param session Thread context
1586
@param get_locks Should we get locks after reopening tables ?
1587
@param mark_share_as_old Mark share as old to protect from a impending
1590
@note Since this function can't properly handle prelocking and
1591
create placeholders it should be used in very special
1592
situations like FLUSH TABLES or ALTER Table. In general
1593
case one should just repeat open_tables()/lock_tables()
1594
combination when one needs tables to be reopened (for
1595
example see openTablesLock()).
1597
@note One should have lock on LOCK_open when calling this.
1599
@return false in case of success, true - otherwise.
2441
Reopen all tables with closed data files.
2443
@param session Thread context
2444
@param get_locks Should we get locks after reopening tables ?
2445
@param mark_share_as_old Mark share as old to protect from a impending
2448
@note Since this function can't properly handle prelocking and
2449
create placeholders it should be used in very special
2450
situations like FLUSH TABLES or ALTER Table. In general
2451
case one should just repeat open_tables()/lock_tables()
2452
combination when one needs tables to be reopened (for
2453
example see open_and_lock_tables()).
2455
@note One should have lock on LOCK_open when calling this.
2457
@return false in case of success, true - otherwise.
1602
bool Session::reopen_tables(bool get_locks, bool mark_share_as_old)
2460
bool reopen_tables(Session *session, bool get_locks, bool mark_share_as_old)
1604
2462
Table *table,*next,**prev;
1605
2463
Table **tables,**tables_ptr; // For locks
1606
2464
bool error=0, not_used;
1607
2465
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
1608
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
1609
DRIZZLE_LOCK_IGNORE_FLUSH;
2466
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2467
DRIZZLE_LOCK_IGNORE_FLUSH;
1611
if (open_tables == NULL)
2469
if (!session->open_tables)
1614
2472
safe_mutex_assert_owner(&LOCK_open);
1932
2791
for (table= session->open_tables; table ; table= table->next)
1934
2793
if (!strcmp(table->s->table_name.str, table_name) &&
1935
!strcmp(table->s->getSchemaName(), db))
2794
!strcmp(table->s->db.str, db))
1937
2796
/* If MERGE child, forward lock handling to parent. */
1938
mysql_lock_abort(session, table);
2797
mysql_lock_abort(session, table, true);
1945
Load a table definition from cursor and open unireg table
2805
Function to assign a new table map id to a table share.
2809
share - Pointer to table share structure
2813
We are intentionally not checking that share->mutex is locked
2814
since this function should only be called when opening a table
2815
share and before it is entered into the table_def_cache (meaning
2816
that it cannot be fetched by another thread, even accidentally).
2821
The LOCK_open mutex is locked
2825
share->table_map_id is given a value that with a high certainty is
2826
not used by any other table (the only case where a table id can be
2827
reused is on wrap-around, which means more than 4 billion table
2828
share opens have been executed while one table was open all the
2831
share->table_map_id is not UINT32_MAX.
2833
void assign_new_table_id(TABLE_SHARE *share)
2835
static uint32_t last_table_id= UINT32_MAX;
2838
assert(share != NULL);
2839
safe_mutex_assert_owner(&LOCK_open);
2841
ulong tid= ++last_table_id; /* get next id */
2843
There is one reserved number that cannot be used. Remember to
2844
change this when 6-byte global table id's are introduced.
2846
if (unlikely(tid == UINT32_MAX))
2847
tid= ++last_table_id;
2848
share->table_map_id= tid;
2850
/* Post conditions */
2851
assert(share->table_map_id != UINT32_MAX);
2857
Load a table definition from file and open unireg table
1949
session Thread handle
1950
entry Store open table definition here
1951
table_list TableList with db, table_name
1953
cache_key Key for share_cache
1954
cache_key_length length of cache_key
2861
session Thread handle
2862
entry Store open table definition here
2863
table_list TableList with db, table_name
2865
cache_key Key for share_cache
2866
cache_key_length length of cache_key
1957
Extra argument for open is taken from session->open_options
1958
One must have a lock on LOCK_open when calling this function
2869
Extra argument for open is taken from session->open_options
2870
One must have a lock on LOCK_open when calling this function
1965
2877
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
2013
2926
if (share->ref_count != 1)
2015
2928
/* Free share and wait until it's released by all threads */
2016
TableShare::release(share);
2929
release_table_share(share, RELEASE_WAIT_FOR_DROP);
2018
2930
if (!session->killed)
2020
2932
drizzle_reset_errors(session, 1); // Clear warnings
2021
2933
session->clear_error(); // Clear error message
2938
if (!entry->s || !entry->s->crashed)
2940
// Code below is for repairing a crashed file
2941
if ((error= lock_table_name(session, table_list, true)))
2945
if (wait_for_locked_table_names(session, table_list))
2947
unlock_table_name(session, table_list);
2951
pthread_mutex_unlock(&LOCK_open);
2952
session->clear_error(); // Clear error message
2954
if (open_table_from_share(session, share, alias,
2955
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2959
ha_open_options | HA_OPEN_FOR_REPAIR,
2960
entry, OTM_OPEN) || ! entry->file ||
2961
(entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
2963
/* Give right error message */
2964
session->clear_error();
2965
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2966
errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2967
share->table_name.str);
2969
entry->closefrm(false);
2973
session->clear_error(); // Clear error message
2974
pthread_mutex_lock(&LOCK_open);
2975
unlock_table_name(session, table_list);
2983
If we are here, there was no fatal error (but error may be still
2986
if (unlikely(entry->file->implicit_emptied))
2988
entry->file->implicit_emptied= 0;
2991
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
2992
if ((query= (char*) malloc(query_buf_size)))
2995
"this DELETE FROM is needed even with row-based binlogging"
2997
We inherited this from MySQL. TODO: fix it to issue a propper truncate
2998
of the table (though that may not be completely right sematics).
3001
end+= sprintf(query, "DELETE FROM `%s`.`%s`", share->db.str,
3002
share->table_name.str);
3003
(void)replicator_statement(session, query, (size_t)(end - query));
3008
errmsg_printf(ERRMSG_LVL_ERROR, _("When opening HEAP table, could not allocate memory "
3009
"to write 'DELETE FROM `%s`.`%s`' to replication"),
3010
table_list->db, table_list->table_name);
3011
my_error(ER_OUTOFMEMORY, MYF(0), query_buf_size);
3012
entry->closefrm(false);
2033
TableShare::release(share);
3020
release_table_share(share, RELEASE_NORMAL);
2166
3160
tables->table= NULL;
2169
3162
return(result);
3167
Check that lock is ok for tables; Call start stmt if ok
3170
check_lock_and_start_stmt()
3171
session Thread handle
3172
table_list Table to check
3173
lock_type Lock used for table
3180
static bool check_lock_and_start_stmt(Session *session, Table *table,
3181
thr_lock_type lock_type)
3185
if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3186
(int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3188
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3191
if ((error=table->file->start_stmt(session, lock_type)))
3193
table->file->print_error(error,MYF(0));
3201
@brief Open and lock one table
3203
@param[in] session thread handle
3204
@param[in] table_l table to open is first table in this list
3205
@param[in] lock_type lock to use for table
3208
@retval != NULL OK, opened table returned
3212
If ok, the following are also set:
3213
table_list->lock_type lock_type
3214
table_list->table table
3217
If table_l is a list, not a single table, the list is temporarily
3221
This function is meant as a replacement for open_ltable() when
3222
MERGE tables can be opened. open_ltable() cannot open MERGE tables.
3224
There may be more differences between open_n_lock_single_table() and
3225
open_ltable(). One known difference is that open_ltable() does
3226
neither call decide_logging_format() nor handle some other logging
3227
and locking issues because it does not call lock_tables().
3230
Table *open_n_lock_single_table(Session *session, TableList *table_l,
3231
thr_lock_type lock_type)
3233
TableList *save_next_global;
3235
/* Remember old 'next' pointer. */
3236
save_next_global= table_l->next_global;
3238
table_l->next_global= NULL;
3240
/* Set requested lock type. */
3241
table_l->lock_type= lock_type;
3243
/* Open the table. */
3244
if (simple_open_n_lock_tables(session, table_l))
3245
table_l->table= NULL; /* Just to be sure. */
3248
table_l->next_global= save_next_global;
3250
return(table_l->table);
2174
3255
Open and lock one table
2178
session Thread Cursor
2179
table_list Table to open is first table in this list
2180
lock_type Lock to use for open
2181
lock_flags Flags passed to mysql_lock_table
3259
session Thread handler
3260
table_list Table to open is first table in this list
3261
lock_type Lock to use for open
3262
lock_flags Flags passed to mysql_lock_table
2184
This function don't do anything like SP/SF/views/triggers analysis done
2185
in open_tables(). It is intended for opening of only one concrete table.
2186
And used only in special contexts.
3265
This function don't do anything like SP/SF/views/triggers analysis done
3266
in open_tables(). It is intended for opening of only one concrete table.
3267
And used only in special contexts.
2192
If ok, the following are also set:
2193
table_list->lock_type lock_type
2194
table_list->table table
3273
If ok, the following are also set:
3274
table_list->lock_type lock_type
3275
table_list->table table
2197
Table *Session::openTableLock(TableList *table_list, thr_lock_type lock_type)
3278
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type lock_type,
3279
uint32_t lock_flags)
2202
set_proc_info("Opening table");
2204
while (!(table= openTable(table_list, &refresh)) &&
3284
session->set_proc_info("Opening table");
3285
session->current_tablenr= 0;
3286
while (!(table= open_table(session, table_list, &refresh, 0)) &&
2210
3292
table_list->lock_type= lock_type;
2211
3293
table_list->table= table;
2213
assert(lock == 0); // You must lock everything at once
2214
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
2215
if (! (lock= mysql_lock_tables(this, &table_list->table, 1, 0, &refresh)))
3294
if (session->locked_tables)
3296
if (check_lock_and_start_stmt(session, table, lock_type))
3301
assert(session->lock == 0); // You must lock everything at once
3302
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3303
if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
3304
lock_flags, &refresh)))
3309
session->set_proc_info(0);
3315
Open all tables in list, locks them and optionally process derived tables.
3318
open_and_lock_tables_derived()
3319
session - thread handler
3320
tables - list of tables for open&locking
3321
derived - if to handle derived tables
3328
The lock will automaticaly be freed by close_thread_tables()
3331
There are two convenience functions:
3332
- simple_open_n_lock_tables(session, tables) without derived handling
3333
- open_and_lock_tables(session, tables) with derived handling
3334
Both inline functions call open_and_lock_tables_derived() with
3335
the third argument set appropriately.
3338
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3345
if (open_tables(session, &tables, &counter, 0))
3348
if (!lock_tables(session, tables, counter, &need_reopen))
3352
close_tables_for_reopen(session, &tables);
3355
(mysql_handle_derived(session->lex, &mysql_derived_prepare) ||
3356
(session->fill_derived_tables() &&
3357
mysql_handle_derived(session->lex, &mysql_derived_filling))))
3358
return(true); /* purecov: inspected */
3364
Open all tables in list and process derived tables
3367
open_normal_and_derived_tables
3368
session - thread handler
3369
tables - list of tables for open
3370
flags - bitmap of flags to modify how the tables will be open:
3371
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3372
done a flush or namelock on it.
3379
This is to be used on prepare stage when you don't read any
3380
data from the tables.
3383
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
3386
assert(!session->fill_derived_tables());
3387
if (open_tables(session, &tables, &counter, flags) ||
3388
mysql_handle_derived(session->lex, &mysql_derived_prepare))
3389
return(true); /* purecov: inspected */
2225
3394
Lock all tables in list
2229
session Thread Cursor
2230
tables Tables to lock
2231
count Number of opened tables
2232
need_reopen Out parameter which if true indicates that some
2233
tables were dropped or altered during this call
2234
and therefore invoker should reopen tables and
2235
try to lock them once again (in this case
2236
lock_tables() will also return error).
3398
session Thread handler
3399
tables Tables to lock
3400
count Number of opened tables
3401
need_reopen Out parameter which if true indicates that some
3402
tables were dropped or altered during this call
3403
and therefore invoker should reopen tables and
3404
try to lock them once again (in this case
3405
lock_tables() will also return error).
2239
You can't call lock_tables twice, as this would break the dead-lock-free
2240
handling thr_lock gives us. You most always get all needed locks at
3408
You can't call lock_tables twice, as this would break the dead-lock-free
3409
handling thr_lock gives us. You most always get all needed locks at
2243
If query for which we are calling this function marked as requring
2244
prelocking, this function will do implicit LOCK TABLES and change
2245
session::prelocked_mode accordingly.
3412
If query for which we are calling this function marked as requring
3413
prelocking, this function will do implicit LOCK TABLES and change
3414
session::prelocked_mode accordingly.
2252
int Session::lock_tables(TableList *tables, uint32_t count, bool *need_reopen)
3421
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
2254
3423
TableList *table;
2255
Session *session= this;
2258
3426
We can't meet statement requiring prelocking if we already
3482
Prepare statement for reopening of tables and recalculation of set of
3486
close_tables_for_reopen()
3487
session in Thread context
3488
tables in/out List of tables which we were trying to open and lock
3492
void close_tables_for_reopen(Session *session, TableList **tables)
3495
If table list consists only from tables from prelocking set, table list
3496
for new attempt should be empty, so we have to update list's root pointer.
3498
if (session->lex->first_not_own_table() == *tables)
3500
session->lex->chop_off_not_own_tables();
3501
for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
3503
close_thread_tables(session);
2289
3508
Open a single table without table caching and don't set it in open_list
2292
open_temporary_table()
2293
session Thread object
2294
path Path (without .frm)
2296
table_name Table name
2297
link_in_list 1 if table should be linked into session->temporary_tables
2300
Used by alter_table to open a temporary table and when creating
2301
a temporary table with CREATE TEMPORARY ...
3511
open_temporary_table()
3512
session Thread object
3513
path Path (without .frm)
3515
table_name Table name
3516
link_in_list 1 if table should be linked into session->temporary_tables
3519
Used by alter_table to open a temporary table and when creating
3520
a temporary table with CREATE TEMPORARY ...
2308
Table *Session::open_temporary_table(TableIdentifier &identifier,
3527
Table *open_temporary_table(Session *session, const char *path, const char *db,
3528
const char *table_name, bool link_in_list,
3529
open_table_mode open_mode)
2311
Table *new_tmp_table;
2313
3533
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2314
3534
uint32_t key_length, path_length;
2315
3535
TableList table_list;
2317
table_list.db= const_cast<char*>(identifier.getSchemaName().c_str());
2318
table_list.table_name= const_cast<char*>(identifier.getTableName().c_str());
3537
table_list.db= (char*) db;
3538
table_list.table_name= (char*) table_name;
2319
3539
/* Create the cache_key for temporary tables */
2320
key_length= table_list.create_table_def_key(cache_key);
2321
path_length= identifier.getPath().length();
3540
key_length= create_table_def_key(session, cache_key, &table_list, 1);
3541
path_length= strlen(path);
2323
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2324
path_length + 1 + key_length)))
3543
if (!(tmp_table= (Table*) malloc(sizeof(*tmp_table) + sizeof(*share) +
3544
path_length + 1 + key_length)))
2327
share= (TableShare*) (new_tmp_table+1);
3547
share= (TABLE_SHARE*) (tmp_table+1);
2328
3548
tmp_path= (char*) (share+1);
2329
saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
3549
saved_cache_key= strcpy(tmp_path, path)+path_length+1;
2330
3550
memcpy(saved_cache_key, cache_key, key_length);
2332
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
3552
init_tmp_table_share(session, share, saved_cache_key, key_length,
3553
strchr(saved_cache_key, '\0')+1, tmp_path);
2335
First open the share, and then open the table from the share we just opened.
2337
if (open_table_def(*this, identifier, share) ||
2338
open_table_from_share(this, share, identifier.getTableName().c_str(),
2339
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3555
if (open_table_def(session, share, 0) ||
3556
open_table_from_share(session, share, table_name,
3557
(open_mode == OTM_ALTER) ? 0 :
3558
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3560
(open_mode == OTM_ALTER) ?
3561
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
2341
3563
ha_open_options,
3564
tmp_table, open_mode))
2344
3566
/* No need to lock share->mutex as this is not needed for tmp tables */
2345
share->free_table_share();
2346
free((char*) new_tmp_table);
3567
free_table_share(share);
3568
free((char*) tmp_table);
2350
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2351
share->tmp_table= message::Table::TEMPORARY;
3572
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
3573
if (open_mode == OTM_ALTER)
3576
Temporary table has been created with frm_only
3577
and has not been created in any storage engine
3579
share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
3582
share->tmp_table= (tmp_table->file->has_transactions() ?
3583
TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
2353
3585
if (link_in_list)
2355
3587
/* growing temp list at the head */
2356
new_tmp_table->next= this->temporary_tables;
2357
if (new_tmp_table->next)
2358
new_tmp_table->next->prev= new_tmp_table;
2359
this->temporary_tables= new_tmp_table;
2360
this->temporary_tables->prev= 0;
2362
new_tmp_table->pos_in_table_list= 0;
2364
return new_tmp_table;
3588
tmp_table->next= session->temporary_tables;
3589
if (tmp_table->next)
3590
tmp_table->next->prev= tmp_table;
3591
session->temporary_tables= tmp_table;
3592
session->temporary_tables->prev= 0;
3593
if (session->slave_thread)
3594
slave_open_temp_tables++;
3596
tmp_table->pos_in_table_list= 0;
3601
bool rm_temporary_table(handlerton *base, char *path)
3607
delete_table_proto_file(path);
3609
strcpy(ext= strchr(path, '\0'), reg_ext);
3610
if (my_delete(path,MYF(0)))
3611
error=1; /* purecov: inspected */
3612
*ext= 0; // remove extension
3613
file= get_new_handler((TABLE_SHARE*) 0, current_session->mem_root, base);
3614
if (file && file->ha_delete_table(path))
3617
errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
2368
3625
/*****************************************************************************
2369
* The following find_field_in_XXX procedures implement the core of the
2370
* name resolution functionality. The entry point to resolve a column name in a
2371
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
2372
* for each table reference. In turn, depending on the type of table reference,
2373
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
2374
* below specific for the type of table reference.
2375
******************************************************************************/
3626
* The following find_field_in_XXX procedures implement the core of the
3627
* name resolution functionality. The entry point to resolve a column name in a
3628
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3629
* for each table reference. In turn, depending on the type of table reference,
3630
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3631
* below specific for the type of table reference.
3632
******************************************************************************/
2377
3634
/* Special Field pointers as return values of find_field_in_XXX functions. */
2378
3635
Field *not_found_field= (Field*) 0x1;
2379
3636
Field *view_ref_found= (Field*) 0x2;
3638
#define WRONG_GRANT (Field*) -1
2381
3640
static void update_field_dependencies(Session *session, Field *field, Table *table)
2383
3642
if (session->mark_used_columns != MARK_COLUMNS_NONE)
2385
MyBitmap *current_bitmap, *other_bitmap;
3644
MY_BITMAP *current_bitmap, *other_bitmap;
2388
3647
We always want to register the used keys, as the column bitmap may have
2389
3648
been set for all fields (for example for view).
2392
table->covering_keys&= field->part_of_key;
2393
table->merge_keys|= field->part_of_key;
3651
table->covering_keys.intersect(field->part_of_key);
3652
table->merge_keys.merge(field->part_of_key);
2395
3654
if (session->mark_used_columns == MARK_COLUMNS_READ)
2418
3682
Find field by name in a NATURAL/USING join table reference.
2421
find_field_in_natural_join()
2422
session [in] thread Cursor
2423
table_ref [in] table reference to search
2424
name [in] name of field
2425
length [in] length of name
2426
ref [in/out] if 'name' is resolved to a view field, ref is
2427
set to point to the found view field
2428
register_tree_change [in] true if ref is not stack variable and we
2429
need register changes in item tree
2430
actual_table [out] the original table reference where the field
2431
belongs - differs from 'table_list' only for
3685
find_field_in_natural_join()
3686
session [in] thread handler
3687
table_ref [in] table reference to search
3688
name [in] name of field
3689
length [in] length of name
3690
ref [in/out] if 'name' is resolved to a view field, ref is
3691
set to point to the found view field
3692
register_tree_change [in] true if ref is not stack variable and we
3693
need register changes in item tree
3694
actual_table [out] the original table reference where the field
3695
belongs - differs from 'table_list' only for
2435
Search for a field among the result fields of a NATURAL/USING join.
2436
Notice that this procedure is called only for non-qualified field
2437
names. In the case of qualified fields, we search directly the base
2438
tables of a natural join.
3699
Search for a field among the result fields of a NATURAL/USING join.
3700
Notice that this procedure is called only for non-qualified field
3701
names. In the case of qualified fields, we search directly the base
3702
tables of a natural join.
2441
NULL if the field was not found
2442
PTR Pointer to the found Field
3705
NULL if the field was not found
3706
WRONG_GRANT if no access rights to the found field
3707
# Pointer to the found Field
2446
find_field_in_natural_join(Session *session, TableList *table_ref,
2447
const char *name, uint32_t , Item **,
2448
bool, TableList **actual_table)
3711
find_field_in_natural_join(Session *session, TableList *table_ref, const char *name,
3712
uint32_t length __attribute__((unused)),
3713
Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
3714
TableList **actual_table)
2450
3716
List_iterator_fast<Natural_join_column>
2451
3717
field_it(*(table_ref->join_columns));
2559
3842
Find field in a table reference.
2562
find_field_in_table_ref()
2563
session [in] thread Cursor
2564
table_list [in] table reference to search
2565
name [in] name of field
2566
length [in] field length of name
2567
item_name [in] name of item if it will be created (VIEW)
2568
db_name [in] optional database name that qualifies the
2569
table_name [in] optional table name that qualifies the field
2570
ref [in/out] if 'name' is resolved to a view field, ref
2571
is set to point to the found view field
2572
allow_rowid [in] do allow finding of "_rowid" field?
2573
cached_field_index_ptr [in] cached position in field list (used to
2574
speedup lookup for fields in prepared tables)
2575
register_tree_change [in] true if ref is not stack variable and we
2576
need register changes in item tree
2577
actual_table [out] the original table reference where the field
2578
belongs - differs from 'table_list' only for
2579
NATURAL_USING joins.
3845
find_field_in_table_ref()
3846
session [in] thread handler
3847
table_list [in] table reference to search
3848
name [in] name of field
3849
length [in] field length of name
3850
item_name [in] name of item if it will be created (VIEW)
3851
db_name [in] optional database name that qualifies the
3852
table_name [in] optional table name that qualifies the field
3853
ref [in/out] if 'name' is resolved to a view field, ref
3854
is set to point to the found view field
3855
check_privileges [in] check privileges
3856
allow_rowid [in] do allow finding of "_rowid" field?
3857
cached_field_index_ptr [in] cached position in field list (used to
3858
speedup lookup for fields in prepared tables)
3859
register_tree_change [in] true if ref is not stack variable and we
3860
need register changes in item tree
3861
actual_table [out] the original table reference where the field
3862
belongs - differs from 'table_list' only for
3863
NATURAL_USING joins.
2582
Find a field in a table reference depending on the type of table
2583
reference. There are three types of table references with respect
2584
to the representation of their result columns:
2585
- an array of Field_translator objects for MERGE views and some
2586
information_schema tables,
2587
- an array of Field objects (and possibly a name hash) for stored
2589
- a list of Natural_join_column objects for NATURAL/USING joins.
2590
This procedure detects the type of the table reference 'table_list'
2591
and calls the corresponding search routine.
3866
Find a field in a table reference depending on the type of table
3867
reference. There are three types of table references with respect
3868
to the representation of their result columns:
3869
- an array of Field_translator objects for MERGE views and some
3870
information_schema tables,
3871
- an array of Field objects (and possibly a name hash) for stored
3873
- a list of Natural_join_column objects for NATURAL/USING joins.
3874
This procedure detects the type of the table reference 'table_list'
3875
and calls the corresponding search routine.
2594
0 field is not found
2595
view_ref_found found value in VIEW (real result is in *ref)
3878
0 field is not found
3879
view_ref_found found value in VIEW (real result is in *ref)
2691
if (session->mark_used_columns != MARK_COLUMNS_NONE)
2694
Get rw_set correct for this field so that the Cursor
2695
knows that this field is involved in the query and gets
2698
Field *field_to_set= NULL;
2699
if (fld == view_ref_found)
3975
if (session->mark_used_columns != MARK_COLUMNS_NONE)
2701
Item *it= (*ref)->real_item();
2702
if (it->type() == Item::FIELD_ITEM)
2703
field_to_set= ((Item_field*)it)->field;
3978
Get rw_set correct for this field so that the handler
3979
knows that this field is involved in the query and gets
3982
Field *field_to_set= NULL;
3983
if (fld == view_ref_found)
3985
Item *it= (*ref)->real_item();
3986
if (it->type() == Item::FIELD_ITEM)
3987
field_to_set= ((Item_field*)it)->field;
3990
if (session->mark_used_columns == MARK_COLUMNS_READ)
3991
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3998
Table *table= field_to_set->table;
2706
3999
if (session->mark_used_columns == MARK_COLUMNS_READ)
2707
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4000
bitmap_set_bit(table->read_set, field_to_set->field_index);
4002
bitmap_set_bit(table->write_set, field_to_set->field_index);
2714
Table *table= field_to_set->table;
2715
if (session->mark_used_columns == MARK_COLUMNS_READ)
2716
table->setReadSet(field_to_set->field_index);
2718
table->setWriteSet(field_to_set->field_index);
4011
Find field in table, no side effects, only purpose is to check for field
4012
in table object and get reference to the field if found.
4015
find_field_in_table_sef()
4017
table table where to find
4018
name Name of field searched for
4021
0 field is not found
4025
Field *find_field_in_table_sef(Table *table, const char *name)
4028
if (table->s->name_hash.records)
4030
field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
4035
field_ptr points to field in TABLE_SHARE. Convert it to the matching
4038
field_ptr= (table->field + (field_ptr - table->s->field));
4043
if (!(field_ptr= table->field))
4045
for (; *field_ptr; ++field_ptr)
4046
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
2727
4057
Find field in table list.
2730
find_field_in_tables()
2731
session pointer to current thread structure
2732
item field item that should be found
2733
first_table list of tables to be searched for item
2734
last_table end of the list of tables to search for item. If NULL
2735
then search to the end of the list 'first_table'.
2736
ref if 'item' is resolved to a view field, ref is set to
2737
point to the found view field
2738
report_error Degree of error reporting:
2739
- IGNORE_ERRORS then do not report any error
2740
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
2741
fields, suppress all other errors
2742
- REPORT_EXCEPT_NON_UNIQUE report all other errors
2743
except when non-unique fields were found
2745
register_tree_change true if ref is not a stack variable and we
2746
to need register changes in item tree
4060
find_field_in_tables()
4061
session pointer to current thread structure
4062
item field item that should be found
4063
first_table list of tables to be searched for item
4064
last_table end of the list of tables to search for item. If NULL
4065
then search to the end of the list 'first_table'.
4066
ref if 'item' is resolved to a view field, ref is set to
4067
point to the found view field
4068
report_error Degree of error reporting:
4069
- IGNORE_ERRORS then do not report any error
4070
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
4071
fields, suppress all other errors
4072
- REPORT_EXCEPT_NON_UNIQUE report all other errors
4073
except when non-unique fields were found
4075
check_privileges need to check privileges
4076
register_tree_change true if ref is not a stack variable and we
4077
to need register changes in item tree
2749
0 If error: the found field is not unique, or there are
2750
no sufficient access priviliges for the found field,
2751
or the field is qualified with non-existing table.
2752
not_found_field The function was called with report_error ==
2753
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
2754
field was not found.
2755
view_ref_found View field is found, item passed through ref parameter
2756
found field If a item was resolved to some field
4080
0 If error: the found field is not unique, or there are
4081
no sufficient access priviliges for the found field,
4082
or the field is qualified with non-existing table.
4083
not_found_field The function was called with report_error ==
4084
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
4085
field was not found.
4086
view_ref_found View field is found, item passed through ref parameter
4087
found field If a item was resolved to some field
2760
4091
find_field_in_tables(Session *session, Item_ident *item,
2761
4092
TableList *first_table, TableList *last_table,
2762
Item **ref, find_item_error_report_type report_error,
2763
bool register_tree_change)
4093
Item **ref, find_item_error_report_type report_error,
4094
bool check_privileges, bool register_tree_change)
2765
4096
Field *found=0;
2766
4097
const char *db= item->db_name;
2767
4098
const char *table_name= item->table_name;
2768
4099
const char *name= item->field_name;
2769
uint32_t length=(uint32_t) strlen(name);
4100
uint32_t length=(uint) strlen(name);
2770
4101
char name_buff[NAME_LEN+1];
2771
4102
TableList *cur_table= first_table;
2772
4103
TableList *actual_table;
3425
4786
Materialize and store the row type of NATURAL/USING join.
3428
store_natural_using_join_columns()
3429
session current thread
3430
natural_using_join the table reference of the NATURAL/USING join
3431
table_ref_1 the first (left) operand (of a NATURAL/USING join).
3432
table_ref_2 the second (right) operand (of a NATURAL/USING join).
3433
using_fields if the join is JOIN...USING - the join columns,
3434
if NATURAL join, then NULL
3435
found_using_fields number of fields from the USING clause that were
3436
found among the common fields
4789
store_natural_using_join_columns()
4790
session current thread
4791
natural_using_join the table reference of the NATURAL/USING join
4792
table_ref_1 the first (left) operand (of a NATURAL/USING join).
4793
table_ref_2 the second (right) operand (of a NATURAL/USING join).
4794
using_fields if the join is JOIN...USING - the join columns,
4795
if NATURAL join, then NULL
4796
found_using_fields number of fields from the USING clause that were
4797
found among the common fields
3439
Iterate over the columns of both join operands and sort and store
3440
all columns into the 'join_columns' list of natural_using_join
3441
where the list is formed by three parts:
3442
part1: The coalesced columns of table_ref_1 and table_ref_2,
3443
sorted according to the column order of the first table.
3444
part2: The other columns of the first table, in the order in
3445
which they were defined in CREATE TABLE.
3446
part3: The other columns of the second table, in the order in
3447
which they were defined in CREATE TABLE.
3448
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
3451
The procedure assumes that mark_common_columns() has been called
3452
for the join that is being processed.
3455
true error: Some common column is ambiguous
4800
Iterate over the columns of both join operands and sort and store
4801
all columns into the 'join_columns' list of natural_using_join
4802
where the list is formed by three parts:
4803
part1: The coalesced columns of table_ref_1 and table_ref_2,
4804
sorted according to the column order of the first table.
4805
part2: The other columns of the first table, in the order in
4806
which they were defined in CREATE TABLE.
4807
part3: The other columns of the second table, in the order in
4808
which they were defined in CREATE TABLE.
4809
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
4812
The procedure assumes that mark_common_columns() has been called
4813
for the join that is being processed.
4816
true error: Some common column is ambiguous
3460
store_natural_using_join_columns(Session *,
4821
store_natural_using_join_columns(Session *session __attribute__((unused)),
3461
4822
TableList *natural_using_join,
3462
4823
TableList *table_ref_1,
3463
4824
TableList *table_ref_2,
4407
5878
table= field->table;
4408
5879
if (field == table->next_number_field)
4409
5880
table->auto_increment_field_not_null= true;
5881
if (field->vcol_info &&
5882
value->type() != Item::DEFAULT_VALUE_ITEM &&
5883
value->type() != Item::NULL_ITEM &&
5884
table->s->table_category != TABLE_CATEGORY_TEMPORARY)
5886
session->abort_on_warning= false;
5887
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
5888
ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
5889
ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
5890
field->field_name, table->s->table_name.str);
5891
session->abort_on_warning= abort_on_warning_saved;
4410
5893
if (value->save_in_field(field, 0) < 0)
5895
tbl_list.push_back(table);
5897
/* Update virtual fields*/
5898
session->abort_on_warning= false;
5899
if (tbl_list.head())
5901
List_iterator_fast<Table> t(tbl_list);
5902
Table *prev_table= 0;
5903
while ((table= t++))
5906
Do simple optimization to prevent unnecessary re-generating
5907
values for virtual fields
5909
if (table != prev_table)
5914
if (update_virtual_fields_marked_for_write(table, false))
5922
session->abort_on_warning= abort_on_warning_saved;
4414
5923
return(session->is_error());
5926
session->abort_on_warning= abort_on_warning_saved;
4418
5928
table->auto_increment_field_not_null= false;
4424
bool drizzle_rm_tmp_tables()
5933
bool drizzle_rm_tmp_tables(void)
5936
char filePath[FN_REFLEN], filePathCopy[FN_REFLEN];
4426
5940
Session *session;
4428
5942
assert(drizzle_tmpdir);
4430
if (!(session= new Session(plugin::Listen::getNullClient())))
5944
if (!(session= new Session))
4432
5946
session->thread_stack= (char*) &session;
4433
session->storeGlobals();
4435
plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir);
5947
session->store_globals();
5949
/* Remove all temp tables in the tmpdir */
5950
/* See if the directory exists */
5951
if ((dirp = my_dir(drizzle_tmpdir ,MYF(MY_WME | MY_DONT_SORT))))
5953
/* Remove all SQLxxx tables from directory */
5954
for (idx=0 ; idx < (uint) dirp->number_off_files ; idx++)
5956
file=dirp->dir_entry+idx;
5958
/* skiping . and .. */
5959
if (file->name[0] == '.' && (!file->name[1] ||
5960
(file->name[1] == '.' && !file->name[2])))
5963
if (!memcmp(file->name, TMP_FILE_PREFIX, TMP_FILE_PREFIX_LENGTH))
5965
char *ext= fn_ext(file->name);
5966
uint32_t ext_len= strlen(ext);
5967
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
5968
"%s%c%s", drizzle_tmpdir, FN_LIBCHAR,
5970
if (!memcmp(reg_ext, ext, ext_len))
5972
handler *handler_file= 0;
5973
/* We should cut file extention before deleting of table */
5974
memcpy(filePathCopy, filePath, filePath_len - ext_len);
5975
filePathCopy[filePath_len - ext_len]= 0;
5976
init_tmp_table_share(session, &share, "", 0, "", filePathCopy);
5977
if (!open_table_def(session, &share, 0) &&
5978
((handler_file= get_new_handler(&share, session->mem_root,
5981
handler_file->ha_delete_table(filePathCopy);
5982
delete handler_file;
5984
free_table_share(&share);
5987
File can be already deleted by tmp_table.file->delete_table().
5988
So we hide error messages which happnes during deleting of these
5991
my_delete(filePath, MYF(0));
4437
5997
delete session;