Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 19, 2023
1 parent 6b3fee5 commit b65a534
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Internal/PostgresHandleConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract public static function connect(
?Cancellation $cancellation = null,
): self;

protected function __construct(private readonly PostgresHandle $handle)
protected function __construct(protected readonly PostgresHandle $handle)
{
}

Expand Down
26 changes: 13 additions & 13 deletions src/PgSqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati
throw new \Error('ext-pgsql is not compatible with pecl-ev; use pecl-pq or a different loop extension');
} // @codeCoverageIgnoreEnd

if (!$connection = \pg_connect($config->getConnectionString(), \PGSQL_CONNECT_ASYNC | \PGSQL_CONNECT_FORCE_NEW)) {
if (!$connection = \pg_connect(
$config->getConnectionString(),
\PGSQL_CONNECT_ASYNC | \PGSQL_CONNECT_FORCE_NEW,
)) {
throw new ConnectionException("Failed to create connection resource");
}

Expand All @@ -36,7 +39,14 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati

$deferred = new DeferredFuture();
/** @psalm-suppress MissingClosureParamType $resource is a resource and cannot be inferred in this context */
$callback = static function (string $callbackId, $resource) use (&$poll, &$await, $connection, $config, $deferred, $hash): void {
$callback = static function (string $callbackId, $resource) use (
&$poll,
&$await,
$connection,
$config,
$deferred,
$hash,
): void {
switch ($result = \pg_connect_poll($connection)) {
case \PGSQL_POLLING_READING:
case \PGSQL_POLLING_WRITING:
Expand All @@ -47,7 +57,7 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati
break;

case \PGSQL_POLLING_OK:
$deferred->complete(new self($connection, $resource, $hash, $config));
$deferred->complete(new self(new Internal\PgSqlHandle($connection, $resource, $hash, $config)));
break;

default:
Expand All @@ -69,14 +79,4 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati
EventLoop::cancel($await);
}
}

/**
* @param \PgSql\Connection $handle PostgreSQL connection handle.
* @param resource $socket PostgreSQL connection stream socket.
* @param string $id Connection identifier for determining which cached type table to use.
*/
protected function __construct(\PgSql\Connection $handle, $socket, string $id, PostgresConfig $config)
{
parent::__construct(new Internal\PgSqlHandle($handle, $socket, $id, $config));
}
}
10 changes: 1 addition & 9 deletions src/PqConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

final class PqConnection extends Internal\PostgresHandleConnection implements PostgresConnection
{
private readonly Internal\PqHandle $handle;

public static function connect(PostgresConfig $config, ?Cancellation $cancellation = null): self
{
try {
Expand All @@ -35,7 +33,7 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati
break;

case pq\Connection::POLLING_OK:
$deferred->complete(new self($connection, $config));
$deferred->complete(new self(new Internal\PqHandle($connection, $config)));
break;

default:
Expand All @@ -58,12 +56,6 @@ public static function connect(PostgresConfig $config, ?Cancellation $cancellati
}
}

protected function __construct(pq\Connection $handle, PostgresConfig $config)
{
$this->handle = new Internal\PqHandle($handle, $config);
parent::__construct($this->handle);
}

/**
* @return bool True if result sets are buffered in memory, false if unbuffered.
*/
Expand Down

0 comments on commit b65a534

Please sign in to comment.