1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

5.0 version of the fix for bug #9481: mysql_insert_id() returns 0 after

insert ... select.

The 5.0 manual page for mysql_insert_id() does not mention anything
about INSERT ... SELECT, though its current behavior is incosistent
with what the manual says about the plain INSERT.

Fixed by changing the AUTO_INCREMENT and mysql_insert_id() handling
logic in INSERT ... SELECT to be consistent with the INSERT behavior,
the manual, and the changes in 5.1 introduced by WL3146:


- mysql_insert_id() now returns the first automatically generated
AUTO_INCREMENT value that was successfully inserted by INSERT ... SELECT

-  if an INSERT ... SELECT statement is executed, and no automatically
generated value is successfully inserted, mysql_insert_id() now returns
the ID of the last inserted row.
This commit is contained in:
kaa@polly.(none)
2007-11-26 18:36:05 +03:00
parent ee1bb66368
commit 24c9d86416
3 changed files with 224 additions and 9 deletions

View File

@ -2644,7 +2644,8 @@ select_insert::select_insert(TABLE_LIST *table_list_par, TABLE *table_par,
enum_duplicates duplic,
bool ignore_check_option_errors)
:table_list(table_list_par), table(table_par), fields(fields_par),
last_insert_id(0),
autoinc_value_of_last_inserted_row(0),
autoinc_value_of_first_inserted_row(0),
insert_into_view(table_list_par && table_list_par->view != 0),
is_bulk_insert_mode(FALSE)
{
@ -2901,15 +2902,25 @@ bool select_insert::send_data(List<Item> &values)
}
if (table->next_number_field)
{
/*
If no value has been autogenerated so far, we need to remember the
value we just saw, we may need to send it to client in the end.
*/
if (!thd->insert_id_used)
autoinc_value_of_last_inserted_row= table->next_number_field->val_int();
/*
Clear auto-increment field for the next record, if triggers are used
we will clear it twice, but this should be cheap.
*/
table->next_number_field->reset();
if (!last_insert_id && thd->insert_id_used)
last_insert_id= thd->last_insert_id;
if (!autoinc_value_of_last_inserted_row && thd->insert_id_used)
autoinc_value_of_last_inserted_row= thd->last_insert_id;
}
}
if (thd->insert_id_used && !autoinc_value_of_first_inserted_row)
autoinc_value_of_first_inserted_row= thd->last_insert_id;
DBUG_RETURN(error);
}
@ -2938,6 +2949,7 @@ bool select_insert::send_eof()
{
int error, error2;
bool changed, transactional_table= table->file->has_transactions();
ulonglong id;
DBUG_ENTER("select_insert::send_eof");
error= (!thd->prelocked_mode) ? table->file->end_bulk_insert():0;
@ -2959,8 +2971,17 @@ bool select_insert::send_eof()
DBUG_ASSERT(transactional_table || !changed ||
thd->transaction.stmt.modified_non_trans_table);
if (last_insert_id)
thd->insert_id(info.copied ? last_insert_id : 0); // For binary log
// For binary log
if (autoinc_value_of_last_inserted_row)
{
if (info.copied)
thd->insert_id(autoinc_value_of_last_inserted_row);
else
{
autoinc_value_of_first_inserted_row= 0;
thd->insert_id(0);
}
}
/* Write to binlog before commiting transaction */
if (mysql_bin_log.is_open())
{
@ -2987,7 +3008,9 @@ bool select_insert::send_eof()
thd->row_count_func= info.copied + info.deleted +
((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
info.touched : info.updated);
::send_ok(thd, (ulong) thd->row_count_func, last_insert_id, buff);
id= autoinc_value_of_first_inserted_row > 0 ?
autoinc_value_of_first_inserted_row : thd->last_insert_id;
::send_ok(thd, (ulong) thd->row_count_func, id, buff);
DBUG_RETURN(0);
}
@ -3016,8 +3039,17 @@ void select_insert::abort()
if ((changed= info.copied || info.deleted || info.updated) &&
!transactional_table)
{
if (last_insert_id)
thd->insert_id(last_insert_id); // For binary log
// For binary log
if (autoinc_value_of_last_inserted_row)
{
if (info.copied)
thd->insert_id(autoinc_value_of_last_inserted_row);
else
{
autoinc_value_of_first_inserted_row= 0;
thd->insert_id(0);
}
}
if (mysql_bin_log.is_open())
{
Query_log_event qinfo(thd, thd->query, thd->query_length,