| 6028 | */ |
| 6029 | |
| 6030 | bool lock_tables(THD *thd, TABLE_LIST *tables, uint count, uint flags) |
| 6031 | { |
| 6032 | TABLE_LIST *table, *first_not_own; |
| 6033 | DBUG_ENTER("lock_tables"); |
| 6034 | /* |
| 6035 | We can't meet statement requiring prelocking if we already |
| 6036 | in prelocked mode. |
| 6037 | */ |
| 6038 | DBUG_ASSERT(thd->locked_tables_mode <= LTM_LOCK_TABLES || |
| 6039 | !thd->lex->requires_prelocking()); |
| 6040 | |
| 6041 | if (!tables && !thd->lex->requires_prelocking()) |
| 6042 | DBUG_RETURN(0); |
| 6043 | |
| 6044 | first_not_own= thd->lex->first_not_own_table(); |
| 6045 | |
| 6046 | /* |
| 6047 | Check for thd->locked_tables_mode to avoid a redundant |
| 6048 | and harmful attempt to lock the already locked tables again. |
| 6049 | Checking for thd->lock is not enough in some situations. For example, |
| 6050 | if a stored function contains |
| 6051 | "drop table t3; create temporary t3 ..; insert into t3 ...;" |
| 6052 | thd->lock may be 0 after drop tables, whereas locked_tables_mode |
| 6053 | is still on. In this situation an attempt to lock temporary |
| 6054 | table t3 will lead to a memory leak. |
| 6055 | */ |
| 6056 | if (! thd->locked_tables_mode) |
| 6057 | { |
| 6058 | DBUG_ASSERT(thd->lock == 0); // You must lock everything at once |
| 6059 | TABLE **start,**ptr; |
| 6060 | bool found_first_not_own= 0; |
| 6061 | |
| 6062 | if (!(ptr= start= thd->alloc<TABLE*>(count))) |
| 6063 | DBUG_RETURN(TRUE); |
| 6064 | |
| 6065 | /* |
| 6066 | Collect changes tables for table lock. |
| 6067 | Mark own tables with query id as this is needed by |
| 6068 | prepare_for_row_logging() |
| 6069 | */ |
| 6070 | for (table= tables; table; table= table->next_global) |
| 6071 | { |
| 6072 | if (table == first_not_own) |
| 6073 | found_first_not_own= 1; |
| 6074 | if (!table->placeholder()) |
| 6075 | { |
| 6076 | DBUG_ASSERT(table->lock_type != TL_IGNORE); |
| 6077 | *(ptr++)= table->table; |
| 6078 | if (!found_first_not_own) |
| 6079 | table->table->query_id= thd->query_id; |
| 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | #ifdef ENABLED_DEBUG_SYNC |
| 6084 | if (!tables || |
| 6085 | !(strcmp(tables->db.str, "mysql") == 0 && |
| 6086 | strcmp(tables->table_name.str, "proc") == 0)) |
| 6087 | DEBUG_SYNC(thd, "before_lock_tables_takes_lock"); |
no test coverage detected