1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-27 04:24:45 +01:00
postgres/lib/PgSqlConnection.php

87 lines
2.9 KiB
PHP
Raw Normal View History

2016-12-30 06:21:17 +01:00
<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres;
use Amp\{ Deferred, TimeoutException };
use AsyncInterop\{ Loop, Promise };
2016-09-14 16:27:39 +02:00
class PgSqlConnection extends AbstractConnection {
/**
* @param string $connectionString
* @param int|null $timeout
*
* @return \AsyncInterop\Promise<\Amp\Postgres\PgSqlConnection>
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\FailureException
*/
2016-11-15 18:06:21 +01:00
public static function connect(string $connectionString, int $timeout = null): Promise {
2016-09-14 16:27:39 +02:00
if (!$connection = @\pg_connect($connectionString, \PGSQL_CONNECT_ASYNC | \PGSQL_CONNECT_FORCE_NEW)) {
throw new FailureException("Failed to create connection resource");
}
if (\pg_connection_status($connection) === \PGSQL_CONNECTION_BAD) {
throw new FailureException(\pg_last_error($connection));
}
if (!$socket = \pg_socket($connection)) {
throw new FailureException("Failed to access connection socket");
}
$deferred = new Deferred;
$callback = function ($watcher, $resource) use (&$poll, &$await, $connection, $deferred) {
try {
switch (\pg_connect_poll($connection)) {
case \PGSQL_POLLING_READING:
return; // Connection not ready, poll again.
case \PGSQL_POLLING_WRITING:
return; // Still writing...
case \PGSQL_POLLING_FAILED:
throw new FailureException("Could not connect to PostgreSQL server");
case \PGSQL_POLLING_OK:
Loop::cancel($poll);
Loop::cancel($await);
$deferred->resolve(new self($connection, $resource));
return;
}
} catch (\Throwable $exception) {
Loop::cancel($poll);
Loop::cancel($await);
\pg_close($connection);
$deferred->fail($exception);
}
};
$poll = Loop::onReadable($socket, $callback);
$await = Loop::onWritable($socket, $callback);
if ($timeout !== null) {
return \Amp\capture(
2016-11-15 18:06:21 +01:00
$deferred->promise(),
2016-09-14 16:27:39 +02:00
TimeoutException::class,
function (\Throwable $exception) use ($connection, $poll, $await) {
Loop::cancel($poll);
Loop::cancel($await);
\pg_close($connection);
throw $exception;
}
);
}
2016-11-15 18:06:21 +01:00
return $deferred->promise();
2016-09-14 16:27:39 +02:00
}
/**
* Connection constructor.
*
* @param resource $handle PostgreSQL connection handle.
* @param resource $socket PostgreSQL connection stream socket.
*/
public function __construct($handle, $socket) {
parent::__construct(new PgSqlExecutor($handle, $socket));
}
}