1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-27 12:34:40 +01:00
postgres/lib/PqExecutor.php

277 lines
7.7 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;
2017-05-17 18:14:12 +02:00
use Amp\{ CallableMaker, Coroutine, Deferred, Emitter, Loop, Promise, function call, function coroutine };
2016-09-14 16:27:39 +02:00
use pq;
class PqExecutor implements Executor {
use CallableMaker;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/** @var \pq\Connection PostgreSQL connection object. */
private $handle;
/** @var \Amp\Deferred|null */
private $deferred;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/** @var \Amp\Deferred */
private $busy;
/** @var string */
private $poll;
/** @var string */
private $await;
2017-05-16 06:28:37 +02:00
/** @var \Amp\Emitter[] */
2016-09-19 18:12:32 +02:00
private $listeners;
2016-09-14 16:27:39 +02:00
/** @var callable */
private $send;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/** @var callable */
private $fetch;
2017-05-16 06:28:37 +02:00
2016-09-19 18:12:32 +02:00
/** @var callable */
private $unlisten;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/** @var callable */
private $release;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/**
* Connection constructor.
*
* @param \pq\Connection $handle
*/
public function __construct(pq\Connection $handle) {
$this->handle = $handle;
2017-05-16 06:28:37 +02:00
$deferred = &$this->deferred;
$listeners = &$this->listeners;
2017-05-16 06:28:37 +02:00
$this->poll = Loop::onReadable($this->handle->socket, static function ($watcher) use (&$deferred, &$listeners, $handle) {
$status = $handle->poll();
2017-05-16 06:28:37 +02:00
if ($deferred === null) {
return; // No active query, only notification listeners.
}
2017-05-16 06:28:37 +02:00
if ($status === pq\Connection::POLLING_FAILED) {
2016-09-14 16:27:39 +02:00
$deferred->fail(new FailureException($handle->errorMessage));
} elseif (!$handle->busy) {
$deferred->resolve($handle->getResult());
2016-09-14 16:27:39 +02:00
}
if (!$handle->busy && empty($listeners)) {
Loop::disable($watcher);
2016-09-14 16:27:39 +02:00
}
});
$this->await = Loop::onWritable($this->handle->socket, static function ($watcher) use (&$deferred, $handle) {
if (!$handle->flush()) {
return; // Not finished sending data, continue polling for writability.
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
Loop::disable($watcher);
});
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
Loop::disable($this->poll);
Loop::disable($this->await);
$this->send = $this->callableFromInstanceMethod("send");
2017-05-16 06:14:02 +02:00
$this->fetch = coroutine($this->callableFromInstanceMethod("fetch"));
2016-09-19 18:12:32 +02:00
$this->unlisten = $this->callableFromInstanceMethod("unlisten");
2016-09-14 16:27:39 +02:00
$this->release = $this->callableFromInstanceMethod("release");
}
/**
* Frees Io watchers from loop.
*/
public function __destruct() {
Loop::cancel($this->poll);
Loop::cancel($this->await);
}
/**
* @param callable $method Method to execute.
* @param mixed ...$args Arguments to pass to function.
*
* @return \Generator
*
* @resolve resource
*
* @throws \Amp\Postgres\FailureException
*/
private function send(callable $method, ...$args): \Generator {
if ($this->busy) {
throw new PendingOperationError;
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
$this->busy = true;
2016-09-14 16:27:39 +02:00
try {
$handle = $method(...$args);
2017-05-16 06:28:37 +02:00
$this->deferred = new Deferred;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
Loop::enable($this->poll);
if (!$this->handle->flush()) {
Loop::enable($this->await);
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
try {
2016-11-15 18:06:21 +01:00
$result = yield $this->deferred->promise();
2016-09-14 16:27:39 +02:00
} finally {
$this->deferred = null;
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 ($handle instanceof pq\Statement) {
return new PqStatement($handle, $this->send);
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
if (!$result instanceof pq\Result) {
throw new FailureException("Unknown query result");
}
} catch (pq\Exception $exception) {
throw new FailureException($this->handle->errorMessage, 0, $exception);
2016-09-14 16:27:39 +02:00
} finally {
$this->busy = false;
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
switch ($result->status) {
case pq\Result::EMPTY_QUERY:
throw new QueryError("Empty query string");
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
case pq\Result::COMMAND_OK:
return new PqCommandResult($result);
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
case pq\Result::TUPLES_OK:
return new PqBufferedResult($result);
2017-05-16 06:28:37 +02:00
2017-02-16 23:25:52 +01:00
case pq\Result::SINGLE_TUPLE:
$this->busy = true;
2016-09-14 16:27:39 +02:00
$result = new PqUnbufferedResult($this->fetch, $result);
$result->onComplete($this->release);
return $result;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
case pq\Result::NONFATAL_ERROR:
case pq\Result::FATAL_ERROR:
throw new QueryError($result->errorMessage);
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
case pq\Result::BAD_RESPONSE:
throw new FailureException($result->errorMessage);
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
default:
throw new FailureException("Unknown result status");
}
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
private function fetch(): \Generator {
if (!$this->handle->busy) { // Results buffered.
$result = $this->handle->getResult();
} else {
$this->deferred = new Deferred;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
Loop::enable($this->poll);
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
try {
2016-11-15 18:06:21 +01:00
$result = yield $this->deferred->promise();
2016-09-14 16:27:39 +02:00
} finally {
$this->deferred = null;
2016-09-14 16:27:39 +02:00
}
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
switch ($result->status) {
2016-09-19 18:12:32 +02:00
case pq\Result::TUPLES_OK: // End of result set.
2016-09-14 16:27:39 +02:00
return null;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
case pq\Result::SINGLE_TUPLE:
return $result;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
default:
throw new FailureException($result->errorMessage);
}
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
private function release() {
$this->busy = false;
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 18:06:21 +01:00
public function query(string $sql): Promise {
2016-09-14 16:27:39 +02:00
return new Coroutine($this->send([$this->handle, "execAsync"], $sql));
}
/**
* {@inheritdoc}
*/
2016-11-15 18:06:21 +01:00
public function execute(string $sql, ...$params): Promise {
2016-09-14 16:27:39 +02:00
return new Coroutine($this->send([$this->handle, "execParamsAsync"], $sql, $params));
}
/**
* {@inheritdoc}
*/
2016-11-15 18:06:21 +01:00
public function prepare(string $sql): Promise {
2016-09-14 16:27:39 +02:00
return new Coroutine($this->send([$this->handle, "prepareAsync"], $sql, $sql));
}
2017-05-16 06:28:37 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 18:06:21 +01:00
public function notify(string $channel, string $payload = ""): Promise {
return new Coroutine($this->send([$this->handle, "notifyAsync"], $channel, $payload));
}
2017-05-16 06:28:37 +02:00
2016-09-19 18:12:32 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 18:06:21 +01:00
public function listen(string $channel): Promise {
2017-05-16 06:14:02 +02:00
return call(function () use ($channel) {
$emitter = new Emitter;
yield from $this->send(
[$this->handle, "listenAsync"],
$channel,
static function (string $channel, string $message, int $pid) use ($emitter) {
$notification = new Notification;
$notification->channel = $channel;
$notification->pid = $pid;
$notification->payload = $message;
$emitter->emit($notification);
});
$this->listeners[$channel] = $emitter;
Loop::enable($this->poll);
2017-05-16 06:14:02 +02:00
return new Listener($emitter->iterate(), $channel, $this->unlisten);
2016-09-19 18:12:32 +02:00
});
}
2017-05-16 06:28:37 +02:00
2016-09-19 18:12:32 +02:00
/**
* @param string $channel
*
* @return \Amp\Promise
2016-09-19 18:12:32 +02:00
*
* @throws \Error
*/
2016-11-15 18:06:21 +01:00
private function unlisten(string $channel): Promise {
2016-09-19 18:12:32 +02:00
if (!isset($this->listeners[$channel])) {
throw new \Error("Not listening on that channel");
}
2017-05-16 06:28:37 +02:00
$emitter = $this->listeners[$channel];
2016-09-19 18:12:32 +02:00
unset($this->listeners[$channel]);
2017-05-16 06:28:37 +02:00
if (empty($this->listeners) && $this->deferred === null) {
Loop::disable($this->poll);
}
2017-05-16 06:28:37 +02:00
2016-11-15 18:06:21 +01:00
$promise = new Coroutine($this->send([$this->handle, "unlistenAsync"], $channel));
2017-03-22 01:16:25 +01:00
$promise->onResolve(function () use ($emitter) {
2017-05-16 06:14:02 +02:00
$emitter->complete();
2016-09-19 18:12:32 +02:00
});
2016-11-15 18:06:21 +01:00
return $promise;
2016-09-19 18:12:32 +02:00
}
2016-09-14 16:27:39 +02:00
}