Skip to content

Commit cdabb4b

Browse files
authored
Merge pull request laminas#212 from weierophinney/hotfix/resource-type-changes-for-8.1
Recognize pgsql resource objects when running in PHP 8.1
2 parents e1bcf24 + c120077 commit cdabb4b

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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;
@@ -26,6 +27,12 @@ class Postgresql extends AbstractPlatform
2627
/** @var null|resource|\PDO|Pdo\Pdo|Pgsql\Pgsql */
2728
protected $driver;
2829

30+
/** @var string[] */
31+
private $knownPgsqlResources = [
32+
'pgsql link',
33+
'pgsql link persistent',
34+
];
35+
2936
/**
3037
* @param null|\Laminas\Db\Adapter\Driver\Pgsql\Pgsql|\Laminas\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
3138
*/
@@ -46,7 +53,8 @@ public function setDriver($driver)
4653
if (
4754
$driver instanceof Pgsql\Pgsql
4855
|| ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() === 'Postgresql')
49-
|| (is_resource($driver) && (in_array(get_resource_type($driver), ['pgsql link', 'pgsql link persistent'])))
56+
|| $driver instanceof PgSqlConnection // PHP 8.1+
57+
|| (is_resource($driver) && in_array(get_resource_type($driver), $this->knownPgsqlResources, true))
5058
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql')
5159
) {
5260
$this->driver = $driver;
@@ -108,15 +116,14 @@ public function quoteTrustedValue($value)
108116
*/
109117
protected function quoteViaDriver($value)
110118
{
111-
if ($this->driver instanceof DriverInterface) {
112-
$resource = $this->driver->getConnection()->getResource();
113-
} else {
114-
$resource = $this->driver;
115-
}
119+
$resource = $this->driver instanceof DriverInterface
120+
? $this->driver->getConnection()->getResource()
121+
: $this->driver;
116122

117-
if (is_resource($resource)) {
123+
if ($resource instanceof PgSqlConnection || is_resource($resource)) {
118124
return '\'' . pg_escape_string($resource, $value) . '\'';
119125
}
126+
120127
if ($resource instanceof \PDO) {
121128
return $resource->quote($value);
122129
}

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)