@@ -33,7 +33,7 @@ final class PgSqlHandle implements Handle
33
33
/** @var array<string, Promise<array<int, array{string, string}>>> */
34
34
private static $ typeCache ;
35
35
36
- /** @var resource PostgreSQL connection handle. */
36
+ /** @var resource|\PgSql\Connection|null PostgreSQL connection handle. */
37
37
private $ handle ;
38
38
39
39
/** @var Promise<array<int, array{string, string}>> */
@@ -163,7 +163,7 @@ public function __construct($handle, $socket, string $id = '')
163
163
*/
164
164
public function __destruct ()
165
165
{
166
- $ this ->close ();
166
+ $ this ->free ();
167
167
}
168
168
169
169
private function fetchTypes (string $ id ): Promise
@@ -194,21 +194,20 @@ private function fetchTypes(string $id): Promise
194
194
*/
195
195
public function close (): void
196
196
{
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 ;
201
200
}
202
201
203
202
$ this ->free ();
204
-
205
- $ this ->handle = null ;
206
203
}
207
204
208
205
private function free (): void
209
206
{
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 " ));
212
211
}
213
212
214
213
Loop::cancel ($ this ->poll );
@@ -220,7 +219,7 @@ private function free(): void
220
219
*/
221
220
public function isAlive (): bool
222
221
{
223
- return \is_resource ($ this ->handle );
222
+ return $ this -> handle instanceof \ PgSql \Connection || \is_resource ($ this ->handle );
224
223
}
225
224
226
225
/**
@@ -251,7 +250,7 @@ private function send(callable $function, ...$args): \Generator
251
250
}
252
251
}
253
252
254
- if (!\is_resource ( $ this ->handle )) {
253
+ if (!$ this ->isAlive ( )) {
255
254
throw new ConnectionException ("The connection to the database has been closed " );
256
255
}
257
256
@@ -351,7 +350,7 @@ public function statementExecute(string $name, array $params): Promise
351
350
*/
352
351
public function statementDeallocate (string $ name ): Promise
353
352
{
354
- if (!\is_resource ( $ this ->handle )) {
353
+ if (!$ this ->isAlive ( )) {
355
354
return new Success ; // Connection closed, no need to deallocate.
356
355
}
357
356
@@ -374,7 +373,7 @@ public function statementDeallocate(string $name): Promise
374
373
*/
375
374
public function query (string $ sql ): Promise
376
375
{
377
- if (!\is_resource ( $ this ->handle )) {
376
+ if (!$ this ->isAlive ( )) {
378
377
throw new \Error ("The connection to the database has been closed " );
379
378
}
380
379
@@ -388,7 +387,7 @@ public function query(string $sql): Promise
388
387
*/
389
388
public function execute (string $ sql , array $ params = []): Promise
390
389
{
391
- if (!\is_resource ( $ this ->handle )) {
390
+ if (!$ this ->isAlive ( )) {
392
391
throw new \Error ("The connection to the database has been closed " );
393
392
}
394
393
@@ -409,7 +408,7 @@ public function execute(string $sql, array $params = []): Promise
409
408
*/
410
409
public function prepare (string $ sql ): Promise
411
410
{
412
- if (!\is_resource ( $ this ->handle )) {
411
+ if (!$ this ->isAlive ( )) {
413
412
throw new \Error ("The connection to the database has been closed " );
414
413
}
415
414
@@ -532,7 +531,7 @@ private function unlisten(string $channel): Promise
532
531
$ emitter = $ this ->listeners [$ channel ];
533
532
unset($ this ->listeners [$ channel ]);
534
533
535
- if (!\is_resource ( $ this ->handle )) {
534
+ if (!$ this ->isAlive ( )) {
536
535
$ promise = new Success ; // Connection already closed.
537
536
} else {
538
537
$ promise = $ this ->query (\sprintf ("UNLISTEN %s " , $ this ->quoteName ($ channel )));
@@ -547,7 +546,7 @@ private function unlisten(string $channel): Promise
547
546
*/
548
547
public function quoteString (string $ data ): string
549
548
{
550
- if (!\is_resource ( $ this ->handle )) {
549
+ if (!$ this ->isAlive ( )) {
551
550
throw new \Error ("The connection to the database has been closed " );
552
551
}
553
552
@@ -559,7 +558,7 @@ public function quoteString(string $data): string
559
558
*/
560
559
public function quoteName (string $ name ): string
561
560
{
562
- if (!\is_resource ( $ this ->handle )) {
561
+ if (!$ this ->isAlive ( )) {
563
562
throw new \Error ("The connection to the database has been closed " );
564
563
}
565
564
0 commit comments