Bug #77855 [NEW]: PG: rowCount returns 0 when using CURSOR_SCROLL
From: php at bohwaz dot net
Operating system: All
PHP version: 7.3.4
Package: PDO PgSQL
Bug Type: Bug
Bug description:PG: rowCount returns 0 when using CURSOR_SCROLL
Description:
------------
When using the "PDO::ATTR_CURSOR" attribute set to "PDO::CURSOR_SCROLL"
on a pgSQL PDO statement, subsequent calls to rowCount will return 0.
That's because in Postgre a cursor has no way to tell how many lines it
will have.
I did change the PHP documentation of rowCount, but having a more
explicit way would be better, perhaps by throwing an error.
Another solution would be to do as described here:
https://github.com/MagicStack/asyncpg/issues/359#issuecomment-420266185
For example rowCount() could execute:
-- Move to the end of the cursor
MOVE FORWARD ALL IN my_cursor;
-- Will return the number of rows in the result cursor as "MOVE xxx",
should store it to return it later
-- Move back to the beginning
MOVE ABSOLUTE 0 IN my_cursor;
But because of the way rowCount is implemented in PDO, it cannot be done
in a driver-specific way. We would have to do that MOVE FORWARD / MOVE
back to 0 inside the pgsql_stmt_execute function and store it in
statement->row_count.
This might have a performance impact though, but I'm not sure as I'm not
very familiar with postgre internals.
Test script:
---------------
--TEST--
PDO PgSQL bug with CURSOR_SCROLL and rowCount
--SKIPIF--
<?php # vim:se ft=php:
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql'))
die('skip not loaded');
require dirname(__FILE__) . '/config.inc';
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$st = $pdo->prepare('SELECT NOW();');
$st->execute();
var_dump($st->rowCount());
$st = $pdo->prepare('SELECT NOW();', [PDO::ATTR_CURSOR =>
PDO::CURSOR_SCROLL]);
$st->execute();
var_dump($st->rowCount());
?>
--EXPECT--
int(1)
int(1)
Expected result:
----------------
int(1)
int(1)
Actual result:
--------------
int(1)
int(0)
--
Edit bug report at https://bugs.php.net/bug.php?id=77855&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77855&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77855&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77855&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=77855&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=77855&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=77855&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=77855&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=77855&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=77855&r=support
Expected behavior: https://bugs.php.net/fix.php?id=77855&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=77855&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=77855&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=77855&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77855&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=77855&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=77855&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=77855&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77855&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=77855&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=77855&r=mysqlcfg
Thread (1 message)
- php at bohwaz dot net