| 4163 | */ |
| 4164 | |
| 4165 | int mysql_insert_select_prepare(THD *thd, select_result *sel_res) |
| 4166 | { |
| 4167 | int res; |
| 4168 | LEX *lex= thd->lex; |
| 4169 | SELECT_LEX *select_lex= lex->first_select_lex(); |
| 4170 | bool cache_insert_values= false; |
| 4171 | DBUG_ENTER("mysql_insert_select_prepare"); |
| 4172 | |
| 4173 | /* |
| 4174 | SELECT_LEX do not belong to INSERT statement, so we can't add WHERE |
| 4175 | clause if table is VIEW |
| 4176 | */ |
| 4177 | |
| 4178 | if ((res= mysql_prepare_insert(thd, lex->query_tables, lex->field_list, 0, |
| 4179 | lex->update_list, lex->value_list, |
| 4180 | lex->duplicates, lex->ignore, |
| 4181 | &select_lex->where, TRUE, &cache_insert_values))) |
| 4182 | DBUG_RETURN(res); |
| 4183 | |
| 4184 | /* |
| 4185 | If sel_res is not empty, it means we have items in returning_list. |
| 4186 | So we prepare the list now |
| 4187 | */ |
| 4188 | if (sel_res) |
| 4189 | sel_res->prepare(lex->returning()->returning_list, NULL); |
| 4190 | |
| 4191 | DBUG_ASSERT(select_lex->leaf_tables.elements != 0); |
| 4192 | List_iterator<TABLE_LIST> ti(select_lex->leaf_tables); |
| 4193 | TABLE_LIST *table; |
| 4194 | uint insert_tables; |
| 4195 | |
| 4196 | if (select_lex->first_cond_optimization) |
| 4197 | { |
| 4198 | /* Back up leaf_tables list. */ |
| 4199 | Query_arena *arena= thd->stmt_arena, backup; |
| 4200 | arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test |
| 4201 | |
| 4202 | insert_tables= select_lex->insert_tables; |
| 4203 | while ((table= ti++) && insert_tables--) |
| 4204 | { |
| 4205 | select_lex->leaf_tables_exec.push_back(table); |
| 4206 | table->tablenr_exec= table->table->tablenr; |
| 4207 | table->map_exec= table->table->map; |
| 4208 | table->maybe_null_exec= table->table->maybe_null; |
| 4209 | } |
| 4210 | if (arena) |
| 4211 | thd->restore_active_arena(arena, &backup); |
| 4212 | } |
| 4213 | ti.rewind(); |
| 4214 | /* |
| 4215 | exclude first table from leaf tables list, because it belong to |
| 4216 | INSERT |
| 4217 | */ |
| 4218 | /* skip all leaf tables belonged to view where we are insert */ |
| 4219 | insert_tables= select_lex->insert_tables; |
| 4220 | while ((table= ti++) && insert_tables--) |
| 4221 | ti.remove(); |
| 4222 |
no test coverage detected