Skip to content

Commit fe42698

Browse files
committed
fix: pgsql now returns resource objects in 8.1
Per: - php/php-src#6791 - https://github.com/php/php-src/blob/a846547ed4bb67f00dc12bbfc529e9c992cbfd07/UPGRADING The pgsql functions now accept and return **resource objects** and not **resources**. As such, the return value of `get_resource_type()` varies, and technically, in PHP 8.1, we should not use `is_resource()`, but a typecheck instead. However, we still need to support pre-8.1 code as well. Signed-off-by: Matthew Weier O'Phinney <[email protected]>
1 parent e1bcf24 commit fe42698

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

src/Adapter/Driver/Pgsql/Connection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Laminas\Db\Adapter\Driver\AbstractConnection;
66
use Laminas\Db\Adapter\Exception;
77
use Laminas\Db\ResultSet\ResultSetInterface;
8+
use PgSql\Connection as PgSqlConnection;
89

910
use function array_filter;
1011
use function defined;
@@ -45,7 +46,7 @@ public function __construct($connectionInfo = null)
4546
{
4647
if (is_array($connectionInfo)) {
4748
$this->setConnectionParameters($connectionInfo);
48-
} elseif (is_resource($connectionInfo)) {
49+
} elseif ($connectionInfo instanceof PgSqlConnection || is_resource($connectionInfo)) {
4950
$this->setResource($connectionInfo);
5051
}
5152
}
@@ -123,7 +124,7 @@ public function getCurrentSchema()
123124
*/
124125
public function connect()
125126
{
126-
if (is_resource($this->resource)) {
127+
if ($this->resource instanceof PgSqlConnection || is_resource($this->resource)) {
127128
return $this;
128129
}
129130

@@ -168,7 +169,7 @@ public function connect()
168169
*/
169170
public function isConnected()
170171
{
171-
return is_resource($this->resource);
172+
return $this->resource instanceof PgSqlConnection || is_resource($this->resource);
172173
}
173174

174175
/**

src/Adapter/Driver/Pgsql/Result.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Laminas\Db\Adapter\Driver\ResultInterface;
66
use Laminas\Db\Adapter\Exception;
7+
use PgSql\Result as PgSqlResult;
78
// phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
89
use ReturnTypeWillChange;
910

@@ -38,7 +39,13 @@ class Result implements ResultInterface
3839
*/
3940
public function initialize($resource, $generatedValue)
4041
{
41-
if (! is_resource($resource) || get_resource_type($resource) !== 'pgsql result') {
42+
if (
43+
! $resource instanceof PgSqlResult
44+
&& (
45+
! is_resource($resource)
46+
|| 'pgsql result' !== get_resource_type($resource)
47+
)
48+
) {
4249
throw new Exception\InvalidArgumentException('Resource not of the correct type.');
4350
}
4451

src/Adapter/Driver/Pgsql/Statement.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Laminas\Db\Adapter\Exception;
77
use Laminas\Db\Adapter\ParameterContainer;
88
use Laminas\Db\Adapter\Profiler;
9+
use PgSql\Connection as PgSqlConnection;
910

1011
use function get_resource_type;
1112
use function is_array;
@@ -77,7 +78,13 @@ public function getProfiler()
7778
*/
7879
public function initialize($pgsql)
7980
{
80-
if (! is_resource($pgsql) || get_resource_type($pgsql) !== 'pgsql link') {
81+
if (
82+
! $pgsql instanceof PgSqlConnection
83+
&& (
84+
! is_resource($pgsql)
85+
|| 'pgsql link' !== get_resource_type($pgsql)
86+
)
87+
) {
8188
throw new Exception\RuntimeException(sprintf(
8289
'%s: Invalid or missing postgresql connection; received "%s"',
8390
__METHOD__,

src/Adapter/Platform/Postgresql.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Laminas\Db\Adapter\Driver\Pdo;
77
use Laminas\Db\Adapter\Driver\Pgsql;
88
use Laminas\Db\Adapter\Exception;
9+
use PgSql\Connection as PgSqlConnection;
910

1011
use function get_resource_type;
1112
use function implode;
12-
use function in_array;
1313
use function is_resource;
1414
use function pg_escape_string;
1515
use function str_replace;
@@ -26,6 +26,12 @@ class Postgresql extends AbstractPlatform
2626
/** @var null|resource|\PDO|Pdo\Pdo|Pgsql\Pgsql */
2727
protected $driver;
2828

29+
/** @var string[] */
30+
private $knownPgsqlResources = [
31+
'pgsql link',
32+
'pgsql link persistent',
33+
];
34+
2935
/**
3036
* @param null|\Laminas\Db\Adapter\Driver\Pgsql\Pgsql|\Laminas\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
3137
*/
@@ -46,7 +52,8 @@ public function setDriver($driver)
4652
if (
4753
$driver instanceof Pgsql\Pgsql
4854
|| ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() === 'Postgresql')
49-
|| (is_resource($driver) && (in_array(get_resource_type($driver), ['pgsql link', 'pgsql link persistent'])))
55+
|| $driver instanceof PgSqlConnection // PHP 8.1+
56+
|| (is_resource($driver) && in_array(get_resource_type($driver), $this->knownPgsqlResources, true))
5057
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql')
5158
) {
5259
$this->driver = $driver;
@@ -108,15 +115,14 @@ public function quoteTrustedValue($value)
108115
*/
109116
protected function quoteViaDriver($value)
110117
{
111-
if ($this->driver instanceof DriverInterface) {
112-
$resource = $this->driver->getConnection()->getResource();
113-
} else {
114-
$resource = $this->driver;
115-
}
118+
$resource = $this->driver instanceof DriverInterface
119+
? $this->driver->getConnection()->getResource()
120+
: $this->driver;
116121

117-
if (is_resource($resource)) {
122+
if ($resource instanceof PgSqlConnection || is_resource($resource)) {
118123
return '\'' . pg_escape_string($resource, $value) . '\'';
119124
}
125+
120126
if ($resource instanceof \PDO) {
121127
return $resource->quote($value);
122128
}

test/integration/Adapter/Platform/PostgresqlTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Laminas\Db\Adapter\Driver\Pdo;
66
use Laminas\Db\Adapter\Driver\Pgsql;
77
use Laminas\Db\Adapter\Platform\Postgresql;
8+
use PgSql\Connection as PgSqlConnection;
89
use PHPUnit\Framework\TestCase;
910

1011
use function extension_loaded;
@@ -46,7 +47,13 @@ protected function setUp(): void
4647

4748
public function testQuoteValueWithPgsql()
4849
{
49-
if (! is_resource($this->adapters['pgsql'])) {
50+
if (
51+
! isset($this->adapters['pgsql'])
52+
|| (
53+
! $this->adapters['pgsql'] instanceof PgSqlConnection
54+
&& ! is_resource($this->adapters['pgsql'])
55+
)
56+
) {
5057
$this->markTestSkipped('Postgres (pgsql) not configured in unit test configuration file');
5158
}
5259
$pgsql = new Postgresql($this->adapters['pgsql']);
@@ -60,7 +67,7 @@ public function testQuoteValueWithPgsql()
6067

6168
public function testQuoteValueWithPdoPgsql()
6269
{
63-
if (! $this->adapters['pdo_pgsql'] instanceof \PDO) {
70+
if (! isset($this->adapters['pdo_pgsql']) || ! $this->adapters['pdo_pgsql'] instanceof \PDO) {
6471
$this->markTestSkipped('Postgres (PDO_PGSQL) not configured in unit test configuration file');
6572
}
6673
$pgsql = new Postgresql($this->adapters['pdo_pgsql']);

0 commit comments

Comments
 (0)