summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <monty@narttu.mysql.fi>2003-06-15 12:34:04 +0300
committerunknown <monty@narttu.mysql.fi>2003-06-15 12:34:04 +0300
commitfb2825e2b5bb05addbc836ae773783f8c652dd54 (patch)
treefbfda6e8853980b09bf127895ff20769c24829bd
parent96d305f86f901e87e9fe4bafebf96fe2da2491e2 (diff)
downloadmariadb-git-fb2825e2b5bb05addbc836ae773783f8c652dd54.tar.gz
LAST_INSERT_ID()}will now return 0 if the last INSERT didn't insert any rows.
mysql-test/r/auto_increment.result: Update results after bug fix
-rw-r--r--mysql-test/r/auto_increment.result2
-rw-r--r--sql/sql_insert.cc15
2 files changed, 14 insertions, 3 deletions
diff --git a/mysql-test/r/auto_increment.result b/mysql-test/r/auto_increment.result
index bd15c913d2e..5553f718799 100644
--- a/mysql-test/r/auto_increment.result
+++ b/mysql-test/r/auto_increment.result
@@ -143,5 +143,5 @@ insert into t1 values (NULL, 10);
ERROR 23000: Duplicate entry '10' for key 2
select last_insert_id();
last_insert_id()
-3
+0
drop table t1;
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index f65ef4b968a..091de9d406f 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -308,6 +308,11 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list,
}
thd->row_count++;
}
+
+ /*
+ Now all rows are inserted. Time to update logs and sends response to
+ user
+ */
if (lock_type == TL_WRITE_DELAYED)
{
if (!error)
@@ -341,7 +346,7 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list,
}
if (id && values_list.elements != 1)
thd->insert_id(id); // For update log
- else if (table->next_number_field)
+ else if (table->next_number_field && info.copied)
id=table->next_number_field->val_int(); // Return auto_increment value
/*
@@ -383,9 +388,15 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list,
thd->next_insert_id=0; // Reset this if wrongly used
if (duplic != DUP_ERROR)
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
+
+ /* Reset value of LAST_INSERT_ID if no rows where inserted */
+ if (!info.copied && thd->insert_id_used)
+ {
+ thd->insert_id(0);
+ id=0;
+ }
if (error)
goto abort;
-
if (values_list.elements == 1 && (!(thd->options & OPTION_WARNINGS) ||
!thd->cuted_fields))
send_ok(thd,info.copied+info.deleted,id);