124
121
uint tablename_to_filename(const char *from, char *to, uint to_length)
126
123
uint errors, length;
127
DBUG_ENTER("tablename_to_filename");
128
DBUG_PRINT("enter", ("from '%s'", from));
130
125
if (from[0] == '#' && !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
131
126
MYSQL50_TABLE_NAME_PREFIX_LENGTH))
132
DBUG_RETURN((uint) (strmake(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
127
return((uint) (strmake(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
134
129
(from + MYSQL50_TABLE_NAME_PREFIX_LENGTH)));
135
130
length= strconvert(system_charset_info, from,
184
178
char dbbuff[FN_REFLEN];
185
179
char tbbuff[FN_REFLEN];
186
DBUG_ENTER("build_table_filename");
187
DBUG_PRINT("enter", ("db: '%s' table_name: '%s' ext: '%s' flags: %x",
188
db, table_name, ext, flags));
190
181
if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
191
182
strnmov(tbbuff, table_name, sizeof(tbbuff));
234
224
uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
236
DBUG_ENTER("build_tmptable_filename");
238
227
char *p= strnmov(buff, mysql_tmpdir, bufflen);
239
my_snprintf(p, bufflen - (p - buff), "/%s%lx_%lx_%x%s",
228
snprintf(p, bufflen - (p - buff), "/%s%lx_%lx_%x%s",
240
229
tmp_file_prefix, current_pid,
241
230
thd->thread_id, thd->tmp_table++, reg_ext);
321
309
read_ddl_log_file_entry()
322
310
entry_no Entry number to read
328
316
static bool read_ddl_log_file_entry(uint entry_no)
331
319
File file_id= global_ddl_log.file_id;
332
320
uchar *file_entry_buf= (uchar*)global_ddl_log.file_entry_buf;
333
uint io_size= global_ddl_log.io_size;
334
DBUG_ENTER("read_ddl_log_file_entry");
321
ssize_t io_size= (ssize_t)global_ddl_log.io_size;
336
323
if (pread(file_id, file_entry_buf, io_size, io_size * entry_no) != io_size)
345
332
write_ddl_log_file_entry()
346
333
entry_no Entry number to read
352
339
static bool write_ddl_log_file_entry(uint entry_no)
355
342
File file_id= global_ddl_log.file_id;
356
343
char *file_entry_buf= (char*)global_ddl_log.file_entry_buf;
357
DBUG_ENTER("write_ddl_log_file_entry");
359
345
if (pwrite(file_id, (uchar*)file_entry_buf,
360
IO_SIZE, IO_SIZE * entry_no) != IO_SIZE)
346
IO_SIZE, IO_SIZE * entry_no) != IO_SIZE)
429
414
char *file_entry_buf= (char*)global_ddl_log.file_entry_buf;
430
415
char file_name[FN_REFLEN];
432
bool successful_open= FALSE;
433
DBUG_ENTER("read_ddl_log_header");
417
bool successful_open= false;
435
419
create_ddl_log_file_name(file_name);
436
420
if ((global_ddl_log.file_id= my_open(file_name,
442
426
sql_print_error("Failed to read ddl log file in recovery");
445
successful_open= TRUE;
429
successful_open= true;
447
431
entry_no= uint4korr(&file_entry_buf[DDL_LOG_NUM_ENTRY_POS]);
448
432
global_ddl_log.name_len= uint4korr(&file_entry_buf[DDL_LOG_NAME_LEN_POS]);
449
433
if (successful_open)
451
435
global_ddl_log.io_size= uint4korr(&file_entry_buf[DDL_LOG_IO_SIZE_POS]);
452
DBUG_ASSERT(global_ddl_log.io_size <=
436
assert(global_ddl_log.io_size <=
453
437
sizeof(global_ddl_log.file_entry_buf));
483
467
char *file_entry_buf= (char*)&global_ddl_log.file_entry_buf;
485
469
uchar single_char;
486
DBUG_ENTER("read_ddl_log_entry");
488
471
if (read_ddl_log_file_entry(read_entry))
492
475
ddl_log_entry->entry_pos= read_entry;
493
476
single_char= file_entry_buf[DDL_LOG_ENTRY_TYPE_POS];
537
519
/* Couldn't create ddl log file, this is serious error */
538
520
sql_print_error("Failed to open ddl log file");
541
global_ddl_log.inited= TRUE;
523
global_ddl_log.inited= true;
542
524
if (write_ddl_log_header())
544
526
VOID(my_close(global_ddl_log.file_id, MYF(MY_WME)));
545
global_ddl_log.inited= FALSE;
527
global_ddl_log.inited= false;
557
539
execute_ddl_log_action()
558
540
ddl_log_entry Information in action entry to execute
564
546
static int execute_ddl_log_action(THD *thd, DDL_LOG_ENTRY *ddl_log_entry)
566
bool frm_action= FALSE;
548
bool frm_action= false;
567
549
LEX_STRING handler_name;
568
550
handler *file= NULL;
569
551
MEM_ROOT mem_root;
571
553
char to_path[FN_REFLEN];
572
554
char from_path[FN_REFLEN];
573
555
handlerton *hton;
574
DBUG_ENTER("execute_ddl_log_action");
576
557
if (ddl_log_entry->entry_type == DDL_IGNORE_LOG_ENTRY_CODE)
580
561
handler_name.str= (char*)ddl_log_entry->handler_name;
581
562
handler_name.length= strlen(ddl_log_entry->handler_name);
582
563
init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
583
564
if (!strcmp(ddl_log_entry->handler_name, reg_ext))
587
568
plugin_ref plugin= ha_resolve_by_name(thd, &handler_name);
625
606
if ((deactivate_ddl_log_entry(ddl_log_entry->entry_pos)))
627
608
VOID(sync_ddl_log());
629
610
if (ddl_log_entry->action_type == DDL_LOG_DELETE_ACTION)
632
DBUG_ASSERT(ddl_log_entry->action_type == DDL_LOG_REPLACE_ACTION);
613
assert(ddl_log_entry->action_type == DDL_LOG_REPLACE_ACTION);
634
615
Fall through and perform the rename action of the replace
635
616
action. We have already indicated the success of the delete
692
672
sizeof(DDL_LOG_MEMORY_ENTRY), MYF(MY_WME))))
694
674
sql_print_error("Failed to allocate memory for ddl log free list");
697
677
global_ddl_log.num_entries++;
698
678
used_entry->entry_pos= global_ddl_log.num_entries;
703
683
used_entry= global_ddl_log.first_free;
704
684
global_ddl_log.first_free= used_entry->next_log_entry;
705
*write_header= FALSE;
685
*write_header= false;
708
688
Move from free list to used list
742
722
DDL_LOG_MEMORY_ENTRY **active_entry)
744
724
bool error, write_header;
745
DBUG_ENTER("write_ddl_log_entry");
747
726
if (init_ddl_log())
751
730
global_ddl_log.file_entry_buf[DDL_LOG_ENTRY_TYPE_POS]=
752
731
(char)DDL_LOG_ENTRY_CODE;
755
734
global_ddl_log.file_entry_buf[DDL_LOG_PHASE_POS]= 0;
756
735
int4store(&global_ddl_log.file_entry_buf[DDL_LOG_NEXT_ENTRY_POS],
757
736
ddl_log_entry->next_entry);
758
DBUG_ASSERT(strlen(ddl_log_entry->name) < FN_LEN);
737
assert(strlen(ddl_log_entry->name) < FN_LEN);
759
738
strmake(&global_ddl_log.file_entry_buf[DDL_LOG_NAME_POS],
760
739
ddl_log_entry->name, FN_LEN - 1);
761
740
if (ddl_log_entry->action_type == DDL_LOG_RENAME_ACTION ||
762
741
ddl_log_entry->action_type == DDL_LOG_REPLACE_ACTION)
764
DBUG_ASSERT(strlen(ddl_log_entry->from_name) < FN_LEN);
743
assert(strlen(ddl_log_entry->from_name) < FN_LEN);
765
744
strmake(&global_ddl_log.file_entry_buf[DDL_LOG_NAME_POS + FN_LEN],
766
745
ddl_log_entry->from_name, FN_LEN - 1);
769
748
global_ddl_log.file_entry_buf[DDL_LOG_NAME_POS + FN_LEN]= 0;
770
DBUG_ASSERT(strlen(ddl_log_entry->handler_name) < FN_LEN);
749
assert(strlen(ddl_log_entry->handler_name) < FN_LEN);
771
750
strmake(&global_ddl_log.file_entry_buf[DDL_LOG_NAME_POS + (2*FN_LEN)],
772
751
ddl_log_entry->handler_name, FN_LEN - 1);
773
752
if (get_free_ddl_log_entry(active_entry, &write_header))
778
757
if (write_ddl_log_file_entry((*active_entry)->entry_pos))
781
760
sql_print_error("Failed to write entry_no = %u",
782
761
(*active_entry)->entry_pos);
917
894
file_entry_buf[DDL_LOG_ENTRY_TYPE_POS]= DDL_IGNORE_LOG_ENTRY_CODE;
918
895
else if (file_entry_buf[DDL_LOG_ACTION_TYPE_POS] == DDL_LOG_REPLACE_ACTION)
920
DBUG_ASSERT(file_entry_buf[DDL_LOG_PHASE_POS] == 0);
897
assert(file_entry_buf[DDL_LOG_PHASE_POS] == 0);
921
898
file_entry_buf[DDL_LOG_PHASE_POS]= 1;
927
904
if (write_ddl_log_file_entry(entry_no))
929
906
sql_print_error("Error in deactivating log entry. Position = %u",
937
914
sql_print_error("Failed in reading entry before deactivating it");
984
960
DDL_LOG_MEMORY_ENTRY *first_free= global_ddl_log.first_free;
985
961
DDL_LOG_MEMORY_ENTRY *next_log_entry= log_entry->next_log_entry;
986
962
DDL_LOG_MEMORY_ENTRY *prev_log_entry= log_entry->prev_log_entry;
987
DBUG_ENTER("release_ddl_log_memory_entry");
989
964
global_ddl_log.first_free= log_entry;
990
965
log_entry->next_log_entry= first_free;
1006
981
execute_ddl_log_entry()
1007
982
first_entry Reference to first action in entry
1013
988
bool execute_ddl_log_entry(THD *thd, uint first_entry)
1015
990
DDL_LOG_ENTRY ddl_log_entry;
1016
991
uint read_entry= first_entry;
1017
DBUG_ENTER("execute_ddl_log_entry");
1019
993
pthread_mutex_lock(&LOCK_gdl);
1054
1028
static void close_ddl_log()
1056
DBUG_ENTER("close_ddl_log");
1057
1030
if (global_ddl_log.file_id >= 0)
1059
1032
VOID(my_close(global_ddl_log.file_id, MYF(MY_WME)));
1060
1033
global_ddl_log.file_id= (File) -1;
1078
1051
DDL_LOG_ENTRY ddl_log_entry;
1079
1052
char file_name[FN_REFLEN];
1080
DBUG_ENTER("execute_ddl_log_recovery");
1083
1055
Initialise global_ddl_log struct
1085
1057
bzero(global_ddl_log.file_entry_buf, sizeof(global_ddl_log.file_entry_buf));
1086
global_ddl_log.inited= FALSE;
1087
global_ddl_log.recovery_phase= TRUE;
1058
global_ddl_log.inited= false;
1059
global_ddl_log.recovery_phase= true;
1088
1060
global_ddl_log.io_size= IO_SIZE;
1089
1061
global_ddl_log.file_id= (File) -1;
1117
1089
close_ddl_log();
1118
1090
create_ddl_log_file_name(file_name);
1119
1091
VOID(my_delete(file_name, MYF(0)));
1120
global_ddl_log.recovery_phase= FALSE;
1092
global_ddl_log.recovery_phase= false;
1122
1094
/* Remember that we don't have a THD */
1123
1095
my_pthread_setspecific_ptr(THR_THD, 0);
1198
1169
if (clear_error)
1199
1170
thd->clear_error();
1200
1171
thd->binlog_query(THD::STMT_QUERY_TYPE,
1201
query, query_length, FALSE, FALSE);
1172
query, query_length, false, false);
1222
1193
not if under LOCK TABLES.
1225
FALSE OK. In this case ok packet is sent to user
1196
false OK. In this case ok packet is sent to user
1230
1201
bool mysql_rm_table(THD *thd,TABLE_LIST *tables, my_bool if_exists,
1231
1202
my_bool drop_temporary)
1233
bool error, need_start_waiting= FALSE;
1234
DBUG_ENTER("mysql_rm_table");
1204
bool error, need_start_waiting= false;
1236
1206
if (tables && tables->schema_table)
1238
1208
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
1242
1212
/* mark for close and remove all cached entries */
1317
1286
built_query.append("DROP TABLE ");
1320
mysql_ha_rm_tables(thd, tables, FALSE);
1289
mysql_ha_rm_tables(thd, tables, false);
1322
1291
pthread_mutex_lock(&LOCK_open);
1350
1319
handlerton *table_type;
1351
1320
enum legacy_db_type frm_db_type;
1353
DBUG_PRINT("table", ("table_l: '%s'.'%s' table: 0x%lx s: 0x%lx",
1354
table->db, table->table_name, (long) table->table,
1355
table->table ? (long) table->table->s : (long) -1));
1357
1323
error= drop_temporary_table(thd, table);
1474
1440
wrong_tables.append(',');
1475
1441
wrong_tables.append(String(table->table_name,system_charset_info));
1477
DBUG_PRINT("table", ("table: 0x%lx s: 0x%lx", (long) table->table,
1478
table->table ? (long) table->table->s : (long) -1));
1481
1445
It's safe to unlock LOCK_open: we have an exclusive lock
1570
1534
char path[FN_REFLEN];
1572
DBUG_ENTER("quick_rm_table");
1574
1537
uint path_length= build_table_filename(path, sizeof(path),
1575
1538
db, table_name, reg_ext, flags);
1576
1539
if (my_delete(path,MYF(0)))
1577
1540
error= 1; /* purecov: inspected */
1578
1541
path[path_length - reg_ext_length]= '\0'; // Remove reg_ext
1579
DBUG_RETURN(ha_delete_table(current_thd, base, path, db, table_name, 0) ||
1542
return(ha_delete_table(current_thd, base, path, db, table_name, 0) ||
1602
1565
if (!(b_flags & HA_NOSAME))
1604
if ((a_flags ^ b_flags) & (HA_NULL_PART_KEY | HA_END_SPACE_KEY))
1567
if ((a_flags ^ b_flags) & (HA_NULL_PART_KEY))
1606
1569
/* Sort NOT NULL keys before other keys */
1607
return (a_flags & (HA_NULL_PART_KEY | HA_END_SPACE_KEY)) ? 1 : -1;
1570
return (a_flags & (HA_NULL_PART_KEY)) ? 1 : -1;
1609
1572
if (a->name == primary_key_name)
1726
1689
int prepare_create_field(Create_field *sql_field,
1727
1690
uint *blob_columns,
1728
1691
int *timestamps, int *timestamps_with_niladic,
1729
longlong table_flags)
1692
longlong table_flags __attribute__((__unused__)))
1731
1694
unsigned int dup_val_count;
1732
DBUG_ENTER("prepare_field");
1735
1697
This code came from mysql_prepare_create_table.
1736
1698
Indent preserved to make patching easier
1738
DBUG_ASSERT(sql_field->charset);
1700
assert(sql_field->charset);
1740
1702
switch (sql_field->sql_type) {
1741
1703
case MYSQL_TYPE_BLOB:
1752
1714
(*blob_columns)++;
1754
1716
case MYSQL_TYPE_VARCHAR:
1755
#ifndef QQ_ALL_HANDLERS_SUPPORT_VARCHAR
1756
if (table_flags & HA_NO_VARCHAR)
1758
/* convert VARCHAR to CHAR because handler is not yet up to date */
1759
sql_field->sql_type= MYSQL_TYPE_VAR_STRING;
1760
sql_field->pack_length= calc_pack_length(sql_field->sql_type,
1761
(uint) sql_field->length);
1762
if ((sql_field->length / sql_field->charset->mbmaxlen) >
1763
MAX_FIELD_CHARLENGTH)
1765
my_printf_error(ER_TOO_BIG_FIELDLENGTH, ER(ER_TOO_BIG_FIELDLENGTH),
1766
MYF(0), sql_field->field_name, MAX_FIELD_CHARLENGTH);
1772
1717
case MYSQL_TYPE_STRING:
1773
1718
sql_field->pack_flag=0;
1774
1719
if (sql_field->charset->state & MY_CS_BINSORT)
1794
1739
if (check_duplicates_in_interval("SET",sql_field->field_name,
1795
1740
sql_field->interval,
1796
1741
sql_field->charset, &dup_val_count))
1798
1743
/* Check that count of unique members is not more then 64 */
1799
1744
if (sql_field->interval->count - dup_val_count > sizeof(longlong)*8)
1801
1746
my_error(ER_TOO_BIG_SET, MYF(0), sql_field->field_name);
1805
case MYSQL_TYPE_DATE: // Rest of string types
1806
case MYSQL_TYPE_NEWDATE:
1750
case MYSQL_TYPE_NEWDATE: // Rest of string types
1807
1751
case MYSQL_TYPE_TIME:
1808
1752
case MYSQL_TYPE_DATETIME:
1809
1753
case MYSQL_TYPE_NULL:
1810
1754
sql_field->pack_flag=f_settype((uint) sql_field->sql_type);
1812
case MYSQL_TYPE_BIT:
1814
We have sql_field->pack_flag already set here, see
1815
mysql_prepare_create_table().
1818
1756
case MYSQL_TYPE_NEWDECIMAL:
1819
1757
sql_field->pack_flag=(FIELDFLAG_NUMBER |
1820
1758
(sql_field->flags & UNSIGNED_FLAG ? 0 :
1903
1841
List_iterator<Create_field> it(alter_info->create_list);
1904
1842
List_iterator<Create_field> it2(alter_info->create_list);
1905
1843
uint total_uneven_bit_length= 0;
1906
DBUG_ENTER("mysql_prepare_create_table");
1908
1845
select_field_pos= alter_info->create_list.elements - select_field_count;
1909
1846
null_fields=blob_columns=0;
1940
1877
strmake(strmake(tmp, save_cs->csname, sizeof(tmp)-4),
1941
1878
STRING_WITH_LEN("_bin"));
1942
1879
my_error(ER_UNKNOWN_COLLATION, MYF(0), tmp);
2001
1938
int comma_length= cs->cset->wc_mb(cs, ',', (uchar*) comma_buf,
2002
1939
(uchar*) comma_buf +
2003
1940
sizeof(comma_buf));
2004
DBUG_ASSERT(comma_length > 0);
1941
assert(comma_length > 0);
2005
1942
for (uint i= 0; (tmp= int_it++); i++)
2073
2010
else /* MYSQL_TYPE_ENUM */
2075
2012
uint32 field_length;
2076
DBUG_ASSERT(sql_field->sql_type == MYSQL_TYPE_ENUM);
2013
assert(sql_field->sql_type == MYSQL_TYPE_ENUM);
2077
2014
if (sql_field->def != NULL)
2079
2016
String str, *def= sql_field->def->val_str(&str);
2103
2040
set_if_smaller(sql_field->length, MAX_FIELD_WIDTH-1);
2106
if (sql_field->sql_type == MYSQL_TYPE_BIT)
2108
sql_field->pack_flag= FIELDFLAG_NUMBER;
2109
if (file->ha_table_flags() & HA_CAN_BIT_FIELD)
2110
total_uneven_bit_length+= sql_field->length & 7;
2112
sql_field->pack_flag|= FIELDFLAG_TREAT_BIT_AS_CHAR;
2115
2043
sql_field->create_length_to_internal_length();
2116
2044
if (prepare_blob_field(thd, sql_field))
2119
2047
if (!(sql_field->flags & NOT_NULL_FLAG))
2185
2113
while ((sql_field=it++))
2187
DBUG_ASSERT(sql_field->charset != 0);
2115
assert(sql_field->charset != 0);
2189
2117
if (prepare_create_field(sql_field, &blob_columns,
2190
2118
×tamps, ×tamps_with_niladic,
2191
2119
file->ha_table_flags()))
2193
2121
if (sql_field->sql_type == MYSQL_TYPE_VARCHAR)
2194
create_info->varchar= TRUE;
2122
create_info->varchar= true;
2195
2123
sql_field->offset= record_offset;
2196
2124
if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
2197
2125
auto_increment++;
2202
2130
my_message(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS,
2203
2131
ER(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS), MYF(0));
2206
2134
if (auto_increment > 1)
2208
2136
my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
2211
2139
if (auto_increment &&
2212
2140
(file->ha_table_flags() & HA_NO_AUTO_INCREMENT))
2214
2142
my_message(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT,
2215
2143
ER(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT), MYF(0));
2219
2147
if (blob_columns && (file->ha_table_flags() & HA_NO_BLOBS))
2221
2149
my_message(ER_TABLE_CANT_HANDLE_BLOB, ER(ER_TABLE_CANT_HANDLE_BLOB),
2226
2154
/* Create keys */
2261
2187
if (key->columns.elements > tmp)
2263
2189
my_error(ER_TOO_MANY_KEY_PARTS,MYF(0),tmp);
2266
2192
if (check_identifier_name(&key->name, ER_TOO_LONG_IDENT))
2268
2194
key_iterator2.rewind ();
2269
2195
if (key->type != Key::FOREIGN_KEY)
2303
2229
!my_strcasecmp(system_charset_info,key->name.str, primary_key_name))
2305
2231
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.str);
2309
2235
tmp=file->max_keys();
2310
2236
if (*key_count > tmp)
2312
2238
my_error(ER_TOO_MANY_KEYS,MYF(0),tmp);
2316
2242
(*key_info_buffer)= key_info= (KEY*) sql_calloc(sizeof(KEY) * (*key_count));
2317
2243
key_part_info=(KEY_PART_INFO*) sql_calloc(sizeof(KEY_PART_INFO)*key_parts);
2318
2244
if (!*key_info_buffer || ! key_part_info)
2319
DBUG_RETURN(TRUE); // Out of memory
2245
return(true); // Out of memory
2321
2247
key_iterator.rewind();
2377
2303
my_error(ER_WRONG_STRING_LENGTH, MYF(0),
2378
2304
key->key_create_info.comment.str,"INDEX COMMENT",
2379
2305
(uint) INDEX_COMMENT_MAXLEN);
2383
2309
key_info->comment.length= key->key_create_info.comment.length;
2425
2351
if (!(file->ha_table_flags() & HA_CAN_INDEX_BLOBS))
2427
2353
my_error(ER_BLOB_USED_AS_KEY, MYF(0), column->field_name.str);
2430
2356
if (!column->length)
2432
2358
my_error(ER_BLOB_KEY_WITHOUT_LENGTH, MYF(0), column->field_name.str);
2436
2362
if (!(sql_field->flags & NOT_NULL_FLAG))
2477
2403
/* not a critical problem */
2478
2404
char warn_buff[MYSQL_ERRMSG_SIZE];
2479
my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
2405
snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
2481
2407
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2482
2408
ER_TOO_LONG_KEY, warn_buff);
2483
2409
/* Align key length to multibyte char boundary */
2516
2442
/* not a critical problem */
2517
2443
char warn_buff[MYSQL_ERRMSG_SIZE];
2518
my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
2444
snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
2520
2446
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2521
2447
ER_TOO_LONG_KEY, warn_buff);
2522
2448
/* Align key length to multibyte char boundary */
2593
2519
(file->ha_table_flags() & HA_REQUIRE_PRIMARY_KEY))
2595
2521
my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
2598
2524
if (auto_increment > 0)
2600
2526
my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
2603
2529
/* Sort keys in optimized order */
2604
2530
my_qsort((uchar*) *key_info_buffer, *key_count, sizeof(KEY),
2685
2611
In this case the error is given
2688
static bool prepare_blob_field(THD *thd, Create_field *sql_field)
2614
static bool prepare_blob_field(THD *thd __attribute__((__unused__)),
2615
Create_field *sql_field)
2690
DBUG_ENTER("prepare_blob_field");
2692
2618
if (sql_field->length > MAX_FIELD_VARCHARLENGTH &&
2693
2619
!(sql_field->flags & BLOB_FLAG))
2695
2621
my_error(ER_TOO_BIG_FIELDLENGTH, MYF(0), sql_field->field_name,
2696
2622
MAX_FIELD_VARCHARLENGTH / sql_field->charset->mbmaxlen);
2700
2626
if ((sql_field->flags & BLOB_FLAG) && sql_field->length)
2750
2676
set_if_smaller(sql_field->length, MAX_FIELD_WIDTH-1);
2753
if (sql_field->sql_type == MYSQL_TYPE_BIT)
2755
sql_field->pack_flag= FIELDFLAG_NUMBER |
2756
FIELDFLAG_TREAT_BIT_AS_CHAR;
2758
2679
sql_field->create_length_to_internal_length();
2759
DBUG_ASSERT(sql_field->def == 0);
2680
assert(sql_field->def == 0);
2760
2681
/* Can't go wrong as sql_field->def is not defined */
2761
2682
(void) prepare_blob_field(thd, sql_field);
2808
2729
uint db_options, key_count;
2809
2730
KEY *key_info_buffer;
2812
DBUG_ENTER("mysql_create_table_no_lock");
2813
DBUG_PRINT("enter", ("db: '%s' table: '%s' tmp: %d",
2814
db, table_name, internal_tmp_table));
2817
2733
/* Check for duplicate fields and check type of table to create */
2818
2734
if (!alter_info->create_list.elements)
2820
2736
my_message(ER_TABLE_MUST_HAVE_COLUMNS, ER(ER_TABLE_MUST_HAVE_COLUMNS),
2824
2740
if (check_engine(thd, table_name, create_info))
2826
2742
db_options= create_info->table_options;
2827
2743
if (create_info->row_type == ROW_TYPE_DYNAMIC)
2828
2744
db_options|=HA_OPTION_PACK_RECORD;
2856
2772
if (strchr(alias, FN_DEVCHAR))
2858
2774
my_error(ER_WRONG_TABLE_NAME, MYF(0), alias);
2862
2778
path_length= build_table_filename(path, sizeof(path), db, alias, reg_ext,
2919
2835
bool create_if_not_exists =
2920
2836
create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS;
2921
2837
int retcode = ha_table_exists_in_engine(thd, db, table_name);
2922
DBUG_PRINT("info", ("exists_in_engine: %u",retcode));
2923
2838
switch (retcode)
2925
2840
case HA_ERR_NO_SUCH_TABLE:
2926
2841
/* Normal case, no table exists. we can go and create it */
2928
2843
case HA_ERR_TABLE_EXIST:
2929
DBUG_PRINT("info", ("Table existed in handler"));
2931
2845
if (create_if_not_exists)
2996
2909
(!thd->current_stmt_binlog_row_based ||
2997
2910
(thd->current_stmt_binlog_row_based &&
2998
2911
!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
2999
write_bin_log(thd, TRUE, thd->query, thd->query_length);
2912
write_bin_log(thd, true, thd->query, thd->query_length);
3001
2914
unlock_and_end:
3002
2915
VOID(pthread_mutex_unlock(&LOCK_open));
3005
2918
thd_proc_info(thd, "After create");
3011
2924
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
3012
2925
ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
3084
2996
pthread_mutex_lock(&LOCK_open);
3085
unlink_open_table(thd, name_lock, FALSE);
2997
unlink_open_table(thd, name_lock, false);
3086
2998
pthread_mutex_unlock(&LOCK_open);
3088
3000
pthread_mutex_lock(&LOCK_lock_db);
3089
3001
if (!--creating_table && creating_database)
3090
3002
pthread_cond_signal(&COND_refresh);
3091
3003
pthread_mutex_unlock(&LOCK_lock_db);
3092
DBUG_RETURN(result);
3169
3081
char tmp_name[NAME_LEN+1];
3172
DBUG_ENTER("mysql_rename_table");
3173
DBUG_PRINT("enter", ("old: '%s'.'%s' new: '%s'.'%s'",
3174
old_db, old_name, new_db, new_name));
3176
3085
file= (base == NULL ? 0 :
3177
3086
get_new_handler((TABLE_SHARE*) 0, thd->mem_root, base));
3242
3151
void wait_while_table_is_used(THD *thd, TABLE *table,
3243
3152
enum ha_extra_function function)
3245
DBUG_ENTER("wait_while_table_is_used");
3246
DBUG_PRINT("enter", ("table: '%s' share: 0x%lx db_stat: %u version: %lu",
3247
table->s->table_name.str, (ulong) table->s,
3248
table->db_stat, table->s->version));
3250
3155
safe_mutex_assert_owner(&LOCK_open);
3252
3157
VOID(table->file->extra(function));
3253
3158
/* Mark all tables that are in use as 'old' */
3254
mysql_lock_abort(thd, table, TRUE); /* end threads waiting on lock */
3159
mysql_lock_abort(thd, table, true); /* end threads waiting on lock */
3256
3161
/* Wait until all there are no other threads that has this table open */
3257
3162
remove_table_from_cache(thd, table->s->db.str,
3258
3163
table->s->table_name.str,
3259
3164
RTFC_WAIT_OTHER_THREAD_FLAG);
3280
3185
void close_cached_table(THD *thd, TABLE *table)
3282
DBUG_ENTER("close_cached_table");
3284
3188
wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
3285
3189
/* Close lock if this is not got with LOCK TABLES */
3289
3193
thd->lock=0; // Start locked threads
3291
3195
/* Close all copies of 'table'. This also frees all LOCK TABLES lock */
3292
unlink_open_table(thd, table, TRUE);
3196
unlink_open_table(thd, table, true);
3294
3198
/* When lock on LOCK_open is freed other threads can continue */
3295
3199
broadcast_refresh();
3299
3203
static int send_check_errmsg(THD *thd, TABLE_LIST* table,
3340
3243
pthread_mutex_unlock(&LOCK_open);
3341
DBUG_RETURN(0); // Can't open frm file
3244
return(0); // Can't open frm file
3344
3247
if (open_table_from_share(thd, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
3346
3249
release_table_share(share, RELEASE_NORMAL);
3347
3250
pthread_mutex_unlock(&LOCK_open);
3348
DBUG_RETURN(0); // Out of memory
3251
return(0); // Out of memory
3350
3253
table= &tmp_table;
3351
3254
pthread_mutex_unlock(&LOCK_open);
3386
3289
if (stat(from, &stat_info))
3387
3290
goto end; // Can't use USE_FRM flag
3389
my_snprintf(tmp, sizeof(tmp), "%s-%lx_%lx",
3390
from, current_pid, thd->thread_id);
3292
snprintf(tmp, sizeof(tmp), "%s-%lx_%lx",
3293
from, current_pid, thd->thread_id);
3392
3295
/* If we could open the table, close it */
3393
3296
if (table_list->table)
3434
3337
to finish the repair in the handler later on.
3436
3339
pthread_mutex_lock(&LOCK_open);
3437
if (reopen_name_locked_table(thd, table_list, TRUE))
3340
if (reopen_name_locked_table(thd, table_list, true))
3439
3342
unlock_table_name(thd, table_list);
3440
3343
pthread_mutex_unlock(&LOCK_open);
3451
3354
closefrm(table, 1); // Free allocated memory
3452
3355
pthread_mutex_unlock(&LOCK_open);
3461
FALSE Message sent to net (admin operation went ok)
3462
TRUE Message should be sent by caller
3364
false Message sent to net (admin operation went ok)
3365
true Message should be sent by caller
3463
3366
(admin operation or network communication failed)
3465
3368
static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
3498
3400
item->maybe_null = 1;
3499
3401
if (protocol->send_fields(&field_list,
3500
3402
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
3503
mysql_ha_rm_tables(thd, tables, FALSE);
3405
mysql_ha_rm_tables(thd, tables, false);
3505
3407
for (table= tables; table; table= table->next_local)
3508
3410
char* db = table->db;
3509
3411
bool fatal_error=0;
3511
DBUG_PRINT("admin", ("table: '%s'.'%s'", table->db, table->table_name));
3512
DBUG_PRINT("admin", ("extra_open_options: %u", extra_open_options));
3513
3413
strxmov(table_name, db, ".", table->table_name, NullS);
3514
3414
thd->open_options|= extra_open_options;
3515
3415
table->lock_type= lock_type;
3539
3439
table->next_local= save_next_local;
3540
3440
thd->open_options&= ~extra_open_options;
3542
DBUG_PRINT("admin", ("table: 0x%lx", (long) table->table));
3544
3443
if (prepare_func)
3546
DBUG_PRINT("admin", ("calling prepare_func"));
3547
3445
switch ((*prepare_func)(thd, table, check_opt)) {
3548
3446
case 1: // error, message written to net
3549
3447
ha_autocommit_or_rollback(thd, 1);
3550
3448
end_trans(thd, ROLLBACK);
3551
3449
close_thread_tables(thd);
3552
DBUG_PRINT("admin", ("simple error, admin next table"));
3554
3451
case -1: // error, message could be written to net
3555
3452
/* purecov: begin inspected */
3556
DBUG_PRINT("admin", ("severe error, stop"));
3558
3454
/* purecov: end */
3559
3455
default: // should be 0 otherwise
3560
DBUG_PRINT("admin", ("prepare_func succeeded"));
3573
3468
if (!table->table)
3575
DBUG_PRINT("admin", ("open table failed"));
3576
3470
if (!thd->warn_list.elements)
3577
3471
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
3578
3472
ER_CHECK_NO_SUCH_TABLE, ER(ER_CHECK_NO_SUCH_TABLE));
3584
3478
/* purecov: begin inspected */
3585
3479
char buff[FN_REFLEN + MYSQL_ERRMSG_SIZE];
3587
DBUG_PRINT("admin", ("sending error message"));
3588
3481
protocol->prepare_for_resend();
3589
3482
protocol->store(table_name, system_charset_info);
3590
3483
protocol->store(operator_name, system_charset_info);
3591
3484
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
3592
length= my_snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
3485
length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
3594
3487
protocol->store(buff, length, system_charset_info);
3595
3488
ha_autocommit_or_rollback(thd, 0);
3596
3489
end_trans(thd, COMMIT);
3597
3490
close_thread_tables(thd);
3598
lex->reset_query_tables_list(FALSE);
3491
lex->reset_query_tables_list(false);
3599
3492
table->table=0; // For query cache
3600
3493
if (protocol->write())
3606
3499
/* Close all instances of the table to allow repair to rename files */
3607
3500
if (lock_type == TL_WRITE && table->table->s->version)
3609
DBUG_PRINT("admin", ("removing table from cache"));
3610
3502
pthread_mutex_lock(&LOCK_open);
3611
3503
const char *old_message=thd->enter_cond(&COND_refresh, &LOCK_open,
3612
3504
"Waiting to get writelock");
3613
mysql_lock_abort(thd,table->table, TRUE);
3505
mysql_lock_abort(thd,table->table, true);
3614
3506
remove_table_from_cache(thd, table->table->s->db.str,
3615
3507
table->table->s->table_name.str,
3616
3508
RTFC_WAIT_OTHER_THREAD_FLAG |
3625
3517
if (table->table->s->crashed && operator_func == &handler::ha_check)
3627
3519
/* purecov: begin inspected */
3628
DBUG_PRINT("admin", ("sending crashed warning"));
3629
3520
protocol->prepare_for_resend();
3630
3521
protocol->store(table_name, system_charset_info);
3631
3522
protocol->store(operator_name, system_charset_info);
3644
3535
(table->table->file->ha_check_for_upgrade(check_opt) ==
3645
3536
HA_ADMIN_NEEDS_ALTER))
3647
DBUG_PRINT("admin", ("recreating table"));
3648
3538
ha_autocommit_or_rollback(thd, 1);
3649
3539
close_thread_tables(thd);
3650
3540
tmp_disable_binlog(thd); // binlogging is done by caller if wanted
3665
DBUG_PRINT("admin", ("calling operator_func '%s'", operator_name));
3666
3555
result_code = (table->table->file->*operator_func)(thd, check_opt);
3667
DBUG_PRINT("admin", ("operator_func returned: %d", result_code));
3694
3582
send_result_message:
3696
DBUG_PRINT("info", ("result_code: %d", result_code));
3697
3584
switch (result_code) {
3698
3585
case HA_ADMIN_NOT_IMPLEMENTED:
3700
3587
char buf[ERRMSGSIZE+20];
3701
uint length=my_snprintf(buf, ERRMSGSIZE,
3702
ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
3588
uint length=snprintf(buf, ERRMSGSIZE,
3589
ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
3703
3590
protocol->store(STRING_WITH_LEN("note"), system_charset_info);
3704
3591
protocol->store(buf, length, system_charset_info);
3708
3595
case HA_ADMIN_NOT_BASE_TABLE:
3710
3597
char buf[ERRMSGSIZE+20];
3711
uint length= my_snprintf(buf, ERRMSGSIZE,
3712
ER(ER_BAD_TABLE_ERROR), table_name);
3598
uint length= snprintf(buf, ERRMSGSIZE,
3599
ER(ER_BAD_TABLE_ERROR), table_name);
3713
3600
protocol->store(STRING_WITH_LEN("note"), system_charset_info);
3714
3601
protocol->store(buf, length, system_charset_info);
3828
3715
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
3829
length=my_snprintf(buf, ERRMSGSIZE, ER(ER_TABLE_NEEDS_UPGRADE), table->table_name);
3716
length=snprintf(buf, ERRMSGSIZE, ER(ER_TABLE_NEEDS_UPGRADE), table->table_name);
3830
3717
protocol->store(buf, length, system_charset_info);
3835
3722
default: // Probably HA_ADMIN_INTERNAL_ERROR
3837
3724
char buf[ERRMSGSIZE+20];
3838
uint length=my_snprintf(buf, ERRMSGSIZE,
3839
"Unknown - internal error %d during operation",
3725
uint length=snprintf(buf, ERRMSGSIZE,
3726
"Unknown - internal error %d during operation",
3841
3728
protocol->store(STRING_WITH_LEN("error"), system_charset_info);
3842
3729
protocol->store(buf, length, system_charset_info);
3878
3765
close_thread_tables(thd); // Shouldn't be needed
3880
3767
table->table=0;
3885
3772
bool mysql_repair_table(THD* thd, TABLE_LIST* tables, HA_CHECK_OPT* check_opt)
3887
DBUG_ENTER("mysql_repair_table");
3888
DBUG_RETURN(mysql_admin_table(thd, tables, check_opt,
3774
return(mysql_admin_table(thd, tables, check_opt,
3889
3775
"repair", TL_WRITE, 1,
3890
3776
test(check_opt->sql_flags & TT_USEFRM),
3891
3777
HA_OPEN_FOR_REPAIR,
3897
3783
bool mysql_optimize_table(THD* thd, TABLE_LIST* tables, HA_CHECK_OPT* check_opt)
3899
DBUG_ENTER("mysql_optimize_table");
3900
DBUG_RETURN(mysql_admin_table(thd, tables, check_opt,
3785
return(mysql_admin_table(thd, tables, check_opt,
3901
3786
"optimize", TL_WRITE, 1,0,0,0,
3902
3787
&handler::ha_optimize));
3930
3814
pthread_mutex_unlock(&LOCK_global_system_variables);
3931
3815
my_error(ER_UNKNOWN_KEY_CACHE, MYF(0), key_cache_name->str);
3934
3818
pthread_mutex_unlock(&LOCK_global_system_variables);
3935
3819
check_opt.key_cache= key_cache;
3936
DBUG_RETURN(mysql_admin_table(thd, tables, &check_opt,
3820
return(mysql_admin_table(thd, tables, &check_opt,
3937
3821
"assign_to_keycache", TL_READ_NO_INSERT, 0, 0,
3938
3822
0, 0, &handler::assign_to_keycache));
3967
int reassign_keycache_tables(THD *thd, KEY_CACHE *src_cache,
3968
KEY_CACHE *dst_cache)
3851
int reassign_keycache_tables(THD *thd __attribute__((__unused__)),
3852
KEY_CACHE *src_cache,
3853
KEY_CACHE *dst_cache)
3970
DBUG_ENTER("reassign_keycache_tables");
3972
DBUG_ASSERT(src_cache != dst_cache);
3973
DBUG_ASSERT(src_cache->in_init);
3855
assert(src_cache != dst_cache);
3856
assert(src_cache->in_init);
3974
3857
src_cache->param_buff_size= 0; // Free key cache
3975
3858
ha_resize_key_cache(src_cache);
3976
3859
ha_change_key_cache(src_cache, dst_cache);
3982
Preload specified indexes for a table into key cache
3985
mysql_preload_keys()
3987
tables Table list (one table only)
3994
bool mysql_preload_keys(THD* thd, TABLE_LIST* tables)
3996
DBUG_ENTER("mysql_preload_keys");
3998
We cannot allow concurrent inserts. The storage engine reads
3999
directly from the index file, bypassing the cache. It could read
4000
outdated information if parallel inserts into cache blocks happen.
4002
DBUG_RETURN(mysql_admin_table(thd, tables, 0,
4003
"preload_keys", TL_READ_NO_INSERT, 0, 0, 0, 0,
4004
&handler::preload_keys));
4010
3864
@brief Create frm file based on I_S table
4028
3880
bool tmp_table= (create_info->options & HA_LEX_CREATE_TMP_TABLE);
4029
3881
uint keys= schema_table->table->s->keys;
4030
3882
uint db_options= 0;
4031
DBUG_ENTER("mysql_create_like_schema_frm");
4033
3884
bzero((char*) &local_create_info, sizeof(local_create_info));
4034
3885
local_create_info.db_type= schema_table->table->s->db_type();
4038
3889
schema_table->table->use_all_columns();
4039
3890
if (mysql_prepare_alter_table(thd, schema_table->table,
4040
3891
&local_create_info, &alter_info))
4042
3893
if (mysql_prepare_create_table(thd, &local_create_info, &alter_info,
4043
3894
tmp_table, &db_options,
4044
3895
schema_table->table->file,
4045
3896
&schema_table->table->s->key_info, &keys, 0))
4047
3898
local_create_info.max_rows= 0;
4048
3899
if (mysql_create_frm(thd, dst_path, NullS, NullS,
4049
3900
&local_create_info, alter_info.create_list,
4050
3901
keys, schema_table->table->s->key_info,
4051
3902
schema_table->table->file))
4229
4079
table->table= name_lock;
4230
4080
VOID(pthread_mutex_lock(&LOCK_open));
4231
if (reopen_name_locked_table(thd, table, FALSE))
4081
if (reopen_name_locked_table(thd, table, false))
4233
4083
VOID(pthread_mutex_unlock(&LOCK_open));
4236
4086
VOID(pthread_mutex_unlock(&LOCK_open));
4238
IF_DBUG(int result=) store_create_info(thd, table, &query,
4088
int result= store_create_info(thd, table, &query,
4241
DBUG_ASSERT(result == 0); // store_create_info() always return 0
4242
write_bin_log(thd, TRUE, query.ptr(), query.length());
4091
assert(result == 0); // store_create_info() always return 0
4092
write_bin_log(thd, true, query.ptr(), query.length());
4245
write_bin_log(thd, TRUE, thd->query, thd->query_length);
4095
write_bin_log(thd, true, thd->query, thd->query_length);
4248
4098
Case 3 and 4 does nothing under RBR
4252
write_bin_log(thd, TRUE, thd->query, thd->query_length);
4102
write_bin_log(thd, true, thd->query, thd->query_length);
4258
4108
if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
4260
4110
char warn_buff[MYSQL_ERRMSG_SIZE];
4261
my_snprintf(warn_buff, sizeof(warn_buff),
4262
ER(ER_TABLE_EXISTS_ERROR), table_name);
4111
snprintf(warn_buff, sizeof(warn_buff),
4112
ER(ER_TABLE_EXISTS_ERROR), table_name);
4263
4113
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
4264
4114
ER_TABLE_EXISTS_ERROR,warn_buff);
4268
4118
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
4273
4123
pthread_mutex_lock(&LOCK_open);
4274
unlink_open_table(thd, name_lock, FALSE);
4124
unlink_open_table(thd, name_lock, false);
4275
4125
pthread_mutex_unlock(&LOCK_open);
4283
4133
thr_lock_type lock_type = TL_READ_NO_INSERT;
4285
DBUG_ENTER("mysql_analyze_table");
4286
DBUG_RETURN(mysql_admin_table(thd, tables, check_opt,
4135
return(mysql_admin_table(thd, tables, check_opt,
4287
4136
"analyze", lock_type, 1, 0, 0, 0,
4288
4137
&handler::ha_analyze));
4294
4143
thr_lock_type lock_type = TL_READ_NO_INSERT;
4296
DBUG_ENTER("mysql_check_table");
4297
DBUG_RETURN(mysql_admin_table(thd, tables, check_opt,
4145
return(mysql_admin_table(thd, tables, check_opt,
4298
4146
"check", lock_type,
4299
4147
0, 0, HA_OPEN_FOR_REPAIR, 0,
4300
4148
&handler::ha_check));
4325
4172
We set this flag so that ha_innobase::open and ::external_lock() do
4326
4173
not complain when we lock the table
4328
thd->tablespace_op= TRUE;
4175
thd->tablespace_op= true;
4329
4176
if (!(table=open_ltable(thd, table_list, TL_WRITE, 0)))
4331
thd->tablespace_op=FALSE;
4178
thd->tablespace_op=false;
4335
4182
error= table->file->ha_discard_or_import_tablespace(discard);
4348
write_bin_log(thd, FALSE, thd->query, thd->query_length);
4195
write_bin_log(thd, false, thd->query, thd->query_length);
4351
4198
ha_autocommit_or_rollback(thd, error);
4352
thd->tablespace_op=FALSE;
4199
thd->tablespace_op=false;
4354
4201
if (error == 0)
4360
4207
table->file->print_error(error, MYF(0));
4480
4326
&ha_alter_info->key_info_buffer,
4481
4327
&ha_alter_info->key_count,
4482
4328
/* select_field_count */ 0))
4484
4330
/* Allocate result buffers. */
4485
4331
if (! (ha_alter_info->index_drop_buffer=
4486
4332
(uint*) thd->alloc(sizeof(uint) * table->s->keys)) ||
4487
4333
! (ha_alter_info->index_add_buffer=
4488
4334
(uint*) thd->alloc(sizeof(uint) *
4489
4335
tmp_alter_info.key_list.elements)))
4493
4339
First we setup ha_alter_flags based on what was detected
4496
4342
setup_ha_alter_flags(alter_info, alter_flags);
4500
char dbug_string[HA_MAX_ALTER_FLAGS+1];
4501
alter_flags->print(dbug_string);
4502
DBUG_PRINT("info", ("alter_flags: %s", (char *) dbug_string));
4507
4346
Some very basic checks. If number of fields changes, or the
4627
4466
KEY *new_key_end=
4628
4467
ha_alter_info->key_info_buffer + ha_alter_info->key_count;
4630
DBUG_PRINT("info", ("index count old: %d new: %d",
4631
table->s->keys, ha_alter_info->key_count));
4633
4470
Step through all keys of the old table and search matching new keys.
4783
4618
*alter_flags|= HA_ADD_INDEX;
4784
4619
*table_changes= IS_EQUAL_NO;
4785
DBUG_PRINT("info", ("index added: '%s'", new_key->name));
4790
char dbug_string[HA_MAX_ALTER_FLAGS+1];
4791
alter_flags->print(dbug_string);
4792
DBUG_PRINT("info", ("alter_flags: %s", (char *) dbug_string));
4817
4644
enum enum_enable_or_disable keys_onoff)
4820
DBUG_ENTER("alter_table_manage_keys");
4821
DBUG_PRINT("enter", ("table=%p were_disabled=%d on_off=%d",
4822
table, indexes_were_disabled, keys_onoff));
4824
4647
switch (keys_onoff) {
4826
4649
error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4857
4680
char index_file[FN_REFLEN], data_file[FN_REFLEN];
4858
4681
handlerton *old_db_type, *new_db_type;
4859
DBUG_ENTER("create_temporary_table");
4860
4682
old_db_type= table->s->db_type();
4861
4683
new_db_type= create_info->db_type;
4906
4728
create_info->data_file_name=create_info->index_file_name=0;
4908
if (new_db_type == old_db_type)
4911
Table has not changed storage engine.
4912
If STORAGE and TABLESPACE have not been changed than copy them
4913
from the original table
4915
if (!create_info->tablespace &&
4916
table->s->tablespace &&
4917
create_info->default_storage_media == HA_SM_DEFAULT)
4918
create_info->tablespace= table->s->tablespace;
4919
if (create_info->default_storage_media == HA_SM_DEFAULT)
4920
create_info->default_storage_media= table->s->default_storage_media;
4924
4731
Create a table with a temporary name.
4925
4732
With create_info->frm_only == 1 this creates a .frm file only.
4965
4772
TABLE *altered_table;
4966
4773
char tmp_name[80];
4967
4774
char path[FN_REFLEN];
4968
DBUG_ENTER("create_altered_table");
4970
my_snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx",
4971
tmp_file_prefix, current_pid, thd->thread_id);
4776
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx",
4777
tmp_file_prefix, current_pid, thd->thread_id);
4972
4778
/* Safety fix for InnoDB */
4973
4779
if (lower_case_table_names)
4974
4780
my_casedn_str(files_charset_info, tmp_name);
4978
4784
&altered_create_info,
4979
4785
alter_info, db_change)))
4981
DBUG_PRINT("info", ("Error %u while creating temporary table", error));
4985
4790
build_table_filename(path, sizeof(path), new_db, tmp_name, "",
4987
4792
altered_table= open_temporary_table(thd, path, new_db, tmp_name, 1,
4989
DBUG_RETURN(altered_table);
4794
return(altered_table);
5120
4924
state of the TABLE object which we used for obtaining of handler
5121
4925
object to make it suitable for reopening.
5123
DBUG_ASSERT(t_table == table);
4927
assert(t_table == table);
5124
4928
table->open_placeholder= 1;
5125
4929
VOID(pthread_mutex_lock(&LOCK_open));
5126
4930
close_handle_and_leave_table_as_lock(table);
5170
4974
Sets create_info->varchar if the table has a VARCHAR column.
5171
4975
Prepares alter_info->create_list and alter_info->key_list with
5172
4976
columns and keys of the new table.
5173
@retval TRUE error, out of memory or a semantical error in ALTER
4977
@retval true error, out of memory or a semantical error in ALTER
5174
4978
TABLE instructions
5175
@retval FALSE success
4979
@retval false success
5195
4999
& ~(HA_OPTION_PACK_RECORD));
5196
5000
uint used_fields= create_info->used_fields;
5197
5001
KEY *key_info=table->key_info;
5200
DBUG_ENTER("mysql_prepare_alter_table");
5202
create_info->varchar= FALSE;
5005
create_info->varchar= false;
5203
5006
/* Let new create options override the old ones */
5204
5007
if (!(used_fields & HA_CREATE_USED_MIN_ROWS))
5205
5008
create_info->min_rows= table->s->min_rows;
5316
5119
If the '0000-00-00' value isn't allowed then raise the error_if_not_empty
5317
5120
flag to allow ALTER TABLE only if the table to be altered is empty.
5319
if ((def->sql_type == MYSQL_TYPE_DATE ||
5320
def->sql_type == MYSQL_TYPE_NEWDATE ||
5122
if ((def->sql_type == MYSQL_TYPE_NEWDATE ||
5321
5123
def->sql_type == MYSQL_TYPE_DATETIME) &&
5322
5124
!alter_info->datetime_field &&
5323
5125
!(~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) &&
5324
5126
thd->variables.sql_mode & MODE_NO_ZERO_DATE)
5326
5128
alter_info->datetime_field= def;
5327
alter_info->error_if_not_empty= TRUE;
5129
alter_info->error_if_not_empty= true;
5329
5131
if (!def->after)
5330
5132
new_create_list.push_back(def);
5595
5397
handlerton *old_db_type, *new_db_type, *save_old_db_type;
5596
5398
legacy_db_type table_type;
5597
5399
frm_type_enum frm_type;
5598
DBUG_ENTER("mysql_alter_table");
5600
5401
if (table_list && table_list->schema_table)
5602
5403
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
5617
5418
build_table_filename(path, sizeof(path), db, table_name, "", 0);
5619
mysql_ha_rm_tables(thd, table_list, FALSE);
5420
mysql_ha_rm_tables(thd, table_list, false);
5621
5422
/* DISCARD/IMPORT TABLESPACE is always alone in an ALTER TABLE */
5622
5423
if (alter_info->tablespace_op != NO_TABLESPACE_OP)
5623
5424
/* Conditionally writes to binlog. */
5624
DBUG_RETURN(mysql_discard_or_import_tablespace(thd,table_list,
5625
alter_info->tablespace_op));
5425
return(mysql_discard_or_import_tablespace(thd,table_list,
5426
alter_info->tablespace_op));
5626
5427
strxnmov(new_name_buff, sizeof (new_name_buff) - 1, mysql_data_home, "/", db,
5627
5428
"/", table_name, reg_ext, NullS);
5628
5429
(void) unpack_filename(new_name_buff, new_name_buff);
5656
5457
(table->s->tmp_table == NO_TMP_TABLE))
5658
5459
my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
5662
5463
/* Check that we are not trying to rename to an existing table */
5665
DBUG_PRINT("info", ("new_db.new_name: '%s'.'%s'", new_db, new_name));
5666
5466
strmov(new_name_buff,new_name);
5667
5467
strmov(new_alias= new_alias_buff, new_name);
5668
5468
if (lower_case_table_names)
5690
5490
if (find_temporary_table(thd,new_db,new_name_buff))
5692
5492
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
5698
5498
if (lock_table_name_if_not_cached(thd, new_db, new_name, &name_lock))
5700
5500
if (!name_lock)
5702
5502
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
5706
5506
build_table_filename(new_name_buff, sizeof(new_name_buff),
5740
5540
if (create_info->row_type == ROW_TYPE_NOT_USED)
5741
5541
create_info->row_type= table->s->row_type;
5743
DBUG_PRINT("info", ("old type: %s new type: %s",
5744
ha_resolve_storage_engine_name(old_db_type),
5745
ha_resolve_storage_engine_name(new_db_type)));
5746
5543
if (ha_check_storage_engine_flag(old_db_type, HTON_ALTER_NOT_SUPPORTED) ||
5747
5544
ha_check_storage_engine_flag(new_db_type, HTON_ALTER_NOT_SUPPORTED))
5749
DBUG_PRINT("info", ("doesn't support alter"));
5750
5546
my_error(ER_ILLEGAL_HA, MYF(0), table_name);
5862
unlink_open_table(thd, name_lock, FALSE);
5658
unlink_open_table(thd, name_lock, false);
5863
5659
VOID(pthread_mutex_unlock(&LOCK_open));
5864
5660
table_list->table= NULL; // For query cache
5868
5664
/* We have to do full alter table. */
5904
5700
HA_ALTER_INFO ha_alter_info;
5905
5701
HA_ALTER_FLAGS ha_alter_flags;
5906
5702
uint table_changes= IS_EQUAL_YES;
5907
bool need_copy_table= TRUE;
5703
bool need_copy_table= true;
5908
5704
/* Check how much the tables differ. */
5909
5705
if (compare_tables(thd, table, alter_info,
5910
5706
create_info, order_num,
5925
char dbug_string[HA_MAX_ALTER_FLAGS+1];
5926
ha_alter_flags.print(dbug_string);
5927
DBUG_PRINT("info", ("need_copy_table: %u, table_changes: %u, Real alter_flags: %s",
5928
need_copy_table, table_changes,
5929
(char *) dbug_string));
5934
5721
If table is not renamed, changed database and
5976
5763
close_temporary_table(thd, altered_table, 1, 1);
5979
need_copy_table= TRUE;
5766
need_copy_table= true;
5981
5768
case HA_ALTER_ERROR:
5983
5770
close_temporary_table(thd, altered_table, 1, 1);
5988
char dbug_string[HA_MAX_ALTER_FLAGS+1];
5989
ha_alter_flags.print(dbug_string);
5990
DBUG_PRINT("info", ("need_copy_table: %u, table_changes: %u, Real alter_flags: %s",
5991
need_copy_table, table_changes,
5992
(char *) dbug_string));
5997
5775
/* TODO need to check if changes can be handled as fast ALTER TABLE */
5998
5776
if (!altered_table)
5999
need_copy_table= TRUE;
5777
need_copy_table= true;
6001
5779
if (!need_copy_table)
6034
5812
close_temporary_table(thd, altered_table, 1, 1);
6037
my_snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx", tmp_file_prefix,
6038
current_pid, thd->thread_id);
5815
snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx", tmp_file_prefix,
5816
current_pid, thd->thread_id);
6039
5817
/* Safety fix for innodb */
6040
5818
if (lower_case_table_names)
6041
5819
my_casedn_str(files_charset_info, tmp_name);
6124
5902
/* We don't replicate alter table statement on temporary tables */
6125
5903
if (!thd->current_stmt_binlog_row_based)
6126
write_bin_log(thd, TRUE, thd->query, thd->query_length);
5904
write_bin_log(thd, true, thd->query, thd->query_length);
6127
5905
goto end_temporary;
6163
5941
thd_proc_info(thd, "rename result table");
6164
my_snprintf(old_name, sizeof(old_name), "%s2-%lx-%lx", tmp_file_prefix,
6165
current_pid, thd->thread_id);
5942
snprintf(old_name, sizeof(old_name), "%s2-%lx-%lx", tmp_file_prefix,
5943
current_pid, thd->thread_id);
6166
5944
if (lower_case_table_names)
6167
5945
my_casedn_str(files_charset_info, old_name);
6224
6002
thd_proc_info(thd, "end");
6226
DBUG_EXECUTE_IF("sleep_alter_before_main_binlog", my_sleep(6000000););
6228
6004
ha_binlog_log_query(thd, create_info->db_type, LOGCOM_ALTER_TABLE,
6229
6005
thd->query, thd->query_length,
6230
6006
db, table_name);
6232
DBUG_ASSERT(!(mysql_bin_log.is_open() &&
6008
assert(!(mysql_bin_log.is_open() &&
6233
6009
thd->current_stmt_binlog_row_based &&
6234
6010
(create_info->options & HA_LEX_CREATE_TMP_TABLE)));
6235
write_bin_log(thd, TRUE, thd->query, thd->query_length);
6011
write_bin_log(thd, true, thd->query, thd->query_length);
6237
6013
if (ha_check_storage_engine_flag(old_db_type, HTON_FLUSH_AFTER_RENAME))
6244
6020
char path[FN_REFLEN];
6245
6021
TABLE *t_table;
6246
6022
build_table_filename(path, sizeof(path), new_db, table_name, "", 0);
6247
t_table= open_temporary_table(thd, path, new_db, tmp_name, FALSE, OTM_OPEN);
6023
t_table= open_temporary_table(thd, path, new_db, tmp_name, false, OTM_OPEN);
6250
6026
intern_close_table(t_table);
6266
6042
LOCK TABLES we can rely on close_thread_tables() doing this job.
6268
6044
pthread_mutex_lock(&LOCK_open);
6269
unlink_open_table(thd, table, FALSE);
6270
unlink_open_table(thd, name_lock, FALSE);
6045
unlink_open_table(thd, table, false);
6046
unlink_open_table(thd, name_lock, false);
6271
6047
pthread_mutex_unlock(&LOCK_open);
6275
my_snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
6276
(ulong) (copied + deleted), (ulong) deleted,
6277
(ulong) thd->cuted_fields);
6051
snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
6052
(ulong) (copied + deleted), (ulong) deleted,
6053
(ulong) thd->cuted_fields);
6278
6054
my_ok(thd, copied + deleted, 0L, tmp_name);
6279
6055
thd->some_tables_deleted=0;
6314
6089
/* Shouldn't get here. */
6317
6092
bool save_abort_on_warning= thd->abort_on_warning;
6318
thd->abort_on_warning= TRUE;
6093
thd->abort_on_warning= true;
6319
6094
make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
6320
6095
f_val, strlength(f_val), t_type,
6321
6096
alter_info->datetime_field->field_name);
6326
6101
pthread_mutex_lock(&LOCK_open);
6327
unlink_open_table(thd, name_lock, FALSE);
6102
unlink_open_table(thd, name_lock, false);
6328
6103
pthread_mutex_unlock(&LOCK_open);
6332
6107
err_with_placeholders:
6335
6110
being altered. To be safe under LOCK TABLES we should remove placeholders
6336
6111
from list of open tables list and table cache.
6338
unlink_open_table(thd, table, FALSE);
6113
unlink_open_table(thd, table, false);
6340
unlink_open_table(thd, name_lock, FALSE);
6115
unlink_open_table(thd, name_lock, false);
6341
6116
VOID(pthread_mutex_unlock(&LOCK_open));
6344
6119
/* mysql_alter_table */
6376
6150
This needs to be done before external_lock
6378
error= ha_enable_transaction(thd, FALSE);
6152
error= ha_enable_transaction(thd, false);
6382
6156
if (!(copy= new Copy_field[to->s->fields]))
6383
DBUG_RETURN(-1); /* purecov: inspected */
6157
return(-1); /* purecov: inspected */
6385
6159
if (to->file->ha_external_lock(thd, F_WRLCK))
6388
6162
/* We need external lock before we can disable/enable keys */
6389
6163
alter_table_manage_keys(to, from->file->indexes_are_disabled(), keys_onoff);
6407
6181
if (*ptr == to->next_number_field)
6409
auto_increment_field_copied= TRUE;
6183
auto_increment_field_copied= true;
6411
6185
If we are going to copy contents of one auto_increment column to
6412
6186
another auto_increment column it is sensible to preserve zeroes.
6428
6202
if (to->s->primary_key != MAX_KEY && to->file->primary_key_is_clustered())
6430
6204
char warn_buff[MYSQL_ERRMSG_SIZE];
6431
my_snprintf(warn_buff, sizeof(warn_buff),
6432
"ORDER BY ignored as there is a user-defined clustered index"
6433
" in the table '%-.192s'", from->s->table_name.str);
6205
snprintf(warn_buff, sizeof(warn_buff),
6206
"ORDER BY ignored as there is a user-defined clustered index"
6207
" in the table '%-.192s'", from->s->table_name.str);
6434
6208
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
6577
6351
HA_CREATE_INFO create_info;
6578
6352
Alter_info alter_info;
6580
DBUG_ENTER("mysql_recreate_table");
6581
DBUG_ASSERT(!table_list->next_global);
6354
assert(!table_list->next_global);
6583
6356
table_list->table has been closed and freed. Do not reference
6584
6357
uninitialized data. open_tables() could fail.
6590
6363
create_info.default_table_charset=default_charset_info;
6591
6364
/* Force alter table to recreate table */
6592
6365
alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
6593
DBUG_RETURN(mysql_alter_table(thd, NullS, NullS, &create_info,
6366
return(mysql_alter_table(thd, NullS, NullS, &create_info,
6594
6367
table_list, &alter_info, 0,
6595
6368
(ORDER *) 0, 0));