Skip to content

Commit f52cc35

Browse files
committed
Update for PHP 8.1
ext-pgsql uses objects now instead of resources.
1 parent 71d1925 commit f52cc35

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/PgSqlHandle.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class PgSqlHandle implements Handle
3333
/** @var array<string, Promise<array<int, array{string, string}>>> */
3434
private static $typeCache;
3535

36-
/** @var resource PostgreSQL connection handle. */
36+
/** @var resource|\PgSql\Connection|null PostgreSQL connection handle. */
3737
private $handle;
3838

3939
/** @var Promise<array<int, array{string, string}>> */
@@ -163,7 +163,7 @@ public function __construct($handle, $socket, string $id = '')
163163
*/
164164
public function __destruct()
165165
{
166-
$this->close();
166+
$this->free();
167167
}
168168

169169
private function fetchTypes(string $id): Promise
@@ -194,21 +194,20 @@ private function fetchTypes(string $id): Promise
194194
*/
195195
public function close(): void
196196
{
197-
if ($this->deferred) {
198-
$deferred = $this->deferred;
199-
$this->deferred = null;
200-
$deferred->fail(new ConnectionException("The connection was closed"));
197+
if ($this->handle instanceof \PgSql\Connection || \is_resource($this->handle)) {
198+
\pg_close($this->handle);
199+
$this->handle = null;
201200
}
202201

203202
$this->free();
204-
205-
$this->handle = null;
206203
}
207204

208205
private function free(): void
209206
{
210-
if (\is_resource($this->handle)) {
211-
\pg_close($this->handle);
207+
if ($this->deferred) {
208+
$deferred = $this->deferred;
209+
$this->deferred = null;
210+
$deferred->fail(new ConnectionException("The connection was closed"));
212211
}
213212

214213
Loop::cancel($this->poll);
@@ -220,7 +219,7 @@ private function free(): void
220219
*/
221220
public function isAlive(): bool
222221
{
223-
return \is_resource($this->handle);
222+
return $this->handle instanceof \PgSql\Connection || \is_resource($this->handle);
224223
}
225224

226225
/**
@@ -251,7 +250,7 @@ private function send(callable $function, ...$args): \Generator
251250
}
252251
}
253252

254-
if (!\is_resource($this->handle)) {
253+
if (!$this->isAlive()) {
255254
throw new ConnectionException("The connection to the database has been closed");
256255
}
257256

@@ -351,7 +350,7 @@ public function statementExecute(string $name, array $params): Promise
351350
*/
352351
public function statementDeallocate(string $name): Promise
353352
{
354-
if (!\is_resource($this->handle)) {
353+
if (!$this->isAlive()) {
355354
return new Success; // Connection closed, no need to deallocate.
356355
}
357356

@@ -374,7 +373,7 @@ public function statementDeallocate(string $name): Promise
374373
*/
375374
public function query(string $sql): Promise
376375
{
377-
if (!\is_resource($this->handle)) {
376+
if (!$this->isAlive()) {
378377
throw new \Error("The connection to the database has been closed");
379378
}
380379

@@ -388,7 +387,7 @@ public function query(string $sql): Promise
388387
*/
389388
public function execute(string $sql, array $params = []): Promise
390389
{
391-
if (!\is_resource($this->handle)) {
390+
if (!$this->isAlive()) {
392391
throw new \Error("The connection to the database has been closed");
393392
}
394393

@@ -409,7 +408,7 @@ public function execute(string $sql, array $params = []): Promise
409408
*/
410409
public function prepare(string $sql): Promise
411410
{
412-
if (!\is_resource($this->handle)) {
411+
if (!$this->isAlive()) {
413412
throw new \Error("The connection to the database has been closed");
414413
}
415414

@@ -532,7 +531,7 @@ private function unlisten(string $channel): Promise
532531
$emitter = $this->listeners[$channel];
533532
unset($this->listeners[$channel]);
534533

535-
if (!\is_resource($this->handle)) {
534+
if (!$this->isAlive()) {
536535
$promise = new Success; // Connection already closed.
537536
} else {
538537
$promise = $this->query(\sprintf("UNLISTEN %s", $this->quoteName($channel)));
@@ -547,7 +546,7 @@ private function unlisten(string $channel): Promise
547546
*/
548547
public function quoteString(string $data): string
549548
{
550-
if (!\is_resource($this->handle)) {
549+
if (!$this->isAlive()) {
551550
throw new \Error("The connection to the database has been closed");
552551
}
553552

@@ -559,7 +558,7 @@ public function quoteString(string $data): string
559558
*/
560559
public function quoteName(string $name): string
561560
{
562-
if (!\is_resource($this->handle)) {
561+
if (!$this->isAlive()) {
563562
throw new \Error("The connection to the database has been closed");
564563
}
565564

0 commit comments

Comments
 (0)