| 297 | */ |
| 298 | |
| 299 | bool Sql_cmd_truncate_table::lock_table(THD *thd, TABLE_LIST *table_ref, |
| 300 | bool *hton_can_recreate) |
| 301 | { |
| 302 | const handlerton *hton; |
| 303 | bool versioned; |
| 304 | bool sequence= false; |
| 305 | TABLE *table= NULL; |
| 306 | DBUG_ENTER("Sql_cmd_truncate_table::lock_table"); |
| 307 | |
| 308 | /* Lock types are set in the parser. */ |
| 309 | DBUG_ASSERT(table_ref->lock_type == TL_WRITE); |
| 310 | /* The handler truncate protocol dictates a exclusive lock. */ |
| 311 | DBUG_ASSERT(table_ref->mdl_request.type == MDL_EXCLUSIVE); |
| 312 | |
| 313 | /* |
| 314 | Before doing anything else, acquire a metadata lock on the table, |
| 315 | or ensure we have one. We don't use open_and_lock_tables() |
| 316 | right away because we want to be able to truncate (and recreate) |
| 317 | corrupted tables, those that we can't fully open. |
| 318 | |
| 319 | MySQL manual documents that TRUNCATE can be used to repair a |
| 320 | damaged table, i.e. a table that can not be fully "opened". |
| 321 | In particular MySQL manual says: As long as the table format |
| 322 | file tbl_name.frm is valid, the table can be re-created as |
| 323 | an empty table with TRUNCATE TABLE, even if the data or index |
| 324 | files have become corrupted. |
| 325 | */ |
| 326 | if (thd->locked_tables_mode) |
| 327 | { |
| 328 | if (!(table= find_table_for_mdl_upgrade(thd, table_ref->db.str, |
| 329 | table_ref->table_name.str, NULL))) |
| 330 | DBUG_RETURN(TRUE); |
| 331 | |
| 332 | versioned= table->versioned(); |
| 333 | hton= table->file->ht; |
| 334 | #ifdef WITH_WSREP |
| 335 | /* Resolve should we replicate truncate. It should |
| 336 | be replicated if storage engine(s) associated |
| 337 | are replicated by Galera. If this is partitioned |
| 338 | table we need to find out default partition |
| 339 | handlerton. |
| 340 | */ |
| 341 | if (WSREP(thd) && |
| 342 | !wsrep_should_replicate_ddl(thd, table->file->partition_ht() ? |
| 343 | table->file->partition_ht() : hton)) |
| 344 | DBUG_RETURN(TRUE); |
| 345 | #endif |
| 346 | |
| 347 | table_ref->mdl_request.ticket= table->mdl_ticket; |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | DBUG_ASSERT(table_ref->next_global == NULL); |
| 352 | if (lock_table_names(thd, table_ref, NULL, |
| 353 | thd->variables.lock_wait_timeout, 0)) |
| 354 | DBUG_RETURN(TRUE); |
| 355 | |
| 356 | TABLE_SHARE *share= tdc_acquire_share(thd, table_ref, GTS_TABLE | GTS_VIEW); |
nothing calls this directly
no test coverage detected