From c78eb5aabb203c7dadc838f9d60c9202527260fd Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Fri, 22 May 2026 11:05:10 -0400 Subject: [PATCH] Return 0 for $sth->rows() when no tuples returned Report: https://github.com/bucardo/dbdpg/issues/194 Reported-by: Yiannis (github user d3flex) --- dbdimp.c | 1 + t/03smethod.t | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/dbdimp.c b/dbdimp.c index 07141cce..81a3ee09 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -3869,6 +3869,7 @@ long dbd_st_execute (SV * sth, imp_sth_t * imp_sth) TRACE_PQCMDTUPLES; ret = atol(PQcmdTuples(imp_sth->result)); gotrows = ret; + imp_sth->rows = ret; } if (!gotrows) { /* No rows affected, but check for change of state */ diff --git a/t/03smethod.t b/t/03smethod.t index 51879b25..5c61658a 100644 --- a/t/03smethod.t +++ b/t/03smethod.t @@ -22,7 +22,7 @@ my $dbh = connect_database(); if (! $dbh) { plan skip_all => 'Connection to database failed, cannot continue testing'; } -plan tests => 152; +plan tests => 159; isnt ($dbh, undef, 'Connect to database for statement handle method testing'); @@ -499,6 +499,46 @@ $rows = $sth->rows(); $sth->finish(); is ($rows, 2, $t); +$t='Statement handle method "rows" returns 1 for ON CONFLICT DO NOTHING (insert valid row)'; +$sth = $dbh->prepare('INSERT INTO dbd_pg_test(id) VALUES (?) ON CONFLICT DO NOTHING'); +$sth->execute(1); +$rows = $sth->rows(); +is ($rows, 1, $t); + +$t='Statement handle method "rows" returns 0 for ON CONFLICT DO NOTHING (insert duplicate row)'; +$sth->execute(1); +$rows = $sth->rows(); +is ($rows, 0, $t); + +$t='Statement handle method "rows" returns 0 for non-matching UPDATE'; +$sth = $dbh->prepare('UPDATE dbd_pg_test set id = -55 where id = ?'); +$sth->execute(-99); +$rows = $sth->rows(); +is ($rows, 0, $t); + +$t='Statement handle method "rows" returns 1 for matching UPDATE'; +$sth->execute(1); +$rows = $sth->rows(); +is ($rows, 1, $t); + +$t='Statement handle method "rows" returns correct number of rows for UPDATE'; +$sth = $dbh->prepare('UPDATE dbd_pg_test set id = id where id > ?'); +$sth->execute(-500); +$rows = $sth->rows(); +cmp_ok ($rows, '>=', 2, $t); + +$t='Statement handle method "rows" returns 0 for DELETE of no rows'; +$sth = $dbh->prepare('DELETE from dbd_pg_test where id = ?'); +$sth->execute(-500); +$rows = $sth->rows(); +is ($rows, 0, $t); + +$t='Statement handle method "rows" returns 1 for DELETE of one row'; +$sth = $dbh->prepare('DELETE from dbd_pg_test where id = ?'); +$sth->execute(-55); +$rows = $sth->rows(); +is ($rows, 1, $t); + # # Test of the "bind_col" statement handle method #