Skip to content

Commit 7d04f54

Browse files
committed
Fix $dbh->last_insert_id after $dbh->mariadb_async_result call
Last insert id for async result was updated only when it was called from statement DBI handle, not when was called from database DBI handle.
1 parent 849ad97 commit 7d04f54

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

dbdimp.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6521,16 +6521,20 @@ my_ulonglong mariadb_db_async_result(SV* h, MYSQL_RES** resp)
65216521
*resp= NULL;
65226522
}
65236523
}
6524+
6525+
/* Some MySQL client versions return correct value from mysql_insert_id()
6526+
* function only after non-SELECT operation. So store insert id into dbh
6527+
* cache and later read it only from cache. */
6528+
if (retval != (my_ulonglong)-1 && !*resp)
6529+
dbh->insertid = mysql_insert_id(svsock);
6530+
65246531
if(htype == DBIt_ST) {
65256532
D_imp_sth(h);
65266533
D_imp_dbh_from_sth;
65276534

65286535
if (retval != (my_ulonglong)-1) {
65296536
if(! *resp) {
6530-
/* Some MySQL client versions return correct value from mysql_insert_id()
6531-
* function only after non-SELECT operation. So store insert id into dbh
6532-
* cache and later read it only from cache. */
6533-
imp_dbh->insertid = imp_sth->insertid = mysql_insert_id(svsock);
6537+
imp_sth->insertid = dbh->insertid;
65346538
if(! mysql_more_results(svsock))
65356539
DBIc_ACTIVE_off(imp_sth);
65366540
} else {

t/88async-multi-stmts.t

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ use lib 't', '.';
99
require 'lib.pl';
1010

1111
my $dbh = DbiTestConnect($test_dsn, $test_user, $test_password,
12-
{ RaiseError => 0, PrintError => 0, AutoCommit => 0 });
13-
plan tests => 8;
12+
{ RaiseError => 0, PrintError => 0, AutoCommit => 0, mariadb_multi_statements => 1 });
13+
plan tests => 45;
1414

1515
$dbh->do(<<SQL);
1616
CREATE TEMPORARY TABLE async_test (
17-
value INTEGER
17+
value INTEGER AUTO_INCREMENT PRIMARY KEY
1818
);
1919
SQL
2020

21-
my $sth0 = $dbh->prepare('INSERT INTO async_test VALUES(0)', { mariadb_async => 1 });
22-
my $sth1 = $dbh->prepare('INSERT INTO async_test VALUES(1)', { mariadb_async => 1 });
21+
my $sth0 = $dbh->prepare('INSERT INTO async_test VALUES(1)', { mariadb_async => 1 });
22+
my $sth1 = $dbh->prepare('INSERT INTO async_test VALUES(2)', { mariadb_async => 1 });
23+
my $sth2 = $dbh->prepare('INSERT INTO async_test VALUES(3); INSERT INTO async_test VALUES(4);', { mariadb_async => 1 });
2324

2425
$sth0->execute;
2526
ok !defined($sth1->mariadb_async_ready);
@@ -32,7 +33,63 @@ ok !$sth1->errstr;
3233
ok defined($sth0->mariadb_async_result);
3334
ok !$sth1->errstr;
3435

36+
is($sth0->last_insert_id(), 1);
37+
is($dbh->last_insert_id(undef, undef, undef, undef), 1);
38+
39+
$sth2->execute;
40+
ok !defined($sth1->mariadb_async_ready);
41+
ok $sth1->err;
42+
ok !defined($sth1->mariadb_async_result);
43+
ok $sth1->err;
44+
45+
is($sth0->last_insert_id(), 1);
46+
is($dbh->last_insert_id(undef, undef, undef, undef), 1);
47+
48+
ok defined($sth2->mariadb_async_ready);
49+
ok !$sth2->err;
50+
51+
is($sth0->last_insert_id(), 1);
52+
is($dbh->last_insert_id(undef, undef, undef, undef), 1);
53+
54+
ok defined($sth2->mariadb_async_result);
55+
ok !$sth2->err;
56+
57+
is($sth0->last_insert_id(), 1);
58+
is($sth2->last_insert_id(), 3);
59+
is($dbh->last_insert_id(undef, undef, undef, undef), 3);
60+
61+
ok $sth2->more_results;
62+
ok defined($sth2->mariadb_async_result);
63+
ok !$sth2->err;
64+
65+
is($sth0->last_insert_id(), 1);
66+
is($sth2->last_insert_id(), 4);
67+
is($dbh->last_insert_id(undef, undef, undef, undef), 4);
68+
69+
ok !$sth2->more_results;
70+
71+
$dbh->do('INSERT INTO async_test VALUES(5)', { mariadb_async => 1 });
72+
73+
is($sth0->last_insert_id(), 1);
74+
is($sth2->last_insert_id(), 4);
75+
is($dbh->last_insert_id(undef, undef, undef, undef), 4);
76+
77+
ok defined($dbh->mariadb_async_ready);
78+
ok !$dbh->err;
79+
80+
is($sth0->last_insert_id(), 1);
81+
is($sth2->last_insert_id(), 4);
82+
is($dbh->last_insert_id(undef, undef, undef, undef), 4);
83+
84+
ok defined($dbh->mariadb_async_result);
85+
ok !$sth2->err;
86+
87+
is($sth0->last_insert_id(), 1);
88+
is($sth2->last_insert_id(), 4);
89+
is($dbh->last_insert_id(undef, undef, undef, undef), 5);
90+
3591
undef $sth0;
3692
undef $sth1;
93+
undef $sth2;
3794

3895
$dbh->disconnect;

0 commit comments

Comments
 (0)