mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
#41760 Inserting into multiple-table views is not working
During insert, we are not reading the rows in a referring table but instead using the last read row that happens to be in table->record[0]. Now INSERT into such view is denied.
This commit is contained in:
@@ -107,8 +107,8 @@ static bool check_view_insertability(THD *thd, TABLE_LIST *view);
|
||||
1 Error
|
||||
*/
|
||||
|
||||
bool check_view_single_update(List<Item> &fields, TABLE_LIST *view,
|
||||
table_map *map)
|
||||
bool check_view_single_update(List<Item> &fields, List<Item> *values,
|
||||
TABLE_LIST *view, table_map *map)
|
||||
{
|
||||
/* it is join view => we need to find the table for update */
|
||||
List_iterator_fast<Item> it(fields);
|
||||
@@ -119,6 +119,17 @@ bool check_view_single_update(List<Item> &fields, TABLE_LIST *view,
|
||||
while ((item= it++))
|
||||
tables|= item->used_tables();
|
||||
|
||||
if (values)
|
||||
{
|
||||
it.init(*values);
|
||||
while ((item= it++))
|
||||
tables|= item->used_tables();
|
||||
}
|
||||
|
||||
/* Convert to real table bits */
|
||||
tables&= ~PSEUDO_TABLE_BITS;
|
||||
|
||||
|
||||
/* Check found map against provided map */
|
||||
if (*map)
|
||||
{
|
||||
@@ -165,7 +176,9 @@ error:
|
||||
|
||||
static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
|
||||
List<Item> &fields, List<Item> &values,
|
||||
bool check_unique, table_map *map)
|
||||
bool check_unique,
|
||||
bool fields_and_values_from_different_maps,
|
||||
table_map *map)
|
||||
{
|
||||
TABLE *table= table_list->table;
|
||||
|
||||
@@ -238,7 +251,10 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
|
||||
|
||||
if (table_list->effective_algorithm == VIEW_ALGORITHM_MERGE)
|
||||
{
|
||||
if (check_view_single_update(fields, table_list, map))
|
||||
if (check_view_single_update(fields,
|
||||
fields_and_values_from_different_maps ?
|
||||
(List<Item>*) 0 : &values,
|
||||
table_list, map))
|
||||
return -1;
|
||||
table= table_list->table;
|
||||
}
|
||||
@@ -298,7 +314,8 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
|
||||
*/
|
||||
|
||||
static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
|
||||
List<Item> &update_fields, table_map *map)
|
||||
List<Item> &update_fields,
|
||||
List<Item> &update_values, table_map *map)
|
||||
{
|
||||
TABLE *table= insert_table_list->table;
|
||||
my_bool timestamp_mark= 0;
|
||||
@@ -318,7 +335,8 @@ static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
|
||||
return -1;
|
||||
|
||||
if (insert_table_list->effective_algorithm == VIEW_ALGORITHM_MERGE &&
|
||||
check_view_single_update(update_fields, insert_table_list, map))
|
||||
check_view_single_update(update_fields, &update_values,
|
||||
insert_table_list, map))
|
||||
return -1;
|
||||
|
||||
if (table->timestamp_field)
|
||||
@@ -1242,9 +1260,9 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
|
||||
table_list->next_local= 0;
|
||||
context->resolve_in_table_list_only(table_list);
|
||||
|
||||
res= check_insert_fields(thd, context->table_list, fields, *values,
|
||||
!insert_into_view, &map) ||
|
||||
setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0);
|
||||
res= (setup_fields(thd, 0, *values, MARK_COLUMNS_READ, 0, 0) ||
|
||||
check_insert_fields(thd, context->table_list, fields, *values,
|
||||
!insert_into_view, 0, &map));
|
||||
|
||||
if (!res && check_fields)
|
||||
{
|
||||
@@ -1257,18 +1275,19 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
|
||||
thd->abort_on_warning= saved_abort_on_warning;
|
||||
}
|
||||
|
||||
if (!res)
|
||||
res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
|
||||
|
||||
if (!res && duplic == DUP_UPDATE)
|
||||
{
|
||||
select_lex->no_wrap_view_item= TRUE;
|
||||
res= check_update_fields(thd, context->table_list, update_fields, &map);
|
||||
res= check_update_fields(thd, context->table_list, update_fields,
|
||||
update_values, &map);
|
||||
select_lex->no_wrap_view_item= FALSE;
|
||||
}
|
||||
|
||||
/* Restore the current context. */
|
||||
ctx_state.restore_state(context, table_list);
|
||||
|
||||
if (!res)
|
||||
res= setup_fields(thd, 0, update_values, MARK_COLUMNS_READ, 0, 0);
|
||||
}
|
||||
|
||||
if (res)
|
||||
@@ -2931,9 +2950,9 @@ select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
|
||||
we are fixing fields from insert list.
|
||||
*/
|
||||
lex->current_select= &lex->select_lex;
|
||||
res= check_insert_fields(thd, table_list, *fields, values,
|
||||
!insert_into_view, &map) ||
|
||||
setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0);
|
||||
res= (setup_fields(thd, 0, values, MARK_COLUMNS_READ, 0, 0) ||
|
||||
check_insert_fields(thd, table_list, *fields, values,
|
||||
!insert_into_view, 1, &map));
|
||||
|
||||
if (!res && fields->elements)
|
||||
{
|
||||
@@ -2960,7 +2979,8 @@ select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
|
||||
|
||||
lex->select_lex.no_wrap_view_item= TRUE;
|
||||
res= res || check_update_fields(thd, context->table_list,
|
||||
*info.update_fields, &map);
|
||||
*info.update_fields, *info.update_values,
|
||||
&map);
|
||||
lex->select_lex.no_wrap_view_item= FALSE;
|
||||
/*
|
||||
When we are not using GROUP BY and there are no ungrouped aggregate functions
|
||||
|
Reference in New Issue
Block a user