2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\CancellationToken;
|
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\NullCancellationToken;
|
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
class PgSqlConnection extends AbstractConnection {
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
2017-06-05 06:42:18 +02:00
|
|
|
* @param \Amp\CancellationToken $token
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\PgSqlConnection>
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-06-05 06:42:18 +02:00
|
|
|
public static function connect(string $connectionString, CancellationToken $token = null): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
if (!$connection = @\pg_connect($connectionString, \PGSQL_CONNECT_ASYNC | \PGSQL_CONNECT_FORCE_NEW)) {
|
2017-02-16 00:36:10 +01:00
|
|
|
return new Failure(new FailureException("Failed to create connection resource"));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
if (\pg_connection_status($connection) === \PGSQL_CONNECTION_BAD) {
|
2017-02-16 00:36:10 +01:00
|
|
|
return new Failure(new FailureException(\pg_last_error($connection)));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
if (!$socket = \pg_socket($connection)) {
|
2017-02-16 00:36:10 +01:00
|
|
|
return new Failure(new FailureException("Failed to access connection socket"));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
$deferred = new Deferred;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-02-16 00:36:10 +01:00
|
|
|
$callback = function ($watcher, $resource) use ($connection, $deferred) {
|
|
|
|
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:
|
2017-06-05 06:42:18 +02:00
|
|
|
$deferred->fail(new FailureException(\pg_last_error($connection)));
|
2017-02-16 00:36:10 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case \PGSQL_POLLING_OK:
|
|
|
|
$deferred->resolve(new self($connection, $resource));
|
|
|
|
return;
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
};
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
$poll = Loop::onReadable($socket, $callback);
|
|
|
|
$await = Loop::onWritable($socket, $callback);
|
2017-02-16 00:36:10 +01:00
|
|
|
|
|
|
|
$promise = $deferred->promise();
|
|
|
|
|
2017-06-05 06:42:18 +02:00
|
|
|
$token = $token ?? new NullCancellationToken;
|
|
|
|
$id = $token->subscribe([$deferred, "fail"]);
|
2017-02-16 00:36:10 +01:00
|
|
|
|
2017-06-05 06:42:18 +02:00
|
|
|
$promise->onResolve(function ($exception) use ($connection, $poll, $await, $id, $token) {
|
2017-02-16 00:36:10 +01:00
|
|
|
if ($exception) {
|
|
|
|
\pg_close($connection);
|
|
|
|
}
|
|
|
|
|
2017-06-05 06:42:18 +02:00
|
|
|
$token->unsubscribe($id);
|
2017-02-16 00:36:10 +01:00
|
|
|
Loop::cancel($poll);
|
|
|
|
Loop::cancel($await);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
|
|
|
* @param resource $handle PostgreSQL connection handle.
|
|
|
|
* @param resource $socket PostgreSQL connection stream socket.
|
|
|
|
*/
|
|
|
|
public function __construct($handle, $socket) {
|
2017-08-01 07:38:12 +02:00
|
|
|
parent::__construct(new PgSqlHandle($handle, $socket));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
}
|