2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
|
|
|
use Amp\{ CallableMaker, Coroutine, Deferred, function pipe };
|
2017-01-18 18:05:05 +01:00
|
|
|
use AsyncInterop\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
abstract class AbstractConnection implements Connection {
|
|
|
|
use CallableMaker;
|
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/** @var \Amp\Postgres\Executor */
|
2016-09-14 16:27:39 +02:00
|
|
|
private $executor;
|
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/** @var \Amp\Deferred|null Used to only allow one transaction at a time. */
|
2016-09-14 16:27:39 +02:00
|
|
|
private $busy;
|
|
|
|
|
|
|
|
/** @var callable */
|
|
|
|
private $release;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
2017-02-16 00:36:10 +01:00
|
|
|
* @param int $timeout Timeout until the connection attempt fails. 0 for no timeout.
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-01-18 18:05:05 +01:00
|
|
|
* @return \AsyncInterop\Promise<\Amp\Postgres\Connection>
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-02-16 00:36:10 +01:00
|
|
|
abstract public static function connect(string $connectionString, int $timeout = 0): Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $executor;
|
|
|
|
*/
|
|
|
|
public function __construct(Executor $executor) {
|
|
|
|
$this->executor = $executor;
|
|
|
|
$this->release = $this->callableFromInstanceMethod("release");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $method Method to execute.
|
|
|
|
* @param mixed ...$args Arguments to pass to function.
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\FailureException
|
|
|
|
*/
|
|
|
|
private function send(callable $method, ...$args): \Generator {
|
|
|
|
while ($this->busy !== null) {
|
2016-11-15 18:06:21 +01:00
|
|
|
yield $this->busy->promise();
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $method(...$args);
|
|
|
|
}
|
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/**
|
|
|
|
* Releases the transaction lock.
|
|
|
|
*/
|
2016-09-14 16:27:39 +02:00
|
|
|
private function release() {
|
|
|
|
$busy = $this->busy;
|
|
|
|
$this->busy = null;
|
|
|
|
$busy->resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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->executor, "query"], $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->executor, "execute"], $sql, ...$params));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function prepare(string $sql): Promise {
|
2016-09-19 18:12:32 +02:00
|
|
|
return new Coroutine($this->send([$this->executor, "prepare"], $sql));
|
|
|
|
}
|
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function notify(string $channel, string $payload = ""): Promise {
|
2016-09-21 07:18:24 +02:00
|
|
|
return new Coroutine($this->send([$this->executor, "notify"], $channel, $payload));
|
|
|
|
}
|
|
|
|
|
2016-09-19 18:12:32 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function listen(string $channel): Promise {
|
2016-09-19 18:12:32 +02:00
|
|
|
return new Coroutine($this->send([$this->executor, "listen"], $channel));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function transaction(int $isolation = Transaction::COMMITTED): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
switch ($isolation) {
|
|
|
|
case Transaction::UNCOMMITTED:
|
2016-11-15 18:06:21 +01:00
|
|
|
$promise = $this->query("BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Transaction::COMMITTED:
|
2016-11-15 18:06:21 +01:00
|
|
|
$promise = $this->query("BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Transaction::REPEATABLE:
|
2016-11-15 18:06:21 +01:00
|
|
|
$promise = $this->query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Transaction::SERIALIZABLE:
|
2016-11-15 18:06:21 +01:00
|
|
|
$promise = $this->query("BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Error("Invalid transaction type");
|
|
|
|
}
|
|
|
|
|
2017-01-18 19:29:48 +01:00
|
|
|
return pipe($promise, function () use ($isolation): Transaction {
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->busy = new Deferred;
|
|
|
|
$transaction = new Transaction($this->executor, $isolation);
|
|
|
|
$transaction->onComplete($this->release);
|
|
|
|
return $transaction;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|