summaryrefslogtreecommitdiff
path: root/sql/sql_insert.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sql_insert.cc')
-rw-r--r--sql/sql_insert.cc95
1 files changed, 48 insertions, 47 deletions
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index b1354ce38bd..2eda80e8b36 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -1,5 +1,5 @@
/*
- Copyright (c) 2000, 2011, Oracle and/or its affiliates.
+ Copyright (c) 2000, 2013, Oracle and/or its affiliates.
Copyright (c) 2009, 2013, Monty Program Ab.
This program is free software; you can redistribute it and/or modify
@@ -95,15 +95,13 @@ static bool check_view_insertability(THD *thd, TABLE_LIST *view);
/*
Check that insert/update fields are from the same single table of a view.
- SYNOPSIS
- check_view_single_update()
- fields The insert/update fields to be checked.
- values Values to use for update
- view The view for insert.
- map [in/out] The insert table map.
+ @param fields The insert/update fields to be checked.
+ @param values The insert/update values to be checked, NULL if
+ checking is not wanted.
+ @param view The view for insert.
+ @param map [in/out] The insert table map.
- DESCRIPTION
- This function is called in 2 cases:
+ This function is called in 2 cases:
1. to check insert fields. In this case *map will be set to 0.
Insert fields are checked to be all from the same single underlying
table of the given view. Otherwise the error is thrown. Found table
@@ -113,9 +111,7 @@ static bool check_view_insertability(THD *thd, TABLE_LIST *view);
the function to check insert fields. Update fields are checked to be
from the same table as the insert fields.
- RETURN
- 0 OK
- 1 Error
+ @returns false if success.
*/
bool check_view_single_update(List<Item> &fields, List<Item> *values,
@@ -180,21 +176,16 @@ error:
/*
Check if insert fields are correct.
- SYNOPSIS
- check_insert_fields()
- thd The current thread.
- table The table for insert.
- fields The insert fields.
- values The insert values.
- check_unique If duplicate values should be rejected.
- fields_and_values_from_different_maps
- Set to 1 if fields and values are using
- different table maps, like on select ... insert
- map Store here table map for used fields
-
- RETURN
- 0 OK
- -1 Error
+ @param thd The current thread.
+ @param table_list The table we are inserting into (may be view)
+ @param fields The insert fields.
+ @param values The insert values.
+ @param check_unique If duplicate values should be rejected.
+ @param fields_and_values_from_different_maps If 'values' are allowed to
+ refer to other tables than those of 'fields'
+ @param map See check_view_single_update
+
+ @returns 0 if success, -1 if error
*/
static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
@@ -312,28 +303,29 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
}
-/*
- Check update fields for the timestamp and auto_increment fields.
+/**
+ Check if update fields are correct.
- SYNOPSIS
- check_update_fields()
- thd The current thread.
- insert_table_list The insert table list.
- table The table for update.
- update_fields The update fields.
+ @param thd The current thread.
+ @param insert_table_list The table we are inserting into (may be view)
+ @param update_fields The update fields.
+ @param update_values The update values.
+ @param fields_and_values_from_different_maps If 'update_values' are allowed to
+ refer to other tables than those of 'update_fields'
+ @param map See check_view_single_update
- NOTE
- If the update fields include an autoinc field, set the
- table->next_number_field_updated flag.
+ @note
+ If the update fields include an autoinc field, set the
+ table->next_number_field_updated flag.
- RETURN
- 0 OK
- -1 Error
+ @returns 0 if success, -1 if error
*/
static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
List<Item> &update_fields,
- List<Item> &update_values, table_map *map)
+ List<Item> &update_values,
+ bool fields_and_values_from_different_maps,
+ table_map *map)
{
TABLE *table= insert_table_list->table;
my_bool autoinc_mark;
@@ -358,7 +350,9 @@ static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
if (insert_table_list->is_view() &&
insert_table_list->is_merged_derived() &&
- check_view_single_update(update_fields, &update_values,
+ check_view_single_update(update_fields,
+ fields_and_values_from_different_maps ?
+ (List<Item>*) 0 : &update_values,
insert_table_list, map, false))
return -1;
@@ -1451,7 +1445,7 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
{
select_lex->no_wrap_view_item= TRUE;
res= check_update_fields(thd, context->table_list, update_fields,
- update_values, &map);
+ update_values, false, &map);
select_lex->no_wrap_view_item= FALSE;
}
@@ -3382,9 +3376,16 @@ select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
context->resolve_in_table_list_only(table_list);
lex->select_lex.no_wrap_view_item= TRUE;
- res= res || check_update_fields(thd, context->table_list,
- *info.update_fields, *info.update_values,
- &map);
+ res= res ||
+ check_update_fields(thd, context->table_list,
+ *info.update_fields, *info.update_values,
+ /*
+ In INSERT SELECT ON DUPLICATE KEY UPDATE col=x
+ 'x' can legally refer to a non-inserted table.
+ 'x' is not even resolved yet.
+ */
+ true,
+ &map);
lex->select_lex.no_wrap_view_item= FALSE;
/*
When we are not using GROUP BY and there are no ungrouped aggregate functions