Skip to content

Commit

Permalink
Merge pull request #212 from weierophinney/hotfix/resource-type-chang…
Browse files Browse the repository at this point in the history
…es-for-8.1

Recognize pgsql resource objects when running in PHP 8.1
  • Loading branch information
Ocramius authored Sep 21, 2021
2 parents e1bcf24 + c120077 commit cdabb4b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/Adapter/Driver/Pgsql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laminas\Db\Adapter\Driver\AbstractConnection;
use Laminas\Db\Adapter\Exception;
use Laminas\Db\ResultSet\ResultSetInterface;
use PgSql\Connection as PgSqlConnection;

use function array_filter;
use function defined;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct($connectionInfo = null)
{
if (is_array($connectionInfo)) {
$this->setConnectionParameters($connectionInfo);
} elseif (is_resource($connectionInfo)) {
} elseif ($connectionInfo instanceof PgSqlConnection || is_resource($connectionInfo)) {
$this->setResource($connectionInfo);
}
}
Expand Down Expand Up @@ -123,7 +124,7 @@ public function getCurrentSchema()
*/
public function connect()
{
if (is_resource($this->resource)) {
if ($this->resource instanceof PgSqlConnection || is_resource($this->resource)) {
return $this;
}

Expand Down Expand Up @@ -168,7 +169,7 @@ public function connect()
*/
public function isConnected()
{
return is_resource($this->resource);
return $this->resource instanceof PgSqlConnection || is_resource($this->resource);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Adapter/Driver/Pgsql/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laminas\Db\Adapter\Driver\ResultInterface;
use Laminas\Db\Adapter\Exception;
use PgSql\Result as PgSqlResult;
// phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
use ReturnTypeWillChange;

Expand Down Expand Up @@ -38,7 +39,13 @@ class Result implements ResultInterface
*/
public function initialize($resource, $generatedValue)
{
if (! is_resource($resource) || get_resource_type($resource) !== 'pgsql result') {
if (
! $resource instanceof PgSqlResult
&& (
! is_resource($resource)
|| 'pgsql result' !== get_resource_type($resource)
)
) {
throw new Exception\InvalidArgumentException('Resource not of the correct type.');
}

Expand Down
9 changes: 8 additions & 1 deletion src/Adapter/Driver/Pgsql/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laminas\Db\Adapter\Exception;
use Laminas\Db\Adapter\ParameterContainer;
use Laminas\Db\Adapter\Profiler;
use PgSql\Connection as PgSqlConnection;

use function get_resource_type;
use function is_array;
Expand Down Expand Up @@ -77,7 +78,13 @@ public function getProfiler()
*/
public function initialize($pgsql)
{
if (! is_resource($pgsql) || get_resource_type($pgsql) !== 'pgsql link') {
if (
! $pgsql instanceof PgSqlConnection
&& (
! is_resource($pgsql)
|| 'pgsql link' !== get_resource_type($pgsql)
)
) {
throw new Exception\RuntimeException(sprintf(
'%s: Invalid or missing postgresql connection; received "%s"',
__METHOD__,
Expand Down
21 changes: 14 additions & 7 deletions src/Adapter/Platform/Postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laminas\Db\Adapter\Driver\Pdo;
use Laminas\Db\Adapter\Driver\Pgsql;
use Laminas\Db\Adapter\Exception;
use PgSql\Connection as PgSqlConnection;

use function get_resource_type;
use function implode;
Expand All @@ -26,6 +27,12 @@ class Postgresql extends AbstractPlatform
/** @var null|resource|\PDO|Pdo\Pdo|Pgsql\Pgsql */
protected $driver;

/** @var string[] */
private $knownPgsqlResources = [
'pgsql link',
'pgsql link persistent',
];

/**
* @param null|\Laminas\Db\Adapter\Driver\Pgsql\Pgsql|\Laminas\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
*/
Expand All @@ -46,7 +53,8 @@ public function setDriver($driver)
if (
$driver instanceof Pgsql\Pgsql
|| ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() === 'Postgresql')
|| (is_resource($driver) && (in_array(get_resource_type($driver), ['pgsql link', 'pgsql link persistent'])))
|| $driver instanceof PgSqlConnection // PHP 8.1+
|| (is_resource($driver) && in_array(get_resource_type($driver), $this->knownPgsqlResources, true))
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql')
) {
$this->driver = $driver;
Expand Down Expand Up @@ -108,15 +116,14 @@ public function quoteTrustedValue($value)
*/
protected function quoteViaDriver($value)
{
if ($this->driver instanceof DriverInterface) {
$resource = $this->driver->getConnection()->getResource();
} else {
$resource = $this->driver;
}
$resource = $this->driver instanceof DriverInterface
? $this->driver->getConnection()->getResource()
: $this->driver;

if (is_resource($resource)) {
if ($resource instanceof PgSqlConnection || is_resource($resource)) {
return '\'' . pg_escape_string($resource, $value) . '\'';
}

if ($resource instanceof \PDO) {
return $resource->quote($value);
}
Expand Down
11 changes: 9 additions & 2 deletions test/integration/Adapter/Platform/PostgresqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laminas\Db\Adapter\Driver\Pdo;
use Laminas\Db\Adapter\Driver\Pgsql;
use Laminas\Db\Adapter\Platform\Postgresql;
use PgSql\Connection as PgSqlConnection;
use PHPUnit\Framework\TestCase;

use function extension_loaded;
Expand Down Expand Up @@ -46,7 +47,13 @@ protected function setUp(): void

public function testQuoteValueWithPgsql()
{
if (! is_resource($this->adapters['pgsql'])) {
if (
! isset($this->adapters['pgsql'])
|| (
! $this->adapters['pgsql'] instanceof PgSqlConnection
&& ! is_resource($this->adapters['pgsql'])
)
) {
$this->markTestSkipped('Postgres (pgsql) not configured in unit test configuration file');
}
$pgsql = new Postgresql($this->adapters['pgsql']);
Expand All @@ -60,7 +67,7 @@ public function testQuoteValueWithPgsql()

public function testQuoteValueWithPdoPgsql()
{
if (! $this->adapters['pdo_pgsql'] instanceof \PDO) {
if (! isset($this->adapters['pdo_pgsql']) || ! $this->adapters['pdo_pgsql'] instanceof \PDO) {
$this->markTestSkipped('Postgres (PDO_PGSQL) not configured in unit test configuration file');
}
$pgsql = new Postgresql($this->adapters['pdo_pgsql']);
Expand Down

0 comments on commit cdabb4b

Please sign in to comment.