18 #include "sql_class.h"
20 #include "sql_table.h"
24 #include "sql_parse.h"
25 #include "sql_truncate.h"
46 append_identifier(NULL, str, field->str, field->length);
47 res|= str->append(
", ");
69 char buffer[STRING_BUFFER_USUAL_SIZE*2];
70 String str(buffer,
sizeof(buffer), system_charset_info);
78 append_identifier(NULL, &str, fk_info->foreign_db->str,
79 fk_info->foreign_db->length);
80 res|= str.append(
".");
81 append_identifier(NULL, &str, fk_info->foreign_table->str,
82 fk_info->foreign_table->length);
83 res|= str.append(
", CONSTRAINT ");
84 append_identifier(NULL, &str, fk_info->foreign_id->str,
85 fk_info->foreign_id->length);
86 res|= str.append(
" FOREIGN KEY (");
87 res|= fk_info_append_fields(&str, &fk_info->foreign_fields);
88 res|= str.append(
") REFERENCES ");
89 append_identifier(NULL, &str, fk_info->referenced_db->str,
90 fk_info->referenced_db->length);
91 res|= str.append(
".");
92 append_identifier(NULL, &str, fk_info->referenced_table->str,
93 fk_info->referenced_table->length);
94 res|= str.append(
" (");
95 res|= fk_info_append_fields(&str, &fk_info->referenced_fields);
96 res|= str.append(
')');
98 return res ? NULL : thd->strmake(str.ptr(), str.length());
120 fk_truncate_illegal_if_parent(THD *thd,
TABLE *
table)
130 if (! table->file->referenced_by_foreign_key())
148 while ((fk_info= it++))
150 DBUG_ASSERT(!my_strcasecmp(system_charset_info,
151 fk_info->referenced_db->str,
154 DBUG_ASSERT(!my_strcasecmp(system_charset_info,
155 fk_info->referenced_table->str,
156 table->s->table_name.str));
158 if (my_strcasecmp(system_charset_info, fk_info->foreign_db->str,
160 my_strcasecmp(system_charset_info, fk_info->foreign_table->str,
161 table->s->table_name.str))
168 my_error(ER_TRUNCATE_ILLEGAL_FK, MYF(0), fk_info_str(thd, fk_info));
192 DBUG_ENTER(
"Sql_cmd_truncate_table::handler_truncate");
209 table_ref->required_type= FRMTYPE_TABLE;
215 flags= MYSQL_OPEN_IGNORE_FLUSH;
223 table_ref->mdl_request.
ticket= NULL;
232 error= fk_truncate_illegal_if_parent(thd, table_ref->table);
234 if (!error && (error= table_ref->table->file->
ha_truncate()))
235 table_ref->table->file->
print_error(error, MYF(0));
253 static bool recreate_temporary_table(THD *thd,
TABLE *table)
259 DBUG_ENTER(
"recreate_temporary_table");
261 memset(&create_info, 0,
sizeof(create_info));
263 table->file->info(HA_STATUS_AUTO | HA_STATUS_NO_LOCK);
266 close_temporary_table(thd, table, FALSE, FALSE);
274 share->table_name.str, &create_info,
true,
true);
277 share->table_name.str,
true,
true))
280 thd->thread_specific_used= TRUE;
285 free_table_share(share);
306 bool *hton_can_recreate)
309 DBUG_ENTER(
"Sql_cmd_truncate_table::lock_table");
312 DBUG_ASSERT(table_ref->lock_type == TL_WRITE);
314 DBUG_ASSERT(table_ref->mdl_request.
type == MDL_EXCLUSIVE);
329 if (thd->locked_tables_mode)
332 table_ref->table_name, FALSE)))
335 *hton_can_recreate= ha_check_storage_engine_flag(table->s->db_type(),
337 table_ref->mdl_request.
ticket= table->mdl_ticket;
342 DBUG_ASSERT(table_ref->next_global == NULL);
344 thd->variables.lock_wait_timeout, 0))
347 if (dd_check_storage_engine_flag(thd, table_ref->db, table_ref->table_name,
348 HTON_CAN_RECREATE, hton_can_recreate))
357 if (thd->locked_tables_mode)
359 DEBUG_SYNC(thd,
"upgrade_lock_for_truncate");
363 m_ticket_downgrade= table->mdl_ticket;
365 if (*hton_can_recreate)
372 table_ref->table_name, FALSE);
397 DBUG_ENTER(
"Sql_cmd_truncate_table::truncate_table");
399 DBUG_ASSERT((!table_ref->table) ||
400 (table_ref->table && table_ref->table->s));
403 m_ticket_downgrade= NULL;
406 if (is_temporary_table(table_ref))
408 TABLE *tmp_table= table_ref->table;
411 binlog_stmt= !thd->is_current_stmt_binlog_format_row();
414 if (ha_check_storage_engine_flag(tmp_table->s->db_type(), HTON_CAN_RECREATE))
416 if ((error= recreate_temporary_table(thd, tmp_table)))
419 DBUG_ASSERT(! thd->transaction.stmt.cannot_safely_rollback());
441 bool hton_can_recreate;
443 if (
lock_table(thd, table_ref, &hton_can_recreate))
446 if (hton_can_recreate)
452 error= dd_recreate_table(thd, table_ref->db, table_ref->table_name);
454 if (thd->locked_tables_mode && thd->locked_tables_list.reopen_tables(thd))
455 thd->locked_tables_list.unlink_all_closed_tables(thd, NULL, 0);
473 binlog_stmt= !error || error != HA_ERR_WRONG_COMMAND;
482 table_ref->table= NULL;
483 query_cache_invalidate3(thd, table_ref, FALSE);
488 error|= write_bin_log(thd, !error, thd->query(), thd->query_length());
495 if (m_ticket_downgrade)
512 TABLE_LIST *first_table= thd->lex->select_lex.table_list.first;
513 DBUG_ENTER(
"Sql_cmd_truncate_table::execute");