2016-09-14 16:27:39 +02:00
|
|
|
<?php declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
|
|
|
use Amp\Coroutine;
|
2016-11-15 18:06:21 +01:00
|
|
|
use Interop\Async\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
class Transaction implements Executor, Operation {
|
|
|
|
use Internal\Operation;
|
|
|
|
|
|
|
|
const UNCOMMITTED = 0;
|
|
|
|
const COMMITTED = 1;
|
|
|
|
const REPEATABLE = 2;
|
|
|
|
const SERIALIZABLE = 4;
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Executor */
|
|
|
|
private $executor;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $isolation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Amp\Postgres\Executor $executor
|
|
|
|
* @param int $isolation
|
|
|
|
*
|
|
|
|
* @throws \Error If the isolation level is invalid.
|
|
|
|
*/
|
|
|
|
public function __construct(Executor $executor, int $isolation = self::COMMITTED) {
|
|
|
|
switch ($isolation) {
|
|
|
|
case self::UNCOMMITTED:
|
|
|
|
case self::COMMITTED:
|
|
|
|
case self::REPEATABLE:
|
|
|
|
case self::SERIALIZABLE:
|
|
|
|
$this->isolation = $isolation;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Error("Isolation must be a valid transaction isolation level");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->executor = $executor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isActive(): bool {
|
|
|
|
return $this->executor !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getIsolationLevel(): int {
|
|
|
|
return $this->isolation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function query(string $sql): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
if ($this->executor === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->executor->query($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function prepare(string $sql): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
if ($this->executor === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->executor->prepare($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
|
|
|
if ($this->executor === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->executor->execute($sql, ...$params);
|
|
|
|
}
|
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
|
|
|
if ($this->executor === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->executor->notify($channel, $payload);
|
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits the transaction and makes it inactive.
|
|
|
|
*
|
2016-11-15 18:06:21 +01:00
|
|
|
* @return \Interop\Async\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function commit(): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
return new Coroutine($this->doCommit());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doCommit(): \Generator {
|
|
|
|
if ($this->executor === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
$executor = $this->executor;
|
|
|
|
$this->executor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = yield $executor->query("COMMIT");
|
|
|
|
} finally {
|
|
|
|
$this->complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rolls back the transaction and makes it inactive.
|
|
|
|
*
|
2016-11-15 18:06:21 +01:00
|
|
|
* @return \Interop\Async\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function rollback(): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
return new Coroutine($this->doRollback());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doRollback(): \Generator {
|
|
|
|
if ($this->executor === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
$executor = $this->executor;
|
|
|
|
$this->executor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = yield $executor->query("ROLLBACK");
|
|
|
|
} finally {
|
|
|
|
$this->complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass untrusted data.
|
|
|
|
*
|
2016-09-21 07:18:24 +02:00
|
|
|
* @param string $identifier Savepoint identifier.
|
|
|
|
*
|
2016-11-15 18:06:21 +01:00
|
|
|
* @return \Interop\Async\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function savepoint(string $identifier): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
return $this->query("SAVEPOINT " . $identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rolls back to the savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass
|
|
|
|
* untrusted data.
|
|
|
|
*
|
2016-09-21 07:18:24 +02:00
|
|
|
* @param string $identifier Savepoint identifier.
|
|
|
|
*
|
2016-11-15 18:06:21 +01:00
|
|
|
* @return \Interop\Async\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function rollbackTo(string $identifier): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
return $this->query("ROLLBACK TO " . $identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass untrusted
|
|
|
|
* data.
|
|
|
|
*
|
2016-09-21 07:18:24 +02:00
|
|
|
* @param string $identifier Savepoint identifier.
|
|
|
|
*
|
2016-11-15 18:06:21 +01:00
|
|
|
* @return \Interop\Async\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError
|
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function release(string $identifier): Promise {
|
2016-09-14 16:27:39 +02:00
|
|
|
return $this->query("RELEASE SAVEPOINT " . $identifier);
|
|
|
|
}
|
|
|
|
}
|