Clean up resource type and phan suppression in postgres code
Replace `resource` with the new types in PHP 8, and drop all the redundant phan suppressions. Change-Id: Ie63253356c373da9f8f8db16823d1b7f41822528
This commit is contained in:
parent
65c90be7ea
commit
8a21d033f8
2 changed files with 13 additions and 33 deletions
|
|
@ -18,11 +18,10 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
// Suppress UnusedPluginSuppression because Phan on PHP 7.4 and PHP 8.1 need different suppressions
|
||||
// @phan-file-suppress UnusedPluginSuppression,UnusedPluginFileSuppression
|
||||
|
||||
namespace Wikimedia\Rdbms;
|
||||
|
||||
use PgSql\Connection;
|
||||
use PgSql\Result;
|
||||
use RuntimeException;
|
||||
use Wikimedia\Rdbms\Platform\PostgresPlatform;
|
||||
use Wikimedia\Rdbms\Replication\ReplicationReporter;
|
||||
|
|
@ -41,7 +40,7 @@ class DatabasePostgres extends Database {
|
|||
/** @var float|string */
|
||||
private $numericVersion;
|
||||
|
||||
/** @var resource|null */
|
||||
/** @var Result|null */
|
||||
private $lastResultHandle;
|
||||
|
||||
/** @var PostgresPlatform */
|
||||
|
|
@ -173,6 +172,10 @@ class DatabasePostgres extends Database {
|
|||
return true;
|
||||
}
|
||||
|
||||
protected function getBindingHandle(): Connection {
|
||||
return parent::getBindingHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $vars
|
||||
* @return string
|
||||
|
|
@ -204,16 +207,11 @@ class DatabasePostgres extends Database {
|
|||
throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
|
||||
}
|
||||
|
||||
// Newer PHP versions use PgSql\Result instead of resource variables
|
||||
// https://www.php.net/manual/en/function.pg-get-result.php
|
||||
$pgRes = pg_get_result( $conn );
|
||||
// Phan on PHP 7.4 and PHP 8.1 need different suppressions
|
||||
// @phan-suppress-next-line PhanTypeMismatchProperty,PhanTypeMismatchPropertyProbablyReal
|
||||
$this->lastResultHandle = $pgRes;
|
||||
$res = pg_result_error( $pgRes ) ? false : $pgRes;
|
||||
|
||||
return new QueryStatus(
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgument
|
||||
is_bool( $res ) ? $res : new PostgresResultWrapper( $this, $conn, $res ),
|
||||
$pgRes ? pg_affected_rows( $pgRes ) : 0,
|
||||
$this->lastError(),
|
||||
|
|
@ -238,7 +236,6 @@ class DatabasePostgres extends Database {
|
|||
];
|
||||
foreach ( $diags as $d ) {
|
||||
$this->logger->debug( sprintf( "PgSQL ERROR(%d): %s",
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$d, pg_result_error_field( $this->lastResultHandle, $d ) ) );
|
||||
}
|
||||
}
|
||||
|
|
@ -256,7 +253,6 @@ class DatabasePostgres extends Database {
|
|||
public function lastError() {
|
||||
if ( $this->conn ) {
|
||||
if ( $this->lastResultHandle ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
return pg_result_error( $this->lastResultHandle );
|
||||
} else {
|
||||
return pg_last_error() ?: $this->lastConnectError;
|
||||
|
|
@ -268,7 +264,6 @@ class DatabasePostgres extends Database {
|
|||
|
||||
public function lastErrno() {
|
||||
if ( $this->lastResultHandle ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$lastErrno = pg_result_error_field( $this->lastResultHandle, PGSQL_DIAG_SQLSTATE );
|
||||
if ( $lastErrno !== false ) {
|
||||
return $lastErrno;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Wikimedia\Rdbms;
|
||||
|
||||
use PgSql\Connection;
|
||||
use PgSql\Result;
|
||||
|
||||
/**
|
||||
* Result wrapper for PostgreSQL database results.
|
||||
*
|
||||
|
|
@ -10,41 +13,31 @@ namespace Wikimedia\Rdbms;
|
|||
class PostgresResultWrapper extends ResultWrapper {
|
||||
/** @var DatabasePostgres */
|
||||
private $db;
|
||||
/** @var resource */
|
||||
private $handle;
|
||||
/** @var resource */
|
||||
private $result;
|
||||
private Connection $handle;
|
||||
private Result $result;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param DatabasePostgres $db
|
||||
* @param resource $handle
|
||||
* @param resource $result
|
||||
*/
|
||||
public function __construct( DatabasePostgres $db, $handle, $result ) {
|
||||
public function __construct( DatabasePostgres $db, Connection $handle, Result $result ) {
|
||||
$this->db = $db;
|
||||
$this->handle = $handle;
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
protected function doNumRows() {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
return pg_num_rows( $this->result );
|
||||
}
|
||||
|
||||
protected function doFetchObject() {
|
||||
// pg_fetch_object may raise a warning after a seek to an invalid offset
|
||||
// @phan-suppress-next-next-line PhanTypeMismatchArgumentInternal
|
||||
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
|
||||
$row = @pg_fetch_object( $this->result );
|
||||
// Map boolean values (T352229)
|
||||
if ( is_object( $row ) ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$numFields = pg_num_fields( $this->result );
|
||||
for ( $i = 0; $i < $numFields; $i++ ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
if ( pg_field_type( $this->result, $i ) === 'bool' ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$name = pg_field_name( $this->result, $i );
|
||||
$row->$name = $this->convertBoolean( $row->$name );
|
||||
}
|
||||
|
|
@ -54,17 +47,13 @@ class PostgresResultWrapper extends ResultWrapper {
|
|||
}
|
||||
|
||||
protected function doFetchRow() {
|
||||
// @phan-suppress-next-next-line PhanTypeMismatchArgumentInternal
|
||||
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
|
||||
$row = @pg_fetch_array( $this->result );
|
||||
// Map boolean values (T352229)
|
||||
if ( is_array( $row ) ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$numFields = pg_num_fields( $this->result );
|
||||
for ( $i = 0; $i < $numFields; $i++ ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
if ( pg_field_type( $this->result, $i ) === 'bool' ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$name = pg_field_name( $this->result, $i );
|
||||
$row[$i] = $this->convertBoolean( $row[$i] );
|
||||
$row[$name] = $this->convertBoolean( $row[$name] );
|
||||
|
|
@ -93,21 +82,17 @@ class PostgresResultWrapper extends ResultWrapper {
|
|||
}
|
||||
|
||||
protected function doSeek( $pos ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
pg_result_seek( $this->result, $pos );
|
||||
}
|
||||
|
||||
protected function doFree() {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
return pg_free_result( $this->result );
|
||||
}
|
||||
|
||||
protected function doGetFieldNames() {
|
||||
$names = [];
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$n = pg_num_fields( $this->result );
|
||||
for ( $i = 0; $i < $n; $i++ ) {
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
|
||||
$names[] = pg_field_name( $this->result, $i );
|
||||
}
|
||||
return $names;
|
||||
|
|
|
|||
Loading…
Reference in a new issue