Skip to content

Commit c788640

Browse files
committed
Fix $dbh->last_insert_id for multi result statements
$dbh->last_insert_id was not correctly set for unfinished multi statement result followed by $dbh->do or $dbh->prepare call with statement which does not modify $dbh->last_insert_id.
1 parent 7d04f54 commit c788640

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

dbdimp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,7 @@ IV mariadb_db_do6(SV *dbh, imp_dbh_t *imp_dbh, SV *statement_sv, SV *attribs, I3
26522652
mariadb_dr_do_error(dbh, mysql_errno(imp_dbh->pmysql), mysql_error(imp_dbh->pmysql), mysql_sqlstate(imp_dbh->pmysql));
26532653
return -2;
26542654
}
2655+
imp_dbh->insertid = mysql_insert_id(imp_dbh->pmysql);
26552656
}
26562657
if (result)
26572658
{
@@ -4021,6 +4022,7 @@ static bool mariadb_st_free_result_sets(SV *sth, imp_sth_t *imp_sth)
40214022
mysql_sqlstate(imp_dbh->pmysql));
40224023
return FALSE;
40234024
}
4025+
imp_dbh->insertid = imp_sth->insertid = mysql_insert_id(imp_dbh->pmysql);
40244026
}
40254027
}
40264028
if (imp_sth->result)

t/76multi_statement.t

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ my $dbh = DbiTestConnect($test_dsn, $test_user, $test_password,
1313
{ RaiseError => 1, PrintError => 0, AutoCommit => 0,
1414
mariadb_multi_statements => 1 });
1515

16-
plan tests => 36;
16+
plan tests => 51;
1717

1818
ok (defined $dbh, "Connected to database with multi statement support");
1919

@@ -73,4 +73,27 @@ $dbh->{mariadb_server_prepare}= 0;
7373
ok(not $sth->more_results());
7474
ok($sth->finish());
7575

76+
# Check that $dbh->last_insert_id works after $dbh->do with multi statements
77+
ok($dbh->do("INSERT INTO dbd_mysql_t76multi2 VALUES(3); INSERT INTO dbd_mysql_t76multi2 VALUES(4);"));
78+
is($dbh->last_insert_id(undef, undef, undef, undef), 4, '$dbh->last_insert_id is correct after $dbh->do with multi statements');
79+
80+
# Check that $dbh->last_insert_id works after prepare, execute and finish but without more_results
81+
ok($sth = $dbh->prepare("INSERT INTO dbd_mysql_t76multi2 VALUES(5); INSERT INTO dbd_mysql_t76multi2 VALUES(6);"));
82+
ok($sth->execute());
83+
ok($sth->finish());
84+
is($dbh->last_insert_id(undef, undef, undef, undef), 6, '$dbh->last_insert_id is correct after multi statement prepare, execute and finish');
85+
86+
# Check that $dbh->last_insert_id works after prepare and execute, but without more_results; after preparing new statement
87+
ok($sth = $dbh->prepare("INSERT INTO dbd_mysql_t76multi2 VALUES(7); INSERT INTO dbd_mysql_t76multi2 VALUES(8);"));
88+
ok($sth->execute());
89+
ok($sth = $dbh->prepare("SELECT 1"));
90+
is($dbh->last_insert_id(undef, undef, undef, undef), 8, '$dbh->last_insert_id is correct after multi statement prepare and execute without finish followed by new prepare');
91+
92+
# Check that $dbh->last_insert_id works after prepare and execute, but without more_results; after calling $dbh->do which do not modify last_insert_id
93+
ok($sth = $dbh->prepare("INSERT INTO dbd_mysql_t76multi2 VALUES(9); INSERT INTO dbd_mysql_t76multi2 VALUES(10);"));
94+
ok($sth->execute());
95+
ok($dbh->do("SELECT 1"));
96+
is($dbh->last_insert_id(undef, undef, undef, undef), 10, '$dbh->last_insert_id is correct after multi statement prepare and execute without finish followed by $dbh->do');
97+
ok($sth->finish());
98+
7699
$dbh->disconnect();

0 commit comments

Comments
 (0)