116
104
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.
108
Create a table cache key
111
create_table_def_key()
112
session Thread handler
113
key Create key here (must be of size MAX_DBKEY_LENGTH)
114
table_list Table definition
115
tmp_table Set if table is a tmp table
118
The table cache_key is created from:
122
if the table is a tmp table, we add the following to make each tmp table
125
4 bytes for master thread id
126
4 bytes pseudo thread id
132
uint32_t create_table_def_key(Session *session, char *key, TableList *table_list,
137
key_pos= strcpy(key_pos, table_list->db) + strlen(table_list->db);
138
key_pos= strcpy(key_pos+1, table_list->table_name) +
139
strlen(table_list->table_name);
140
key_length= (uint32_t)(key_pos-key)+1;
144
int4store(key + key_length, session->getServerId());
145
int4store(key + key_length + 4, session->variables.pseudo_thread_id);
146
key_length+= TMP_TABLE_KEY_EXTRA;
153
/*****************************************************************************
154
Functions to handle table definition cach (TABLE_SHARE)
155
*****************************************************************************/
157
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
160
TABLE_SHARE *entry=(TABLE_SHARE*) record;
161
*length= entry->table_cache_key.length;
162
return (unsigned char*) entry->table_cache_key.str;
166
static void table_def_free_entry(TABLE_SHARE *share)
170
/* remove from old_unused_share list */
171
pthread_mutex_lock(&LOCK_table_share);
172
*share->prev= share->next;
173
share->next->prev= share->prev;
174
pthread_mutex_unlock(&LOCK_table_share);
176
free_table_share(share);
181
bool table_def_init(void)
184
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
185
oldest_unused_share= &end_of_unused_share;
186
end_of_unused_share.prev= &oldest_unused_share;
188
return hash_init(&table_def_cache, &my_charset_bin, (size_t)table_def_size,
190
(hash_free_key) table_def_free_entry, 0);
194
void table_def_free(void)
196
if (table_def_inited)
199
pthread_mutex_destroy(&LOCK_table_share);
200
hash_free(&table_def_cache);
206
uint32_t cached_table_definitions(void)
208
return table_def_cache.records;
213
Get TABLE_SHARE for a table.
216
session Thread handle
217
table_list Table that should be opened
219
key_length Length of key
220
db_flags Flags to open_table_def():
222
error out: Error code from open_table_def()
225
Get a table definition from the table definition cache.
226
If it doesn't exist, create a new from the table definition file.
229
We must have wrlock on LOCK_open when we come here
230
(To be changed later)
237
TABLE_SHARE *get_table_share(Session *session, TableList *table_list, char *key,
238
uint32_t key_length, uint32_t db_flags, int *error)
244
/* Read table definition from cache */
245
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
249
if (!(share= alloc_table_share(table_list, key, key_length)))
255
Lock mutex to be able to read table definition from file without
258
(void) pthread_mutex_lock(&share->mutex);
261
We assign a new table id under the protection of the LOCK_open and
262
the share's own mutex. We do this insted of creating a new mutex
263
and using it for the sole purpose of serializing accesses to a
264
static variable, we assign the table id here. We assign it to the
265
share before inserting it into the table_def_cache to be really
266
sure that it cannot be read from the cache without having a table
269
CAVEAT. This means that the table cannot be used for
270
binlogging/replication purposes, unless get_table_share() has been
271
called directly or indirectly.
273
assign_new_table_id(share);
275
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
277
free_table_share(share);
278
return(0); // return error
280
if (open_table_def(session, share, db_flags))
282
*error= share->error;
283
(void) hash_delete(&table_def_cache, (unsigned char*) share);
286
share->ref_count++; // Mark in use
287
(void) pthread_mutex_unlock(&share->mutex);
292
We found an existing table definition. Return it if we didn't get
293
an error when reading the table definition from file.
296
/* We must do a lock to ensure that the structure is initialized */
297
(void) pthread_mutex_lock(&share->mutex);
300
/* Table definition contained an error */
301
open_table_error(share, share->error, share->open_errno, share->errarg);
302
(void) pthread_mutex_unlock(&share->mutex);
306
if (!share->ref_count++ && share->prev)
309
Share was not used before and it was in the old_unused_share list
310
Unlink share from this list
312
pthread_mutex_lock(&LOCK_table_share);
313
*share->prev= share->next;
314
share->next->prev= share->prev;
317
pthread_mutex_unlock(&LOCK_table_share);
319
(void) pthread_mutex_unlock(&share->mutex);
321
/* Free cache if too big */
322
while (table_def_cache.records > table_def_size &&
323
oldest_unused_share->next)
325
pthread_mutex_lock(&oldest_unused_share->mutex);
326
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
334
Get a table share. If it didn't exist, try creating it from engine
336
For arguments and return values, see get_table_from_share()
340
*get_table_share_with_create(Session *session, TableList *table_list,
341
char *key, uint32_t key_length,
342
uint32_t db_flags, int *error)
346
share= get_table_share(session, table_list, key, key_length, db_flags, error);
348
If share is not NULL, we found an existing share.
350
If share is NULL, and there is no error, we're inside
351
pre-locking, which silences 'ER_NO_SUCH_TABLE' errors
352
with the intention to silently drop non-existing tables
353
from the pre-locking list. In this case we still need to try
354
auto-discover before returning a NULL share.
356
If share is NULL and the error is ER_NO_SUCH_TABLE, this is
357
the same as above, only that the error was not silenced by
358
pre-locking. Once again, we need to try to auto-discover
361
Finally, if share is still NULL, it's a real error and we need
364
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
366
if (share || (session->is_error() && (session->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
375
Mark that we are not using table share anymore.
378
release_table_share()
380
release_type How the release should be done:
382
- Release without checking
383
RELEASE_WAIT_FOR_DROP
384
- Don't return until we get a signal that the
385
table is deleted or the thread is killed.
388
If ref_count goes to zero and (we have done a refresh or if we have
389
already too many open table shares) then delete the definition.
391
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
392
that the table is deleted or the thread is killed.
395
void release_table_share(TABLE_SHARE *share,
398
bool to_be_deleted= 0;
400
safe_mutex_assert_owner(&LOCK_open);
402
pthread_mutex_lock(&share->mutex);
403
if (!--share->ref_count)
405
if (share->version != refresh_version)
409
/* Link share last in used_table_share list */
410
assert(share->next == 0);
411
pthread_mutex_lock(&LOCK_table_share);
412
share->prev= end_of_unused_share.prev;
413
*end_of_unused_share.prev= share;
414
end_of_unused_share.prev= &share->next;
415
share->next= &end_of_unused_share;
416
pthread_mutex_unlock(&LOCK_table_share);
418
to_be_deleted= (table_def_cache.records > table_def_size);
424
hash_delete(&table_def_cache, (unsigned char*) share);
427
pthread_mutex_unlock(&share->mutex);
433
Check if table definition exits in cache
436
get_cached_table_share()
438
table_name Table name
442
# TABLE_SHARE for table
445
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
447
char key[NAME_LEN*2+2];
448
TableList table_list;
450
safe_mutex_assert_owner(&LOCK_open);
452
table_list.db= (char*) db;
453
table_list.table_name= (char*) table_name;
454
key_length= create_table_def_key((Session*) 0, key, &table_list, 0);
455
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
460
Close file handle, but leave the table in the table cache
463
close_handle_and_leave_table_as_lock()
467
By leaving the table in the table cache, it disallows any other thread
470
session->killed will be set if we run out of memory
472
If closing a MERGE child, the calling function has to take care for
473
closing the parent too, if necessary.
138
477
void close_handle_and_leave_table_as_lock(Table *table)
140
TableShare *share, *old_share= table->s;
479
TABLE_SHARE *share, *old_share= table->s;
142
memory::Root *mem_root= &table->mem_root;
481
MEM_ROOT *mem_root= &table->mem_root;
144
483
assert(table->db_stat);
156
495
memset(share, 0, sizeof(*share));
157
496
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
158
497
old_share->table_cache_key.length);
159
share->tmp_table= message::Table::INTERNAL; // for intern_close_table()
498
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);
501
table->file->close();
502
table->db_stat= 0; // Mark file closed
503
release_table_share(table->s, RELEASE_NORMAL);
166
table->cursor->change_table_ptr(table, table->s);
505
table->file->change_table_ptr(table, table->s);
513
Create a list for all open tables matching SQL expression
517
wild SQL like expression
520
One gets only a list of tables for which one has any kind of privilege.
521
db and table names are allocated in result struct, so one doesn't need
522
a lock on LOCK_open when traversing the return list.
525
NULL Error (Probably OOM)
526
# Pointer to list of names of open tables.
529
OPEN_TableList *list_open_tables(const char *db, const char *wild)
532
OPEN_TableList **start_list, *open_list;
533
TableList table_list;
535
pthread_mutex_lock(&LOCK_open);
536
memset(&table_list, 0, sizeof(table_list));
537
start_list= &open_list;
540
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
542
OPEN_TableList *table;
543
Table *entry=(Table*) hash_element(&open_cache,idx);
544
TABLE_SHARE *share= entry->s;
546
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
548
if (wild && wild_compare(share->table_name.str, wild, 0))
551
/* Check if user has SELECT privilege for any column in the table */
552
table_list.db= share->db.str;
553
table_list.table_name= share->table_name.str;
555
/* need to check if we haven't already listed it */
556
for (table= open_list ; table ; table=table->next)
558
if (!strcmp(table->table, share->table_name.str) &&
559
!strcmp(table->db, share->db.str))
563
if (entry->locked_by_name)
570
if (!(*start_list = (OPEN_TableList *)
571
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
573
open_list=0; // Out of memory
576
strcpy((*start_list)->table=
577
strcpy(((*start_list)->db= (char*) ((*start_list)+1)),
578
share->db.str)+share->db.length+1,
579
share->table_name.str);
580
(*start_list)->in_use= entry->in_use ? 1 : 0;
581
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
582
start_list= &(*start_list)->next;
585
pthread_mutex_unlock(&LOCK_open);
170
589
/*****************************************************************************
171
590
* Functions to free open table cache
172
591
****************************************************************************/
175
void Table::intern_close_table()
594
void intern_close_table(Table *table)
176
595
{ // Free all structures
178
if (cursor) // Not true if name lock
179
closefrm(true); // close cursor
596
free_io_cache(table);
597
if (table->file) // Not true if name lock
598
table->closefrm(true); // close file
183
603
Remove table from the open table cache
187
entry Table to remove
607
entry Table to remove
190
We need to have a lock on LOCK_open when calling this
610
We need to have a lock on LOCK_open when calling this
193
613
void free_cache_entry(void *entry)
195
615
Table *table= static_cast<Table *>(entry);
196
table->intern_close_table();
197
if (not table->in_use)
616
intern_close_table(table);
199
619
table->next->prev=table->prev; /* remove from used chain */
200
620
table->prev->next=table->next;
373
809
table->s->version= refresh_version;
813
pthread_mutex_unlock(&LOCK_open);
814
if (wait_for_refresh)
816
pthread_mutex_lock(&session->mysys_var->mutex);
817
session->mysys_var->current_mutex= 0;
818
session->mysys_var->current_cond= 0;
819
session->set_proc_info(0);
820
pthread_mutex_unlock(&session->mysys_var->mutex);
827
Close all tables which match specified connection string or
828
if specified string is NULL, then any table with a connection string.
831
bool close_cached_connection_tables(Session *session, bool if_wait_for_refresh,
832
LEX_STRING *connection, bool have_lock)
835
TableList tmp, *tables= NULL;
839
memset(&tmp, 0, sizeof(TableList));
842
pthread_mutex_lock(&LOCK_open);
844
for (idx= 0; idx < table_def_cache.records; idx++)
846
TABLE_SHARE *share= (TABLE_SHARE *) hash_element(&table_def_cache, idx);
848
/* Ignore if table is not open or does not have a connect_string */
849
if (!share->connect_string.length || !share->ref_count)
852
/* Compare the connection string */
854
(connection->length > share->connect_string.length ||
855
(connection->length < share->connect_string.length &&
856
(share->connect_string.str[connection->length] != '/' &&
857
share->connect_string.str[connection->length] != '\\')) ||
858
strncasecmp(connection->str, share->connect_string.str,
859
connection->length)))
862
/* close_cached_tables() only uses these elements */
863
tmp.db= share->db.str;
864
tmp.table_name= share->table_name.str;
865
tmp.next_local= tables;
867
tables= (TableList *) memdup_root(session->mem_root, (char*)&tmp,
872
result= close_cached_tables(session, tables, true, false, false);
875
pthread_mutex_unlock(&LOCK_open);
877
if (if_wait_for_refresh)
879
pthread_mutex_lock(&session->mysys_var->mutex);
880
session->mysys_var->current_mutex= 0;
881
session->mysys_var->current_cond= 0;
882
session->set_proc_info(0);
883
pthread_mutex_unlock(&session->mysys_var->mutex);
891
Mark all temporary tables which were used by the current statement or
892
substatement as free for reuse, but only if the query_id can be cleared.
894
@param session thread context
896
@remark For temp tables associated with a open SQL HANDLER the query_id
897
is not reset until the HANDLER is closed.
900
static void mark_temp_tables_as_free_for_reuse(Session *session)
902
for (Table *table= session->temporary_tables ; table ; table= table->next)
904
if ((table->query_id == session->query_id) && ! table->open_by_handler)
907
table->file->ha_reset();
914
Mark all tables in the list which were used by current substatement
918
mark_used_tables_as_free_for_reuse()
919
session - thread context
920
table - head of the list of tables
923
Marks all tables in the list which were used by current substatement
924
(they are marked by its query_id) as free for reuse.
927
The reason we reset query_id is that it's not enough to just test
928
if table->query_id != session->query_id to know if a table is in use.
931
SELECT f1_that_uses_t1() FROM t1;
932
In f1_that_uses_t1() we will see one instance of t1 where query_id is
933
set to query_id of original query.
936
static void mark_used_tables_as_free_for_reuse(Session *session, Table *table)
938
for (; table ; table= table->next)
940
if (table->query_id == session->query_id)
943
table->file->ha_reset();
950
Auxiliary function to close all tables in the open_tables list.
952
@param session Thread context.
954
@remark It should not ordinarily be called directly.
957
static void close_open_tables(Session *session)
959
bool found_old_table= 0;
961
safe_mutex_assert_not_owner(&LOCK_open);
963
pthread_mutex_lock(&LOCK_open);
965
while (session->open_tables)
966
found_old_table|= close_thread_table(session, &session->open_tables);
967
session->some_tables_deleted= 0;
969
/* Free tables to hold down open files */
970
while (open_cache.records > table_cache_size && unused_tables)
971
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
974
/* Tell threads waiting for refresh that something has happened */
377
978
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
983
Close all tables used by the current substatement, or all tables
984
used by this thread if we are on the upper level.
987
close_thread_tables()
988
session Thread handler
991
Unlocks tables and frees derived tables.
992
Put all normal tables used by thread in free list.
994
It will only close/mark as free for reuse tables opened by this
995
substatement, it will also check if we are closing tables after
996
execution of complete query (i.e. we are on upper level) and will
997
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);
1000
void close_thread_tables(Session *session)
1005
We are assuming here that session->derived_tables contains ONLY derived
1006
tables for this substatement. i.e. instead of approach which uses
1007
query_id matching for determining which of the derived tables belong
1008
to this substatement we rely on the ability of substatements to
1009
save/restore session->derived_tables during their execution.
1011
TODO: Probably even better approach is to simply associate list of
1012
derived tables with (sub-)statement instead of thread and destroy
1013
them at the end of its execution.
1015
if (session->derived_tables)
1019
Close all derived tables generated in queries like
1020
SELECT * FROM (SELECT * FROM t1)
1022
for (table= session->derived_tables ; table ; table= next)
1025
table->free_tmp_table(session);
1027
session->derived_tables= 0;
1031
Mark all temporary tables used by this statement as free for reuse.
1033
mark_temp_tables_as_free_for_reuse(session);
1035
Let us commit transaction for statement. Since in 5.0 we only have
1036
one statement transaction and don't allow several nested statement
1037
transactions this call will do nothing if we are inside of stored
1038
function or trigger (i.e. statement transaction is already active and
1039
does not belong to statement for which we do close_thread_tables()).
1040
TODO: This should be fixed in later releases.
1042
if (!(session->state_flags & Open_tables_state::BACKUPS_AVAIL))
1044
session->main_da.can_overwrite_status= true;
1045
ha_autocommit_or_rollback(session, session->is_error());
1046
session->main_da.can_overwrite_status= false;
1047
session->transaction.stmt.reset();
1050
if (session->locked_tables)
1053
/* Ensure we are calling ha_reset() for all used tables */
1054
mark_used_tables_as_free_for_reuse(session, session->open_tables);
1057
We are under simple LOCK TABLES so should not do anything else.
1065
For RBR we flush the pending event just before we unlock all the
1066
tables. This means that we are at the end of a topmost
1067
statement, so we ensure that the STMT_END_F flag is set on the
1068
pending event. For statements that are *inside* stored
1069
functions, the pending event will not be flushed: that will be
1070
handled either before writing a query log event (inside
1071
binlog_query()) or when preparing a pending event.
1073
mysql_unlock_tables(session, session->lock);
1077
Note that we need to hold LOCK_open while changing the
1078
open_tables list. Another thread may work on it.
1079
(See: remove_table_from_cache(), mysql_wait_completed_table())
1080
Closing a MERGE child before the parent would be fatal if the
1081
other thread tries to abort the MERGE lock in between.
1083
if (session->open_tables)
1084
close_open_tables(session);
1090
/* move one table to free list */
1092
bool close_thread_table(Session *session, Table **table_ptr)
1094
bool found_old_table= 0;
1095
Table *table= *table_ptr;
402
1097
assert(table->key_read == 0);
403
assert(!table->cursor || table->cursor->inited == Cursor::NONE);
1098
assert(!table->file || table->file->inited == handler::NONE);
405
open_tables= table->next;
1100
*table_ptr=table->next;
407
1102
if (table->needs_reopen_or_name_lock() ||
408
version != refresh_version || !table->db_stat)
1103
session->version != refresh_version || !table->db_stat)
410
1105
hash_delete(&open_cache,(unsigned char*) table);
411
found_old_table= true;
504
1168
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
1172
session thread handle
1173
table table which should be checked
1174
table_list list of tables
1175
check_alias whether to check tables' aliases
1177
NOTE: to exclude derived tables from check we use following mechanism:
1178
a) during derived table processing set Session::derived_tables_processing
1179
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
1180
Session::derived_tables_processing set. (we can't use JOIN::execute
1181
because for PS we perform only JOIN::prepare, but we can't set this
1182
flag in JOIN::prepare if we are not sure that we are in derived table
1183
processing loop, because multi-update call fix_fields() for some its
1184
items (which mean JOIN::prepare for subqueries) before unique_table
1185
call to detect which tables should be locked for write).
1186
c) unique_table skip all tables which belong to SELECT with
1187
SELECT::exclude_from_table_unique_test set.
1188
Also SELECT::exclude_from_table_unique_test used to exclude from check
1189
tables of main SELECT of multi-delete and multi-update
1191
We also skip tables with TableList::prelocking_placeholder set,
1192
because we want to allow SELECTs from them, and their modification
1193
will rise the error anyway.
1195
TODO: when we will have table/view change detection we can do this check
1200
0 if table is unique
539
TableList* unique_table(TableList *table, TableList *table_list,
1203
TableList* unique_table(Session *session, TableList *table, TableList *table_list,
543
1207
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
1257
Issue correct error message in case we found 2 duplicate tables which
1258
prevent some update operation
1261
update_non_unique_table_error()
1262
update table which we try to update
1263
operation name of update operation
1264
duplicate duplicate table which we found
1267
here we hide view underlying tables if we have them
1270
void update_non_unique_table_error(TableList *update,
1274
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1278
Table *find_temporary_table(Session *session, const char *db, const char *table_name)
1280
TableList table_list;
1282
table_list.db= (char*) db;
1283
table_list.table_name= (char*) table_name;
1284
return find_temporary_table(session, &table_list);
1288
Table *find_temporary_table(Session *session, TableList *table_list)
1290
char key[MAX_DBKEY_LENGTH];
1294
key_length= create_table_def_key(session, key, table_list, 1);
1295
for (table=session->temporary_tables ; table ; table= table->next)
1297
if (table->s->table_cache_key.length == key_length &&
1298
!memcmp(table->s->table_cache_key.str, key, key_length))
1301
return(0); // Not a temporary table
726
1325
@retval 0 the table was found and dropped successfully.
727
1326
@retval 1 the table was not found in the list of temporary tables
729
1328
@retval -1 the table is in use by a outer query
732
int Session::drop_temporary_table(TableList *table_list)
1331
int drop_temporary_table(Session *session, TableList *table_list)
736
if (not (table= find_temporary_table(table_list)))
1335
if (!(table= find_temporary_table(session, table_list)))
739
1338
/* Table might be in use by some outer statement. */
740
if (table->query_id && table->query_id != query_id)
1339
if (table->query_id && table->query_id != session->query_id)
742
1341
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
746
close_temporary_table(table);
752
/* move table first in unused links */
1346
If LOCK TABLES list is not empty and contains this table,
1347
unlock the table and remove the table from this list.
1349
mysql_lock_remove(session, session->locked_tables, table, false);
1350
close_temporary_table(session, table, 1, 1);
1355
unlink from session->temporary tables and close temporary table
1358
void close_temporary_table(Session *session, Table *table,
1359
bool free_share, bool delete_table)
1363
table->prev->next= table->next;
1364
if (table->prev->next)
1365
table->next->prev= table->prev;
1369
/* removing the item from the list */
1370
assert(table == session->temporary_tables);
1372
slave must reset its temporary list pointer to zero to exclude
1373
passing non-zero value to end_slave via rli->save_temporary_tables
1374
when no temp tables opened, see an invariant below.
1376
session->temporary_tables= table->next;
1377
if (session->temporary_tables)
1378
table->next->prev= 0;
1380
close_temporary(table, free_share, delete_table);
1386
Close and delete a temporary table
1389
This dosn't unlink table from session->temporary
1390
If this is needed, use close_temporary_table()
1393
void close_temporary(Table *table, bool free_share, bool delete_table)
1395
StorageEngine *table_type= table->s->db_type();
1397
free_io_cache(table);
1398
table->closefrm(false);
1401
rm_temporary_table(table_type, table->s->path.str);
1405
free_table_share(table->s);
1406
free((char*) table);
1413
Used by ALTER Table when the table is a temporary one. It changes something
1414
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1416
Prepares a table cache key, which is the concatenation of db, table_name and
1417
session->slave_proxy_id, separated by '\0'.
1420
bool rename_temporary_table(Session* session, Table *table, const char *db,
1421
const char *table_name)
1424
uint32_t key_length;
1425
TABLE_SHARE *share= table->s;
1426
TableList table_list;
1428
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1429
return true; /* purecov: inspected */
1431
table_list.db= (char*) db;
1432
table_list.table_name= (char*) table_name;
1433
key_length= create_table_def_key(session, key, &table_list, 1);
1434
share->set_table_cache_key(key, key_length);
1440
/* move table first in unused links */
754
1442
static void relink_unused(Table *table)
812
1510
// Notify any 'refresh' threads
813
1511
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.
1517
Auxiliary routine which closes and drops open table.
1519
@param session Thread handle
1520
@param table Table object for table to be dropped
1521
@param db_name Name of database for this table
1522
@param table_name Name of this table
1524
@note This routine assumes that table to be closed is open only
1525
by calling thread so we needn't wait until other threads
1526
will close the table. Also unless called under implicit or
1527
explicit LOCK TABLES mode it assumes that table to be
1528
dropped is already unlocked. In the former case it will
1529
also remove lock on the table. But one should not rely on
1530
this behaviour as it may change in future.
1531
Currently, however, this function is never called for a
1532
table that was locked with LOCK TABLES.
836
void Session::drop_open_table(Table *table, TableIdentifier &identifier)
1535
void drop_open_table(Session *session, Table *table, const char *db_name,
1536
const char *table_name)
838
1538
if (table->s->tmp_table)
840
close_temporary_table(table);
1539
close_temporary_table(session, table, 1, 1);
844
pthread_mutex_lock(&LOCK_open); /* Close and drop a table (AUX routine) */
1542
StorageEngine *table_type= table->s->db_type();
1543
pthread_mutex_lock(&LOCK_open);
846
1545
unlink_open_table() also tells threads waiting for refresh or close
847
1546
that something has happened.
849
unlink_open_table(table);
850
quick_rm_table(*this, identifier);
1548
unlink_open_table(session, table, false);
1549
quick_rm_table(table_type, db_name, table_name, 0);
851
1550
pthread_mutex_unlock(&LOCK_open);
857
Wait for condition but allow the user to send a kill to mysqld
1556
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
1559
wait_for_condition()
1560
session Thread handler
1561
mutex mutex that is currently hold that is associated with condition
1562
Will be unlocked on return
1563
cond Condition to wait for
867
void Session::wait_for_condition(pthread_mutex_t *mutex, pthread_cond_t *cond)
1566
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
869
1568
/* 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");
1569
const char *proc_info;
1570
session->mysys_var->current_mutex= mutex;
1571
session->mysys_var->current_cond= cond;
1572
proc_info=session->get_proc_info();
1573
session->set_proc_info("Waiting for table");
1574
if (!session->killed)
876
1575
(void) pthread_cond_wait(cond, mutex);
973
1706
table->next= orig_table.next;
976
table->tablenr= current_tablenr++;
977
table->used_fields= 0;
978
table->const_table= 0;
1709
table->tablenr=session->current_tablenr++;
1710
table->used_fields=0;
1711
table->const_table=0;
979
1712
table->null_row= false;
980
1713
table->maybe_null= false;
981
1714
table->force_index= false;
982
table->status= STATUS_NO_RECORD;
1715
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
1721
Create and insert into table cache placeholder for table
1722
which will prevent its opening (or creation) (a.k.a lock
1725
@param session Thread context
1726
@param key Table cache key for name to be locked
1727
@param key_length Table cache key length
1729
@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)
1733
Table *table_cache_insert_placeholder(Session *session, const char *key,
1734
uint32_t key_length)
1005
1738
char *key_buff;
1007
1740
safe_mutex_assert_owner(&LOCK_open);
1010
1743
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
1744
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,
1747
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
1748
&table, sizeof(*table),
1749
&share, sizeof(*share),
1750
&key_buff, key_length,
1021
1754
table->s= share;
1022
1755
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;
1756
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1757
table->in_use= session;
1025
1758
table->locked_by_name=1;
1027
1760
if (my_hash_insert(&open_cache, (unsigned char*)table))
1029
1762
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.
1771
Obtain an exclusive name lock on the table if it is not cached
1774
@param session Thread context
1775
@param db Name of database
1776
@param table_name Name of table
1777
@param[out] table Out parameter which is either:
1778
- set to NULL if table cache contains record for
1780
- set to point to the Table instance used for
1783
@note This function takes into account all records for table in table
1784
cache, even placeholders used for name-locking. This means that
1785
'table' parameter can be set to NULL for some situations when
1786
table does not really exist.
1788
@retval true Error occured (OOM)
1789
@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)
1792
bool lock_table_name_if_not_cached(Session *session, const char *db,
1793
const char *table_name, Table **table)
1066
1795
char key[MAX_DBKEY_LENGTH];
1067
1796
char *key_pos= key;
1068
1797
uint32_t key_length;
1070
key_pos= strcpy(key_pos, new_db) + strlen(new_db);
1799
key_pos= strcpy(key_pos, db) + strlen(db);
1071
1800
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1072
1801
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) */
1803
pthread_mutex_lock(&LOCK_open);
1076
1805
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1078
1807
pthread_mutex_unlock(&LOCK_open);
1082
if (not (*table= table_cache_insert_placeholder(key, key_length)))
1811
if (!(*table= table_cache_insert_placeholder(session, key, key_length)))
1084
1813
pthread_mutex_unlock(&LOCK_open);
1087
(*table)->open_placeholder= true;
1088
(*table)->next= open_tables;
1089
open_tables= *table;
1816
(*table)->open_placeholder= 1;
1817
(*table)->next= session->open_tables;
1818
session->open_tables= *table;
1090
1819
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.
1828
session Thread context.
1829
table_list Open first table in list.
1830
refresh INOUT Pointer to memory that will be set to 1 if
1831
we need to close all tables and reopen them.
1832
If this is a NULL pointer, then the table is not
1833
put in the thread-open-list.
1834
flags Bitmap of flags to modify how open works:
1835
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1836
someone has done a flush or namelock on it.
1837
No version number checking is done.
1838
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1839
table not the base table or view.
1114
Uses a cache of open tables to find a table not in use.
1842
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.
1844
If table list element for the table to be opened has "create" flag
1845
set and table does not exist, this function will automatically insert
1846
a placeholder for exclusive name lock into the open tables cache and
1847
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.
1850
NULL Open failed. If refresh is set then one should close
1851
all other tables and retry the open.
1852
# Success. Pointer to Table object for open table.
1128
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
1856
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags)
1858
register Table *table;
1131
1859
char key[MAX_DBKEY_LENGTH];
1132
1860
unsigned int key_length;
1133
const char *alias= table_list->alias;
1861
char *alias= table_list->alias;
1134
1862
HASH_SEARCH_STATE state;
1136
1864
/* Parsing of partitioning information from .frm needs session->lex set up. */
1137
assert(lex->is_lex_started);
1865
assert(session->lex->is_lex_started);
1139
1867
/* find a unused table in the open table cache */
1143
1871
/* 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);
1872
if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1875
if (session->killed)
1878
key_length= (create_table_def_key(session, key, table_list, 1) -
1879
TMP_TABLE_KEY_EXTRA);
1153
1882
Unless requested otherwise, try to resolve this table in the list
1154
1883
of temporary tables of this thread. In MySQL temporary tables
1155
1884
are always thread-local and "shadow" possible base tables with the
1156
1885
same name. This block implements the behaviour.
1157
TODO -> move this block into a separate function.
1886
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))
1889
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)
1891
if (table->s->table_cache_key.length == key_length +
1892
TMP_TABLE_KEY_EXTRA && !memcmp(table->s->table_cache_key.str, key,
1893
key_length + TMP_TABLE_KEY_EXTRA))
1171
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1896
We're trying to use the same temporary table twice in a query.
1897
Right now we don't support this because a temporary table
1898
is always represented by only one Table object in Session, and
1899
it can not be cloned. Emit an error for an unsupported behaviour.
1901
if (table->query_id)
1903
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1906
table->query_id= session->query_id;
1907
session->thread_specific_used= true;
1174
table->query_id= getQueryId();
1179
1913
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
1181
1915
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 */
1920
The table is not temporary - if we're in pre-locked or LOCK TABLES
1921
mode, let's try to find the requested table in the list of pre-opened
1922
and locked tables. If the table is not there, return an error - we can't
1923
open not pre-opened tables in pre-locked/LOCK TABLES mode.
1924
TODO: move this block into a separate function.
1926
if (session->locked_tables)
1927
{ // Using table locks
1928
Table *best_table= 0;
1929
int best_distance= INT_MIN;
1930
bool check_if_used= false;
1931
for (table=session->open_tables; table ; table=table->next)
1933
if (table->s->table_cache_key.length == key_length &&
1934
!memcmp(table->s->table_cache_key.str, key, key_length))
1936
if (check_if_used && table->query_id &&
1937
table->query_id != session->query_id)
1940
If we are in stored function or trigger we should ensure that
1941
we won't change table that is already used by calling statement.
1942
So if we are opening table for writing, we should check that it
1943
is not already open by some calling stamement.
1945
my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
1946
table->s->table_name.str);
1950
When looking for a usable Table, ignore MERGE children, as they
1951
belong to their parent and cannot be used explicitly.
1953
if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
1954
table->query_id != session->query_id) /* skip tables already used */
1956
int distance= ((int) table->reginfo.lock_type -
1957
(int) table_list->lock_type);
1959
Find a table that either has the exact lock type requested,
1960
or has the best suitable lock. In case there is no locked
1961
table that has an equal or higher lock than requested,
1962
we us the closest matching lock to be able to produce an error
1963
message about wrong lock mode on the table. The best_table
1964
is changed if bd < 0 <= d or bd < d < 0 or 0 <= d < bd.
1966
distance < 0 - No suitable lock found
1967
distance > 0 - we have lock mode higher then we require
1968
distance == 0 - we have lock mode exactly which we need
1970
if ((best_distance < 0 && distance > best_distance) || (distance >= 0 && distance < best_distance))
1972
best_distance= distance;
1974
if (best_distance == 0 && !check_if_used)
1977
If we have found perfect match and we don't need to check that
1978
table is not used by one of calling statements (assuming that
1979
we are inside of function or trigger) we can finish iterating
1980
through open tables list.
1991
table->query_id= session->query_id;
1995
No table in the locked tables list. In case of explicit LOCK TABLES
1996
this can happen if a user did not include the able into the list.
1997
In case of pre-locked mode locked tables list is generated automatically,
1998
so we may only end up here if the table did not exist when
1999
locked tables list was created.
2001
my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
1343
2158
/* Free cache if too big */
1344
2159
while (open_cache.records > table_cache_size && unused_tables)
1345
hash_delete(&open_cache,(unsigned char*) unused_tables);
2160
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
1347
2162
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))
2164
if(ha_table_exists_in_engine(session, table_list->db,
2165
table_list->table_name)
2166
!= HA_ERR_TABLE_EXIST)
1354
2169
Table to be created, so we need to create placeholder in table-cache.
1356
if (!(table= table_cache_insert_placeholder(key, key_length)))
2171
if (!(table= table_cache_insert_placeholder(session, key, key_length)))
1358
2173
pthread_mutex_unlock(&LOCK_open);
1362
2177
Link placeholder to the open tables list so it will be automatically
1363
2178
removed once tables are closed. Also mark it so it won't be ignored
1364
2179
by other trying to take name-lock.
1366
table->open_placeholder= true;
1367
table->next= open_tables;
2181
table->open_placeholder= 1;
2182
table->next= session->open_tables;
2183
session->open_tables= table;
1369
2184
pthread_mutex_unlock(&LOCK_open);
1373
2187
/* Table exists. Let us try to open it. */
1376
2190
/* make a new table */
1377
table= (Table *)malloc(sizeof(Table));
2191
table= (Table *) malloc(sizeof(*table));
1378
2192
if (table == NULL)
1380
2194
pthread_mutex_unlock(&LOCK_open);
1384
error= open_unireg_entry(this, table, table_list, alias, key, key_length);
2198
error= open_unireg_entry(session, table, table_list, alias, key, key_length);
1385
2199
if (error != 0)
1388
2202
pthread_mutex_unlock(&LOCK_open);
1391
my_hash_insert(&open_cache, (unsigned char*) table);
2205
my_hash_insert(&open_cache,(unsigned char*) table);
1394
2208
pthread_mutex_unlock(&LOCK_open);
1397
table->next= open_tables; /* Link into simple list */
2211
table->next=session->open_tables; /* Link into simple list */
2212
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())
2214
table->reginfo.lock_type=TL_READ; /* Assume read */
2217
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2219
if (session->lex->need_correct_ident())
1406
2220
table->alias_name_used= my_strcasecmp(table_alias_charset,
1407
2221
table->s->table_name.str, alias);
1408
2222
/* Fix alias if table name changes */
1567
2403
for target table name if we process ALTER Table ... RENAME.
1568
2404
So loop below makes sense even if we are not under LOCK TABLES.
1570
for (table= open_tables; table ; table=table->next)
2406
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))
2408
if (!strcmp(table->s->table_name.str, table_name) &&
2409
!strcmp(table->s->db.str, db))
1575
table->open_placeholder= true;
2411
if (session->locked_tables)
2413
mysql_lock_remove(session, session->locked_tables, table, true);
2415
table->open_placeholder= 1;
1576
2416
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.
2424
Reopen all tables with closed data files.
2426
@param session Thread context
2427
@param get_locks Should we get locks after reopening tables ?
2428
@param mark_share_as_old Mark share as old to protect from a impending
2431
@note Since this function can't properly handle prelocking and
2432
create placeholders it should be used in very special
2433
situations like FLUSH TABLES or ALTER Table. In general
2434
case one should just repeat open_tables()/lock_tables()
2435
combination when one needs tables to be reopened (for
2436
example see open_and_lock_tables()).
2438
@note One should have lock on LOCK_open when calling this.
2440
@return false in case of success, true - otherwise.
1602
bool Session::reopen_tables(bool get_locks, bool mark_share_as_old)
2443
bool reopen_tables(Session *session, bool get_locks, bool mark_share_as_old)
1604
2445
Table *table,*next,**prev;
1605
2446
Table **tables,**tables_ptr; // For locks
1606
2447
bool error=0, not_used;
1607
2448
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
1608
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
1609
DRIZZLE_LOCK_IGNORE_FLUSH;
2449
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2450
DRIZZLE_LOCK_IGNORE_FLUSH;
1611
if (open_tables == NULL)
2452
if (!session->open_tables)
1614
2455
safe_mutex_assert_owner(&LOCK_open);
1932
2773
for (table= session->open_tables; table ; table= table->next)
1934
2775
if (!strcmp(table->s->table_name.str, table_name) &&
1935
!strcmp(table->s->getSchemaName(), db))
2776
!strcmp(table->s->db.str, db))
1937
2778
/* If MERGE child, forward lock handling to parent. */
1938
mysql_lock_abort(session, table);
2779
mysql_lock_abort(session, table, true);
1945
Load a table definition from cursor and open unireg table
2787
Function to assign a new table map id to a table share.
2791
share - Pointer to table share structure
2795
We are intentionally not checking that share->mutex is locked
2796
since this function should only be called when opening a table
2797
share and before it is entered into the table_def_cache (meaning
2798
that it cannot be fetched by another thread, even accidentally).
2803
The LOCK_open mutex is locked
2807
share->table_map_id is given a value that with a high certainty is
2808
not used by any other table (the only case where a table id can be
2809
reused is on wrap-around, which means more than 4 billion table
2810
share opens have been executed while one table was open all the
2813
share->table_map_id is not UINT32_MAX.
2815
void assign_new_table_id(TABLE_SHARE *share)
2817
static uint32_t last_table_id= UINT32_MAX;
2820
assert(share != NULL);
2821
safe_mutex_assert_owner(&LOCK_open);
2823
ulong tid= ++last_table_id; /* get next id */
2825
There is one reserved number that cannot be used. Remember to
2826
change this when 6-byte global table id's are introduced.
2828
if (unlikely(tid == UINT32_MAX))
2829
tid= ++last_table_id;
2830
share->table_map_id= tid;
2832
/* Post conditions */
2833
assert(share->table_map_id != UINT32_MAX);
2839
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
2843
session Thread handle
2844
entry Store open table definition here
2845
table_list TableList with db, table_name
2847
cache_key Key for share_cache
2848
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
2851
Extra argument for open is taken from session->open_options
2852
One must have a lock on LOCK_open when calling this function
1965
2859
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
2013
2908
if (share->ref_count != 1)
2015
2910
/* Free share and wait until it's released by all threads */
2016
TableShare::release(share);
2911
release_table_share(share, RELEASE_WAIT_FOR_DROP);
2018
2912
if (!session->killed)
2020
2914
drizzle_reset_errors(session, 1); // Clear warnings
2021
2915
session->clear_error(); // Clear error message
2920
if (!entry->s || !entry->s->crashed)
2922
// Code below is for repairing a crashed file
2923
if ((error= lock_table_name(session, table_list, true)))
2927
if (wait_for_locked_table_names(session, table_list))
2929
unlock_table_name(session, table_list);
2933
pthread_mutex_unlock(&LOCK_open);
2934
session->clear_error(); // Clear error message
2936
if (open_table_from_share(session, share, alias,
2937
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2941
ha_open_options | HA_OPEN_FOR_REPAIR,
2942
entry, OTM_OPEN) || ! entry->file ||
2943
(entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
2945
/* Give right error message */
2946
session->clear_error();
2947
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2948
errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2949
share->table_name.str);
2951
entry->closefrm(false);
2955
session->clear_error(); // Clear error message
2956
pthread_mutex_lock(&LOCK_open);
2957
unlock_table_name(session, table_list);
2965
If we are here, there was no fatal error (but error may be still
2968
if (unlikely(entry->file->implicit_emptied))
2970
entry->file->implicit_emptied= 0;
2973
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
2974
if ((query= (char*) malloc(query_buf_size)))
2977
"this DELETE FROM is needed even with row-based binlogging"
2979
We inherited this from MySQL. TODO: fix it to issue a propper truncate
2980
of the table (though that may not be completely right sematics).
2983
end+= sprintf(query, "DELETE FROM `%s`.`%s`", share->db.str,
2984
share->table_name.str);
2985
transaction_services.rawStatement(session, query, (size_t)(end - query));
2990
errmsg_printf(ERRMSG_LVL_ERROR, _("When opening HEAP table, could not allocate memory "
2991
"to write 'DELETE FROM `%s`.`%s`' to replication"),
2992
table_list->db, table_list->table_name);
2993
my_error(ER_OUTOFMEMORY, MYF(0), query_buf_size);
2994
entry->closefrm(false);
2033
TableShare::release(share);
3002
release_table_share(share, RELEASE_NORMAL);
2166
3142
tables->table= NULL;
2169
3144
return(result);
3149
Check that lock is ok for tables; Call start stmt if ok
3152
check_lock_and_start_stmt()
3153
session Thread handle
3154
table_list Table to check
3155
lock_type Lock used for table
3162
static bool check_lock_and_start_stmt(Session *session, Table *table,
3163
thr_lock_type lock_type)
3167
if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3168
(int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3170
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3173
if ((error=table->file->start_stmt(session, lock_type)))
3175
table->file->print_error(error,MYF(0));
3183
@brief Open and lock one table
3185
@param[in] session thread handle
3186
@param[in] table_l table to open is first table in this list
3187
@param[in] lock_type lock to use for table
3190
@retval != NULL OK, opened table returned
3194
If ok, the following are also set:
3195
table_list->lock_type lock_type
3196
table_list->table table
3199
If table_l is a list, not a single table, the list is temporarily
3203
This function is meant as a replacement for open_ltable() when
3204
MERGE tables can be opened. open_ltable() cannot open MERGE tables.
3206
There may be more differences between open_n_lock_single_table() and
3207
open_ltable(). One known difference is that open_ltable() does
3208
neither call decide_logging_format() nor handle some other logging
3209
and locking issues because it does not call lock_tables().
3212
Table *open_n_lock_single_table(Session *session, TableList *table_l,
3213
thr_lock_type lock_type)
3215
TableList *save_next_global;
3217
/* Remember old 'next' pointer. */
3218
save_next_global= table_l->next_global;
3220
table_l->next_global= NULL;
3222
/* Set requested lock type. */
3223
table_l->lock_type= lock_type;
3225
/* Open the table. */
3226
if (simple_open_n_lock_tables(session, table_l))
3227
table_l->table= NULL; /* Just to be sure. */
3230
table_l->next_global= save_next_global;
3232
return(table_l->table);
2174
3237
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
3241
session Thread handler
3242
table_list Table to open is first table in this list
3243
lock_type Lock to use for open
3244
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.
3247
This function don't do anything like SP/SF/views/triggers analysis done
3248
in open_tables(). It is intended for opening of only one concrete table.
3249
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
3255
If ok, the following are also set:
3256
table_list->lock_type lock_type
3257
table_list->table table
2197
Table *Session::openTableLock(TableList *table_list, thr_lock_type lock_type)
3260
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type lock_type,
3261
uint32_t lock_flags)
2202
set_proc_info("Opening table");
2204
while (!(table= openTable(table_list, &refresh)) &&
3266
session->set_proc_info("Opening table");
3267
session->current_tablenr= 0;
3268
while (!(table= open_table(session, table_list, &refresh, 0)) &&
2210
3274
table_list->lock_type= lock_type;
2211
3275
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)))
3276
if (session->locked_tables)
3278
if (check_lock_and_start_stmt(session, table, lock_type))
3283
assert(session->lock == 0); // You must lock everything at once
3284
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3285
if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
3286
lock_flags, &refresh)))
3291
session->set_proc_info(0);
3297
Open all tables in list, locks them and optionally process derived tables.
3300
open_and_lock_tables_derived()
3301
session - thread handler
3302
tables - list of tables for open&locking
3303
derived - if to handle derived tables
3310
The lock will automaticaly be freed by close_thread_tables()
3313
There are two convenience functions:
3314
- simple_open_n_lock_tables(session, tables) without derived handling
3315
- open_and_lock_tables(session, tables) with derived handling
3316
Both inline functions call open_and_lock_tables_derived() with
3317
the third argument set appropriately.
3320
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3327
if (open_tables(session, &tables, &counter, 0))
3330
if (!lock_tables(session, tables, counter, &need_reopen))
3334
close_tables_for_reopen(session, &tables);
3337
(mysql_handle_derived(session->lex, &mysql_derived_prepare) ||
3338
(session->fill_derived_tables() &&
3339
mysql_handle_derived(session->lex, &mysql_derived_filling))))
3340
return(true); /* purecov: inspected */
3346
Open all tables in list and process derived tables
3349
open_normal_and_derived_tables
3350
session - thread handler
3351
tables - list of tables for open
3352
flags - bitmap of flags to modify how the tables will be open:
3353
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3354
done a flush or namelock on it.
3361
This is to be used on prepare stage when you don't read any
3362
data from the tables.
3365
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
3368
assert(!session->fill_derived_tables());
3369
if (open_tables(session, &tables, &counter, flags) ||
3370
mysql_handle_derived(session->lex, &mysql_derived_prepare))
3371
return(true); /* purecov: inspected */
2225
3376
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).
3380
session Thread handler
3381
tables Tables to lock
3382
count Number of opened tables
3383
need_reopen Out parameter which if true indicates that some
3384
tables were dropped or altered during this call
3385
and therefore invoker should reopen tables and
3386
try to lock them once again (in this case
3387
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
3390
You can't call lock_tables twice, as this would break the dead-lock-free
3391
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.
3394
If query for which we are calling this function marked as requring
3395
prelocking, this function will do implicit LOCK TABLES and change
3396
session::prelocked_mode accordingly.
2252
int Session::lock_tables(TableList *tables, uint32_t count, bool *need_reopen)
3403
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
2254
3405
TableList *table;
2255
Session *session= this;
2258
3408
We can't meet statement requiring prelocking if we already
3464
Prepare statement for reopening of tables and recalculation of set of
3468
close_tables_for_reopen()
3469
session in Thread context
3470
tables in/out List of tables which we were trying to open and lock
3474
void close_tables_for_reopen(Session *session, TableList **tables)
3477
If table list consists only from tables from prelocking set, table list
3478
for new attempt should be empty, so we have to update list's root pointer.
3480
if (session->lex->first_not_own_table() == *tables)
3482
session->lex->chop_off_not_own_tables();
3483
for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
3485
close_thread_tables(session);
2289
3490
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 ...
3493
open_temporary_table()
3494
session Thread object
3495
path Path (without .frm)
3497
table_name Table name
3498
link_in_list 1 if table should be linked into session->temporary_tables
3501
Used by alter_table to open a temporary table and when creating
3502
a temporary table with CREATE TEMPORARY ...
2308
Table *Session::open_temporary_table(TableIdentifier &identifier,
3509
Table *open_temporary_table(Session *session, const char *path, const char *db,
3510
const char *table_name, bool link_in_list,
3511
open_table_mode open_mode)
2311
Table *new_tmp_table;
2313
3515
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
2314
3516
uint32_t key_length, path_length;
2315
3517
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());
3519
table_list.db= (char*) db;
3520
table_list.table_name= (char*) table_name;
2319
3521
/* Create the cache_key for temporary tables */
2320
key_length= table_list.create_table_def_key(cache_key);
2321
path_length= identifier.getPath().length();
3522
key_length= create_table_def_key(session, cache_key, &table_list, 1);
3523
path_length= strlen(path);
2323
if (!(new_tmp_table= (Table*) malloc(sizeof(*new_tmp_table) + sizeof(*share) +
2324
path_length + 1 + key_length)))
3525
if (!(tmp_table= (Table*) malloc(sizeof(*tmp_table) + sizeof(*share) +
3526
path_length + 1 + key_length)))
2327
share= (TableShare*) (new_tmp_table+1);
3529
share= (TABLE_SHARE*) (tmp_table+1);
2328
3530
tmp_path= (char*) (share+1);
2329
saved_cache_key= strcpy(tmp_path, identifier.getPath().c_str())+path_length+1;
3531
saved_cache_key= strcpy(tmp_path, path)+path_length+1;
2330
3532
memcpy(saved_cache_key, cache_key, key_length);
2332
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
3534
init_tmp_table_share(session, share, saved_cache_key, key_length,
3535
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(),
3537
if (open_table_def(session, share, 0) ||
3538
open_table_from_share(session, share, table_name,
3539
(open_mode == OTM_ALTER) ? 0 :
2339
3540
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3542
(open_mode == OTM_ALTER) ?
3543
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
2341
3545
ha_open_options,
3546
tmp_table, open_mode))
2344
3548
/* 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);
3549
free_table_share(share);
3550
free((char*) tmp_table);
2350
new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
2351
share->tmp_table= message::Table::TEMPORARY;
3554
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
3555
if (open_mode == OTM_ALTER)
3558
Temporary table has been created with frm_only
3559
and has not been created in any storage engine
3561
share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
3564
share->tmp_table= (tmp_table->file->has_transactions() ?
3565
TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
2353
3567
if (link_in_list)
2355
3569
/* 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;
3570
tmp_table->next= session->temporary_tables;
3571
if (tmp_table->next)
3572
tmp_table->next->prev= tmp_table;
3573
session->temporary_tables= tmp_table;
3574
session->temporary_tables->prev= 0;
3576
tmp_table->pos_in_table_list= 0;
3581
bool rm_temporary_table(StorageEngine *base, char *path)
3586
if(delete_table_proto_file(path))
3587
error=1; /* purecov: inspected */
3589
file= get_new_handler((TABLE_SHARE*) 0, current_session->mem_root, base);
3590
if (file && file->ha_delete_table(path))
3593
errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
3601
* Helper function which tests whether a bit is set in the
3602
* bitset or not. It also sets the bit after this test is
3605
static bool test_and_set_bit(bitset<MAX_FIELDS> *bitmap, uint32_t pos)
3608
if (bitmap->test(pos))
2368
3614
/*****************************************************************************
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
******************************************************************************/
3615
* The following find_field_in_XXX procedures implement the core of the
3616
* name resolution functionality. The entry point to resolve a column name in a
3617
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3618
* for each table reference. In turn, depending on the type of table reference,
3619
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3620
* below specific for the type of table reference.
3621
******************************************************************************/
2377
3623
/* Special Field pointers as return values of find_field_in_XXX functions. */
2378
3624
Field *not_found_field= (Field*) 0x1;
2379
3625
Field *view_ref_found= (Field*) 0x2;
3627
#define WRONG_GRANT (Field*) -1
2381
3629
static void update_field_dependencies(Session *session, Field *field, Table *table)
2383
3631
if (session->mark_used_columns != MARK_COLUMNS_NONE)
2385
MyBitmap *current_bitmap, *other_bitmap;
3633
bitset<MAX_FIELDS> *current_bitmap, *other_bitmap;
2388
3636
We always want to register the used keys, as the column bitmap may have
2389
3637
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;
3640
table->covering_keys.intersect(field->part_of_key);
3641
table->merge_keys.merge(field->part_of_key);
2395
3643
if (session->mark_used_columns == MARK_COLUMNS_READ)
2559
3830
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.
3833
find_field_in_table_ref()
3834
session [in] thread handler
3835
table_list [in] table reference to search
3836
name [in] name of field
3837
length [in] field length of name
3838
item_name [in] name of item if it will be created (VIEW)
3839
db_name [in] optional database name that qualifies the
3840
table_name [in] optional table name that qualifies the field
3841
ref [in/out] if 'name' is resolved to a view field, ref
3842
is set to point to the found view field
3843
check_privileges [in] check privileges
3844
allow_rowid [in] do allow finding of "_rowid" field?
3845
cached_field_index_ptr [in] cached position in field list (used to
3846
speedup lookup for fields in prepared tables)
3847
register_tree_change [in] true if ref is not stack variable and we
3848
need register changes in item tree
3849
actual_table [out] the original table reference where the field
3850
belongs - differs from 'table_list' only for
3851
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.
3854
Find a field in a table reference depending on the type of table
3855
reference. There are three types of table references with respect
3856
to the representation of their result columns:
3857
- an array of Field_translator objects for MERGE views and some
3858
information_schema tables,
3859
- an array of Field objects (and possibly a name hash) for stored
3861
- a list of Natural_join_column objects for NATURAL/USING joins.
3862
This procedure detects the type of the table reference 'table_list'
3863
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)
3866
0 field is not found
3867
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)
3963
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;
3966
Get rw_set correct for this field so that the handler
3967
knows that this field is involved in the query and gets
3970
Field *field_to_set= NULL;
3971
if (fld == view_ref_found)
3973
Item *it= (*ref)->real_item();
3974
if (it->type() == Item::FIELD_ITEM)
3975
field_to_set= ((Item_field*)it)->field;
3978
if (session->mark_used_columns == MARK_COLUMNS_READ)
3979
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3986
Table *table= field_to_set->table;
2706
3987
if (session->mark_used_columns == MARK_COLUMNS_READ)
2707
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3988
table->read_set->set(field_to_set->field_index);
3990
table->write_set->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);
3999
Find field in table, no side effects, only purpose is to check for field
4000
in table object and get reference to the field if found.
4003
find_field_in_table_sef()
4005
table table where to find
4006
name Name of field searched for
4009
0 field is not found
4013
Field *find_field_in_table_sef(Table *table, const char *name)
4016
if (table->s->name_hash.records)
4018
field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
4023
field_ptr points to field in TABLE_SHARE. Convert it to the matching
4026
field_ptr= (table->field + (field_ptr - table->s->field));
4031
if (!(field_ptr= table->field))
4033
for (; *field_ptr; ++field_ptr)
4034
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
2727
4045
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
4048
find_field_in_tables()
4049
session pointer to current thread structure
4050
item field item that should be found
4051
first_table list of tables to be searched for item
4052
last_table end of the list of tables to search for item. If NULL
4053
then search to the end of the list 'first_table'.
4054
ref if 'item' is resolved to a view field, ref is set to
4055
point to the found view field
4056
report_error Degree of error reporting:
4057
- IGNORE_ERRORS then do not report any error
4058
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
4059
fields, suppress all other errors
4060
- REPORT_EXCEPT_NON_UNIQUE report all other errors
4061
except when non-unique fields were found
4063
check_privileges need to check privileges
4064
register_tree_change true if ref is not a stack variable and we
4065
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
4068
0 If error: the found field is not unique, or there are
4069
no sufficient access priviliges for the found field,
4070
or the field is qualified with non-existing table.
4071
not_found_field The function was called with report_error ==
4072
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
4073
field was not found.
4074
view_ref_found View field is found, item passed through ref parameter
4075
found field If a item was resolved to some field
2760
4079
find_field_in_tables(Session *session, Item_ident *item,
2761
4080
TableList *first_table, TableList *last_table,
2762
Item **ref, find_item_error_report_type report_error,
2763
bool register_tree_change)
4081
Item **ref, find_item_error_report_type report_error,
4082
bool check_privileges, bool register_tree_change)
2765
4084
Field *found=0;
2766
4085
const char *db= item->db_name;
3425
4774
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
4777
store_natural_using_join_columns()
4778
session current thread
4779
natural_using_join the table reference of the NATURAL/USING join
4780
table_ref_1 the first (left) operand (of a NATURAL/USING join).
4781
table_ref_2 the second (right) operand (of a NATURAL/USING join).
4782
using_fields if the join is JOIN...USING - the join columns,
4783
if NATURAL join, then NULL
4784
found_using_fields number of fields from the USING clause that were
4785
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
4788
Iterate over the columns of both join operands and sort and store
4789
all columns into the 'join_columns' list of natural_using_join
4790
where the list is formed by three parts:
4791
part1: The coalesced columns of table_ref_1 and table_ref_2,
4792
sorted according to the column order of the first table.
4793
part2: The other columns of the first table, in the order in
4794
which they were defined in CREATE TABLE.
4795
part3: The other columns of the second table, in the order in
4796
which they were defined in CREATE TABLE.
4797
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
4800
The procedure assumes that mark_common_columns() has been called
4801
for the join that is being processed.
4804
true error: Some common column is ambiguous
4407
5866
table= field->table;
4408
5867
if (field == table->next_number_field)
4409
5868
table->auto_increment_field_not_null= true;
5869
if (field->vcol_info &&
5870
value->type() != Item::DEFAULT_VALUE_ITEM &&
5871
value->type() != Item::NULL_ITEM &&
5872
table->s->table_category != TABLE_CATEGORY_TEMPORARY)
5874
session->abort_on_warning= false;
5875
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
5876
ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
5877
ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
5878
field->field_name, table->s->table_name.str);
5879
session->abort_on_warning= abort_on_warning_saved;
4410
5881
if (value->save_in_field(field, 0) < 0)
5883
tbl_list.push_back(table);
5885
/* Update virtual fields*/
5886
session->abort_on_warning= false;
5887
if (tbl_list.head())
5889
List_iterator_fast<Table> t(tbl_list);
5890
Table *prev_table= 0;
5891
while ((table= t++))
5894
Do simple optimization to prevent unnecessary re-generating
5895
values for virtual fields
5897
if (table != prev_table)
5902
if (update_virtual_fields_marked_for_write(table, false))
5910
session->abort_on_warning= abort_on_warning_saved;
4414
5911
return(session->is_error());
5914
session->abort_on_warning= abort_on_warning_saved;
4418
5916
table->auto_increment_field_not_null= false;
4424
bool drizzle_rm_tmp_tables()
5921
bool drizzle_rm_tmp_tables(void)
5924
char filePath[FN_REFLEN], filePathCopy[FN_REFLEN];
4426
5928
Session *session;
4428
5930
assert(drizzle_tmpdir);
4430
if (!(session= new Session(plugin::Listen::getNullClient())))
5932
if (!(session= new Session(get_protocol())))
4432
5934
session->thread_stack= (char*) &session;
4433
session->storeGlobals();
4435
plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir);
5935
session->store_globals();
5937
/* Remove all temp tables in the tmpdir */
5938
/* See if the directory exists */
5939
if ((dirp = my_dir(drizzle_tmpdir ,MYF(MY_WME | MY_DONT_SORT))))
5941
/* Remove all SQLxxx tables from directory */
5942
for (idx=0 ; idx < (uint32_t) dirp->number_off_files ; idx++)
5944
file=dirp->dir_entry+idx;
5946
/* skiping . and .. */
5947
if (file->name[0] == '.' && (!file->name[1] ||
5948
(file->name[1] == '.' && !file->name[2])))
5951
if (!memcmp(file->name, TMP_FILE_PREFIX, TMP_FILE_PREFIX_LENGTH))
5953
char *ext= fn_ext(file->name);
5954
uint32_t ext_len= strlen(ext);
5955
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
5956
"%s%c%s", drizzle_tmpdir, FN_LIBCHAR,
5958
if (!memcmp(".dfe", ext, ext_len))
5960
handler *handler_file= 0;
5961
/* We should cut file extention before deleting of table */
5962
memcpy(filePathCopy, filePath, filePath_len - ext_len);
5963
filePathCopy[filePath_len - ext_len]= 0;
5964
init_tmp_table_share(session, &share, "", 0, "", filePathCopy);
5965
if (!open_table_def(session, &share, 0) &&
5966
((handler_file= get_new_handler(&share, session->mem_root,
5969
handler_file->ha_delete_table(filePathCopy);
5970
delete handler_file;
5972
free_table_share(&share);
5975
File can be already deleted by tmp_table.file->delete_table().
5976
So we hide error messages which happnes during deleting of these
5979
my_delete(filePath, MYF(0));
4437
5985
delete session;