18
18
#include <drizzled/server_includes.h>
19
19
#include <storage/myisam/myisam.h>
20
20
#include <drizzled/sql_show.h>
21
#include <drizzled/error.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/data_home.h>
24
#include <drizzled/sql_parse.h>
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
26
24
int creating_table= 0; // How many mysql_create_table are running
28
const char * primary_key_name="PRIMARY";
26
const char *primary_key_name="PRIMARY";
30
28
static bool check_if_keyname_exists(const char *name,KEY *start, KEY *end);
31
29
static char *make_unique_key_name(const char *field_name,KEY *start,KEY *end);
32
30
static int copy_data_between_tables(Table *from,Table *to,
33
31
List<Create_field> &create, bool ignore,
34
uint32_t order_num, order_st *order,
32
uint order_num, order_st *order,
35
33
ha_rows *copied,ha_rows *deleted,
36
34
enum enum_enable_or_disable keys_onoff,
37
35
bool error_if_not_empty);
39
static bool prepare_blob_field(Session *session, Create_field *sql_field);
40
static bool check_engine(Session *, const char *, HA_CREATE_INFO *);
37
static bool prepare_blob_field(THD *thd, Create_field *sql_field);
38
static bool check_engine(THD *, const char *, HA_CREATE_INFO *);
42
mysql_prepare_create_table(Session *session, HA_CREATE_INFO *create_info,
40
mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
43
41
Alter_info *alter_info,
46
44
handler *file, KEY **key_info_buffer,
47
uint32_t *key_count, int select_field_count);
45
uint *key_count, int select_field_count);
49
mysql_prepare_alter_table(Session *session, Table *table,
47
mysql_prepare_alter_table(THD *thd, Table *table,
50
48
HA_CREATE_INFO *create_info,
51
49
Alter_info *alter_info);
65
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length)
63
uint filename_to_tablename(const char *from, char *to, uint to_length)
70
68
if (!memcmp(from, tmp_file_prefix, tmp_file_prefix_length))
72
70
/* Temporary table name. */
73
res= (my_stpncpy(to, from, to_length) - to);
71
res= (stpncpy(to, from, to_length) - to);
78
76
system_charset_info, to, to_length, &errors);
79
77
if (errors) // Old 5.0 name
81
strcpy(to, MYSQL50_TABLE_NAME_PREFIX);
82
strncat(to, from, to_length-MYSQL50_TABLE_NAME_PREFIX_LENGTH-1);
79
res= (strxnmov(to, to_length, MYSQL50_TABLE_NAME_PREFIX, from, NullS) -
84
81
sql_print_error(_("Invalid (old?) table or database name '%s'"), from);
104
uint32_t tablename_to_filename(const char *from, char *to, uint32_t to_length)
102
uint tablename_to_filename(const char *from, char *to, uint to_length)
106
uint32_t errors, length;
108
106
if (from[0] == '#' && !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
109
107
MYSQL50_TABLE_NAME_PREFIX_LENGTH))
158
uint32_t build_table_filename(char *buff, size_t bufflen, const char *db,
159
const char *table_name, const char *ext, uint32_t flags)
156
uint build_table_filename(char *buff, size_t bufflen, const char *db,
157
const char *table_name, const char *ext, uint flags)
161
159
char dbbuff[FN_REFLEN];
162
160
char tbbuff[FN_REFLEN];
164
162
if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
165
my_stpncpy(tbbuff, table_name, sizeof(tbbuff));
163
stpncpy(tbbuff, table_name, sizeof(tbbuff));
167
tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
165
VOID(tablename_to_filename(table_name, tbbuff, sizeof(tbbuff)));
169
tablename_to_filename(db, dbbuff, sizeof(dbbuff));
167
VOID(tablename_to_filename(db, dbbuff, sizeof(dbbuff)));
171
169
char *end = buff + bufflen;
172
170
/* Don't add FN_ROOTDIR if mysql_data_home already includes it */
173
char *pos = my_stpncpy(buff, mysql_data_home, bufflen);
171
char *pos = stpncpy(buff, mysql_data_home, bufflen);
174
172
int rootdir_len= strlen(FN_ROOTDIR);
175
173
if (pos - rootdir_len >= buff &&
176
174
memcmp(pos - rootdir_len, FN_ROOTDIR, rootdir_len) != 0)
177
pos= my_stpncpy(pos, FN_ROOTDIR, end - pos);
178
pos= my_stpncpy(pos, dbbuff, end-pos);
179
pos= my_stpncpy(pos, FN_ROOTDIR, end-pos);
175
pos= stpncpy(pos, FN_ROOTDIR, end - pos);
176
pos= strxnmov(pos, end - pos, dbbuff, FN_ROOTDIR, NullS);
180
177
#ifdef USE_SYMDIR
181
178
unpack_dirname(buff, buff);
182
179
pos= strend(buff);
184
pos= my_stpncpy(pos, tbbuff, end - pos);
185
pos= my_stpncpy(pos, ext, end - pos);
181
pos= strxnmov(pos, end - pos, tbbuff, ext, NullS);
187
183
return(pos - buff);
209
uint32_t build_tmptable_filename(Session* session, char *buff, size_t bufflen)
205
uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
212
char *p= my_stpncpy(buff, mysql_tmpdir, bufflen);
213
snprintf(p, bufflen - (p - buff), "/%s%lx_%"PRIx64"_%x%s",
208
char *p= stpncpy(buff, mysql_tmpdir, bufflen);
209
snprintf(p, bufflen - (p - buff), "/%s%lx_%lx_%x%s",
214
210
tmp_file_prefix, current_pid,
215
session->thread_id, session->tmp_table++, reg_ext);
211
thd->thread_id, thd->tmp_table++, reg_ext);
217
213
if (lower_case_table_names)
243
void write_bin_log(Session *session, bool clear_error,
239
void write_bin_log(THD *thd, bool clear_error,
244
240
char const *query, ulong query_length)
246
242
if (mysql_bin_log.is_open())
249
session->clear_error();
250
session->binlog_query(Session::STMT_QUERY_TYPE,
246
thd->binlog_query(THD::STMT_QUERY_TYPE,
251
247
query, query_length, false, false);
301
297
LOCK_open during wait_if_global_read_lock(), other threads could not
302
298
close their tables. This would make a pretty deadlock.
304
error= mysql_rm_table_part2(session, tables, if_exists, drop_temporary, 0, 0);
300
error= mysql_rm_table_part2(thd, tables, if_exists, drop_temporary, 0, 0);
306
302
if (need_start_waiting)
307
start_waiting_global_read_lock(session);
303
start_waiting_global_read_lock(thd);
342
338
-1 Thread was killed
345
int mysql_rm_table_part2(Session *session, TableList *tables, bool if_exists,
341
int mysql_rm_table_part2(THD *thd, TableList *tables, bool if_exists,
346
342
bool drop_temporary, bool drop_view,
347
343
bool dont_log_query)
349
345
TableList *table;
350
346
char path[FN_REFLEN], *alias;
351
uint32_t path_length;
352
348
String wrong_tables;
354
350
int non_temp_tables_count= 0;
355
351
bool some_tables_deleted=0, tmp_table_deleted=0, foreign_key_error=0;
356
352
String built_query;
358
if (session->current_stmt_binlog_row_based && !dont_log_query)
354
if (thd->current_stmt_binlog_row_based && !dont_log_query)
360
356
built_query.set_charset(system_charset_info);
382
378
table->db_type= share->db_type();
385
if (!drop_temporary && lock_table_names_exclusively(session, tables))
381
if (!drop_temporary && lock_table_names_exclusively(thd, tables))
387
383
pthread_mutex_unlock(&LOCK_open);
391
387
/* Don't give warnings for not found errors, as we already generate notes */
392
session->no_warnings_for_error= 1;
388
thd->no_warnings_for_error= 1;
394
390
for (table= tables; table; table= table->next_local)
419
416
being built. The string always end in a comma and the comma
420
417
will be chopped off before being written to the binary log.
422
if (session->current_stmt_binlog_row_based && !dont_log_query)
419
if (thd->current_stmt_binlog_row_based && !dont_log_query)
424
421
non_temp_tables_count++;
426
423
Don't write the database name if it is the current one (or if
427
session->db is NULL).
429
426
built_query.append("`");
430
if (session->db == NULL || strcmp(db,session->db) != 0)
427
if (thd->db == NULL || strcmp(db,thd->db) != 0)
432
429
built_query.append(db);
433
430
built_query.append("`.`");
441
438
if (!drop_temporary)
443
440
Table *locked_table;
444
abort_locked_tables(session, db, table->table_name);
445
remove_table_from_cache(session, db, table->table_name,
441
abort_locked_tables(thd, db, table->table_name);
442
remove_table_from_cache(thd, db, table->table_name,
446
443
RTFC_WAIT_OTHER_THREAD_FLAG |
447
444
RTFC_CHECK_KILLED_FLAG);
449
446
If the table was used in lock tables, remember it so that
450
447
unlock_table_names can free it
452
if ((locked_table= drop_locked_tables(session, db, table->table_name)))
449
if ((locked_table= drop_locked_tables(thd, db, table->table_name)))
453
450
table->table= locked_table;
458
455
goto err_with_placeholders;
466
463
if (drop_temporary ||
467
((table_type == NULL && (access(path, F_OK) && ha_create_table_from_engine(session, db, alias))) ||
468
(!drop_view && mysql_frm_type(session, path, &frm_db_type) != true)))
464
((table_type == NULL && (access(path, F_OK) && ha_create_table_from_engine(thd, db, alias))) ||
465
(!drop_view && mysql_frm_type(thd, path, &frm_db_type) != true)))
470
467
// Table was not found on disk and table can't be created from engine
472
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
469
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
473
470
ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
474
471
table->table_name);
481
478
if (table_type == NULL)
483
mysql_frm_type(session, path, &frm_db_type);
484
table_type= ha_resolve_by_legacy_type(session, frm_db_type);
480
mysql_frm_type(thd, path, &frm_db_type);
481
table_type= ha_resolve_by_legacy_type(thd, frm_db_type);
486
483
// Remove extension for delete
487
484
*(end= path + path_length - reg_ext_length)= '\0';
488
error= ha_delete_table(session, table_type, path, db, table->table_name,
485
error= ha_delete_table(thd, table_type, path, db, table->table_name,
489
486
!dont_log_query);
490
487
if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) &&
491
488
(if_exists || table_type == NULL))
494
session->clear_error();
496
493
if (error == HA_ERR_ROW_IS_REFERENCED)
551
548
tables). In this case, we can write the original query into
554
write_bin_log(session, !error, session->query, session->query_length);
551
write_bin_log(thd, !error, thd->query, thd->query_length);
556
else if (session->current_stmt_binlog_row_based &&
553
else if (thd->current_stmt_binlog_row_based &&
557
554
non_temp_tables_count > 0 &&
558
555
tmp_table_deleted)
572
569
built_query.chop(); // Chop of the last comma
573
570
built_query.append(" /* generated by server */");
574
write_bin_log(session, !error, built_query.ptr(), built_query.length());
571
write_bin_log(thd, !error, built_query.ptr(), built_query.length());
577
574
The remaining cases are:
586
583
pthread_mutex_lock(&LOCK_open);
587
584
err_with_placeholders:
588
unlock_table_names(session, tables, (TableList*) 0);
585
unlock_table_names(thd, tables, (TableList*) 0);
589
586
pthread_mutex_unlock(&LOCK_open);
590
session->no_warnings_for_error= 0;
587
thd->no_warnings_for_error= 0;
610
607
bool quick_rm_table(handlerton *base,const char *db,
611
const char *table_name, uint32_t flags)
608
const char *table_name, uint flags)
613
610
char path[FN_REFLEN];
616
uint32_t path_length= build_table_filename(path, sizeof(path),
613
uint path_length= build_table_filename(path, sizeof(path),
617
614
db, table_name, reg_ext, flags);
618
615
if (my_delete(path,MYF(0)))
619
616
error= 1; /* purecov: inspected */
620
617
path[path_length - reg_ext_length]= '\0'; // Remove reg_ext
621
return(ha_delete_table(current_session, base, path, db, table_name, 0) ||
618
return(ha_delete_table(current_thd, base, path, db, table_name, 0) ||
734
731
uint32_t *max_length, uint32_t *tot_length)
736
733
const char **pos;
738
735
*max_length= *tot_length= 0;
739
736
for (pos= interval->type_names, len= interval->type_lengths;
740
737
*pos ; pos++, len++)
742
uint32_t length= cs->cset->numchars(cs, *pos, *pos + *len);
739
uint length= cs->cset->numchars(cs, *pos, *pos + *len);
743
740
*tot_length+= length;
744
741
set_if_bigger(*max_length, (uint32_t)length);
843
840
(sql_field->decimals << FIELDFLAG_DEC_SHIFT));
846
if (!(sql_field->flags & NOT_NULL_FLAG) ||
847
(sql_field->vcol_info)) /* Make virtual columns always allow NULL values */
843
if (!(sql_field->flags & NOT_NULL_FLAG))
848
844
sql_field->pack_flag|= FIELDFLAG_MAYBE_NULL;
849
845
if (sql_field->flags & NO_DEFAULT_VALUE_FLAG)
850
846
sql_field->pack_flag|= FIELDFLAG_NO_DEFAULT;
881
mysql_prepare_create_table(Session *session, HA_CREATE_INFO *create_info,
877
mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
882
878
Alter_info *alter_info,
884
uint32_t *db_options,
885
881
handler *file, KEY **key_info_buffer,
886
uint32_t *key_count, int select_field_count)
882
uint *key_count, int select_field_count)
888
884
const char *key_name;
889
885
Create_field *sql_field,*dup_field;
896
892
int select_field_pos,auto_increment=0;
897
893
List_iterator<Create_field> it(alter_info->create_list);
898
894
List_iterator<Create_field> it2(alter_info->create_list);
899
uint32_t total_uneven_bit_length= 0;
895
uint total_uneven_bit_length= 0;
901
897
select_field_pos= alter_info->create_list.elements - select_field_count;
902
898
null_fields=blob_columns=0;
982
978
occupied memory at the same time when we free this
983
979
sql_field -- at the end of execution.
985
interval= sql_field->interval= typelib(session->mem_root,
981
interval= sql_field->interval= typelib(thd->mem_root,
986
982
sql_field->interval_list);
987
983
List_iterator<String> int_it(sql_field->interval_list);
988
984
String conv, *tmp;
989
985
char comma_buf[4];
990
int comma_length= cs->cset->wc_mb(cs, ',', (unsigned char*) comma_buf,
991
(unsigned char*) comma_buf +
986
int comma_length= cs->cset->wc_mb(cs, ',', (uchar*) comma_buf,
992
988
sizeof(comma_buf));
993
989
assert(comma_length > 0);
994
for (uint32_t i= 0; (tmp= int_it++); i++)
990
for (uint i= 0; (tmp= int_it++); i++)
997
993
if (String::needs_conversion(tmp->length(), tmp->charset(),
1001
997
conv.copy(tmp->ptr(), tmp->length(), tmp->charset(), cs, &cnv_errs);
1002
interval->type_names[i]= strmake_root(session->mem_root, conv.ptr(),
998
interval->type_names[i]= strmake_root(thd->mem_root, conv.ptr(),
1004
1000
interval->type_lengths[i]= conv.length();
1008
1004
lengthsp= cs->cset->lengthsp(cs, interval->type_names[i],
1009
1005
interval->type_lengths[i]);
1010
1006
interval->type_lengths[i]= lengthsp;
1011
((unsigned char *)interval->type_names[i])[lengthsp]= '\0';
1007
((uchar *)interval->type_names[i])[lengthsp]= '\0';
1013
1009
sql_field->interval_list.empty(); // Don't need interval_list anymore
1131
1125
sql_field->offset= record_offset;
1132
1126
if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
1133
1127
auto_increment++;
1135
For now skip fields that are not physically stored in the database
1136
(virtual fields) and update their offset later
1137
(see the next loop).
1139
if (sql_field->is_stored)
1140
record_offset+= sql_field->pack_length;
1142
/* Update virtual fields' offset */
1144
while ((sql_field=it++))
1146
if (not sql_field->is_stored)
1148
sql_field->offset= record_offset;
1149
record_offset+= sql_field->pack_length;
1128
record_offset+= sql_field->pack_length;
1152
1130
if (timestamps_with_niladic > 1)
1180
1158
List_iterator<Key> key_iterator(alter_info->key_list);
1181
1159
List_iterator<Key> key_iterator2(alter_info->key_list);
1182
uint32_t key_parts=0, fk_key_count=0;
1160
uint key_parts=0, fk_key_count=0;
1183
1161
bool primary_key=0,unique_key=0;
1184
1162
Key *key, *key2;
1185
uint32_t tmp, key_number;
1163
uint tmp, key_number;
1186
1164
/* special marker for keys to be ignored */
1187
1165
static char ignore_key[1];
1318
1294
if (key_info->block_size)
1319
1295
key_info->flags|= HA_USES_BLOCK_SIZE;
1321
uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
1297
uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
1322
1298
key->key_create_info.comment.str,
1323
1299
key->key_create_info.comment.str +
1324
1300
key->key_create_info.comment.length,
1388
if (not sql_field->is_stored)
1390
/* Key fields must always be physically stored. */
1391
my_error(ER_KEY_BASED_ON_GENERATED_VIRTUAL_COLUMN, MYF(0));
1394
if (key->type == Key::PRIMARY && sql_field->vcol_info)
1396
my_error(ER_PRIMARY_KEY_BASED_ON_VIRTUAL_COLUMN, MYF(0));
1399
1364
if (!(sql_field->flags & NOT_NULL_FLAG))
1401
1366
if (key->type == Key::PRIMARY)
1434
1399
if ((length=column->length) > max_key_length ||
1435
1400
length > file->max_key_part_length())
1437
length=cmin(max_key_length, file->max_key_part_length());
1402
length=min(max_key_length, file->max_key_part_length());
1438
1403
if (key->type == Key::MULTIPLE)
1440
1405
/* not a critical problem */
1441
1406
char warn_buff[DRIZZLE_ERRMSG_SIZE];
1442
1407
snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1444
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1409
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1445
1410
ER_TOO_LONG_KEY, warn_buff);
1446
1411
/* Align key length to multibyte char boundary */
1447
1412
length-= length % sql_field->charset->mbmaxlen;
1480
1445
char warn_buff[DRIZZLE_ERRMSG_SIZE];
1481
1446
snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1483
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1448
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1484
1449
ER_TOO_LONG_KEY, warn_buff);
1485
1450
/* Align key length to multibyte char boundary */
1486
1451
length-= length % sql_field->charset->mbmaxlen;
1565
1530
/* Sort keys in optimized order */
1566
my_qsort((unsigned char*) *key_info_buffer, *key_count, sizeof(KEY),
1531
my_qsort((uchar*) *key_info_buffer, *key_count, sizeof(KEY),
1567
1532
(qsort_cmp) sort_keys);
1568
1533
create_info->null_bits= null_fields;
1642
Preparation of Create_field for SP function return values.
1643
Based on code used in the inner loop of mysql_prepare_create_table()
1647
sp_prepare_create_field()
1649
sql_field Field to prepare
1652
Prepares the field structures for field creation.
1656
void sp_prepare_create_field(THD *thd, Create_field *sql_field)
1658
if (sql_field->sql_type == DRIZZLE_TYPE_ENUM)
1660
uint32_t field_length, dummy;
1661
/* DRIZZLE_TYPE_ENUM */
1663
calculate_interval_lengths(sql_field->charset,
1664
sql_field->interval,
1665
&field_length, &dummy);
1666
sql_field->length= field_length;
1668
set_if_smaller(sql_field->length, MAX_FIELD_WIDTH-1);
1671
sql_field->create_length_to_internal_length();
1672
assert(sql_field->def == 0);
1673
/* Can't go wrong as sql_field->def is not defined */
1674
(void) prepare_blob_field(thd, sql_field);
1680
1682
mysql_create_table_no_lock()
1681
session Thread object
1683
1685
table_name Table name
1684
1686
create_info Create information (like MAX_ROWS)
1709
bool mysql_create_table_no_lock(Session *session,
1711
bool mysql_create_table_no_lock(THD *thd,
1710
1712
const char *db, const char *table_name,
1711
1713
HA_CREATE_INFO *create_info,
1712
1714
Alter_info *alter_info,
1713
1715
bool internal_tmp_table,
1714
uint32_t select_field_count,
1715
bool lock_open_lock)
1716
uint select_field_count)
1717
1718
char path[FN_REFLEN];
1718
uint32_t path_length;
1719
1720
const char *alias;
1720
1721
uint db_options, key_count;
1721
1722
KEY *key_info_buffer;
1731
if (check_engine(session, table_name, create_info))
1732
if (check_engine(thd, table_name, create_info))
1733
1734
db_options= create_info->table_options;
1734
1735
if (create_info->row_type == ROW_TYPE_DYNAMIC)
1735
1736
db_options|=HA_OPTION_PACK_RECORD;
1736
1737
alias= table_case_name(create_info, table_name);
1737
if (!(file= get_new_handler((TABLE_SHARE*) 0, session->mem_root,
1738
if (!(file= get_new_handler((TABLE_SHARE*) 0, thd->mem_root,
1738
1739
create_info->db_type)))
1740
1741
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(handler));
1744
set_table_default_charset(session, create_info, (char*) db);
1745
set_table_default_charset(thd, create_info, (char*) db);
1746
if (mysql_prepare_create_table(session, create_info, alter_info,
1747
if (mysql_prepare_create_table(thd, create_info, alter_info,
1747
1748
internal_tmp_table,
1748
1749
&db_options, file,
1749
1750
&key_info_buffer, &key_count,
1753
1754
/* Check if table exists */
1754
1755
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1756
path_length= build_tmptable_filename(session, path, sizeof(path));
1757
path_length= build_tmptable_filename(thd, path, sizeof(path));
1757
1758
create_info->table_options|=HA_CREATE_DELAY_KEY_WRITE;
1773
1774
/* Check if table already exists */
1774
1775
if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1775
find_temporary_table(session, db, table_name))
1776
find_temporary_table(thd, db, table_name))
1777
1778
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1779
1780
create_info->table_existed= 1; // Mark that table existed
1780
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1781
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1781
1782
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1827
1827
bool create_if_not_exists =
1828
1828
create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS;
1829
int retcode = ha_table_exists_in_engine(session, db, table_name);
1829
int retcode = ha_table_exists_in_engine(thd, db, table_name);
1830
1830
switch (retcode)
1832
1832
case HA_ERR_NO_SUCH_TABLE:
1864
1864
#endif /* HAVE_READLINK */
1866
1866
if (create_info->data_file_name)
1867
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1867
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1868
1868
"DATA DIRECTORY option ignored");
1869
1869
if (create_info->index_file_name)
1870
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1870
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1871
1871
"INDEX DIRECTORY option ignored");
1872
1872
create_info->data_file_name= create_info->index_file_name= 0;
1874
1874
create_info->table_options=db_options;
1876
1876
path[path_length - reg_ext_length]= '\0'; // Remove .frm extension
1877
if (rea_create_table(session, path, db, table_name,
1877
if (rea_create_table(thd, path, db, table_name,
1878
1878
create_info, alter_info->create_list,
1879
1879
key_count, key_info_buffer, file))
1880
1880
goto unlock_and_end;
1882
1882
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1884
1884
/* Open table and put in temporary table list */
1885
if (!(open_temporary_table(session, path, db, table_name, 1, OTM_OPEN)))
1885
if (!(open_temporary_table(thd, path, db, table_name, 1, OTM_OPEN)))
1887
1887
(void) rm_temporary_table(create_info->db_type, path, false);
1888
1888
goto unlock_and_end;
1890
session->thread_specific_used= true;
1890
thd->thread_specific_used= true;
1898
1898
Otherwise, the statement shall be binlogged.
1900
1900
if (!internal_tmp_table &&
1901
(!session->current_stmt_binlog_row_based ||
1902
(session->current_stmt_binlog_row_based &&
1901
(!thd->current_stmt_binlog_row_based ||
1902
(thd->current_stmt_binlog_row_based &&
1903
1903
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
1904
write_bin_log(session, true, session->query, session->query_length);
1904
write_bin_log(thd, true, thd->query, thd->query_length);
1906
1906
unlock_and_end:
1908
pthread_mutex_unlock(&LOCK_open);
1907
VOID(pthread_mutex_unlock(&LOCK_open));
1911
session->set_proc_info("After create");
1910
thd_proc_info(thd, "After create");
1917
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1916
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1918
1917
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1920
1919
create_info->table_existed= 1; // Mark that table existed
1926
1925
Database locking aware wrapper for mysql_create_table_no_lock(),
1929
bool mysql_create_table(Session *session, const char *db, const char *table_name,
1928
bool mysql_create_table(THD *thd, const char *db, const char *table_name,
1930
1929
HA_CREATE_INFO *create_info,
1931
1930
Alter_info *alter_info,
1932
1931
bool internal_tmp_table,
1933
uint32_t select_field_count)
1932
uint select_field_count)
1935
1934
Table *name_lock= 0;
1938
1937
/* Wait for any database locks */
1939
1938
pthread_mutex_lock(&LOCK_lock_db);
1940
while (!session->killed &&
1941
hash_search(&lock_db_cache,(unsigned char*) db, strlen(db)))
1939
while (!thd->killed &&
1940
hash_search(&lock_db_cache,(uchar*) db, strlen(db)))
1943
wait_for_condition(session, &LOCK_lock_db, &COND_refresh);
1942
wait_for_condition(thd, &LOCK_lock_db, &COND_refresh);
1944
1943
pthread_mutex_lock(&LOCK_lock_db);
1947
if (session->killed)
1949
1948
pthread_mutex_unlock(&LOCK_lock_db);
1964
1963
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1966
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1965
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1967
1966
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1969
1968
create_info->table_existed= 1;
1981
result= mysql_create_table_no_lock(session, db, table_name, create_info,
1980
result= mysql_create_table_no_lock(thd, db, table_name, create_info,
1983
1982
internal_tmp_table,
1984
select_field_count, true);
1983
select_field_count);
1989
1988
pthread_mutex_lock(&LOCK_open);
1990
unlink_open_table(session, name_lock, false);
1989
unlink_open_table(thd, name_lock, false);
1991
1990
pthread_mutex_unlock(&LOCK_open);
1993
1992
pthread_mutex_lock(&LOCK_lock_db);
2067
2066
mysql_rename_table(handlerton *base, const char *old_db,
2068
2067
const char *old_name, const char *new_db,
2069
const char *new_name, uint32_t flags)
2068
const char *new_name, uint flags)
2071
Session *session= current_session;
2070
THD *thd= current_thd;
2072
2071
char from[FN_REFLEN], to[FN_REFLEN], lc_from[FN_REFLEN], lc_to[FN_REFLEN];
2073
2072
char *from_base= from, *to_base= to;
2074
2073
char tmp_name[NAME_LEN+1];
2078
2077
file= (base == NULL ? 0 :
2079
get_new_handler((TABLE_SHARE*) 0, session->mem_root, base));
2078
get_new_handler((TABLE_SHARE*) 0, thd->mem_root, base));
2081
2080
build_table_filename(from, sizeof(from), old_db, old_name, "",
2082
2081
flags & FN_FROM_IS_TMP);
2091
2090
if (lower_case_table_names == 2 && file &&
2092
2091
!(file->ha_table_flags() & HA_FILE_BASED))
2094
my_stpcpy(tmp_name, old_name);
2093
stpcpy(tmp_name, old_name);
2095
2094
my_casedn_str(files_charset_info, tmp_name);
2096
2095
build_table_filename(lc_from, sizeof(lc_from), old_db, tmp_name, "",
2097
2096
flags & FN_FROM_IS_TMP);
2098
2097
from_base= lc_from;
2100
my_stpcpy(tmp_name, new_name);
2099
stpcpy(tmp_name, new_name);
2101
2100
my_casedn_str(files_charset_info, tmp_name);
2102
2101
build_table_filename(lc_to, sizeof(lc_to), new_db, tmp_name, "",
2103
2102
flags & FN_TO_IS_TMP);
2141
2140
Win32 clients must also have a WRITE LOCK on the table !
2144
void wait_while_table_is_used(Session *session, Table *table,
2143
void wait_while_table_is_used(THD *thd, Table *table,
2145
2144
enum ha_extra_function function)
2148
2147
safe_mutex_assert_owner(&LOCK_open);
2150
table->file->extra(function);
2149
VOID(table->file->extra(function));
2151
2150
/* Mark all tables that are in use as 'old' */
2152
mysql_lock_abort(session, table, true); /* end threads waiting on lock */
2151
mysql_lock_abort(thd, table, true); /* end threads waiting on lock */
2154
2153
/* Wait until all there are no other threads that has this table open */
2155
remove_table_from_cache(session, table->s->db.str,
2154
remove_table_from_cache(thd, table->s->db.str,
2156
2155
table->s->table_name.str,
2157
2156
RTFC_WAIT_OTHER_THREAD_FLAG);
2175
2174
Win32 clients must also have a WRITE LOCK on the table !
2178
void close_cached_table(Session *session, Table *table)
2177
void close_cached_table(THD *thd, Table *table)
2181
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
2180
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
2182
2181
/* Close lock if this is not got with LOCK TABLES */
2185
mysql_unlock_tables(session, session->lock);
2186
session->lock=0; // Start locked threads
2184
mysql_unlock_tables(thd, thd->lock);
2185
thd->lock=0; // Start locked threads
2188
2187
/* Close all copies of 'table'. This also frees all LOCK TABLES lock */
2189
unlink_open_table(session, table, true);
2188
unlink_open_table(thd, table, true);
2191
2190
/* When lock on LOCK_open is freed other threads can continue */
2192
2191
broadcast_refresh();
2196
static int send_check_errmsg(Session *session, TableList* table,
2195
static int send_check_errmsg(THD *thd, TableList* table,
2197
2196
const char* operator_name, const char* errmsg)
2200
Protocol *protocol= session->protocol;
2199
Protocol *protocol= thd->protocol;
2201
2200
protocol->prepare_for_resend();
2202
2201
protocol->store(table->alias, system_charset_info);
2203
2202
protocol->store((char*) operator_name, system_charset_info);
2204
2203
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2205
2204
protocol->store(errmsg, system_charset_info);
2206
session->clear_error();
2207
2206
if (protocol->write())
2213
static int prepare_for_repair(Session *session, TableList *table_list,
2212
static int prepare_for_repair(THD *thd, TableList *table_list,
2214
2213
HA_CHECK_OPT *check_opt)
2226
2225
if (!(table= table_list->table)) /* if open_ltable failed */
2228
2227
char key[MAX_DBKEY_LENGTH];
2229
uint32_t key_length;
2231
key_length= create_table_def_key(session, key, table_list, 0);
2230
key_length= create_table_def_key(thd, key, table_list, 0);
2232
2231
pthread_mutex_lock(&LOCK_open);
2233
if (!(share= (get_table_share(session, table_list, key, key_length, 0,
2232
if (!(share= (get_table_share(thd, table_list, key, key_length, 0,
2236
2235
pthread_mutex_unlock(&LOCK_open);
2237
2236
return(0); // Can't open frm file
2240
if (open_table_from_share(session, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2239
if (open_table_from_share(thd, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2242
2241
release_table_share(share, RELEASE_NORMAL);
2243
2242
pthread_mutex_unlock(&LOCK_open);
2278
2277
goto end; // No data file
2280
2279
// Name of data file
2281
strxmov(from, table->s->normalized_path.str, ext[1], NULL);
2280
strxmov(from, table->s->normalized_path.str, ext[1], NullS);
2282
2281
if (stat(from, &stat_info))
2283
2282
goto end; // Can't use USE_FRM flag
2285
snprintf(tmp, sizeof(tmp), "%s-%lx_%"PRIx64,
2286
from, current_pid, session->thread_id);
2284
snprintf(tmp, sizeof(tmp), "%s-%lx_%lx",
2285
from, current_pid, thd->thread_id);
2288
2287
/* If we could open the table, close it */
2289
2288
if (table_list->table)
2291
2290
pthread_mutex_lock(&LOCK_open);
2292
close_cached_table(session, table);
2291
close_cached_table(thd, table);
2293
2292
pthread_mutex_unlock(&LOCK_open);
2295
if (lock_and_wait_for_table_name(session,table_list))
2294
if (lock_and_wait_for_table_name(thd,table_list))
2300
2299
if (my_rename(from, tmp, MYF(MY_WME)))
2302
2301
pthread_mutex_lock(&LOCK_open);
2303
unlock_table_name(session, table_list);
2302
unlock_table_name(thd, table_list);
2304
2303
pthread_mutex_unlock(&LOCK_open);
2305
error= send_check_errmsg(session, table_list, "repair",
2304
error= send_check_errmsg(thd, table_list, "repair",
2306
2305
"Failed renaming data file");
2309
if (mysql_truncate(session, table_list, 1))
2308
if (mysql_truncate(thd, table_list, 1))
2311
2310
pthread_mutex_lock(&LOCK_open);
2312
unlock_table_name(session, table_list);
2311
unlock_table_name(thd, table_list);
2313
2312
pthread_mutex_unlock(&LOCK_open);
2314
error= send_check_errmsg(session, table_list, "repair",
2313
error= send_check_errmsg(thd, table_list, "repair",
2315
2314
"Failed generating table from .frm file");
2318
2317
if (my_rename(tmp, from, MYF(MY_WME)))
2320
2319
pthread_mutex_lock(&LOCK_open);
2321
unlock_table_name(session, table_list);
2320
unlock_table_name(thd, table_list);
2322
2321
pthread_mutex_unlock(&LOCK_open);
2323
error= send_check_errmsg(session, table_list, "repair",
2322
error= send_check_errmsg(thd, table_list, "repair",
2324
2323
"Failed restoring .MYD file");
2330
2329
to finish the repair in the handler later on.
2332
2331
pthread_mutex_lock(&LOCK_open);
2333
if (reopen_name_locked_table(session, table_list, true))
2332
if (reopen_name_locked_table(thd, table_list, true))
2335
unlock_table_name(session, table_list);
2334
unlock_table_name(thd, table_list);
2336
2335
pthread_mutex_unlock(&LOCK_open);
2337
error= send_check_errmsg(session, table_list, "repair",
2336
error= send_check_errmsg(thd, table_list, "repair",
2338
2337
"Failed to open partially repaired table");
2358
2357
true Message should be sent by caller
2359
2358
(admin operation or network communication failed)
2361
static bool mysql_admin_table(Session* session, TableList* tables,
2360
static bool mysql_admin_table(THD* thd, TableList* tables,
2362
2361
HA_CHECK_OPT* check_opt,
2363
2362
const char *operator_name,
2364
2363
thr_lock_type lock_type,
2365
2364
bool open_for_modify,
2366
2365
bool no_warnings_for_error,
2367
uint32_t extra_open_options,
2368
int (*prepare_func)(Session *, TableList *,
2366
uint extra_open_options,
2367
int (*prepare_func)(THD *, TableList *,
2369
2368
HA_CHECK_OPT *),
2370
int (handler::*operator_func)(Session *,
2369
int (handler::*operator_func)(THD *,
2371
2370
HA_CHECK_OPT *))
2373
2372
TableList *table;
2374
SELECT_LEX *select= &session->lex->select_lex;
2373
SELECT_LEX *select= &thd->lex->select_lex;
2375
2374
List<Item> field_list;
2377
Protocol *protocol= session->protocol;
2378
LEX *lex= session->lex;
2376
Protocol *protocol= thd->protocol;
2379
2378
int result_code= 0;
2380
2379
const CHARSET_INFO * const cs= system_charset_info;
2382
if (end_active_trans(session))
2381
if (end_active_trans(thd))
2384
2383
field_list.push_back(item = new Item_empty_string("Table",
2385
2384
NAME_CHAR_LEN * 2,
2403
2402
char* db = table->db;
2404
2403
bool fatal_error=0;
2406
strxmov(table_name, db, ".", table->table_name, NULL);
2407
session->open_options|= extra_open_options;
2405
strxmov(table_name, db, ".", table->table_name, NullS);
2406
thd->open_options|= extra_open_options;
2408
2407
table->lock_type= lock_type;
2409
2408
/* open only one table from local list of command */
2423
2422
lex->query_tables= table;
2424
2423
lex->query_tables_last= &table->next_global;
2425
2424
lex->query_tables_own_last= 0;
2426
session->no_warnings_for_error= no_warnings_for_error;
2425
thd->no_warnings_for_error= no_warnings_for_error;
2428
open_and_lock_tables(session, table);
2429
session->no_warnings_for_error= 0;
2427
open_and_lock_tables(thd, table);
2428
thd->no_warnings_for_error= 0;
2430
2429
table->next_global= save_next_global;
2431
2430
table->next_local= save_next_local;
2432
session->open_options&= ~extra_open_options;
2431
thd->open_options&= ~extra_open_options;
2435
2434
if (prepare_func)
2437
switch ((*prepare_func)(session, table, check_opt)) {
2436
switch ((*prepare_func)(thd, table, check_opt)) {
2438
2437
case 1: // error, message written to net
2439
ha_autocommit_or_rollback(session, 1);
2440
end_trans(session, ROLLBACK);
2441
close_thread_tables(session);
2438
ha_autocommit_or_rollback(thd, 1);
2439
end_trans(thd, ROLLBACK);
2440
close_thread_tables(thd);
2443
2442
case -1: // error, message could be written to net
2444
2443
/* purecov: begin inspected */
2460
2459
if (!table->table)
2462
if (!session->warn_list.elements)
2463
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2461
if (!thd->warn_list.elements)
2462
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2464
2463
ER_CHECK_NO_SUCH_TABLE, ER(ER_CHECK_NO_SUCH_TABLE));
2465
2464
goto send_result;
2477
2476
length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
2479
2478
protocol->store(buff, length, system_charset_info);
2480
ha_autocommit_or_rollback(session, 0);
2481
end_trans(session, COMMIT);
2482
close_thread_tables(session);
2479
ha_autocommit_or_rollback(thd, 0);
2480
end_trans(thd, COMMIT);
2481
close_thread_tables(thd);
2483
2482
lex->reset_query_tables_list(false);
2484
2483
table->table=0; // For query cache
2485
2484
if (protocol->write())
2492
2491
if (lock_type == TL_WRITE && table->table->s->version)
2494
2493
pthread_mutex_lock(&LOCK_open);
2495
const char *old_message=session->enter_cond(&COND_refresh, &LOCK_open,
2494
const char *old_message=thd->enter_cond(&COND_refresh, &LOCK_open,
2496
2495
"Waiting to get writelock");
2497
mysql_lock_abort(session,table->table, true);
2498
remove_table_from_cache(session, table->table->s->db.str,
2496
mysql_lock_abort(thd,table->table, true);
2497
remove_table_from_cache(thd, table->table->s->db.str,
2499
2498
table->table->s->table_name.str,
2500
2499
RTFC_WAIT_OTHER_THREAD_FLAG |
2501
2500
RTFC_CHECK_KILLED_FLAG);
2502
session->exit_cond(old_message);
2503
if (session->killed)
2501
thd->exit_cond(old_message);
2505
2504
open_for_modify= 0;
2526
2525
(table->table->file->ha_check_for_upgrade(check_opt) ==
2527
2526
HA_ADMIN_NEEDS_ALTER))
2529
ha_autocommit_or_rollback(session, 1);
2530
close_thread_tables(session);
2531
tmp_disable_binlog(session); // binlogging is done by caller if wanted
2532
result_code= mysql_recreate_table(session, table);
2533
reenable_binlog(session);
2528
ha_autocommit_or_rollback(thd, 1);
2529
close_thread_tables(thd);
2530
tmp_disable_binlog(thd); // binlogging is done by caller if wanted
2531
result_code= mysql_recreate_table(thd, table);
2532
reenable_binlog(thd);
2535
2534
mysql_recreate_table() can push OK or ERROR.
2536
2535
Clear 'OK' status. If there is an error, keep it:
2537
2536
we will store the error message in a result set row
2538
2537
and then clear.
2540
if (session->main_da.is_ok())
2541
session->main_da.reset_diagnostics_area();
2539
if (thd->main_da.is_ok())
2540
thd->main_da.reset_diagnostics_area();
2542
2541
goto send_result;
2546
result_code = (table->table->file->*operator_func)(session, check_opt);
2545
result_code = (table->table->file->*operator_func)(thd, check_opt);
2550
2549
lex->cleanup_after_one_table_open();
2551
session->clear_error(); // these errors shouldn't get client
2550
thd->clear_error(); // these errors shouldn't get client
2553
List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
2552
List_iterator_fast<DRIZZLE_ERROR> it(thd->warn_list);
2554
2553
DRIZZLE_ERROR *err;
2555
2554
while ((err= it++))
2576
2575
case HA_ADMIN_NOT_IMPLEMENTED:
2578
2577
char buf[ERRMSGSIZE+20];
2579
uint32_t length=snprintf(buf, ERRMSGSIZE,
2578
uint length=snprintf(buf, ERRMSGSIZE,
2580
2579
ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
2581
2580
protocol->store(STRING_WITH_LEN("note"), system_charset_info);
2582
2581
protocol->store(buf, length, system_charset_info);
2586
2585
case HA_ADMIN_NOT_BASE_TABLE:
2588
2587
char buf[ERRMSGSIZE+20];
2589
uint32_t length= snprintf(buf, ERRMSGSIZE,
2588
uint length= snprintf(buf, ERRMSGSIZE,
2590
2589
ER(ER_BAD_TABLE_ERROR), table_name);
2591
2590
protocol->store(STRING_WITH_LEN("note"), system_charset_info);
2592
2591
protocol->store(buf, length, system_charset_info);
2636
2635
"try with alter", so here we close the table, do an ALTER Table,
2637
2636
reopen the table and do ha_innobase::analyze() on it.
2639
ha_autocommit_or_rollback(session, 0);
2640
close_thread_tables(session);
2638
ha_autocommit_or_rollback(thd, 0);
2639
close_thread_tables(thd);
2641
2640
TableList *save_next_local= table->next_local,
2642
2641
*save_next_global= table->next_global;
2643
2642
table->next_local= table->next_global= 0;
2644
tmp_disable_binlog(session); // binlogging is done by caller if wanted
2645
result_code= mysql_recreate_table(session, table);
2646
reenable_binlog(session);
2643
tmp_disable_binlog(thd); // binlogging is done by caller if wanted
2644
result_code= mysql_recreate_table(thd, table);
2645
reenable_binlog(thd);
2648
2647
mysql_recreate_table() can push OK or ERROR.
2649
2648
Clear 'OK' status. If there is an error, keep it:
2650
2649
we will store the error message in a result set row
2651
2650
and then clear.
2653
if (session->main_da.is_ok())
2654
session->main_da.reset_diagnostics_area();
2655
ha_autocommit_or_rollback(session, 0);
2656
close_thread_tables(session);
2652
if (thd->main_da.is_ok())
2653
thd->main_da.reset_diagnostics_area();
2654
ha_autocommit_or_rollback(thd, 0);
2655
close_thread_tables(thd);
2657
2656
if (!result_code) // recreation went ok
2659
if ((table->table= open_ltable(session, table, lock_type, 0)) &&
2660
((result_code= table->table->file->ha_analyze(session, check_opt)) > 0))
2658
if ((table->table= open_ltable(thd, table, lock_type, 0)) &&
2659
((result_code= table->table->file->ha_analyze(thd, check_opt)) > 0))
2661
2660
result_code= 0; // analyze went ok
2663
2662
if (result_code) // either mysql_recreate_table or analyze failed
2665
assert(session->is_error());
2666
if (session->is_error())
2664
assert(thd->is_error());
2665
if (thd->is_error())
2668
const char *err_msg= session->main_da.message();
2669
if (!session->vio_ok())
2667
const char *err_msg= thd->main_da.message();
2671
sql_print_error("%s",err_msg);
2670
sql_print_error(err_msg);
2713
2712
default: // Probably HA_ADMIN_INTERNAL_ERROR
2715
2714
char buf[ERRMSGSIZE+20];
2716
uint32_t length=snprintf(buf, ERRMSGSIZE,
2715
uint length=snprintf(buf, ERRMSGSIZE,
2717
2716
_("Unknown - internal error %d during operation"),
2719
2718
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2735
2734
pthread_mutex_lock(&LOCK_open);
2736
remove_table_from_cache(session, table->table->s->db.str,
2735
remove_table_from_cache(thd, table->table->s->db.str,
2737
2736
table->table->s->table_name.str, RTFC_NO_FLAG);
2738
2737
pthread_mutex_unlock(&LOCK_open);
2742
ha_autocommit_or_rollback(session, 0);
2743
end_trans(session, COMMIT);
2744
close_thread_tables(session);
2741
ha_autocommit_or_rollback(thd, 0);
2742
end_trans(thd, COMMIT);
2743
close_thread_tables(thd);
2745
2744
table->table=0; // For query cache
2746
2745
if (protocol->write())
2754
ha_autocommit_or_rollback(session, 1);
2755
end_trans(session, ROLLBACK);
2756
close_thread_tables(session); // Shouldn't be needed
2753
ha_autocommit_or_rollback(thd, 1);
2754
end_trans(thd, ROLLBACK);
2755
close_thread_tables(thd); // Shouldn't be needed
2758
2757
table->table=0;
2763
bool mysql_repair_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2762
bool mysql_repair_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2765
return(mysql_admin_table(session, tables, check_opt,
2764
return(mysql_admin_table(thd, tables, check_opt,
2766
2765
"repair", TL_WRITE, 1,
2767
2766
test(check_opt->sql_flags & TT_USEFRM),
2768
2767
HA_OPEN_FOR_REPAIR,
2774
bool mysql_optimize_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
2773
bool mysql_optimize_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2776
return(mysql_admin_table(session, tables, check_opt,
2775
return(mysql_admin_table(thd, tables, check_opt,
2777
2776
"optimize", TL_WRITE, 1,0,0,0,
2778
2777
&handler::ha_optimize));
2809
2808
pthread_mutex_unlock(&LOCK_global_system_variables);
2810
2809
check_opt.key_cache= key_cache;
2811
return(mysql_admin_table(session, tables, &check_opt,
2810
return(mysql_admin_table(thd, tables, &check_opt,
2812
2811
"assign_to_keycache", TL_READ_NO_INSERT, 0, 0,
2813
2812
0, 0, &handler::assign_to_keycache));
2863
2862
@retval 0 success
2864
2863
@retval 1 error
2866
bool mysql_create_like_schema_frm(Session* session, TableList* schema_table,
2865
bool mysql_create_like_schema_frm(THD* thd, TableList* schema_table,
2867
2866
char *dst_path, HA_CREATE_INFO *create_info)
2869
2868
HA_CREATE_INFO local_create_info;
2870
2869
Alter_info alter_info;
2871
2870
bool tmp_table= (create_info->options & HA_LEX_CREATE_TMP_TABLE);
2872
uint32_t keys= schema_table->table->s->keys;
2873
uint32_t db_options= 0;
2871
uint keys= schema_table->table->s->keys;
2875
2874
memset(&local_create_info, 0, sizeof(local_create_info));
2876
2875
local_create_info.db_type= schema_table->table->s->db_type();
2878
2877
local_create_info.default_table_charset=default_charset_info;
2879
2878
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
2880
2879
schema_table->table->use_all_columns();
2881
if (mysql_prepare_alter_table(session, schema_table->table,
2880
if (mysql_prepare_alter_table(thd, schema_table->table,
2882
2881
&local_create_info, &alter_info))
2884
if (mysql_prepare_create_table(session, &local_create_info, &alter_info,
2883
if (mysql_prepare_create_table(thd, &local_create_info, &alter_info,
2885
2884
tmp_table, &db_options,
2886
2885
schema_table->table->file,
2887
2886
&schema_table->table->s->key_info, &keys, 0))
2889
2888
local_create_info.max_rows= 0;
2890
if (mysql_create_frm(session, dst_path, NULL, NULL,
2889
if (mysql_create_frm(thd, dst_path, NullS, NullS,
2891
2890
&local_create_info, alter_info.create_list,
2892
2891
keys, schema_table->table->s->key_info,
2893
2892
schema_table->table->file))
2914
bool mysql_create_like_table(Session* session, TableList* table, TableList* src_table,
2913
bool mysql_create_like_table(THD* thd, TableList* table, TableList* src_table,
2915
2914
HA_CREATE_INFO *create_info)
2917
2916
Table *name_lock= 0;
2918
2917
char src_path[FN_REFLEN], dst_path[FN_REFLEN];
2919
uint32_t dst_path_length;
2918
uint dst_path_length;
2920
2919
char *db= table->db;
2921
2920
char *table_name= table->table_name;
2923
2922
bool res= true;
2927
2926
By opening source table we guarantee that it exists and no concurrent
2932
2931
we ensure that our statement is properly isolated from all concurrent
2933
2932
operations which matter.
2935
if (open_tables(session, &src_table, ¬_used, 0))
2934
if (open_tables(thd, &src_table, ¬_used, 0))
2938
strxmov(src_path, src_table->table->s->path.str, reg_ext, NULL);
2937
strxmov(src_path, src_table->table->s->path.str, reg_ext, NullS);
2941
2940
Check that destination tables does not exist. Note that its name
2944
2943
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2946
if (find_temporary_table(session, db, table_name))
2945
if (find_temporary_table(thd, db, table_name))
2947
2946
goto table_exists;
2948
dst_path_length= build_tmptable_filename(session, dst_path, sizeof(dst_path));
2947
dst_path_length= build_tmptable_filename(thd, dst_path, sizeof(dst_path));
2949
2948
create_info->table_options|= HA_CREATE_DELAY_KEY_WRITE;
2953
if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
2952
if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock))
2955
2954
if (!name_lock)
2956
2955
goto table_exists;
2973
2972
Also some engines (e.g. NDB cluster) require that LOCK_open should be held
2974
2973
during the call to ha_create_table(). See bug #28614 for more info.
2976
pthread_mutex_lock(&LOCK_open);
2975
VOID(pthread_mutex_lock(&LOCK_open));
2977
2976
if (src_table->schema_table)
2979
if (mysql_create_like_schema_frm(session, src_table, dst_path, create_info))
2978
if (mysql_create_like_schema_frm(thd, src_table, dst_path, create_info))
2981
pthread_mutex_unlock(&LOCK_open);
2980
VOID(pthread_mutex_unlock(&LOCK_open));
2998
2997
and temporary tables).
3000
2999
dst_path[dst_path_length - reg_ext_length]= '\0'; // Remove .frm
3001
if (session->variables.keep_files_on_create)
3000
if (thd->variables.keep_files_on_create)
3002
3001
create_info->options|= HA_CREATE_KEEP_FILES;
3003
err= ha_create_table(session, dst_path, db, table_name, create_info, 1);
3004
pthread_mutex_unlock(&LOCK_open);
3002
err= ha_create_table(thd, dst_path, db, table_name, create_info, 1);
3003
VOID(pthread_mutex_unlock(&LOCK_open));
3006
3005
if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
3008
if (err || !open_temporary_table(session, dst_path, db, table_name, 1,
3007
if (err || !open_temporary_table(thd, dst_path, db, table_name, 1,
3011
3010
(void) rm_temporary_table(create_info->db_type,
3056
3055
of this function.
3058
3057
table->table= name_lock;
3059
pthread_mutex_lock(&LOCK_open);
3060
if (reopen_name_locked_table(session, table, false))
3058
VOID(pthread_mutex_lock(&LOCK_open));
3059
if (reopen_name_locked_table(thd, table, false))
3062
pthread_mutex_unlock(&LOCK_open);
3061
VOID(pthread_mutex_unlock(&LOCK_open));
3065
pthread_mutex_unlock(&LOCK_open);
3064
VOID(pthread_mutex_unlock(&LOCK_open));
3067
int result= store_create_info(session, table, &query,
3066
int result= store_create_info(thd, table, &query,
3070
3069
assert(result == 0); // store_create_info() always return 0
3071
write_bin_log(session, true, query.ptr(), query.length());
3070
write_bin_log(thd, true, query.ptr(), query.length());
3074
write_bin_log(session, true, session->query, session->query_length);
3073
write_bin_log(thd, true, thd->query, thd->query_length);
3077
3076
Case 3 and 4 does nothing under RBR
3081
write_bin_log(session, true, session->query, session->query_length);
3080
write_bin_log(thd, true, thd->query, thd->query_length);
3089
3088
char warn_buff[DRIZZLE_ERRMSG_SIZE];
3090
3089
snprintf(warn_buff, sizeof(warn_buff),
3091
3090
ER(ER_TABLE_EXISTS_ERROR), table_name);
3092
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3091
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3093
3092
ER_TABLE_EXISTS_ERROR,warn_buff);
3102
3101
pthread_mutex_lock(&LOCK_open);
3103
unlink_open_table(session, name_lock, false);
3102
unlink_open_table(thd, name_lock, false);
3104
3103
pthread_mutex_unlock(&LOCK_open);
3110
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
3109
bool mysql_analyze_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
3112
3111
thr_lock_type lock_type = TL_READ_NO_INSERT;
3114
return(mysql_admin_table(session, tables, check_opt,
3113
return(mysql_admin_table(thd, tables, check_opt,
3115
3114
"analyze", lock_type, 1, 0, 0, 0,
3116
3115
&handler::ha_analyze));
3120
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
3119
bool mysql_check_table(THD* thd, TableList* tables,HA_CHECK_OPT* check_opt)
3122
3121
thr_lock_type lock_type = TL_READ_NO_INSERT;
3124
return(mysql_admin_table(session, tables, check_opt,
3123
return(mysql_admin_table(thd, tables, check_opt,
3125
3124
"check", lock_type,
3126
3125
0, 0, HA_OPEN_FOR_REPAIR, 0,
3127
3126
&handler::ha_check));
3151
3150
We set this flag so that ha_innobase::open and ::external_lock() do
3152
3151
not complain when we lock the table
3154
session->tablespace_op= true;
3155
if (!(table=open_ltable(session, table_list, TL_WRITE, 0)))
3153
thd->tablespace_op= true;
3154
if (!(table=open_ltable(thd, table_list, TL_WRITE, 0)))
3157
session->tablespace_op=false;
3156
thd->tablespace_op=false;
3161
3160
error= table->file->ha_discard_or_import_tablespace(discard);
3163
session->set_proc_info("end");
3162
thd_proc_info(thd, "end");
3168
3167
/* The ALTER Table is always in its own transaction */
3169
error = ha_autocommit_or_rollback(session, 0);
3170
if (end_active_trans(session))
3168
error = ha_autocommit_or_rollback(thd, 0);
3169
if (end_active_trans(thd))
3174
write_bin_log(session, false, session->query, session->query_length);
3173
write_bin_log(thd, false, thd->query, thd->query_length);
3177
ha_autocommit_or_rollback(session, error);
3178
session->tablespace_op=false;
3176
ha_autocommit_or_rollback(thd, error);
3177
thd->tablespace_op=false;
3180
3179
if (error == 0)
3259
compare_tables(Session *session,
3258
compare_tables(THD *thd,
3261
3260
Alter_info *alter_info,
3262
3261
HA_CREATE_INFO *create_info,
3264
3263
HA_ALTER_FLAGS *alter_flags,
3265
3264
HA_ALTER_INFO *ha_alter_info,
3266
uint32_t *table_changes)
3265
uint *table_changes)
3268
3267
Field **f_ptr, *field;
3269
uint32_t table_changes_local= 0;
3268
uint table_changes_local= 0;
3270
3269
List_iterator_fast<Create_field> new_field_it(alter_info->create_list);
3271
3270
Create_field *new_field;
3272
3271
KEY_PART_INFO *key_part;
3293
3292
to evaluate possibility of fast ALTER Table, and then
3294
3293
destroy the copy.
3296
Alter_info tmp_alter_info(*alter_info, session->mem_root);
3297
Session *session= table->in_use;
3298
uint32_t db_options= 0; /* not used */
3295
Alter_info tmp_alter_info(*alter_info, thd->mem_root);
3296
THD *thd= table->in_use;
3297
uint db_options= 0; /* not used */
3299
3298
/* Create the prepared information. */
3300
if (mysql_prepare_create_table(session, create_info,
3299
if (mysql_prepare_create_table(thd, create_info,
3301
3300
&tmp_alter_info,
3302
3301
(table->s->tmp_table != NO_TMP_TABLE),
3309
3308
/* Allocate result buffers. */
3310
3309
if (! (ha_alter_info->index_drop_buffer=
3311
(uint*) session->alloc(sizeof(uint) * table->s->keys)) ||
3310
(uint*) thd->alloc(sizeof(uint) * table->s->keys)) ||
3312
3311
! (ha_alter_info->index_add_buffer=
3313
(uint*) session->alloc(sizeof(uint) *
3312
(uint*) thd->alloc(sizeof(uint) *
3314
3313
tmp_alter_info.key_list.elements)))
3408
3407
if (!(table_changes_local= field->is_equal(new_field)))
3409
3408
*alter_flags|= HA_ALTER_COLUMN_TYPE;
3412
Check if the altered column is a stored virtual field.
3413
TODO: Mark such a column with an alter flag only if
3414
the expression functions are not equal.
3416
if (field->is_stored && field->vcol_info)
3417
*alter_flags|= HA_ALTER_STORED_VCOL;
3419
3410
/* Check if field was renamed */
3420
3411
field->flags&= ~FIELD_IS_RENAMED;
3421
3412
if (my_strcasecmp(system_charset_info,
3646
3637
if (error == HA_ERR_WRONG_COMMAND)
3648
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3639
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3649
3640
ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
3650
3641
table->s->table_name.str);
3697
3688
if (create_info->index_file_name)
3699
3690
/* Fix index_file_name to have 'tmp_name' as basename */
3700
my_stpcpy(index_file, tmp_name);
3691
stpcpy(index_file, tmp_name);
3701
3692
create_info->index_file_name=fn_same(index_file,
3702
3693
create_info->index_file_name,
3705
3696
if (create_info->data_file_name)
3707
3698
/* Fix data_file_name to have 'tmp_name' as basename */
3708
my_stpcpy(data_file, tmp_name);
3699
stpcpy(data_file, tmp_name);
3709
3700
create_info->data_file_name=fn_same(data_file,
3710
3701
create_info->data_file_name,
3719
3710
With create_info->frm_only == 1 this creates a .frm file only.
3720
3711
We don't log the statement, it will be logged later.
3722
tmp_disable_binlog(session);
3723
error= mysql_create_table(session, new_db, tmp_name,
3713
tmp_disable_binlog(thd);
3714
error= mysql_create_table(thd, new_db, tmp_name,
3724
3715
create_info, alter_info, 1, 0);
3725
reenable_binlog(session);
3716
reenable_binlog(thd);
3747
3738
The temporary table is created without storing it in any storage engine
3748
3739
and is opened only to get the table struct and frm file reference.
3750
Table *create_altered_table(Session *session,
3741
Table *create_altered_table(THD *thd,
3753
3744
HA_CREATE_INFO *create_info,
3760
3751
char tmp_name[80];
3761
3752
char path[FN_REFLEN];
3763
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64,
3764
tmp_file_prefix, current_pid, session->thread_id);
3754
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx",
3755
tmp_file_prefix, current_pid, thd->thread_id);
3765
3756
/* Safety fix for InnoDB */
3766
3757
if (lower_case_table_names)
3767
3758
my_casedn_str(files_charset_info, tmp_name);
3768
3759
altered_create_info.options&= ~HA_LEX_CREATE_TMP_TABLE;
3769
3760
altered_create_info.frm_only= 1;
3770
if ((error= create_temporary_table(session, table, new_db, tmp_name,
3761
if ((error= create_temporary_table(thd, table, new_db, tmp_name,
3771
3762
&altered_create_info,
3772
3763
alter_info, db_change)))
3777
3768
build_table_filename(path, sizeof(path), new_db, tmp_name, "",
3779
altered_table= open_temporary_table(session, path, new_db, tmp_name, 1,
3770
altered_table= open_temporary_table(thd, path, new_db, tmp_name, 1,
3781
3772
return(altered_table);
3854
3845
The final .frm file is already created as a temporary file
3855
3846
and will be renamed to the original table name.
3857
pthread_mutex_lock(&LOCK_open);
3858
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
3848
VOID(pthread_mutex_lock(&LOCK_open));
3849
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
3859
3850
alter_table_manage_keys(table, table->file->indexes_are_disabled(),
3861
close_data_files_and_morph_locks(session,
3852
close_data_files_and_morph_locks(thd,
3862
3853
table->pos_in_table_list->db,
3863
3854
table->pos_in_table_list->table_name);
3864
3855
if (mysql_rename_table(NULL,
3868
3859
table->s->table_name.str, FN_FROM_IS_TMP))
3871
pthread_mutex_unlock(&LOCK_open);
3862
VOID(pthread_mutex_unlock(&LOCK_open));
3874
3865
broadcast_refresh();
3875
pthread_mutex_unlock(&LOCK_open);
3866
VOID(pthread_mutex_unlock(&LOCK_open));
3878
3869
The ALTER Table is always in its own transaction.
3880
3871
wait_if_global_read_lock(), which could create a deadlock if called
3881
3872
with LOCK_open.
3883
error= ha_autocommit_or_rollback(session, 0);
3874
error= ha_autocommit_or_rollback(thd, 0);
3885
if (ha_commit(session))
3891
pthread_mutex_lock(&LOCK_open);
3882
VOID(pthread_mutex_lock(&LOCK_open));
3892
3883
if (reopen_table(table))
3897
pthread_mutex_unlock(&LOCK_open);
3888
VOID(pthread_mutex_unlock(&LOCK_open));
3898
3889
t_table= table;
3901
3892
Tell the handler that the changed frm is on disk and table
3902
3893
has been re-opened
3904
if ((error= t_table->file->alter_table_phase3(session, t_table)))
3895
if ((error= t_table->file->alter_table_phase3(thd, t_table)))
3914
3905
assert(t_table == table);
3915
3906
table->open_placeholder= 1;
3916
pthread_mutex_lock(&LOCK_open);
3907
VOID(pthread_mutex_lock(&LOCK_open));
3917
3908
close_handle_and_leave_table_as_lock(table);
3918
pthread_mutex_unlock(&LOCK_open);
3909
VOID(pthread_mutex_unlock(&LOCK_open));
3940
3931
instructions that require change in table data, not only in
3941
3932
table definition or indexes.
3943
@param[in,out] session thread handle. Used as a memory pool
3934
@param[in,out] thd thread handle. Used as a memory pool
3944
3935
and source of environment information.
3945
3936
@param[in] table the source table, open and locked
3946
3937
Used as an interface to the storage engine
3982
3973
List_iterator<Create_field> find_it(new_create_list);
3983
3974
List_iterator<Create_field> field_it(new_create_list);
3984
3975
List<Key_part_spec> key_parts;
3985
uint32_t db_create_options= (table->s->db_create_options
3976
uint db_create_options= (table->s->db_create_options
3986
3977
& ~(HA_OPTION_PACK_RECORD));
3987
uint32_t used_fields= create_info->used_fields;
3978
uint used_fields= create_info->used_fields;
3988
3979
KEY *key_info=table->key_info;
4115
4101
def->sql_type == DRIZZLE_TYPE_DATETIME) &&
4116
4102
!alter_info->datetime_field &&
4117
4103
!(~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) &&
4118
session->variables.sql_mode & MODE_NO_ZERO_DATE)
4104
thd->variables.sql_mode & MODE_NO_ZERO_DATE)
4120
4106
alter_info->datetime_field= def;
4121
4107
alter_info->error_if_not_empty= true;
4380
bool mysql_alter_table(Session *session,char *new_db, char *new_name,
4363
bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
4381
4364
HA_CREATE_INFO *create_info,
4382
4365
TableList *table_list,
4383
4366
Alter_info *alter_info,
4384
uint32_t order_num, order_st *order, bool ignore)
4367
uint order_num, order_st *order, bool ignore)
4386
4369
Table *table, *new_table=0, *name_lock= 0;;
4403
4386
to simplify further comparisons: we want to see if it's a RENAME
4404
4387
later just by comparing the pointers, avoiding the need for strcmp.
4406
session->set_proc_info("init");
4389
thd_proc_info(thd, "init");
4407
4390
table_name=table_list->table_name;
4408
4391
alias= (lower_case_table_names == 2) ? table_list->alias : table_name;
4409
4392
db=table_list->db;
4412
4395
build_table_filename(path, sizeof(path), db, table_name, "", 0);
4414
mysql_ha_rm_tables(session, table_list, false);
4397
mysql_ha_rm_tables(thd, table_list, false);
4416
4399
/* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
4417
4400
if (alter_info->tablespace_op != NO_TABLESPACE_OP)
4418
4401
/* Conditionally writes to binlog. */
4419
return(mysql_discard_or_import_tablespace(session,table_list,
4402
return(mysql_discard_or_import_tablespace(thd,table_list,
4420
4403
alter_info->tablespace_op));
4421
char* pos= new_name_buff;
4422
char* pos_end= pos+strlen(new_name_buff)-1;
4423
pos= my_stpncpy(new_name_buff, mysql_data_home, pos_end-pos);
4424
pos= my_stpncpy(new_name_buff, "/", pos_end-pos);
4425
pos= my_stpncpy(new_name_buff, db, pos_end-pos);
4426
pos= my_stpncpy(new_name_buff, "/", pos_end-pos);
4427
pos= my_stpncpy(new_name_buff, table_name, pos_end-pos);
4428
pos= my_stpncpy(new_name_buff, reg_ext, pos_end-pos);
4404
strxnmov(new_name_buff, sizeof (new_name_buff) - 1, mysql_data_home, "/", db,
4405
"/", table_name, reg_ext, NullS);
4430
4406
(void) unpack_filename(new_name_buff, new_name_buff);
4432
4408
If this is just a rename of a view, short cut to the
4441
4417
into the main table list, like open_tables does).
4442
4418
This code is wrong and will be removed, please do not copy.
4444
(void)mysql_frm_type(session, new_name_buff, &table_type);
4420
(void)mysql_frm_type(thd, new_name_buff, &table_type);
4446
if (!(table= open_n_lock_single_table(session, table_list, TL_WRITE_ALLOW_READ)))
4422
if (!(table= open_n_lock_single_table(thd, table_list, TL_WRITE_ALLOW_READ)))
4448
4424
table->use_all_columns();
4450
4426
/* Check that we are not trying to rename to an existing table */
4453
my_stpcpy(new_name_buff,new_name);
4454
my_stpcpy(new_alias= new_alias_buff, new_name);
4429
stpcpy(new_name_buff,new_name);
4430
stpcpy(new_alias= new_alias_buff, new_name);
4455
4431
if (lower_case_table_names)
4457
4433
if (lower_case_table_names != 2)
4475
4451
if (table->s->tmp_table != NO_TMP_TABLE)
4477
if (find_temporary_table(session,new_db,new_name_buff))
4453
if (find_temporary_table(thd,new_db,new_name_buff))
4479
4455
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
4552
4528
while the fact that the table is still open gives us protection
4553
4529
from concurrent DDL statements.
4555
pthread_mutex_lock(&LOCK_open);
4556
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4557
pthread_mutex_unlock(&LOCK_open);
4531
VOID(pthread_mutex_lock(&LOCK_open));
4532
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4533
VOID(pthread_mutex_unlock(&LOCK_open));
4558
4534
error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4559
4535
/* COND_refresh will be signaled in close_thread_tables() */
4562
pthread_mutex_lock(&LOCK_open);
4563
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4564
pthread_mutex_unlock(&LOCK_open);
4538
VOID(pthread_mutex_lock(&LOCK_open));
4539
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4540
VOID(pthread_mutex_unlock(&LOCK_open));
4565
4541
error=table->file->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4566
4542
/* COND_refresh will be signaled in close_thread_tables() */
4573
4549
if (error == HA_ERR_WRONG_COMMAND)
4576
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
4552
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
4577
4553
ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
4581
pthread_mutex_lock(&LOCK_open);
4557
VOID(pthread_mutex_lock(&LOCK_open));
4583
4559
Unlike to the above case close_cached_table() below will remove ALL
4584
4560
instances of Table from table cache (it will also remove table lock
4591
4567
if (!error && (new_name != table_name || new_db != db))
4593
session->set_proc_info("rename");
4569
thd_proc_info(thd, "rename");
4595
4571
Then do a 'simple' rename of the table. First we need to close all
4596
4572
instances of 'source' table.
4598
close_cached_table(session, table);
4574
close_cached_table(thd, table);
4600
4576
Then, we want check once again that target table does not exist.
4601
4577
Actually the order of these two steps does not matter since
4626
4602
if (error == HA_ERR_WRONG_COMMAND)
4629
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
4605
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
4630
4606
ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
4636
write_bin_log(session, true, session->query, session->query_length);
4612
write_bin_log(thd, true, thd->query, thd->query_length);
4639
4615
else if (error > 0)
4645
unlink_open_table(session, name_lock, false);
4646
pthread_mutex_unlock(&LOCK_open);
4621
unlink_open_table(thd, name_lock, false);
4622
VOID(pthread_mutex_unlock(&LOCK_open));
4647
4623
table_list->table= NULL; // For query cache
4664
4640
new_db_type= create_info->db_type;
4666
if (mysql_prepare_alter_table(session, table, create_info, alter_info))
4642
if (mysql_prepare_alter_table(thd, table, create_info, alter_info))
4669
set_table_default_charset(session, create_info, db);
4672
if (session->variables.old_alter_table
4645
set_table_default_charset(thd, create_info, db);
4648
if (thd->variables.old_alter_table
4673
4649
|| (table->s->db_type() != create_info->db_type)
4676
4652
if (alter_info->build_method == HA_BUILD_ONLINE)
4678
my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4654
my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4681
4657
alter_info->build_method= HA_BUILD_OFFLINE;
4686
4662
Table *altered_table= 0;
4687
4663
HA_ALTER_INFO ha_alter_info;
4688
4664
HA_ALTER_FLAGS ha_alter_flags;
4689
uint32_t table_changes= IS_EQUAL_YES;
4665
uint table_changes= IS_EQUAL_YES;
4690
4666
bool need_copy_table= true;
4691
4667
/* Check how much the tables differ. */
4692
if (compare_tables(session, table, alter_info,
4668
if (compare_tables(thd, table, alter_info,
4693
4669
create_info, order_num,
4694
4670
&ha_alter_flags,
4695
4671
&ha_alter_info,
4712
4688
if (new_name == table_name && new_db == db &&
4713
4689
ha_alter_flags.is_set())
4715
Alter_info tmp_alter_info(*alter_info, session->mem_root);
4691
Alter_info tmp_alter_info(*alter_info, thd->mem_root);
4718
4694
If no table rename,
4719
4695
check if table can be altered on-line
4721
if (!(altered_table= create_altered_table(session,
4697
if (!(altered_table= create_altered_table(thd,
4746
4722
case HA_ALTER_NOT_SUPPORTED:
4747
4723
if (alter_info->build_method == HA_BUILD_ONLINE)
4749
my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4750
close_temporary_table(session, altered_table, 1, 1);
4725
my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4726
close_temporary_table(thd, altered_table, 1, 1);
4753
4729
need_copy_table= true;
4755
4731
case HA_ALTER_ERROR:
4757
close_temporary_table(session, altered_table, 1, 1);
4733
close_temporary_table(thd, altered_table, 1, 1);
4766
4742
if (!need_copy_table)
4768
error= mysql_fast_or_online_alter_table(session,
4744
error= mysql_fast_or_online_alter_table(thd,
4772
4748
&ha_alter_info,
4773
4749
&ha_alter_flags,
4774
4750
alter_info->keys_onoff);
4777
mysql_unlock_tables(session, session->lock);
4753
mysql_unlock_tables(thd, thd->lock);
4780
close_temporary_table(session, altered_table, 1, 1);
4756
close_temporary_table(thd, altered_table, 1, 1);
4798
4774
if (altered_table)
4799
close_temporary_table(session, altered_table, 1, 1);
4775
close_temporary_table(thd, altered_table, 1, 1);
4802
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, tmp_file_prefix,
4803
current_pid, session->thread_id);
4778
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx", tmp_file_prefix,
4779
current_pid, thd->thread_id);
4804
4780
/* Safety fix for innodb */
4805
4781
if (lower_case_table_names)
4806
4782
my_casedn_str(files_charset_info, tmp_name);
4809
4785
/* Create a temporary table with the new format */
4810
if ((error= create_temporary_table(session, table, new_db, tmp_name,
4786
if ((error= create_temporary_table(thd, table, new_db, tmp_name,
4811
4787
create_info, alter_info,
4812
4788
!strcmp(db, new_db))))
4821
4797
memset(&tbl, 0, sizeof(tbl));
4822
4798
tbl.db= new_db;
4823
4799
tbl.table_name= tbl.alias= tmp_name;
4824
/* Table is in session->temporary_tables */
4825
new_table= open_table(session, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
4800
/* Table is in thd->temporary_tables */
4801
new_table= open_table(thd, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
4831
4807
build_table_filename(path, sizeof(path), new_db, tmp_name, "",
4833
4809
/* Open our intermediate table */
4834
new_table=open_temporary_table(session, path, new_db, tmp_name, 0, OTM_OPEN);
4810
new_table=open_temporary_table(thd, path, new_db, tmp_name, 0, OTM_OPEN);
4836
4812
if (!new_table)
4839
4815
/* Copy the data if necessary. */
4840
session->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields
4841
session->cuted_fields=0L;
4842
session->set_proc_info("copy to tmp table");
4816
thd->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields
4817
thd->cuted_fields=0L;
4818
thd_proc_info(thd, "copy to tmp table");
4843
4819
copied=deleted=0;
4845
4821
We do not copy data for MERGE tables. Only the children have data.
4861
pthread_mutex_lock(&LOCK_open);
4862
wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4863
pthread_mutex_unlock(&LOCK_open);
4837
VOID(pthread_mutex_lock(&LOCK_open));
4838
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
4839
VOID(pthread_mutex_unlock(&LOCK_open));
4864
4840
alter_table_manage_keys(table, table->file->indexes_are_disabled(),
4865
4841
alter_info->keys_onoff);
4866
error= ha_autocommit_or_rollback(session, 0);
4867
if (end_active_trans(session))
4842
error= ha_autocommit_or_rollback(thd, 0);
4843
if (end_active_trans(thd))
4870
session->count_cuted_fields= CHECK_FIELD_IGNORE;
4846
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
4872
4848
if (table->s->tmp_table != NO_TMP_TABLE)
4877
4853
/* Close lock if this is a transactional table */
4880
mysql_unlock_tables(session, session->lock);
4856
mysql_unlock_tables(thd, thd->lock);
4883
4859
/* Remove link to old table and rename the new one */
4884
close_temporary_table(session, table, 1, 1);
4860
close_temporary_table(thd, table, 1, 1);
4885
4861
/* Should pass the 'new_name' as we store table name in the cache */
4886
if (rename_temporary_table(session, new_table, new_db, new_name))
4862
if (rename_temporary_table(thd, new_table, new_db, new_name))
4888
4864
/* We don't replicate alter table statement on temporary tables */
4889
if (!session->current_stmt_binlog_row_based)
4890
write_bin_log(session, true, session->query, session->query_length);
4865
if (!thd->current_stmt_binlog_row_based)
4866
write_bin_log(thd, true, thd->query, thd->query_length);
4891
4867
goto end_temporary;
4898
4874
Note that MERGE tables do not have their children attached here.
4900
4876
intern_close_table(new_table);
4877
my_free(new_table,MYF(0));
4903
pthread_mutex_lock(&LOCK_open);
4879
VOID(pthread_mutex_lock(&LOCK_open));
4906
quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
4907
pthread_mutex_unlock(&LOCK_open);
4882
VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
4883
VOID(pthread_mutex_unlock(&LOCK_open));
4924
4900
call to remove name-locks from table cache and list of open table.
4927
session->set_proc_info("rename result table");
4928
snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, tmp_file_prefix,
4929
current_pid, session->thread_id);
4903
thd_proc_info(thd, "rename result table");
4904
snprintf(old_name, sizeof(old_name), "%s2-%lx-%lx", tmp_file_prefix,
4905
current_pid, thd->thread_id);
4930
4906
if (lower_case_table_names)
4931
4907
my_casedn_str(files_charset_info, old_name);
4933
wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
4934
close_data_files_and_morph_locks(session, db, table_name);
4909
wait_while_table_is_used(thd, table, HA_EXTRA_PREPARE_FOR_RENAME);
4910
close_data_files_and_morph_locks(thd, db, table_name);
4937
4913
save_old_db_type= old_db_type;
4956
quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
4932
VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
4958
4934
else if (mysql_rename_table(new_db_type, new_db, tmp_name, new_db,
4959
4935
new_alias, FN_FROM_IS_TMP) || ((new_name != table_name || new_db != db) && 0))
4961
4937
/* Try to get everything back. */
4963
quick_rm_table(new_db_type,new_db,new_alias, 0);
4964
quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
4965
mysql_rename_table(old_db_type, db, old_name, db, alias,
4939
VOID(quick_rm_table(new_db_type,new_db,new_alias, 0));
4940
VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
4941
VOID(mysql_rename_table(old_db_type, db, old_name, db, alias,
4972
4948
goto err_with_placeholders;
4975
quick_rm_table(old_db_type, db, old_name, FN_IS_TMP);
4951
VOID(quick_rm_table(old_db_type, db, old_name, FN_IS_TMP));
4978
if (session->locked_tables && new_name == table_name && new_db == db)
4954
if (thd->locked_tables && new_name == table_name && new_db == db)
4980
session->in_lock_tables= 1;
4981
error= reopen_tables(session, 1, 1);
4982
session->in_lock_tables= 0;
4956
thd->in_lock_tables= 1;
4957
error= reopen_tables(thd, 1, 1);
4958
thd->in_lock_tables= 0;
4984
4960
goto err_with_placeholders;
4986
pthread_mutex_unlock(&LOCK_open);
4988
session->set_proc_info("end");
4962
VOID(pthread_mutex_unlock(&LOCK_open));
4964
thd_proc_info(thd, "end");
4966
ha_binlog_log_query(thd, create_info->db_type, LOGCOM_ALTER_TABLE,
4967
thd->query, thd->query_length,
4990
4970
assert(!(mysql_bin_log.is_open() &&
4991
session->current_stmt_binlog_row_based &&
4971
thd->current_stmt_binlog_row_based &&
4992
4972
(create_info->options & HA_LEX_CREATE_TMP_TABLE)));
4993
write_bin_log(session, true, session->query, session->query_length);
4973
write_bin_log(thd, true, thd->query, thd->query_length);
4995
4975
if (ha_check_storage_engine_flag(old_db_type, HTON_FLUSH_AFTER_RENAME))
5002
4982
char path[FN_REFLEN];
5003
4983
Table *t_table;
5004
4984
build_table_filename(path, sizeof(path), new_db, table_name, "", 0);
5005
t_table= open_temporary_table(session, path, new_db, tmp_name, false, OTM_OPEN);
4985
t_table= open_temporary_table(thd, path, new_db, tmp_name, false, OTM_OPEN);
5008
4988
intern_close_table(t_table);
4989
my_free(t_table, MYF(0));
5012
4992
sql_print_warning(_("Could not open table %s.%s after rename\n"),
5024
5004
LOCK TABLES we can rely on close_thread_tables() doing this job.
5026
5006
pthread_mutex_lock(&LOCK_open);
5027
unlink_open_table(session, table, false);
5028
unlink_open_table(session, name_lock, false);
5007
unlink_open_table(thd, table, false);
5008
unlink_open_table(thd, name_lock, false);
5029
5009
pthread_mutex_unlock(&LOCK_open);
5033
5013
snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
5034
5014
(ulong) (copied + deleted), (ulong) deleted,
5035
(ulong) session->cuted_fields);
5036
my_ok(session, copied + deleted, 0L, tmp_name);
5037
session->some_tables_deleted=0;
5015
(ulong) thd->cuted_fields);
5016
my_ok(thd, copied + deleted, 0L, tmp_name);
5017
thd->some_tables_deleted=0;
5043
5023
/* close_temporary_table() frees the new_table pointer. */
5044
close_temporary_table(session, new_table, 1, 1);
5024
close_temporary_table(thd, new_table, 1, 1);
5047
quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
5027
VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
5071
5051
/* Shouldn't get here. */
5074
bool save_abort_on_warning= session->abort_on_warning;
5075
session->abort_on_warning= true;
5076
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
5054
bool save_abort_on_warning= thd->abort_on_warning;
5055
thd->abort_on_warning= true;
5056
make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
5077
5057
f_val, strlength(f_val), t_type,
5078
5058
alter_info->datetime_field->field_name);
5079
session->abort_on_warning= save_abort_on_warning;
5059
thd->abort_on_warning= save_abort_on_warning;
5083
5063
pthread_mutex_lock(&LOCK_open);
5084
unlink_open_table(session, name_lock, false);
5064
unlink_open_table(thd, name_lock, false);
5085
5065
pthread_mutex_unlock(&LOCK_open);
5092
5072
being altered. To be safe under LOCK TABLES we should remove placeholders
5093
5073
from list of open tables list and table cache.
5095
unlink_open_table(session, table, false);
5075
unlink_open_table(thd, table, false);
5097
unlink_open_table(session, name_lock, false);
5098
pthread_mutex_unlock(&LOCK_open);
5077
unlink_open_table(thd, name_lock, false);
5078
VOID(pthread_mutex_unlock(&LOCK_open));
5101
5081
/* mysql_alter_table */
5104
5084
copy_data_between_tables(Table *from,Table *to,
5105
5085
List<Create_field> &create,
5107
uint32_t order_num, order_st *order,
5087
uint order_num, order_st *order,
5108
5088
ha_rows *copied,
5109
5089
ha_rows *deleted,
5110
5090
enum enum_enable_or_disable keys_onoff,
5132
5112
This needs to be done before external_lock
5134
error= ha_enable_transaction(session, false);
5114
error= ha_enable_transaction(thd, false);
5138
5118
if (!(copy= new Copy_field[to->s->fields]))
5139
5119
return(-1); /* purecov: inspected */
5141
if (to->file->ha_external_lock(session, F_WRLCK))
5121
if (to->file->ha_external_lock(thd, F_WRLCK))
5144
5124
/* We need external lock before we can disable/enable keys */
5145
5125
alter_table_manage_keys(to, from->file->indexes_are_disabled(), keys_onoff);
5147
5127
/* We can abort alter table for any table type */
5148
session->abort_on_warning= !ignore;
5128
thd->abort_on_warning= !ignore;
5150
5130
from->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5151
5131
to->file->ha_start_bulk_insert(from->file->stats.records);
5153
save_sql_mode= session->variables.sql_mode;
5133
save_sql_mode= thd->variables.sql_mode;
5155
5135
List_iterator<Create_field> it(create);
5156
5136
Create_field *def;
5179
5159
_("order_st BY ignored because there is a user-defined clustered "
5180
5160
"index in the table '%-.192s'"),
5181
5161
from->s->table_name.str);
5182
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
5162
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
5192
5172
tables.db= from->s->db.str;
5195
if (session->lex->select_lex.setup_ref_array(session, order_num) ||
5196
setup_order(session, session->lex->select_lex.ref_pointer_array,
5175
if (thd->lex->select_lex.setup_ref_array(thd, order_num) ||
5176
setup_order(thd, thd->lex->select_lex.ref_pointer_array,
5197
5177
&tables, fields, all_fields, order) ||
5198
5178
!(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
5199
(from->sort.found_records= filesort(session, from, sortorder, length,
5179
(from->sort.found_records= filesort(thd, from, sortorder, length,
5200
5180
(SQL_SELECT *) 0, HA_POS_ERROR,
5201
5181
1, &examined_rows)) ==
5207
5187
/* Tell handler that we have values for all columns in the to table */
5208
5188
to->use_all_columns();
5209
init_read_record(&info, session, from, (SQL_SELECT *) 0, 1,1);
5189
init_read_record(&info, thd, from, (SQL_SELECT *) 0, 1,1);
5211
5191
to->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
5212
session->row_count= 0;
5213
5193
restore_record(to, s->default_values); // Create empty record
5214
5194
while (!(error=info.read_record(&info)))
5216
if (session->killed)
5218
session->send_kill_message();
5198
thd->send_kill_message();
5222
session->row_count++;
5223
5203
/* Return error if source table isn't empty. */
5224
5204
if (error_if_not_empty)
5292
5271
Ensure that the new table is saved properly to disk so that we
5293
5272
can do a rename
5295
if (ha_autocommit_or_rollback(session, 0))
5274
if (ha_autocommit_or_rollback(thd, 0))
5297
if (end_active_trans(session))
5276
if (end_active_trans(thd))
5301
session->variables.sql_mode= save_sql_mode;
5302
session->abort_on_warning= 0;
5280
thd->variables.sql_mode= save_sql_mode;
5281
thd->abort_on_warning= 0;
5303
5282
free_io_cache(from);
5304
5283
*copied= found_count;
5305
5284
*deleted=delete_count;
5306
5285
to->file->ha_release_auto_increment();
5307
if (to->file->ha_external_lock(session,F_UNLCK))
5286
if (to->file->ha_external_lock(thd,F_UNLCK))
5309
5288
return(error > 0 ? -1 : 0);
5338
5317
create_info.default_table_charset=default_charset_info;
5339
5318
/* Force alter table to recreate table */
5340
5319
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
5341
return(mysql_alter_table(session, NULL, NULL, &create_info,
5320
return(mysql_alter_table(thd, NullS, NullS, &create_info,
5342
5321
table_list, &alter_info, 0,
5343
5322
(order_st *) 0, 0));
5347
bool mysql_checksum_table(Session *session, TableList *tables,
5326
bool mysql_checksum_table(THD *thd, TableList *tables,
5348
5327
HA_CHECK_OPT *check_opt)
5350
5329
TableList *table;
5351
5330
List<Item> field_list;
5353
Protocol *protocol= session->protocol;
5332
Protocol *protocol= thd->protocol;
5355
5334
field_list.push_back(item = new Item_empty_string("Table", NAME_LEN*2));
5356
5335
item->maybe_null= 1;
5367
5346
char table_name[NAME_LEN*2+2];
5370
strxmov(table_name, table->db ,".", table->table_name, NULL);
5349
strxmov(table_name, table->db ,".", table->table_name, NullS);
5372
t= table->table= open_n_lock_single_table(session, table, TL_READ);
5373
session->clear_error(); // these errors shouldn't get client
5351
t= table->table= open_n_lock_single_table(thd, table, TL_READ);
5352
thd->clear_error(); // these errors shouldn't get client
5375
5354
protocol->prepare_for_resend();
5376
5355
protocol->store(table_name, system_charset_info);
5442
5421
t->file->ha_rnd_end();
5445
session->clear_error();
5446
close_thread_tables(session);
5425
close_thread_tables(thd);
5447
5426
table->table=0; // For query cache
5449
5428
if (protocol->write())
5457
close_thread_tables(session); // Shouldn't be needed
5436
close_thread_tables(thd); // Shouldn't be needed
5459
5438
table->table=0;
5463
static bool check_engine(Session *session, const char *table_name,
5442
static bool check_engine(THD *thd, const char *table_name,
5464
5443
HA_CREATE_INFO *create_info)
5466
5445
handlerton **new_engine= &create_info->db_type;
5467
5446
handlerton *req_engine= *new_engine;
5468
5447
bool no_substitution= 1;
5469
if (!(*new_engine= ha_checktype(session, ha_legacy_type(req_engine),
5448
if (!(*new_engine= ha_checktype(thd, ha_legacy_type(req_engine),
5470
5449
no_substitution, 1)))
5473
5452
if (req_engine && req_engine != *new_engine)
5475
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5454
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5476
5455
ER_WARN_USING_OTHER_HANDLER,
5477
5456
ER(ER_WARN_USING_OTHER_HANDLER),
5478
5457
ha_resolve_storage_engine_name(*new_engine),