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

156 lines
4.0 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\CallableMaker;
use Amp\CancellationToken;
use Amp\Coroutine;
use Amp\Deferred;
use Amp\Promise;
use function Amp\call;
2016-09-14 16:27:39 +02:00
abstract class AbstractConnection implements Connection {
2016-09-14 16:27:39 +02:00
use CallableMaker;
2017-05-16 06:28:37 +02:00
/** @var \Amp\Postgres\Executor */
private $handle;
2017-05-16 06:28:37 +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
*
* @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 \Amp\Postgres\Handle $handle
2016-09-14 16:27:39 +02:00
*/
public function __construct(Handle $handle) {
$this->handle = $handle;
2016-09-14 16:27:39 +02:00
$this->release = $this->callableFromInstanceMethod("release");
}
/**
* {@inheritdoc}
*/
public function isAlive(): bool {
return $this->handle->isAlive();
}
2016-09-14 16:27:39 +02:00
/**
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.
*
* @return \Amp\Promise
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\FailureException
*/
private function send(string $methodName, ...$args): \Generator {
while ($this->busy) {
yield $this->busy->promise();
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
return $this->handle->{$methodName}(...$args);
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
/**
* Releases the transaction lock.
*/
2016-09-14 16:27:39 +02:00
private function release() {
$deferred = $this->busy;
$this->busy = null;
$deferred->resolve();
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 {
return new Coroutine($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 {
return new Coroutine($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 {
return new Coroutine($this->send("prepare", $sql));
2016-09-19 18:12:32 +02:00
}
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("notify", $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 {
return new Coroutine($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 {
return call(function () use ($isolation) {
switch ($isolation) {
case Transaction::UNCOMMITTED:
yield $this->handle->query("BEGIN TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
break;
2017-05-16 06:28:37 +02:00
case Transaction::COMMITTED:
yield $this->handle->query("BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED");
break;
2017-05-16 06:28:37 +02:00
case Transaction::REPEATABLE:
yield $this->handle->query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ");
break;
2017-05-16 06:28:37 +02:00
case Transaction::SERIALIZABLE:
yield $this->handle->query("BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE");
break;
2017-05-16 06:28:37 +02:00
default:
throw new \Error("Invalid transaction type");
}
2017-05-16 06:14:02 +02:00
$this->busy = new Deferred;
$transaction = new Transaction($this->handle, $isolation);
2016-09-14 16:27:39 +02:00
$transaction->onComplete($this->release);
return $transaction;
});
}
/**
* {@inheritdoc}
*/
public function quoteString(string $data): string {
return $this->handle->quoteString($data);
}
/**
* {@inheritdoc}
*/
public function quoteName(string $name): string {
return $this->handle->quoteName($name);
}
2016-09-14 16:27:39 +02:00
}