18
18
#include <drizzled/server_includes.h>
19
19
#include <drizzled/sql_select.h>
20
20
#include <mysys/my_dir.h>
21
#include <drizzled/error.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/nested_join.h>
25
#if TIME_WITH_SYS_TIME
26
# include <sys/time.h>
30
# include <sys/time.h>
37
return true if the table was created explicitly.
39
inline bool is_user_table(Table * table)
41
const char *name= table->s->table_name.str;
42
return strncmp(name, tmp_file_prefix, tmp_file_prefix_length);
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
24
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
47
27
@defgroup Data_Dictionary Data Dictionary
54
34
static pthread_mutex_t LOCK_table_share;
55
35
static bool table_def_inited= 0;
57
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
37
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
59
char *cache_key, uint32_t cache_key_length);
39
char *cache_key, uint cache_key_length);
60
40
static void free_cache_entry(Table *entry);
61
static void close_old_data_files(Session *session, Table *table, bool morph_locks,
41
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
62
42
bool send_refresh);
65
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
45
extern "C" uchar *table_cache_key(const uchar *record, size_t *length,
66
46
bool not_used __attribute__((unused)))
68
48
Table *entry=(Table*) record;
69
49
*length= entry->s->table_cache_key.length;
70
return (unsigned char*) entry->s->table_cache_key.str;
50
return (uchar*) entry->s->table_cache_key.str;
122
uint32_t create_table_def_key(Session *session, char *key, TableList *table_list,
102
uint create_table_def_key(THD *thd, char *key, TableList *table_list,
125
uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
105
uint key_length= (uint) (stpcpy(stpcpy(key, table_list->db)+1,
126
106
table_list->table_name)-key)+1;
129
int4store(key + key_length, session->server_id);
130
int4store(key + key_length + 4, session->variables.pseudo_thread_id);
109
int4store(key + key_length, thd->server_id);
110
int4store(key + key_length + 4, thd->variables.pseudo_thread_id);
131
111
key_length+= TMP_TABLE_KEY_EXTRA;
133
113
return key_length;
139
119
Functions to handle table definition cach (TABLE_SHARE)
140
120
*****************************************************************************/
142
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
122
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
143
123
bool not_used __attribute__((unused)))
145
125
TABLE_SHARE *entry=(TABLE_SHARE*) record;
146
126
*length= entry->table_cache_key.length;
147
return (unsigned char*) entry->table_cache_key.str;
127
return (uchar*) entry->table_cache_key.str;
219
199
# Share for table
222
TABLE_SHARE *get_table_share(Session *session, TableList *table_list, char *key,
223
uint32_t key_length, uint32_t db_flags, int *error)
202
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
203
uint key_length, uint db_flags, int *error)
225
205
TABLE_SHARE *share;
229
209
/* Read table definition from cache */
230
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
210
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
258
238
assign_new_table_id(share);
260
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
240
if (my_hash_insert(&table_def_cache, (uchar*) share))
262
242
free_table_share(share);
263
243
return(0); // return error
265
if (open_table_def(session, share, db_flags))
245
if (open_table_def(thd, share, db_flags))
267
247
*error= share->error;
268
(void) hash_delete(&table_def_cache, (unsigned char*) share);
248
(void) hash_delete(&table_def_cache, (uchar*) share);
271
251
share->ref_count++; // Mark in use
324
304
static TABLE_SHARE
325
*get_table_share_with_create(Session *session, TableList *table_list,
326
char *key, uint32_t key_length,
327
uint32_t db_flags, int *error)
305
*get_table_share_with_create(THD *thd, TableList *table_list,
306
char *key, uint key_length,
307
uint db_flags, int *error)
329
309
TABLE_SHARE *share;
332
share= get_table_share(session, table_list, key, key_length, db_flags, error);
312
share= get_table_share(thd, table_list, key, key_length, db_flags, error);
334
314
If share is not NULL, we found an existing share.
350
330
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
352
if (share || (session->is_error() && (session->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
332
if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
356
336
/* Table didn't exist. Check if some engine can provide it */
357
if ((tmp= ha_create_table_from_engine(session, table_list->db,
337
if ((tmp= ha_create_table_from_engine(thd, table_list->db,
358
338
table_list->table_name)) < 0)
363
343
/* Give right error message */
364
session->clear_error();
365
345
my_printf_error(ER_UNKNOWN_ERROR,
366
346
"Failed to open '%-.64s', error while "
367
347
"unpacking from engine",
371
351
/* Table existed in engine. Let's open it */
372
drizzle_reset_errors(session, 1); // Clear warnings
373
session->clear_error(); // Clear error message
374
return(get_table_share(session, table_list, key, key_length,
352
drizzle_reset_errors(thd, 1); // Clear warnings
353
thd->clear_error(); // Clear error message
354
return(get_table_share(thd, table_list, key, key_length,
375
355
db_flags, error));
452
432
char key[NAME_LEN*2+2];
453
433
TableList table_list;
455
435
safe_mutex_assert_owner(&LOCK_open);
457
437
table_list.db= (char*) db;
458
438
table_list.table_name= (char*) table_name;
459
key_length= create_table_def_key((Session*) 0, key, &table_list, 0);
460
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
439
key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
440
return (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key, key_length);
532
512
# Pointer to list of names of open tables.
535
OPEN_TableList *list_open_tables(Session *session __attribute__((unused)),
515
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
536
516
const char *db, const char *wild)
539
519
OPEN_TableList **start_list, *open_list;
540
520
TableList table_list;
542
pthread_mutex_lock(&LOCK_open);
522
VOID(pthread_mutex_lock(&LOCK_open));
543
523
memset(&table_list, 0, sizeof(table_list));
544
524
start_list= &open_list;
547
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
527
for (uint idx=0 ; result == 0 && idx < open_cache.records; idx++)
549
529
OPEN_TableList *table;
550
530
Table *entry=(Table*) hash_element(&open_cache,idx);
660
640
won't proceed while write-locked tables are being reopened by other
663
@remark Session can be NULL, but then wait_for_refresh must be false
643
@remark THD can be NULL, but then wait_for_refresh must be false
664
644
and tables must be NULL.
667
bool close_cached_tables(Session *session, TableList *tables, bool have_lock,
647
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
668
648
bool wait_for_refresh, bool wait_for_placeholders)
671
assert(session || (!wait_for_refresh && !tables));
651
assert(thd || (!wait_for_refresh && !tables));
674
pthread_mutex_lock(&LOCK_open);
654
VOID(pthread_mutex_lock(&LOCK_open));
677
657
refresh_version++; // Force close of open tables
678
658
while (unused_tables)
680
660
#ifdef EXTRA_DEBUG
681
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
661
if (hash_delete(&open_cache,(uchar*) unused_tables))
682
662
printf("Warning: Couldn't delete open table from hash\n");
684
hash_delete(&open_cache,(unsigned char*) unused_tables);
664
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
687
667
/* Free table shares */
688
668
while (oldest_unused_share->next)
690
670
pthread_mutex_lock(&oldest_unused_share->mutex);
691
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
671
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
693
673
if (wait_for_refresh)
756
736
If there is any table that has a lower refresh_version, wait until
757
737
this is closed (or this thread is killed) before returning
759
session->mysys_var->current_mutex= &LOCK_open;
760
session->mysys_var->current_cond= &COND_refresh;
761
session->set_proc_info("Flushing tables");
739
thd->mysys_var->current_mutex= &LOCK_open;
740
thd->mysys_var->current_cond= &COND_refresh;
741
thd_proc_info(thd, "Flushing tables");
763
close_old_data_files(session,session->open_tables,1,1);
764
mysql_ha_flush(session);
743
close_old_data_files(thd,thd->open_tables,1,1);
767
747
/* Wait until all threads has closed all the tables we had locked */
768
while (found && ! session->killed)
748
while (found && ! thd->killed)
771
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
751
for (uint idx=0 ; idx < open_cache.records ; idx++)
773
753
Table *table=(Table*) hash_element(&open_cache,idx);
774
754
/* Avoid a self-deadlock. */
775
if (table->in_use == session)
755
if (table->in_use == thd)
778
758
Note that we wait here only for tables which are actually open, and
802
782
old locks. This should always succeed (unless some external process
803
783
has removed the tables)
805
session->in_lock_tables=1;
806
result=reopen_tables(session,1,1);
807
session->in_lock_tables=0;
785
thd->in_lock_tables=1;
786
result=reopen_tables(thd,1,1);
787
thd->in_lock_tables=0;
808
788
/* Set version for table */
809
for (Table *table=session->open_tables; table ; table= table->next)
789
for (Table *table=thd->open_tables; table ; table= table->next)
812
792
Preserve the version (0) of write locked tables so that a impending
820
pthread_mutex_unlock(&LOCK_open);
800
VOID(pthread_mutex_unlock(&LOCK_open));
821
801
if (wait_for_refresh)
823
pthread_mutex_lock(&session->mysys_var->mutex);
824
session->mysys_var->current_mutex= 0;
825
session->mysys_var->current_cond= 0;
826
session->set_proc_info(0);
827
pthread_mutex_unlock(&session->mysys_var->mutex);
803
pthread_mutex_lock(&thd->mysys_var->mutex);
804
thd->mysys_var->current_mutex= 0;
805
thd->mysys_var->current_cond= 0;
806
thd_proc_info(thd, 0);
807
pthread_mutex_unlock(&thd->mysys_var->mutex);
835
815
if specified string is NULL, then any table with a connection string.
838
bool close_cached_connection_tables(Session *session, bool if_wait_for_refresh,
818
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
839
819
LEX_STRING *connection, bool have_lock)
842
822
TableList tmp, *tables= NULL;
843
823
bool result= false;
846
826
memset(&tmp, 0, sizeof(TableList));
849
pthread_mutex_lock(&LOCK_open);
829
VOID(pthread_mutex_lock(&LOCK_open));
851
831
for (idx= 0; idx < table_def_cache.records; idx++)
871
851
tmp.table_name= share->table_name.str;
872
852
tmp.next_local= tables;
874
tables= (TableList *) memdup_root(session->mem_root, (char*)&tmp,
854
tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp,
875
855
sizeof(TableList));
879
result= close_cached_tables(session, tables, true, false, false);
859
result= close_cached_tables(thd, tables, true, false, false);
882
pthread_mutex_unlock(&LOCK_open);
862
VOID(pthread_mutex_unlock(&LOCK_open));
884
864
if (if_wait_for_refresh)
886
pthread_mutex_lock(&session->mysys_var->mutex);
887
session->mysys_var->current_mutex= 0;
888
session->mysys_var->current_cond= 0;
889
session->set_proc_info(0);
890
pthread_mutex_unlock(&session->mysys_var->mutex);
866
pthread_mutex_lock(&thd->mysys_var->mutex);
867
thd->mysys_var->current_mutex= 0;
868
thd->mysys_var->current_cond= 0;
869
thd->set_proc_info(0);
870
pthread_mutex_unlock(&thd->mysys_var->mutex);
898
878
Mark all temporary tables which were used by the current statement or
899
879
substatement as free for reuse, but only if the query_id can be cleared.
901
@param session thread context
881
@param thd thread context
903
883
@remark For temp tables associated with a open SQL HANDLER the query_id
904
884
is not reset until the HANDLER is closed.
907
static void mark_temp_tables_as_free_for_reuse(Session *session)
887
static void mark_temp_tables_as_free_for_reuse(THD *thd)
909
for (Table *table= session->temporary_tables ; table ; table= table->next)
889
for (Table *table= thd->temporary_tables ; table ; table= table->next)
911
if ((table->query_id == session->query_id) && ! table->open_by_handler)
891
if ((table->query_id == thd->query_id) && ! table->open_by_handler)
913
893
table->query_id= 0;
914
894
table->file->ha_reset();
957
937
Auxiliary function to close all tables in the open_tables list.
959
@param session Thread context.
939
@param thd Thread context.
961
941
@remark It should not ordinarily be called directly.
964
static void close_open_tables(Session *session)
944
static void close_open_tables(THD *thd)
966
946
bool found_old_table= 0;
968
948
safe_mutex_assert_not_owner(&LOCK_open);
970
pthread_mutex_lock(&LOCK_open);
950
VOID(pthread_mutex_lock(&LOCK_open));
972
while (session->open_tables)
973
found_old_table|= close_thread_table(session, &session->open_tables);
974
session->some_tables_deleted= 0;
952
while (thd->open_tables)
953
found_old_table|= close_thread_table(thd, &thd->open_tables);
954
thd->some_tables_deleted= 0;
976
956
/* Free tables to hold down open files */
977
957
while (open_cache.records > table_cache_size && unused_tables)
978
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
958
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
979
959
if (found_old_table)
981
961
/* Tell threads waiting for refresh that something has happened */
982
962
broadcast_refresh();
985
pthread_mutex_unlock(&LOCK_open);
965
VOID(pthread_mutex_unlock(&LOCK_open));
1004
984
leave prelocked mode if needed.
1007
void close_thread_tables(Session *session)
987
void close_thread_tables(THD *thd)
1012
We are assuming here that session->derived_tables contains ONLY derived
992
We are assuming here that thd->derived_tables contains ONLY derived
1013
993
tables for this substatement. i.e. instead of approach which uses
1014
994
query_id matching for determining which of the derived tables belong
1015
995
to this substatement we rely on the ability of substatements to
1016
save/restore session->derived_tables during their execution.
996
save/restore thd->derived_tables during their execution.
1018
998
TODO: Probably even better approach is to simply associate list of
1019
999
derived tables with (sub-)statement instead of thread and destroy
1020
1000
them at the end of its execution.
1022
if (session->derived_tables)
1002
if (thd->derived_tables)
1026
1006
Close all derived tables generated in queries like
1027
1007
SELECT * FROM (SELECT * FROM t1)
1029
for (table= session->derived_tables ; table ; table= next)
1009
for (table= thd->derived_tables ; table ; table= next)
1031
1011
next= table->next;
1032
table->free_tmp_table(session);
1012
table->free_tmp_table(thd);
1034
session->derived_tables= 0;
1014
thd->derived_tables= 0;
1038
1018
Mark all temporary tables used by this statement as free for reuse.
1040
mark_temp_tables_as_free_for_reuse(session);
1020
mark_temp_tables_as_free_for_reuse(thd);
1042
1022
Let us commit transaction for statement. Since in 5.0 we only have
1043
1023
one statement transaction and don't allow several nested statement
1046
1026
does not belong to statement for which we do close_thread_tables()).
1047
1027
TODO: This should be fixed in later releases.
1049
if (!(session->state_flags & Open_tables_state::BACKUPS_AVAIL))
1029
if (!(thd->state_flags & Open_tables_state::BACKUPS_AVAIL))
1051
session->main_da.can_overwrite_status= true;
1052
ha_autocommit_or_rollback(session, session->is_error());
1053
session->main_da.can_overwrite_status= false;
1054
session->transaction.stmt.reset();
1031
thd->main_da.can_overwrite_status= true;
1032
ha_autocommit_or_rollback(thd, thd->is_error());
1033
thd->main_da.can_overwrite_status= false;
1034
thd->transaction.stmt.reset();
1057
if (session->locked_tables)
1037
if (thd->locked_tables)
1060
1040
/* Ensure we are calling ha_reset() for all used tables */
1061
mark_used_tables_as_free_for_reuse(session, session->open_tables);
1041
mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
1064
1044
We are under simple LOCK TABLES so should not do anything else.
1077
1057
handled either before writing a query log event (inside
1078
1058
binlog_query()) or when preparing a pending event.
1080
session->binlog_flush_pending_rows_event(true);
1081
mysql_unlock_tables(session, session->lock);
1060
thd->binlog_flush_pending_rows_event(true);
1061
mysql_unlock_tables(thd, thd->lock);
1085
1065
Note that we need to hold LOCK_open while changing the
1151
1131
creates one DROP TEMPORARY Table binlog event for each pseudo-thread
1154
void close_temporary_tables(Session *session)
1134
void close_temporary_tables(THD *thd)
1157
1137
Table *next= NULL;
1158
1138
Table *prev_table;
1159
/* Assume session->options has OPTION_QUOTE_SHOW_CREATE */
1139
/* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
1160
1140
bool was_quote_show= true;
1162
if (!session->temporary_tables)
1142
if (!thd->temporary_tables)
1165
if (!mysql_bin_log.is_open() || session->current_stmt_binlog_row_based)
1145
if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
1167
1147
Table *tmp_next;
1168
for (table= session->temporary_tables; table; table= tmp_next)
1148
for (table= thd->temporary_tables; table; table= tmp_next)
1170
1150
tmp_next= table->next;
1171
1151
close_temporary(table, 1, 1);
1173
session->temporary_tables= 0;
1153
thd->temporary_tables= 0;
1177
1157
/* Better add "if exists", in case a RESET MASTER has been done */
1178
1158
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1179
uint32_t stub_len= sizeof(stub) - 1;
1159
uint stub_len= sizeof(stub) - 1;
1181
1161
String s_query= String(buf, sizeof(buf), system_charset_info);
1182
1162
bool found_user_tables= false;
1198
1178
if (!found_user_tables)
1199
1179
found_user_tables= true;
1200
for (prev_sorted= NULL, sorted= session->temporary_tables; sorted != table;
1180
for (prev_sorted= NULL, sorted= thd->temporary_tables; sorted != table;
1201
1181
prev_sorted= sorted, sorted= sorted->next)
1203
1183
if (!is_user_table(sorted) ||
1204
tmpkeyval(session, sorted) > tmpkeyval(session, table))
1184
tmpkeyval(thd, sorted) > tmpkeyval(thd, table))
1206
1186
/* move into the sorted part of the list from the unsorted */
1207
1187
prev_table->next= table->next;
1224
1204
/* We always quote db,table names though it is slight overkill */
1225
1205
if (found_user_tables &&
1226
!(was_quote_show= test(session->options & OPTION_QUOTE_SHOW_CREATE)))
1206
!(was_quote_show= test(thd->options & OPTION_QUOTE_SHOW_CREATE)))
1228
session->options |= OPTION_QUOTE_SHOW_CREATE;
1208
thd->options |= OPTION_QUOTE_SHOW_CREATE;
1231
1211
/* scan sorted tmps to generate sequence of DROP */
1232
for (table= session->temporary_tables; table; table= next)
1212
for (table= thd->temporary_tables; table; table= next)
1234
1214
if (is_user_table(table))
1236
my_thread_id save_pseudo_thread_id= session->variables.pseudo_thread_id;
1216
my_thread_id save_pseudo_thread_id= thd->variables.pseudo_thread_id;
1237
1217
/* Set pseudo_thread_id to be that of the processed table */
1238
session->variables.pseudo_thread_id= tmpkeyval(session, table);
1218
thd->variables.pseudo_thread_id= tmpkeyval(thd, table);
1240
1220
Loop forward through all tables within the sublist of
1241
1221
common pseudo_thread_id to create single DROP query.
1243
1223
for (s_query.length(stub_len);
1244
1224
table && is_user_table(table) &&
1245
tmpkeyval(session, table) == session->variables.pseudo_thread_id;
1225
tmpkeyval(thd, table) == thd->variables.pseudo_thread_id;
1249
1229
We are going to add 4 ` around the db/table names and possible more
1250
1230
due to special characters in the names
1252
append_identifier(session, &s_query, table->s->db.str, strlen(table->s->db.str));
1232
append_identifier(thd, &s_query, table->s->db.str, strlen(table->s->db.str));
1253
1233
s_query.append('.');
1254
append_identifier(session, &s_query, table->s->table_name.str,
1234
append_identifier(thd, &s_query, table->s->table_name.str,
1255
1235
strlen(table->s->table_name.str));
1256
1236
s_query.append(',');
1257
1237
next= table->next;
1258
1238
close_temporary(table, 1, 1);
1260
session->clear_error();
1261
const CHARSET_INFO * const cs_save= session->variables.character_set_client;
1262
session->variables.character_set_client= system_charset_info;
1263
Query_log_event qinfo(session, s_query.ptr(),
1241
const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1242
thd->variables.character_set_client= system_charset_info;
1243
Query_log_event qinfo(thd, s_query.ptr(),
1264
1244
s_query.length() - 1 /* to remove trailing ',' */,
1266
session->variables.character_set_client= cs_save;
1246
thd->variables.character_set_client= cs_save;
1268
1248
Imagine the thread had created a temp table, then was doing a
1269
1249
SELECT, and the SELECT was killed. Then it's not clever to
1332
session thread handle
1333
1313
table table which should be checked
1334
1314
table_list list of tables
1335
1315
check_alias whether to check tables' aliases
1337
1317
NOTE: to exclude derived tables from check we use following mechanism:
1338
a) during derived table processing set Session::derived_tables_processing
1318
a) during derived table processing set THD::derived_tables_processing
1339
1319
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
1340
Session::derived_tables_processing set. (we can't use JOIN::execute
1320
THD::derived_tables_processing set. (we can't use JOIN::execute
1341
1321
because for PS we perform only JOIN::prepare, but we can't set this
1342
1322
flag in JOIN::prepare if we are not sure that we are in derived table
1343
1323
processing loop, because multi-update call fix_fields() for some its
1397
1377
if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) &&
1398
(! (res= mysql_lock_have_duplicate(session, table, table_list)))) ||
1378
(! (res= mysql_lock_have_duplicate(thd, table, table_list)))) ||
1399
1379
((!res->table || res->table != table->table) &&
1400
1380
(!check_alias || !(lower_case_table_names ?
1401
1381
my_strcasecmp(files_charset_info, t_alias, res->alias) :
1438
Table *find_temporary_table(Session *session, const char *db, const char *table_name)
1418
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
1440
1420
TableList table_list;
1442
1422
table_list.db= (char*) db;
1443
1423
table_list.table_name= (char*) table_name;
1444
return find_temporary_table(session, &table_list);
1424
return find_temporary_table(thd, &table_list);
1448
Table *find_temporary_table(Session *session, TableList *table_list)
1428
Table *find_temporary_table(THD *thd, TableList *table_list)
1450
1430
char key[MAX_DBKEY_LENGTH];
1451
1431
uint key_length;
1454
key_length= create_table_def_key(session, key, table_list, 1);
1455
for (table=session->temporary_tables ; table ; table= table->next)
1434
key_length= create_table_def_key(thd, key, table_list, 1);
1435
for (table=thd->temporary_tables ; table ; table= table->next)
1457
1437
if (table->s->table_cache_key.length == key_length &&
1458
1438
!memcmp(table->s->table_cache_key.str, key, key_length))
1466
1446
Drop a temporary table.
1468
Try to locate the table in the list of session->temporary_tables.
1448
Try to locate the table in the list of thd->temporary_tables.
1469
1449
If the table is found:
1470
1450
- if the table is being used by some outer statement, fail.
1471
- if the table is in session->locked_tables, unlock it and
1451
- if the table is in thd->locked_tables, unlock it and
1472
1452
remove it from the list of locked tables. Currently only transactional
1473
1453
temporary tables are present in the locked_tables list.
1474
1454
- Close the temporary table, remove its .FRM
1479
1459
or ALTER Table. Even though part of the work done by this function
1480
1460
is redundant when the table is internal, as long as we
1481
1461
link both internal and user temporary tables into the same
1482
session->temporary_tables list, it's impossible to tell here whether
1462
thd->temporary_tables list, it's impossible to tell here whether
1483
1463
we're dealing with an internal or a user temporary table.
1485
1465
@retval 0 the table was found and dropped successfully.
1488
1468
@retval -1 the table is in use by a outer query
1491
int drop_temporary_table(Session *session, TableList *table_list)
1471
int drop_temporary_table(THD *thd, TableList *table_list)
1495
if (!(table= find_temporary_table(session, table_list)))
1475
if (!(table= find_temporary_table(thd, table_list)))
1498
1478
/* Table might be in use by some outer statement. */
1499
if (table->query_id && table->query_id != session->query_id)
1479
if (table->query_id && table->query_id != thd->query_id)
1501
1481
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1506
1486
If LOCK TABLES list is not empty and contains this table,
1507
1487
unlock the table and remove the table from this list.
1509
mysql_lock_remove(session, session->locked_tables, table, false);
1510
close_temporary_table(session, table, 1, 1);
1489
mysql_lock_remove(thd, thd->locked_tables, table, false);
1490
close_temporary_table(thd, table, 1, 1);
1515
unlink from session->temporary tables and close temporary table
1495
unlink from thd->temporary tables and close temporary table
1518
void close_temporary_table(Session *session, Table *table,
1498
void close_temporary_table(THD *thd, Table *table,
1519
1499
bool free_share, bool delete_table)
1521
1501
if (table->prev)
1529
1509
/* removing the item from the list */
1530
assert(table == session->temporary_tables);
1510
assert(table == thd->temporary_tables);
1532
1512
slave must reset its temporary list pointer to zero to exclude
1533
1513
passing non-zero value to end_slave via rli->save_temporary_tables
1534
1514
when no temp tables opened, see an invariant below.
1536
session->temporary_tables= table->next;
1537
if (session->temporary_tables)
1516
thd->temporary_tables= table->next;
1517
if (thd->temporary_tables)
1538
1518
table->next->prev= 0;
1540
if (session->slave_thread)
1520
if (thd->slave_thread)
1542
1522
/* natural invariant of temporary_tables */
1543
assert(slave_open_temp_tables || !session->temporary_tables);
1523
assert(slave_open_temp_tables || !thd->temporary_tables);
1544
1524
slave_open_temp_tables--;
1546
1526
close_temporary(table, free_share, delete_table);
1583
1563
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1585
1565
Prepares a table cache key, which is the concatenation of db, table_name and
1586
session->slave_proxy_id, separated by '\0'.
1566
thd->slave_proxy_id, separated by '\0'.
1589
bool rename_temporary_table(Session* session, Table *table, const char *db,
1569
bool rename_temporary_table(THD* thd, Table *table, const char *db,
1590
1570
const char *table_name)
1593
uint32_t key_length;
1594
1574
TABLE_SHARE *share= table->s;
1595
1575
TableList table_list;
1600
1580
table_list.db= (char*) db;
1601
1581
table_list.table_name= (char*) table_name;
1602
key_length= create_table_def_key(session, key, &table_list, 1);
1582
key_length= create_table_def_key(thd, key, &table_list, 1);
1603
1583
share->set_table_cache_key(key, key_length);
1637
1617
not locked (for example already unlocked).
1640
void unlink_open_table(Session *session, Table *find, bool unlock)
1620
void unlink_open_table(THD *thd, Table *find, bool unlock)
1642
1622
char key[MAX_DBKEY_LENGTH];
1643
uint32_t key_length= find->s->table_cache_key.length;
1623
uint key_length= find->s->table_cache_key.length;
1644
1624
Table *list, **prev;
1646
1626
safe_mutex_assert_owner(&LOCK_open);
1653
1633
Closing a MERGE child before the parent would be fatal if the
1654
1634
other thread tries to abort the MERGE lock in between.
1656
for (prev= &session->open_tables; *prev; )
1636
for (prev= &thd->open_tables; *prev; )
1660
1640
if (list->s->table_cache_key.length == key_length &&
1661
1641
!memcmp(list->s->table_cache_key.str, key, key_length))
1663
if (unlock && session->locked_tables)
1664
mysql_lock_remove(session, session->locked_tables, list, true);
1643
if (unlock && thd->locked_tables)
1644
mysql_lock_remove(thd, thd->locked_tables, list, true);
1666
1646
/* Remove table from open_tables list. */
1667
1647
*prev= list->next;
1668
1648
/* Close table. */
1669
hash_delete(&open_cache,(unsigned char*) list); // Close table
1649
VOID(hash_delete(&open_cache,(uchar*) list)); // Close table
1700
1680
table that was locked with LOCK TABLES.
1703
void drop_open_table(Session *session, Table *table, const char *db_name,
1683
void drop_open_table(THD *thd, Table *table, const char *db_name,
1704
1684
const char *table_name)
1706
1686
if (table->s->tmp_table)
1707
close_temporary_table(session, table, 1, 1);
1687
close_temporary_table(thd, table, 1, 1);
1710
1690
handlerton *table_type= table->s->db_type();
1711
pthread_mutex_lock(&LOCK_open);
1691
VOID(pthread_mutex_lock(&LOCK_open));
1713
1693
unlink_open_table() also tells threads waiting for refresh or close
1714
1694
that something has happened.
1716
unlink_open_table(session, table, false);
1696
unlink_open_table(thd, table, false);
1717
1697
quick_rm_table(table_type, db_name, table_name, 0);
1718
pthread_mutex_unlock(&LOCK_open);
1698
VOID(pthread_mutex_unlock(&LOCK_open));
1727
1707
wait_for_condition()
1728
session Thread handler
1729
1709
mutex mutex that is currently hold that is associated with condition
1730
1710
Will be unlocked on return
1731
1711
cond Condition to wait for
1734
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
1714
void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond)
1736
1716
/* Wait until the current table is up to date */
1737
1717
const char *proc_info;
1738
session->mysys_var->current_mutex= mutex;
1739
session->mysys_var->current_cond= cond;
1740
proc_info=session->get_proc_info();
1741
session->set_proc_info("Waiting for table");
1742
if (!session->killed)
1718
thd->mysys_var->current_mutex= mutex;
1719
thd->mysys_var->current_cond= cond;
1720
proc_info=thd->get_proc_info();
1721
thd_proc_info(thd, "Waiting for table");
1743
1723
(void) pthread_cond_wait(cond, mutex);
1756
1736
pthread_mutex_unlock(mutex);
1757
pthread_mutex_lock(&session->mysys_var->mutex);
1758
session->mysys_var->current_mutex= 0;
1759
session->mysys_var->current_cond= 0;
1760
session->set_proc_info(proc_info);
1761
pthread_mutex_unlock(&session->mysys_var->mutex);
1737
pthread_mutex_lock(&thd->mysys_var->mutex);
1738
thd->mysys_var->current_mutex= 0;
1739
thd->mysys_var->current_cond= 0;
1740
thd_proc_info(thd, proc_info);
1741
pthread_mutex_unlock(&thd->mysys_var->mutex);
1767
1747
Exclusively name-lock a table that is already write-locked by the
1768
1748
current thread.
1770
@param session current thread context
1750
@param thd current thread context
1771
1751
@param tables table list containing one table to open.
1773
1753
@return false on success, true otherwise.
1776
bool name_lock_locked_table(Session *session, TableList *tables)
1756
bool name_lock_locked_table(THD *thd, TableList *tables)
1778
1758
/* Under LOCK TABLES we must only accept write locked tables. */
1779
tables->table= find_locked_table(session, tables->db, tables->table_name);
1759
tables->table= find_locked_table(thd, tables->db, tables->table_name);
1781
1761
if (!tables->table)
1782
1762
my_error(ER_TABLE_NOT_LOCKED, MYF(0), tables->alias);
1803
1783
reopen_name_locked_table()
1804
session Thread handle
1805
1785
table_list TableList object for table to be open, TableList::table
1806
1786
member should point to Table object which was used for
1808
1788
link_in true - if Table object for table to be opened should be
1809
linked into Session::open_tables list.
1789
linked into THD::open_tables list.
1810
1790
false - placeholder used for name-locking is already in
1811
1791
this list so we only need to preserve Table::next
1829
1809
safe_mutex_assert_owner(&LOCK_open);
1831
if (session->killed || !table)
1811
if (thd->killed || !table)
1834
1814
orig_table= *table;
1836
if (open_unireg_entry(session, table, table_list, table_name,
1816
if (open_unireg_entry(thd, table, table_list, table_name,
1837
1817
table->s->table_cache_key.str,
1838
1818
table->s->table_cache_key.length))
1858
1838
before we will get table-level lock on this table.
1860
1840
share->version=0;
1861
table->in_use = session;
1841
table->in_use = thd;
1865
table->next= session->open_tables;
1866
session->open_tables= table;
1845
table->next= thd->open_tables;
1846
thd->open_tables= table;
1871
Table object should be already in Session::open_tables list so we just
1851
Table object should be already in THD::open_tables list so we just
1872
1852
need to set Table::next correctly.
1874
1854
table->next= orig_table.next;
1877
table->tablenr=session->current_tablenr++;
1857
table->tablenr=thd->current_tablenr++;
1878
1858
table->used_fields=0;
1879
1859
table->const_table=0;
1880
1860
table->null_row= false;
1922
1902
table->s= share;
1923
1903
share->set_table_cache_key(key_buff, key, key_length);
1924
1904
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1925
table->in_use= session;
1926
1906
table->locked_by_name=1;
1928
if (my_hash_insert(&open_cache, (unsigned char*)table))
1908
if (my_hash_insert(&open_cache, (uchar*)table))
1930
free((unsigned char*) table);
1910
my_free((uchar*) table, MYF(0));
1957
1937
@retval false Success. 'table' parameter set according to above rules.
1960
bool lock_table_name_if_not_cached(Session *session, const char *db,
1940
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1961
1941
const char *table_name, Table **table)
1963
1943
char key[MAX_DBKEY_LENGTH];
1964
uint32_t key_length;
1966
key_length= (uint)(my_stpcpy(my_stpcpy(key, db) + 1, table_name) - key) + 1;
1967
pthread_mutex_lock(&LOCK_open);
1969
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1946
key_length= (uint)(stpcpy(stpcpy(key, db) + 1, table_name) - key) + 1;
1947
VOID(pthread_mutex_lock(&LOCK_open));
1949
if (hash_search(&open_cache, (uchar *)key, key_length))
1971
pthread_mutex_unlock(&LOCK_open);
1951
VOID(pthread_mutex_unlock(&LOCK_open));
1975
if (!(*table= table_cache_insert_placeholder(session, key, key_length)))
1955
if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1977
pthread_mutex_unlock(&LOCK_open);
1957
VOID(pthread_mutex_unlock(&LOCK_open));
1980
1960
(*table)->open_placeholder= 1;
1981
(*table)->next= session->open_tables;
1982
session->open_tables= *table;
1983
pthread_mutex_unlock(&LOCK_open);
1961
(*table)->next= thd->open_tables;
1962
thd->open_tables= *table;
1963
VOID(pthread_mutex_unlock(&LOCK_open));
1989
1969
Check that table exists in table definition cache, on disk
1990
1970
or in some storage engine.
1992
@param session Thread context
1972
@param thd Thread context
1993
1973
@param table Table list element
1994
1974
@param[out] exists Out parameter which is set to true if table
1995
1975
exists and to false otherwise.
2089
2069
char *alias= table_list->alias;
2090
2070
HASH_SEARCH_STATE state;
2092
/* Parsing of partitioning information from .frm needs session->lex set up. */
2093
assert(session->lex->is_lex_started);
2072
/* Parsing of partitioning information from .frm needs thd->lex set up. */
2073
assert(thd->lex->is_lex_started);
2095
2075
/* find a unused table in the open table cache */
2099
2079
/* an open table operation needs a lot of the stack space */
2100
if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
2103
if (session->killed)
2106
key_length= (create_table_def_key(session, key, table_list, 1) -
2080
if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
2086
key_length= (create_table_def_key(thd, key, table_list, 1) -
2107
2087
TMP_TABLE_KEY_EXTRA);
2152
2132
open not pre-opened tables in pre-locked/LOCK TABLES mode.
2153
2133
TODO: move this block into a separate function.
2155
if (session->locked_tables)
2135
if (thd->locked_tables)
2156
2136
{ // Using table locks
2157
2137
Table *best_table= 0;
2158
2138
int best_distance= INT_MIN;
2159
2139
bool check_if_used= false;
2160
for (table=session->open_tables; table ; table=table->next)
2140
for (table=thd->open_tables; table ; table=table->next)
2162
2142
if (table->s->table_cache_key.length == key_length &&
2163
2143
!memcmp(table->s->table_cache_key.str, key, key_length))
2165
2145
if (check_if_used && table->query_id &&
2166
table->query_id != session->query_id)
2146
table->query_id != thd->query_id)
2169
2149
If we are in stored function or trigger we should ensure that
2257
2237
and try to reopen them.
2258
2238
Note: refresh_version is currently changed only during FLUSH TABLES.
2260
if (!session->open_tables)
2261
session->version=refresh_version;
2262
else if ((session->version != refresh_version) &&
2240
if (!thd->open_tables)
2241
thd->version=refresh_version;
2242
else if ((thd->version != refresh_version) &&
2263
2243
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
2265
2245
/* Someone did a refresh while thread was opening tables */
2268
pthread_mutex_unlock(&LOCK_open);
2248
VOID(pthread_mutex_unlock(&LOCK_open));
2289
2269
an implicit "pending locks queue" - see
2290
2270
wait_for_locked_table_names for details.
2292
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
2272
for (table= (Table*) hash_first(&open_cache, (uchar*) key, key_length,
2294
2274
table && table->in_use ;
2295
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
2275
table= (Table*) hash_next(&open_cache, (uchar*) key, key_length,
2318
2298
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
2320
2300
/* Force close at once after usage */
2321
session->version= table->s->version;
2301
thd->version= table->s->version;
2325
2305
/* Avoid self-deadlocks by detecting self-dependencies. */
2326
if (table->open_placeholder && table->in_use == session)
2306
if (table->open_placeholder && table->in_use == thd)
2328
pthread_mutex_unlock(&LOCK_open);
2308
VOID(pthread_mutex_unlock(&LOCK_open));
2329
2309
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2343
2323
the earlier call to mysql_ha_flush() in this same critical
2346
close_old_data_files(session,session->open_tables,0,0);
2326
close_old_data_files(thd,thd->open_tables,0,0);
2348
2328
Back-off part 2: try to avoid "busy waiting" on the table:
2349
2329
if the table is in use by some other thread, we suspend
2359
2339
after we open first instance but before we open second
2362
if (table->in_use != session)
2342
if (table->in_use != thd)
2364
2344
/* wait_for_conditionwill unlock LOCK_open for us */
2365
wait_for_condition(session, &LOCK_open, &COND_refresh);
2345
wait_for_condition(thd, &LOCK_open, &COND_refresh);
2369
pthread_mutex_unlock(&LOCK_open);
2349
VOID(pthread_mutex_unlock(&LOCK_open));
2372
2352
There is a refresh in progress for this table.
2397
2377
/* Free cache if too big */
2398
2378
while (open_cache.records > table_cache_size && unused_tables)
2399
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
2379
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2401
2381
if (table_list->create)
2405
if (check_if_table_exists(session, table_list, &exists))
2385
if (check_if_table_exists(thd, table_list, &exists))
2407
pthread_mutex_unlock(&LOCK_open);
2387
VOID(pthread_mutex_unlock(&LOCK_open));
2414
2394
Table to be created, so we need to create placeholder in table-cache.
2416
if (!(table= table_cache_insert_placeholder(session, key, key_length)))
2396
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2418
pthread_mutex_unlock(&LOCK_open);
2398
VOID(pthread_mutex_unlock(&LOCK_open));
2424
2404
by other trying to take name-lock.
2426
2406
table->open_placeholder= 1;
2427
table->next= session->open_tables;
2428
session->open_tables= table;
2429
pthread_mutex_unlock(&LOCK_open);
2407
table->next= thd->open_tables;
2408
thd->open_tables= table;
2409
VOID(pthread_mutex_unlock(&LOCK_open));
2432
2412
/* Table exists. Let us try to open it. */
2435
2415
/* make a new table */
2436
2416
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
2438
pthread_mutex_unlock(&LOCK_open);
2418
VOID(pthread_mutex_unlock(&LOCK_open));
2442
error= open_unireg_entry(session, table, table_list, alias, key, key_length);
2422
error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
2443
2423
/* Combine the follow two */
2446
free((unsigned char*)table);
2447
pthread_mutex_unlock(&LOCK_open);
2426
my_free((uchar*)table, MYF(0));
2427
VOID(pthread_mutex_unlock(&LOCK_open));
2452
free((unsigned char*)table);
2453
pthread_mutex_unlock(&LOCK_open);
2432
my_free((uchar*)table, MYF(0));
2433
VOID(pthread_mutex_unlock(&LOCK_open));
2454
2434
return(0); // VIEW
2456
my_hash_insert(&open_cache,(unsigned char*) table);
2436
VOID(my_hash_insert(&open_cache,(uchar*) table));
2459
pthread_mutex_unlock(&LOCK_open);
2439
VOID(pthread_mutex_unlock(&LOCK_open));
2462
table->next=session->open_tables; /* Link into simple list */
2463
session->open_tables=table;
2442
table->next=thd->open_tables; /* Link into simple list */
2443
thd->open_tables=table;
2465
2445
table->reginfo.lock_type=TL_READ; /* Assume read */
2468
2448
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2470
if (session->lex->need_correct_ident())
2450
if (thd->lex->need_correct_ident())
2471
2451
table->alias_name_used= my_strcasecmp(table_alias_charset,
2472
2452
table->s->table_name.str, alias);
2473
2453
/* Fix alias if table name changes */
2474
2454
if (strcmp(table->alias, alias))
2476
uint32_t length=(uint) strlen(alias)+1;
2456
uint length=(uint) strlen(alias)+1;
2477
2457
table->alias= (char*) my_realloc((char*) table->alias, length,
2479
2459
memcpy((void*) table->alias, alias, length);
2481
2461
/* These variables are also set in reopen_table() */
2482
table->tablenr=session->current_tablenr++;
2462
table->tablenr=thd->current_tablenr++;
2483
2463
table->used_fields=0;
2484
2464
table->const_table=0;
2485
2465
table->null_row= false;
2502
Table *find_locked_table(Session *session, const char *db,const char *table_name)
2482
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
2504
2484
char key[MAX_DBKEY_LENGTH];
2505
uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
2485
uint key_length=(uint) (stpcpy(stpcpy(key,db)+1,table_name)-key)+1;
2507
for (Table *table=session->open_tables; table ; table=table->next)
2487
for (Table *table=thd->open_tables; table ; table=table->next)
2509
2489
if (table->s->table_cache_key.length == key_length &&
2510
2490
!memcmp(table->s->table_cache_key.str, key, key_length))
2552
2532
table_list.table_name= table->s->table_name.str;
2553
2533
table_list.table= table;
2555
if (wait_for_locked_table_names(session, &table_list))
2535
if (wait_for_locked_table_names(thd, &table_list))
2556
2536
return(1); // Thread was killed
2558
if (open_unireg_entry(session, &tmp, &table_list,
2538
if (open_unireg_entry(thd, &tmp, &table_list,
2560
2540
table->s->table_cache_key.str,
2561
2541
table->s->table_cache_key.length))
2628
2608
the strings are used in a loop even after the share may be freed.
2631
void close_data_files_and_morph_locks(Session *session, const char *db,
2611
void close_data_files_and_morph_locks(THD *thd, const char *db,
2632
2612
const char *table_name)
2636
2616
safe_mutex_assert_owner(&LOCK_open);
2641
2621
If we are not under LOCK TABLES we should have only one table
2642
2622
open and locked so it makes sense to remove the lock at once.
2644
mysql_unlock_tables(session, session->lock);
2624
mysql_unlock_tables(thd, thd->lock);
2650
2630
for target table name if we process ALTER Table ... RENAME.
2651
2631
So loop below makes sense even if we are not under LOCK TABLES.
2653
for (table=session->open_tables; table ; table=table->next)
2633
for (table=thd->open_tables; table ; table=table->next)
2655
2635
if (!strcmp(table->s->table_name.str, table_name) &&
2656
2636
!strcmp(table->s->db.str, db))
2658
if (session->locked_tables)
2638
if (thd->locked_tables)
2660
mysql_lock_remove(session, session->locked_tables, table, true);
2640
mysql_lock_remove(thd, thd->locked_tables, table, true);
2662
2642
table->open_placeholder= 1;
2663
2643
close_handle_and_leave_table_as_lock(table);
2687
2667
@return false in case of success, true - otherwise.
2690
bool reopen_tables(Session *session, bool get_locks, bool mark_share_as_old)
2670
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2692
2672
Table *table,*next,**prev;
2693
2673
Table **tables,**tables_ptr; // For locks
2694
2674
bool error=0, not_used;
2695
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2675
const uint flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2696
2676
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2697
2677
DRIZZLE_LOCK_IGNORE_FLUSH;
2699
if (!session->open_tables)
2679
if (!thd->open_tables)
2702
2682
safe_mutex_assert_owner(&LOCK_open);
2706
2686
The ptr is checked later
2707
2687
Do not handle locks of MERGE children.
2710
for (table= session->open_tables; table ; table=table->next)
2690
for (table= thd->open_tables; table ; table=table->next)
2712
2692
tables= (Table**) my_alloca(sizeof(Table*)*opens);
2715
tables= &session->open_tables;
2695
tables= &thd->open_tables;
2716
2696
tables_ptr =tables;
2718
prev= &session->open_tables;
2719
for (table=session->open_tables; table ; table=next)
2698
prev= &thd->open_tables;
2699
for (table=thd->open_tables; table ; table=next)
2721
uint32_t db_stat=table->db_stat;
2701
uint db_stat=table->db_stat;
2722
2702
next=table->next;
2723
2703
if (!tables || (!db_stat && reopen_table(table)))
2725
2705
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2726
hash_delete(&open_cache,(unsigned char*) table);
2706
VOID(hash_delete(&open_cache,(uchar*) table));
2749
2729
wait_for_tables() as it tries to acquire LOCK_open, which is
2750
2730
already locked.
2752
session->some_tables_deleted=0;
2753
if ((lock= mysql_lock_tables(session, tables, (uint) (tables_ptr - tables),
2732
thd->some_tables_deleted=0;
2733
if ((lock= mysql_lock_tables(thd, tables, (uint) (tables_ptr - tables),
2754
2734
flags, ¬_used)))
2756
session->locked_tables=mysql_lock_merge(session->locked_tables,lock);
2736
thd->locked_tables=mysql_lock_merge(thd->locked_tables,lock);
2779
2759
Close handlers for tables in list, but leave the Table structure
2780
2760
intact so that we can re-open these quickly.
2782
@param session Thread context
2762
@param thd Thread context
2783
2763
@param table Head of the list of Table objects
2784
2764
@param morph_locks true - remove locks which we have on tables being closed
2785
2765
but ensure that no DML or DDL will sneak in before
2815
2795
lock on it. This will also give them a chance to close their
2816
2796
instances of this table.
2818
mysql_lock_abort(session, ulcktbl, true);
2819
mysql_lock_remove(session, session->locked_tables, ulcktbl, true);
2798
mysql_lock_abort(thd, ulcktbl, true);
2799
mysql_lock_remove(thd, thd->locked_tables, ulcktbl, true);
2820
2800
ulcktbl->lock_count= 0;
2822
2802
if ((ulcktbl != table) && ulcktbl->db_stat)
2874
2854
char *key= table->s->table_cache_key.str;
2875
uint32_t key_length= table->s->table_cache_key.length;
2855
uint key_length= table->s->table_cache_key.length;
2877
2857
HASH_SEARCH_STATE state;
2878
for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
2858
for (Table *search= (Table*) hash_first(&open_cache, (uchar*) key,
2879
2859
key_length, &state);
2881
search= (Table*) hash_next(&open_cache, (unsigned char*) key,
2861
search= (Table*) hash_next(&open_cache, (uchar*) key,
2882
2862
key_length, &state))
2884
2864
if (search->in_use == table->in_use)
2902
2882
/* Wait until all used tables are refreshed */
2904
bool wait_for_tables(Session *session)
2884
bool wait_for_tables(THD *thd)
2908
session->set_proc_info("Waiting for tables");
2888
thd_proc_info(thd, "Waiting for tables");
2909
2889
pthread_mutex_lock(&LOCK_open);
2910
while (!session->killed)
2890
while (!thd->killed)
2912
session->some_tables_deleted=0;
2913
close_old_data_files(session,session->open_tables,0,dropping_tables != 0);
2914
mysql_ha_flush(session);
2915
if (!table_is_used(session->open_tables,1))
2892
thd->some_tables_deleted=0;
2893
close_old_data_files(thd,thd->open_tables,0,dropping_tables != 0);
2894
mysql_ha_flush(thd);
2895
if (!table_is_used(thd->open_tables,1))
2917
2897
(void) pthread_cond_wait(&COND_refresh,&LOCK_open);
2919
if (session->killed)
2920
2900
result= 1; // aborted
2923
2903
/* Now we can open all tables without any interference */
2924
session->set_proc_info("Reopen tables");
2925
session->version= refresh_version;
2926
result=reopen_tables(session,0,0);
2904
thd_proc_info(thd, "Reopen tables");
2905
thd->version= refresh_version;
2906
result=reopen_tables(thd,0,0);
2928
2908
pthread_mutex_unlock(&LOCK_open);
2929
session->set_proc_info(0);
2909
thd_proc_info(thd, 0);
2930
2910
return(result);
2958
Table *drop_locked_tables(Session *session,const char *db, const char *table_name)
2938
Table *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2960
2940
Table *table,*next,**prev, *found= 0;
2961
prev= &session->open_tables;
2941
prev= &thd->open_tables;
2964
2944
Note that we need to hold LOCK_open while changing the
2967
2947
Closing a MERGE child before the parent would be fatal if the
2968
2948
other thread tries to abort the MERGE lock in between.
2970
for (table= session->open_tables; table ; table=next)
2950
for (table= thd->open_tables; table ; table=next)
2972
2952
next=table->next;
2973
2953
if (!strcmp(table->s->table_name.str, table_name) &&
2974
2954
!strcmp(table->s->db.str, db))
2976
mysql_lock_remove(session, session->locked_tables, table, true);
2956
mysql_lock_remove(thd, thd->locked_tables, table, true);
3015
2995
other threads trying to get the lock.
3018
void abort_locked_tables(Session *session,const char *db, const char *table_name)
2998
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
3021
for (table= session->open_tables; table ; table= table->next)
3001
for (table= thd->open_tables; table ; table= table->next)
3023
3003
if (!strcmp(table->s->table_name.str, table_name) &&
3024
3004
!strcmp(table->s->db.str, db))
3026
3006
/* If MERGE child, forward lock handling to parent. */
3027
mysql_lock_abort(session, table, true);
3007
mysql_lock_abort(thd, table, true);
3107
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
3087
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
3108
3088
const char *alias,
3109
char *cache_key, uint32_t cache_key_length)
3089
char *cache_key, uint cache_key_length)
3112
3092
TABLE_SHARE *share;
3113
uint32_t discover_retry_count= 0;
3093
uint discover_retry_count= 0;
3115
3095
safe_mutex_assert_owner(&LOCK_open);
3117
if (!(share= get_table_share_with_create(session, table_list, cache_key,
3097
if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3118
3098
cache_key_length,
3119
3099
table_list->i_s_requested_object,
3123
while ((error= open_table_from_share(session, share, alias,
3103
while ((error= open_table_from_share(thd, share, alias,
3124
3104
(uint) (HA_OPEN_KEYFILE |
3125
3105
HA_OPEN_RNDFILE |
3127
3107
HA_TRY_READ_ONLY),
3128
3108
(EXTRA_RECORD),
3129
session->open_options, entry, OTM_OPEN)))
3109
thd->open_options, entry, OTM_OPEN)))
3131
3111
if (error == 7) // Table def changed
3160
3140
/* Free share and wait until it's released by all threads */
3161
3141
release_table_share(share, RELEASE_WAIT_FOR_DROP);
3162
if (!session->killed)
3164
drizzle_reset_errors(session, 1); // Clear warnings
3165
session->clear_error(); // Clear error message
3144
drizzle_reset_errors(thd, 1); // Clear warnings
3145
thd->clear_error(); // Clear error message
3170
3150
if (!entry->s || !entry->s->crashed)
3172
3152
// Code below is for repairing a crashed file
3173
if ((error= lock_table_name(session, table_list, true)))
3153
if ((error= lock_table_name(thd, table_list, true)))
3177
if (wait_for_locked_table_names(session, table_list))
3157
if (wait_for_locked_table_names(thd, table_list))
3179
unlock_table_name(session, table_list);
3159
unlock_table_name(thd, table_list);
3183
3163
pthread_mutex_unlock(&LOCK_open);
3184
session->clear_error(); // Clear error message
3164
thd->clear_error(); // Clear error message
3186
if (open_table_from_share(session, share, alias,
3166
if (open_table_from_share(thd, share, alias,
3187
3167
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3189
3169
HA_TRY_READ_ONLY),
3191
3171
ha_open_options | HA_OPEN_FOR_REPAIR,
3192
3172
entry, OTM_OPEN) || ! entry->file ||
3193
(entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
3173
(entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
3195
3175
/* Give right error message */
3196
session->clear_error();
3197
3177
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3198
3178
sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
3199
3179
share->table_name.str);
3221
3201
if (mysql_bin_log.is_open())
3223
3203
char *query, *end;
3224
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
3204
uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3225
3205
if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3227
3207
/* this DELETE FROM is needed even with row-based binlogging */
3228
end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3229
share->db.str,"`.`",share->table_name.str,"`", NULL);
3230
session->binlog_query(Session::STMT_QUERY_TYPE,
3208
end = strxmov(stpcpy(query, "DELETE FROM `"),
3209
share->db.str,"`.`",share->table_name.str,"`", NullS);
3210
thd->binlog_query(THD::STMT_QUERY_TYPE,
3231
3211
query, (ulong)(end-query), false, false);
3212
my_free(query, MYF(0));
3371
3351
result= -1; // Fatal error
3374
if (tables->lock_type != TL_UNLOCK && ! session->locked_tables)
3354
if (tables->lock_type != TL_UNLOCK && ! thd->locked_tables)
3376
3356
if (tables->lock_type == TL_WRITE_DEFAULT)
3377
tables->table->reginfo.lock_type= session->update_lock_default;
3357
tables->table->reginfo.lock_type= thd->update_lock_default;
3378
3358
else if (tables->table->s->tmp_table == NO_TMP_TABLE)
3379
3359
tables->table->reginfo.lock_type= tables->lock_type;
3383
session->set_proc_info(0);
3363
thd_proc_info(thd, 0);
3384
3364
free_root(&new_frm_mem, MYF(0)); // Free pre-alloced block
3386
3366
if (result && tables)
3389
3369
Some functions determine success as (tables->table != NULL).
3390
tables->table is in session->open_tables.
3370
tables->table is in thd->open_tables.
3392
3372
tables->table= NULL;
3507
3487
table_list->table table
3510
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type lock_type,
3511
uint32_t lock_flags)
3490
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
3516
session->set_proc_info("Opening table");
3517
session->current_tablenr= 0;
3518
while (!(table= open_table(session, table_list, &refresh, 0)) &&
3496
thd_proc_info(thd, "Opening table");
3497
thd->current_tablenr= 0;
3498
while (!(table= open_table(thd, table_list, &refresh, 0)) &&
3524
3504
table_list->lock_type= lock_type;
3525
3505
table_list->table= table;
3526
if (session->locked_tables)
3506
if (thd->locked_tables)
3528
if (check_lock_and_start_stmt(session, table, lock_type))
3508
if (check_lock_and_start_stmt(thd, table, lock_type))
3533
assert(session->lock == 0); // You must lock everything at once
3513
assert(thd->lock == 0); // You must lock everything at once
3534
3514
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3535
if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
3515
if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
3536
3516
lock_flags, &refresh)))
3541
session->set_proc_info(0);
3521
thd_proc_info(thd, 0);
3563
3543
There are two convenience functions:
3564
- simple_open_n_lock_tables(session, tables) without derived handling
3565
- open_and_lock_tables(session, tables) with derived handling
3544
- simple_open_n_lock_tables(thd, tables) without derived handling
3545
- open_and_lock_tables(thd, tables) with derived handling
3566
3546
Both inline functions call open_and_lock_tables_derived() with
3567
3547
the third argument set appropriately.
3570
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3550
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
3573
3553
bool need_reopen;
3577
if (open_tables(session, &tables, &counter, 0))
3557
if (open_tables(thd, &tables, &counter, 0))
3580
if (!lock_tables(session, tables, counter, &need_reopen))
3560
if (!lock_tables(thd, tables, counter, &need_reopen))
3582
3562
if (!need_reopen)
3584
close_tables_for_reopen(session, &tables);
3564
close_tables_for_reopen(thd, &tables);
3587
(mysql_handle_derived(session->lex, &mysql_derived_prepare) ||
3588
(session->fill_derived_tables() &&
3589
mysql_handle_derived(session->lex, &mysql_derived_filling))))
3567
(mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3568
(thd->fill_derived_tables() &&
3569
mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3590
3570
return(true); /* purecov: inspected */
3612
3592
data from the tables.
3615
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
3595
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint flags)
3618
assert(!session->fill_derived_tables());
3619
if (open_tables(session, &tables, &counter, flags) ||
3620
mysql_handle_derived(session->lex, &mysql_derived_prepare))
3598
assert(!thd->fill_derived_tables());
3599
if (open_tables(thd, &tables, &counter, flags) ||
3600
mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3621
3601
return(true); /* purecov: inspected */
3657
3637
3. Otherwise, statement-based logging is used.
3659
@param session Client thread
3639
@param thd Client thread
3660
3640
@param tables Tables involved in the query
3663
int decide_logging_format(Session *session, TableList *tables)
3643
int decide_logging_format(THD *thd, TableList *tables)
3665
if (mysql_bin_log.is_open() && (session->options & OPTION_BIN_LOG))
3645
if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3667
3647
handler::Table_flags flags_some_set= handler::Table_flags();
3668
3648
handler::Table_flags flags_all_set= ~handler::Table_flags();
3688
3668
"Statement cannot be logged to the binary log in"
3689
3669
" row-based nor statement-based format");
3691
else if (session->variables.binlog_format == BINLOG_FORMAT_STMT &&
3671
else if (thd->variables.binlog_format == BINLOG_FORMAT_STMT &&
3692
3672
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3694
3674
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3695
3675
"Statement-based format required for this statement,"
3696
3676
" but not allowed by this combination of engines");
3698
else if ((session->variables.binlog_format == BINLOG_FORMAT_ROW ||
3699
session->lex->is_stmt_unsafe()) &&
3678
else if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW ||
3679
thd->lex->is_stmt_unsafe()) &&
3700
3680
(flags_all_set & HA_BINLOG_ROW_CAPABLE) == 0)
3702
3682
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3704
3684
" but not allowed by this combination of engines");
3688
If more than one engine is involved in the statement and at
3689
least one is doing it's own logging (is *self-logging*), the
3690
statement cannot be logged atomically, so we generate an error
3691
rather than allowing the binlog to become corrupt.
3694
(flags_some_set & HA_HAS_OWN_BINLOGGING))
3696
error= ER_BINLOG_LOGGING_IMPOSSIBLE;
3697
my_error(error, MYF(0),
3698
"Statement cannot be written atomically since more"
3699
" than one engine involved and at least one engine"
3700
" is self-logging");
3751
3747
If query for which we are calling this function marked as requring
3752
3748
prelocking, this function will do implicit LOCK TABLES and change
3753
session::prelocked_mode accordingly.
3749
thd::prelocked_mode accordingly.
3760
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
3756
int lock_tables(THD *thd, TableList *tables, uint count, bool *need_reopen)
3762
3758
TableList *table;
3768
3764
*need_reopen= false;
3771
return(decide_logging_format(session, tables));
3767
return(decide_logging_format(thd, tables));
3773
if (!session->locked_tables)
3769
if (!thd->locked_tables)
3775
assert(session->lock == 0); // You must lock everything at once
3771
assert(thd->lock == 0); // You must lock everything at once
3776
3772
Table **start,**ptr;
3777
uint32_t lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
3773
uint lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
3779
if (!(ptr=start=(Table**) session->alloc(sizeof(Table*)*count)))
3775
if (!(ptr=start=(Table**) thd->alloc(sizeof(Table*)*count)))
3781
3777
for (table= tables; table; table= table->next_global)
3806
3802
table= table->next_global)
3808
3804
if (!table->placeholder() &&
3809
check_lock_and_start_stmt(session, table->table, table->lock_type))
3805
check_lock_and_start_stmt(thd, table->table, table->lock_type))
3816
return(decide_logging_format(session, tables));
3812
return(decide_logging_format(thd, tables));
3825
3821
close_tables_for_reopen()
3826
session in Thread context
3822
thd in Thread context
3827
3823
tables in/out List of tables which we were trying to open and lock
3831
void close_tables_for_reopen(Session *session, TableList **tables)
3827
void close_tables_for_reopen(THD *thd, TableList **tables)
3834
3830
If table list consists only from tables from prelocking set, table list
3835
3831
for new attempt should be empty, so we have to update list's root pointer.
3837
if (session->lex->first_not_own_table() == *tables)
3833
if (thd->lex->first_not_own_table() == *tables)
3839
session->lex->chop_off_not_own_tables();
3835
thd->lex->chop_off_not_own_tables();
3840
3836
for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
3842
close_thread_tables(session);
3838
close_thread_tables(thd);
3866
Table *open_temporary_table(Session *session, const char *path, const char *db,
3862
Table *open_temporary_table(THD *thd, const char *path, const char *db,
3867
3863
const char *table_name, bool link_in_list,
3868
3864
open_table_mode open_mode)
3870
3866
Table *tmp_table;
3871
3867
TABLE_SHARE *share;
3872
3868
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3873
uint32_t key_length;
3874
3870
TableList table_list;
3876
3872
table_list.db= (char*) db;
3877
3873
table_list.table_name= (char*) table_name;
3878
3874
/* Create the cache_key for temporary tables */
3879
key_length= create_table_def_key(session, cache_key, &table_list, 1);
3875
key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3881
3877
if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3882
3878
strlen(path)+1 + key_length,
3886
3882
share= (TABLE_SHARE*) (tmp_table+1);
3887
3883
tmp_path= (char*) (share+1);
3888
saved_cache_key= my_stpcpy(tmp_path, path)+1;
3884
saved_cache_key= stpcpy(tmp_path, path)+1;
3889
3885
memcpy(saved_cache_key, cache_key, key_length);
3891
init_tmp_table_share(session, share, saved_cache_key, key_length,
3887
init_tmp_table_share(thd, share, saved_cache_key, key_length,
3892
3888
strchr(saved_cache_key, '\0')+1, tmp_path);
3894
if (open_table_def(session, share, 0) ||
3895
open_table_from_share(session, share, table_name,
3890
if (open_table_def(thd, share, 0) ||
3891
open_table_from_share(thd, share, table_name,
3896
3892
(open_mode == OTM_ALTER) ? 0 :
3897
3893
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3924
3920
if (link_in_list)
3926
3922
/* growing temp list at the head */
3927
tmp_table->next= session->temporary_tables;
3923
tmp_table->next= thd->temporary_tables;
3928
3924
if (tmp_table->next)
3929
3925
tmp_table->next->prev= tmp_table;
3930
session->temporary_tables= tmp_table;
3931
session->temporary_tables->prev= 0;
3932
if (session->slave_thread)
3926
thd->temporary_tables= tmp_table;
3927
thd->temporary_tables->prev= 0;
3928
if (thd->slave_thread)
3933
3929
slave_open_temp_tables++;
3935
3931
tmp_table->pos_in_table_list= 0;
3946
my_stpcpy(ext= strchr(path, '\0'), reg_ext);
3942
stpcpy(ext= strchr(path, '\0'), reg_ext);
3947
3943
if (my_delete(path,MYF(0)))
3948
3944
error=1; /* purecov: inspected */
3949
3945
*ext= 0; // remove extension
3950
file= get_new_handler((TABLE_SHARE*) 0, current_session->mem_root, base);
3946
file= get_new_handler((TABLE_SHARE*) 0, current_thd->mem_root, base);
3951
3947
if (!frm_only && file && file->ha_delete_table(path))
3975
3971
#define WRONG_GRANT (Field*) -1
3977
static void update_field_dependencies(Session *session, Field *field, Table *table)
3973
static void update_field_dependencies(THD *thd, Field *field, Table *table)
3979
if (session->mark_used_columns != MARK_COLUMNS_NONE)
3975
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3981
3977
MY_BITMAP *current_bitmap, *other_bitmap;
4048
find_field_in_natural_join(Session *session, TableList *table_ref, const char *name,
4049
uint32_t length __attribute__((unused)),
4044
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4045
uint length __attribute__((unused)),
4050
4046
Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4051
4047
TableList **actual_table)
4077
4073
/* This is a base table. */
4078
4074
assert(nj_col->table_ref->table == nj_col->table_field->table);
4079
4075
found_field= nj_col->table_field;
4080
update_field_dependencies(session, found_field, nj_col->table_ref->table);
4076
update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4083
4079
*actual_table= nj_col->table_ref;
4108
find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
4109
bool allow_rowid, uint32_t *cached_field_index_ptr)
4104
find_field_in_table(THD *thd, Table *table, const char *name, uint length,
4105
bool allow_rowid, uint *cached_field_index_ptr)
4111
4107
Field **field_ptr, *field;
4112
uint32_t cached_field_index= *cached_field_index_ptr;
4108
uint cached_field_index= *cached_field_index_ptr;
4114
4110
/* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4115
4111
if (cached_field_index < table->s->fields &&
4141
4137
if (field_ptr && *field_ptr)
4143
if ((*field_ptr)->vcol_info)
4145
if (session->mark_used_columns != MARK_COLUMNS_NONE)
4147
Item *vcol_item= (*field_ptr)->vcol_info->expr_item;
4149
vcol_item->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4151
Set the virtual field for write here if
4152
1) this procedure is called for a read-only operation (SELECT), and
4153
2) the virtual column is not phycically stored in the table
4155
if ((session->mark_used_columns != MARK_COLUMNS_WRITE) &&
4156
(not (*field_ptr)->is_stored))
4157
bitmap_set_bit((*field_ptr)->table->write_set, (*field_ptr)->field_index);
4160
4139
*cached_field_index_ptr= field_ptr - table->field;
4161
4140
field= *field_ptr;
4221
find_field_in_table_ref(Session *session, TableList *table_list,
4222
const char *name, uint32_t length,
4200
find_field_in_table_ref(THD *thd, TableList *table_list,
4201
const char *name, uint length,
4223
4202
const char *item_name, const char *db_name,
4224
4203
const char *table_name, Item **ref,
4225
4204
bool check_privileges, bool allow_rowid,
4226
uint32_t *cached_field_index_ptr,
4205
uint *cached_field_index_ptr,
4227
4206
bool register_tree_change, TableList **actual_table)
4229
4208
Field *fld= NULL;
4273
4252
/* 'table_list' is a stored table. */
4274
4253
assert(table_list->table);
4275
if ((fld= find_field_in_table(session, table_list->table, name, length,
4254
if ((fld= find_field_in_table(thd, table_list->table, name, length,
4277
4256
cached_field_index_ptr)))
4278
4257
*actual_table= table_list;
4292
4271
TableList *table;
4293
4272
while ((table= it++))
4295
if ((fld= find_field_in_table_ref(session, table, name, length, item_name,
4274
if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
4296
4275
db_name, table_name, ref,
4297
4276
check_privileges, allow_rowid,
4298
4277
cached_field_index_ptr,
4307
4286
natural join, thus if the field is not qualified, we will search
4308
4287
directly the top-most NATURAL/USING join.
4310
fld= find_field_in_natural_join(session, table_list, name, length, ref,
4289
fld= find_field_in_natural_join(thd, table_list, name, length, ref,
4311
4290
register_tree_change, actual_table);
4316
if (session->mark_used_columns != MARK_COLUMNS_NONE)
4295
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4319
4298
Get rw_set correct for this field so that the handler
4432
find_field_in_tables(Session *session, Item_ident *item,
4411
find_field_in_tables(THD *thd, Item_ident *item,
4433
4412
TableList *first_table, TableList *last_table,
4434
4413
Item **ref, find_item_error_report_type report_error,
4435
4414
bool check_privileges, bool register_tree_change)
4470
4449
when table_ref->field_translation != NULL.
4472
4451
if (table_ref->table)
4473
found= find_field_in_table(session, table_ref->table, name, length,
4452
found= find_field_in_table(thd, table_ref->table, name, length,
4474
4453
true, &(item->cached_field_index));
4476
found= find_field_in_table_ref(session, table_ref, name, length, item->name,
4455
found= find_field_in_table_ref(thd, table_ref, name, length, item->name,
4477
4456
NULL, NULL, ref, check_privileges,
4478
4457
true, &(item->cached_field_index),
4479
4458
register_tree_change,
4491
SELECT_LEX *current_sel= session->lex->current_select;
4470
SELECT_LEX *current_sel= thd->lex->current_select;
4492
4471
SELECT_LEX *last_select= table_ref->select_lex;
4494
4473
If the field was an outer referencee, mark all selects using this
4495
4474
sub query as dependent on the outer query
4497
4476
if (current_sel != last_select)
4498
mark_select_range_as_dependent(session, last_select, current_sel,
4477
mark_select_range_as_dependent(thd, last_select, current_sel,
4499
4478
found, *ref, item);
4520
4499
for (; cur_table != last_table ;
4521
4500
cur_table= cur_table->next_name_resolution_table)
4523
Field *cur_field= find_field_in_table_ref(session, cur_table, name, length,
4502
Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length,
4524
4503
item->name, db, table_name, ref,
4525
(session->lex->sql_command ==
4504
(thd->lex->sql_command ==
4526
4505
SQLCOM_SHOW_FIELDS)
4527
4506
? false : check_privileges,
4534
4513
if (cur_field == WRONG_GRANT)
4536
if (session->lex->sql_command != SQLCOM_SHOW_FIELDS)
4515
if (thd->lex->sql_command != SQLCOM_SHOW_FIELDS)
4537
4516
return (Field*) 0;
4539
session->clear_error();
4540
cur_field= find_field_in_table_ref(session, cur_table, name, length,
4519
cur_field= find_field_in_table_ref(thd, cur_table, name, length,
4541
4520
item->name, db, table_name, ref,
4598
4577
char buff[NAME_LEN*2+1];
4599
4578
if (db && db[0])
4601
/* We're in an error condition, two extra strlen's aren't going
4603
assert(strlen(db) <= NAME_LEN);
4604
assert(strlen(table_name) <= NAME_LEN);
4607
strcat(buff, table_name);
4580
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
4608
4581
table_name=buff;
4610
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where);
4583
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
4614
4587
if (report_error == REPORT_ALL_ERRORS ||
4615
4588
report_error == REPORT_EXCEPT_NON_UNIQUE)
4616
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where);
4589
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
4618
4591
found= not_found_field;
4932
set_new_item_local_context(Session *session, Item_ident *item, TableList *table_ref)
4905
set_new_item_local_context(THD *thd, Item_ident *item, TableList *table_ref)
4934
4907
Name_resolution_context *context;
4935
if (!(context= new (session->mem_root) Name_resolution_context))
4908
if (!(context= new (thd->mem_root) Name_resolution_context))
4937
4910
context->init();
4938
4911
context->first_name_resolution_table=
4977
mark_common_columns(Session *session, TableList *table_ref_1, TableList *table_ref_2,
4978
List<String> *using_fields, uint32_t *found_using_fields)
4950
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4951
List<String> *using_fields, uint *found_using_fields)
4980
4953
Field_iterator_table_ref it_1, it_2;
4981
4954
Natural_join_column *nj_col_1, *nj_col_2;
5065
5038
if (nj_col_2 && (!using_fields ||is_using_column_1))
5067
Item *item_1= nj_col_1->create_item(session);
5068
Item *item_2= nj_col_2->create_item(session);
5040
Item *item_1= nj_col_1->create_item(thd);
5041
Item *item_2= nj_col_2->create_item(thd);
5069
5042
Field *field_1= nj_col_1->field();
5070
5043
Field *field_2= nj_col_2->field();
5071
5044
Item_ident *item_ident_1, *item_ident_2;
5095
5068
resolution of these items, and to enable proper name resolution of
5096
5069
the items during the execute phase of PS.
5098
if (set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref) ||
5099
set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref))
5071
if (set_new_item_local_context(thd, item_ident_1, nj_col_1->table_ref) ||
5072
set_new_item_local_context(thd, item_ident_2, nj_col_2->table_ref))
5102
5075
if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
5191
store_natural_using_join_columns(Session *session __attribute__((unused)),
5164
store_natural_using_join_columns(THD *thd __attribute__((unused)),
5192
5165
TableList *natural_using_join,
5193
5166
TableList *table_ref_1,
5194
5167
TableList *table_ref_2,
5195
5168
List<String> *using_fields,
5196
uint32_t found_using_fields)
5169
uint found_using_fields)
5198
5171
Field_iterator_table_ref it_1, it_2;
5199
5172
Natural_join_column *nj_col_1, *nj_col_2;
5355
5328
same_level_right_neighbor : right_neighbor;
5357
5330
if (cur_table_ref->nested_join &&
5358
store_top_level_join_columns(session, cur_table_ref,
5331
store_top_level_join_columns(thd, cur_table_ref,
5359
5332
real_left_neighbor, real_right_neighbor))
5361
5334
same_level_right_neighbor= cur_table_ref;
5388
5361
if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5389
5362
std::swap(table_ref_1, table_ref_2);
5390
if (mark_common_columns(session, table_ref_1, table_ref_2,
5363
if (mark_common_columns(thd, table_ref_1, table_ref_2,
5391
5364
using_fields, &found_using_fields))
5399
5372
if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5400
5373
std::swap(table_ref_1, table_ref_2);
5401
if (store_natural_using_join_columns(session, table_ref, table_ref_1,
5374
if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
5402
5375
table_ref_2, using_fields,
5403
5376
found_using_fields))
5466
static bool setup_natural_join_row_types(Session *session,
5439
static bool setup_natural_join_row_types(THD *thd,
5467
5440
List<TableList> *from_clause,
5468
5441
Name_resolution_context *context)
5470
session->where= "from clause";
5443
thd->where= "from clause";
5471
5444
if (from_clause->elements == 0)
5472
5445
return false; /* We come here in the case of UNIONs. */
5484
5457
table_ref= left_neighbor;
5485
5458
left_neighbor= table_ref_it++;
5486
if (store_top_level_join_columns(session, table_ref,
5487
left_neighbor, right_neighbor))
5459
/* For stored procedures do not redo work if already done. */
5460
if (context->select_lex->first_execution)
5491
TableList *first_leaf_on_the_right;
5492
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5493
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5462
if (store_top_level_join_columns(thd, table_ref,
5463
left_neighbor, right_neighbor))
5467
TableList *first_leaf_on_the_right;
5468
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5469
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5495
5472
right_neighbor= table_ref;
5533
5510
((Item_field*) item)->field_name[0] == '*' &&
5534
5511
!((Item_field*) item)->field)
5536
uint32_t elem= fields.elements;
5513
uint elem= fields.elements;
5537
5514
bool any_privileges= ((Item_field *) item)->any_privileges;
5538
Item_subselect *subsel= session->lex->current_select->master_unit()->item;
5515
Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
5540
5517
subsel->substype() == Item_subselect::EXISTS_SUBS)
5547
5524
it.replace(new Item_int("Not_used", (int64_t) 1,
5548
5525
MY_INT64_NUM_DECIMAL_DIGITS));
5550
else if (insert_fields(session, ((Item_field*) item)->context,
5527
else if (insert_fields(thd, ((Item_field*) item)->context,
5551
5528
((Item_field*) item)->db_name,
5552
5529
((Item_field*) item)->table_name, &it,
5553
5530
any_privileges))
5576
5553
** Check that all given fields exists and fill struct with current data
5577
5554
****************************************************************************/
5579
bool setup_fields(Session *session, Item **ref_pointer_array,
5556
bool setup_fields(THD *thd, Item **ref_pointer_array,
5580
5557
List<Item> &fields, enum_mark_columns mark_used_columns,
5581
5558
List<Item> *sum_func_list, bool allow_sum_func)
5583
5560
register Item *item;
5584
enum_mark_columns save_mark_used_columns= session->mark_used_columns;
5585
nesting_map save_allow_sum_func= session->lex->allow_sum_func;
5561
enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
5562
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
5586
5563
List_iterator<Item> it(fields);
5587
5564
bool save_is_item_list_lookup;
5589
session->mark_used_columns= mark_used_columns;
5566
thd->mark_used_columns= mark_used_columns;
5590
5567
if (allow_sum_func)
5591
session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
5592
session->where= Session::DEFAULT_WHERE;
5593
save_is_item_list_lookup= session->lex->current_select->is_item_list_lookup;
5594
session->lex->current_select->is_item_list_lookup= 0;
5568
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
5569
thd->where= THD::DEFAULT_WHERE;
5570
save_is_item_list_lookup= thd->lex->current_select->is_item_list_lookup;
5571
thd->lex->current_select->is_item_list_lookup= 0;
5597
5574
To prevent fail on forward lookup we fill it with zerows,
5608
5585
memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
5610
5587
Item **ref= ref_pointer_array;
5611
session->lex->current_select->cur_pos_in_select_list= 0;
5588
thd->lex->current_select->cur_pos_in_select_list= 0;
5612
5589
while ((item= it++))
5614
if ((!item->fixed && item->fix_fields(session, it.ref())) || (item= *(it.ref()))->check_cols(1))
5591
if ((!item->fixed && item->fix_fields(thd, it.ref())) || (item= *(it.ref()))->check_cols(1))
5616
session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5617
session->lex->allow_sum_func= save_allow_sum_func;
5618
session->mark_used_columns= save_mark_used_columns;
5593
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5594
thd->lex->allow_sum_func= save_allow_sum_func;
5595
thd->mark_used_columns= save_mark_used_columns;
5619
5596
return(true); /* purecov: inspected */
5622
5599
*(ref++)= item;
5623
5600
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
5625
item->split_sum_func(session, ref_pointer_array, *sum_func_list);
5626
session->used_tables|= item->used_tables();
5627
session->lex->current_select->cur_pos_in_select_list++;
5602
item->split_sum_func(thd, ref_pointer_array, *sum_func_list);
5603
thd->used_tables|= item->used_tables();
5604
thd->lex->current_select->cur_pos_in_select_list++;
5629
session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5630
session->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5606
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5607
thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5632
session->lex->allow_sum_func= save_allow_sum_func;
5633
session->mark_used_columns= save_mark_used_columns;
5634
return(test(session->is_error()));
5609
thd->lex->allow_sum_func= save_allow_sum_func;
5610
thd->mark_used_columns= save_mark_used_columns;
5611
return(test(thd->is_error()));
5689
bool setup_tables(Session *session, Name_resolution_context *context,
5666
bool setup_tables(THD *thd, Name_resolution_context *context,
5690
5667
List<TableList> *from_clause, TableList *tables,
5691
5668
TableList **leaves, bool select_insert)
5693
uint32_t tablenr= 0;
5695
5672
assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
5696
5673
(context->table_list && context->first_name_resolution_table));
5769
5746
TableList *leaves_tmp= NULL;
5770
5747
bool first_table= true;
5772
if (setup_tables(session, context, from_clause, tables,
5749
if (setup_tables(thd, context, from_clause, tables,
5773
5750
&leaves_tmp, select_insert))
5846
insert_fields(Session *session, Name_resolution_context *context, const char *db_name,
5823
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
5847
5824
const char *table_name, List_iterator<Item> *it,
5848
5825
bool any_privileges __attribute__((unused)))
5921
5898
/* Mark fields as used to allow storage engine to optimze access */
5922
5899
bitmap_set_bit(field->table->read_set, field->field_index);
5924
Mark virtual fields for write and others that the virtual fields
5927
if (field->vcol_info)
5929
Item *vcol_item= field->vcol_info->expr_item;
5931
vcol_item->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
5932
bitmap_set_bit(field->table->write_set, field->field_index);
5936
5902
table->covering_keys.intersect(field->part_of_key);
6005
5971
false if all is OK
6008
int setup_conds(Session *session, TableList *tables __attribute__((unused)),
5974
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
6009
5975
TableList *leaves,
6012
SELECT_LEX *select_lex= session->lex->current_select;
5978
SELECT_LEX *select_lex= thd->lex->current_select;
6013
5979
TableList *table= NULL; // For HP compilers
6014
void *save_session_marker= session->session_marker;
5980
void *save_thd_marker= thd->thd_marker;
6016
5982
it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
6017
5983
which belong to LEX, i.e. most up SELECT) will be updated by
6023
5989
bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
6024
5990
select_lex->is_item_list_lookup= 0;
6026
session->mark_used_columns= MARK_COLUMNS_READ;
5992
thd->mark_used_columns= MARK_COLUMNS_READ;
6027
5993
select_lex->cond_count= 0;
6028
5994
select_lex->between_count= 0;
6029
5995
select_lex->max_equal_elems= 0;
6031
session->session_marker= (void*)1;
5997
thd->thd_marker= (void*)1;
6034
session->where="where clause";
6035
if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
6000
thd->where="where clause";
6001
if ((!(*conds)->fixed && (*conds)->fix_fields(thd, conds)) ||
6036
6002
(*conds)->check_cols(1))
6037
6003
goto err_no_arena;
6039
session->session_marker= save_session_marker;
6005
thd->thd_marker= save_thd_marker;
6042
6008
Apply fix_fields() to all ON clauses at all levels of nesting,
6052
6018
if (embedded->on_expr)
6054
6020
/* Make a join an a expression */
6055
session->session_marker= (void*)embedded;
6056
session->where="on clause";
6057
if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(session, &embedded->on_expr)) ||
6021
thd->thd_marker= (void*)embedded;
6022
thd->where="on clause";
6023
if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(thd, &embedded->on_expr)) ||
6058
6024
embedded->on_expr->check_cols(1))
6059
6025
goto err_no_arena;
6060
6026
select_lex->cond_count++;
6065
6031
embedding->nested_join->join_list.head() == embedded);
6068
session->session_marker= save_session_marker;
6034
thd->thd_marker= save_thd_marker;
6070
session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6071
return(test(session->is_error()));
6036
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6037
return(test(thd->is_error()));
6074
6040
select_lex->is_item_list_lookup= save_is_item_list_lookup;
6106
fill_record(Session * session, List<Item> &fields, List<Item> &values, bool ignore_errors)
6072
fill_record(THD * thd, List<Item> &fields, List<Item> &values, bool ignore_errors)
6108
6074
List_iterator_fast<Item> f(fields),v(values);
6109
6075
Item *value, *fld;
6110
6076
Item_field *field;
6111
6077
Table *table= 0;
6112
List<Table> tbl_list;
6113
bool abort_on_warning_saved= session->abort_on_warning;
6117
6080
Reset the table->auto_increment_field_not_null as it is valid for
6145
6108
table= rfield->table;
6146
6109
if (rfield == table->next_number_field)
6147
6110
table->auto_increment_field_not_null= true;
6148
if (rfield->vcol_info &&
6149
value->type() != Item::DEFAULT_VALUE_ITEM &&
6150
value->type() != Item::NULL_ITEM &&
6151
table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6153
session->abort_on_warning= false;
6154
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6155
ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6156
ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6157
rfield->field_name, table->s->table_name.str);
6158
session->abort_on_warning= abort_on_warning_saved;
6160
6111
if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
6162
6113
my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
6165
tbl_list.push_back(table);
6167
/* Update virtual fields*/
6168
session->abort_on_warning= false;
6169
if (tbl_list.head())
6171
List_iterator_fast<Table> t(tbl_list);
6172
Table *prev_table= 0;
6173
while ((table= t++))
6176
Do simple optimization to prevent unnecessary re-generating
6177
values for virtual fields
6179
if (table != prev_table)
6184
if (update_virtual_fields_marked_for_write(table, false))
6190
session->abort_on_warning= abort_on_warning_saved;
6191
return(session->is_error());
6117
return(thd->is_error());
6193
session->abort_on_warning= abort_on_warning_saved;
6195
6120
table->auto_increment_field_not_null= false;
6221
fill_record(Session *session, Field **ptr, List<Item> &values,
6146
fill_record(THD *thd, Field **ptr, List<Item> &values,
6222
6147
bool ignore_errors __attribute__((unused)))
6224
6149
List_iterator_fast<Item> v(values);
6226
6151
Table *table= 0;
6228
List<Table> tbl_list;
6229
bool abort_on_warning_saved= session->abort_on_warning;
6233
6155
Reset the table->auto_increment_field_not_null as it is valid for
6242
6164
table= (*ptr)->table;
6243
6165
table->auto_increment_field_not_null= false;
6245
while ((field = *ptr++) && ! session->is_error())
6167
while ((field = *ptr++) && ! thd->is_error())
6248
6170
table= field->table;
6249
6171
if (field == table->next_number_field)
6250
6172
table->auto_increment_field_not_null= true;
6251
if (field->vcol_info &&
6252
value->type() != Item::DEFAULT_VALUE_ITEM &&
6253
value->type() != Item::NULL_ITEM &&
6254
table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6256
session->abort_on_warning= false;
6257
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6258
ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6259
ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6260
field->field_name, table->s->table_name.str);
6261
session->abort_on_warning= abort_on_warning_saved;
6263
6173
if (value->save_in_field(field, 0) < 0)
6265
tbl_list.push_back(table);
6267
/* Update virtual fields*/
6268
session->abort_on_warning= false;
6269
if (tbl_list.head())
6271
List_iterator_fast<Table> t(tbl_list);
6272
Table *prev_table= 0;
6273
while ((table= t++))
6276
Do simple optimization to prevent unnecessary re-generating
6277
values for virtual fields
6279
if (table != prev_table)
6284
if (update_virtual_fields_marked_for_write(table, false))
6292
session->abort_on_warning= abort_on_warning_saved;
6293
return(session->is_error());
6176
return(thd->is_error());
6296
session->abort_on_warning= abort_on_warning_saved;
6298
6180
table->auto_increment_field_not_null= false;
6303
6185
bool mysql_rm_tmp_tables(void)
6306
6188
char filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
6308
6190
FILEINFO *file;
6309
6191
TABLE_SHARE share;
6312
if (!(session= new Session))
6194
if (!(thd= new THD))
6314
session->thread_stack= (char*) &session;
6315
session->store_globals();
6196
thd->thread_stack= (char*) &thd;
6197
thd->store_globals();
6317
6199
for (i=0; i<=mysql_tmpdir_list.max; i++)
6335
6217
if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
6337
6219
char *ext= fn_ext(file->name);
6338
uint32_t ext_len= strlen(ext);
6339
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
6220
uint ext_len= strlen(ext);
6221
uint filePath_len= snprintf(filePath, sizeof(filePath),
6340
6222
"%s%c%s", tmpdir, FN_LIBCHAR,
6342
6224
if (!memcmp(reg_ext, ext, ext_len))
6345
6227
/* We should cut file extention before deleting of table */
6346
6228
memcpy(filePathCopy, filePath, filePath_len - ext_len);
6347
6229
filePathCopy[filePath_len - ext_len]= 0;
6348
init_tmp_table_share(session, &share, "", 0, "", filePathCopy);
6349
if (!open_table_def(session, &share, 0) &&
6350
((handler_file= get_new_handler(&share, session->mem_root,
6230
init_tmp_table_share(thd, &share, "", 0, "", filePathCopy);
6231
if (!open_table_def(thd, &share, 0) &&
6232
((handler_file= get_new_handler(&share, thd->mem_root,
6351
6233
share.db_type()))))
6353
6235
handler_file->ha_delete_table(filePathCopy);
6438
6320
1 Table is in use by another thread
6441
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
6323
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
6444
6326
char key[MAX_DBKEY_LENGTH];
6445
uint32_t key_length;
6447
6329
TABLE_SHARE *share;
6448
6330
bool result= 0, signalled= 0;
6450
key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
6332
key_length=(uint) (stpcpy(stpcpy(key,db)+1,table_name)-key)+1;
6453
6335
HASH_SEARCH_STATE state;
6454
6336
result= signalled= 0;
6456
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
6338
for (table= (Table*) hash_first(&open_cache, (uchar*) key, key_length,
6459
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
6341
table= (Table*) hash_next(&open_cache, (uchar*) key, key_length,
6464
6346
table->s->version=0L; /* Free when thread is ready */
6465
6347
if (!(in_use=table->in_use))
6467
6349
relink_unused(table);
6469
else if (in_use != session)
6351
else if (in_use != thd)
6472
6354
Mark that table is going to be deleted from cache. This will
6487
6369
open_tables list. Aborting the MERGE lock after a child was
6488
6370
closed and before the parent is closed would be fatal.
6490
for (Table *session_table= in_use->open_tables;
6492
session_table= session_table->next)
6372
for (Table *thd_table= in_use->open_tables;
6374
thd_table= thd_table->next)
6494
6376
/* Do not handle locks of MERGE children. */
6495
if (session_table->db_stat) // If table is open
6496
signalled|= mysql_lock_abort_for_thread(session, session_table);
6377
if (thd_table->db_stat) // If table is open
6378
signalled|= mysql_lock_abort_for_thread(thd, thd_table);
6500
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
6382
result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
6502
6384
while (unused_tables && !unused_tables->s->version)
6503
hash_delete(&open_cache,(unsigned char*) unused_tables);
6385
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6505
6387
/* Remove table from table definition cache if it's not in use */
6506
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
6388
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6509
6391
share->version= 0; // Mark for delete
6510
6392
if (share->ref_count == 0)
6512
6394
pthread_mutex_lock(&share->mutex);
6513
hash_delete(&table_def_cache, (unsigned char*) share);
6395
VOID(hash_delete(&table_def_cache, (uchar*) share));