2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-06-05 06:42:18 +02:00
|
|
|
use Amp\{ CallableMaker, CancellationToken, Promise, function call };
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
abstract class AbstractConnection implements Connection {
|
|
|
|
use CallableMaker;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/** @var \Amp\Postgres\Executor */
|
2016-09-14 16:27:39 +02:00
|
|
|
private $executor;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
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;
|
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
|
|
|
/**
|
|
|
|
* @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\Connection>
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-06-05 06:42:18 +02:00
|
|
|
abstract public static function connect(string $connectionString, CancellationToken $token = null): Promise;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
|
|
|
* @param $executor;
|
|
|
|
*/
|
|
|
|
public function __construct(Executor $executor) {
|
|
|
|
$this->executor = $executor;
|
|
|
|
$this->release = $this->callableFromInstanceMethod("release");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-05 05:15:50 +02:00
|
|
|
* @param string $methodName Method to execute.
|
2016-09-14 16:27:39 +02:00
|
|
|
* @param mixed ...$args Arguments to pass to function.
|
|
|
|
*
|
2017-05-26 20:14:04 +02:00
|
|
|
* @return \Amp\Promise
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\FailureException
|
2017-05-26 20:14:04 +02:00
|
|
|
* @throws \Amp\Postgres\PendingOperationError
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-06-05 05:15:50 +02:00
|
|
|
private function send(string $methodName, ...$args): Promise {
|
2017-05-26 20:14:04 +02:00
|
|
|
if ($this->busy) {
|
|
|
|
throw new PendingOperationError;
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-05-26 20:14:04 +02:00
|
|
|
$this->busy = true;
|
|
|
|
|
|
|
|
try {
|
2017-06-05 05:15:50 +02:00
|
|
|
return $this->executor->{$methodName}(...$args);
|
2017-05-26 20:14:04 +02:00
|
|
|
} finally {
|
|
|
|
$this->busy = false;
|
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/**
|
|
|
|
* Releases the transaction lock.
|
|
|
|
*/
|
2016-09-14 16:27:39 +02:00
|
|
|
private function release() {
|
2017-05-26 20:14:04 +02:00
|
|
|
$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 {
|
2017-06-05 05:15:50 +02:00
|
|
|
return $this->send("query", $sql);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function execute(string $sql, ...$params): Promise {
|
2017-06-05 05:15:50 +02:00
|
|
|
return $this->send("execute", $sql, ...$params);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function prepare(string $sql): Promise {
|
2017-06-05 05:15:50 +02:00
|
|
|
return $this->send("prepare", $sql);
|
2016-09-19 18:12:32 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
|
|
|
|
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 {
|
2017-06-05 05:15:50 +02:00
|
|
|
return $this->send("notify", $channel, $payload);
|
2016-09-21 07:18:24 +02:00
|
|
|
}
|
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-06-05 05:15:50 +02:00
|
|
|
return $this->send("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 {
|
2017-05-26 20:14:04 +02:00
|
|
|
if ($this->busy) {
|
|
|
|
throw new PendingOperationError;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->busy = true;
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
switch ($isolation) {
|
|
|
|
case Transaction::UNCOMMITTED:
|
2017-05-26 20:14:04 +02:00
|
|
|
$promise = $this->executor->query("BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
case Transaction::COMMITTED:
|
2017-05-26 20:14:04 +02:00
|
|
|
$promise = $this->executor->query("BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
case Transaction::REPEATABLE:
|
2017-05-26 20:14:04 +02:00
|
|
|
$promise = $this->executor->query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
case Transaction::SERIALIZABLE:
|
2017-05-26 20:14:04 +02:00
|
|
|
$promise = $this->executor->query("BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE");
|
2016-09-14 16:27:39 +02:00
|
|
|
break;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
default:
|
|
|
|
throw new \Error("Invalid transaction type");
|
|
|
|
}
|
2017-05-16 06:14:02 +02:00
|
|
|
|
|
|
|
return call(function () use ($promise, $isolation) {
|
|
|
|
yield $promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
$transaction = new Transaction($this->executor, $isolation);
|
|
|
|
$transaction->onComplete($this->release);
|
|
|
|
return $transaction;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|