mirror of
https://github.com/MariaDB/server.git
synced 2025-08-01 03:47:19 +03:00
MDEV-29587 Allowing insert into a view with columns that are not part the table
We can check only fields which take part in inserts.
This commit is contained in:
@ -96,7 +96,8 @@ static void end_delayed_insert(THD *thd);
|
||||
pthread_handler_t handle_delayed_insert(void *arg);
|
||||
static void unlink_blobs(TABLE *table);
|
||||
#endif
|
||||
static bool check_view_insertability(THD *thd, TABLE_LIST *view);
|
||||
static bool check_view_insertability(THD *thd, TABLE_LIST *view,
|
||||
List<Item> &fields);
|
||||
static int binlog_show_create_table_(THD *thd, TABLE *table,
|
||||
Table_specification_st *create_info);
|
||||
|
||||
@ -311,7 +312,7 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
|
||||
|
||||
if (check_key_in_view(thd, table_list) ||
|
||||
(table_list->view &&
|
||||
check_view_insertability(thd, table_list)))
|
||||
check_view_insertability(thd, table_list, fields)))
|
||||
{
|
||||
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT");
|
||||
DBUG_RETURN(-1);
|
||||
@ -1426,6 +1427,7 @@ abort:
|
||||
check_view_insertability()
|
||||
thd - thread handler
|
||||
view - reference on VIEW
|
||||
fields - fields used in insert
|
||||
|
||||
IMPLEMENTATION
|
||||
A view is insertable if the folloings are true:
|
||||
@ -1441,7 +1443,8 @@ abort:
|
||||
TRUE - can't be used for insert
|
||||
*/
|
||||
|
||||
static bool check_view_insertability(THD * thd, TABLE_LIST *view)
|
||||
static bool check_view_insertability(THD *thd, TABLE_LIST *view,
|
||||
List<Item> &fields)
|
||||
{
|
||||
uint num= view->view->first_select_lex()->item_list.elements;
|
||||
TABLE *table= view->table;
|
||||
@ -1452,6 +1455,8 @@ static bool check_view_insertability(THD * thd, TABLE_LIST *view)
|
||||
uint32 *used_fields_buff= (uint32*)thd->alloc(used_fields_buff_size);
|
||||
MY_BITMAP used_fields;
|
||||
enum_column_usage saved_column_usage= thd->column_usage;
|
||||
List_iterator_fast<Item> it(fields);
|
||||
Item *ex;
|
||||
DBUG_ENTER("check_key_in_view");
|
||||
|
||||
if (!used_fields_buff)
|
||||
@ -1480,6 +1485,17 @@ static bool check_view_insertability(THD * thd, TABLE_LIST *view)
|
||||
/* simple SELECT list entry (field without expression) */
|
||||
if (!(field= trans->item->field_for_view_update()))
|
||||
{
|
||||
// Do not check fields which we are not inserting into
|
||||
while((ex= it++))
|
||||
{
|
||||
// The field used in the INSERT
|
||||
if (ex->real_item()->field_for_view_update() ==
|
||||
trans->item->field_for_view_update())
|
||||
break;
|
||||
}
|
||||
it.rewind();
|
||||
if (!ex)
|
||||
continue;
|
||||
thd->column_usage= saved_column_usage;
|
||||
DBUG_RETURN(TRUE);
|
||||
}
|
||||
@ -1494,11 +1510,12 @@ static bool check_view_insertability(THD * thd, TABLE_LIST *view)
|
||||
}
|
||||
thd->column_usage= saved_column_usage;
|
||||
/* unique test */
|
||||
for (trans= trans_start; trans != trans_end; trans++)
|
||||
while((ex= it++))
|
||||
{
|
||||
/* Thanks to test above, we know that all columns are of type Item_field */
|
||||
Item_field *field= (Item_field *)trans->item;
|
||||
/* check fields belong to table in which we are inserting */
|
||||
DBUG_ASSERT(ex->real_item()->field_for_view_update()->type() ==
|
||||
Item::FIELD_ITEM);
|
||||
Item_field *field= (Item_field *)ex->real_item()->field_for_view_update();
|
||||
if (field->field->table == table &&
|
||||
bitmap_fast_test_and_set(&used_fields, field->field->field_index))
|
||||
DBUG_RETURN(TRUE);
|
||||
|
Reference in New Issue
Block a user